#include <stc8a8k.h>
#include <intrins.h>
sbit LED=P1^7;
sbit HC595_LATCH = P1^0;//latch pin or rck pin STCP ss 595pin12
sbit HC595_OE = P1^1;//oe blank pin 595pin13
// 0data of 74hc595 pin14 ---->mosi P13
//clk of 595 shcp 595pin11 ---->sclk P15
void SendTo595( unsigned char byteData);
void Delay10us(); //@22.1184MHz
void Delay500ms() //@22.1184MHz
{
unsigned char i, j, k;
i = 57;
j = 27;
k = 112;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void portmode()
{
P0M0=0x00;P0M1=0x00;
P1M0=0x00;P1M1=0x00;
P2M0=0x00;P2M1=0x00;
P3M0=0x00;P3M1=0x00;
P4M0=0x00;P4M1=0x00;
P5M0=0x00;P5M1=0x00;
P6M0=0x00;P6M1=0x00;
P7M0=0x00;P7M1=0x00;
}
void Delay30us() //@22.1184MHz
{
unsigned char i;
i = 219;
while (--i);
}
void main()
{
portmode();
LED=0;
Delay500ms();
LED=1;
Delay500ms();
LED=0;
Delay500ms();
LED=1;
Delay500ms();
LED=0;
Delay500ms();
LED=1;
Delay500ms();
LED=0;
Delay500ms();
LED=1;
Delay500ms();
SPCTL = 0x50; //??SPI????
SPSTAT = 0xc0; //?????
HC595_LATCH =1;
HC595_OE =0;
while(1)
{
//低电平亮
HC595_LATCH = 0; //????SS??
SPDAT = 0x10; //??????
while (!(SPSTAT & 0x80)); //??????
SPSTAT = 0xc0; //?????
HC595_LATCH = 1;
}
}
void Delay10us() //@22.1184MHz
{
unsigned char i;
i = 71;
while (--i);
}
void SendTo595( unsigned char byteData)
{
// char i=0;
// for(;i<8;i++)
// {
// P13 = byteData>>7;
0000 0001 >>1 0000 0000
0000 0001 <<1 0000 0010
// byteData= byteData<<1;
// P10 = 0;
// Delay10us();
// P10 = 1;
// }
// HC595_LATCH = 0; //p10
//
// Delay10us();
// HC595_LATCH =1;
// HC595_OE=0;
}
// HC595_LATCH = 0; //????SS??
// SPDAT = 0x5a; //??????
// while (!(SPSTAT & 0x80)); //??????
// SPSTAT = 0xc0; //?????
// HC595_LATCH = 0;
用的是P1口的SPI。
接线
595DS,数据口----------P13 mosi
OE---------------------P11
RCK,上升沿输出,–latch,,,,----P10
SCK------- P15 sclk