74HC
595是8位串行
移位寄存器,亦能串行输出控制下一级级联芯片。关于级联、595芯片具体我就不多介绍,大家自己查吧!下面直接贴代码
此程序已成功移植到STM32平台,敬请关注!
// 注意 此代码没有什么技术含量,也没有按规范些,旨在说明595怎么控制,大家不要模仿。
// 此代码刷新速度有点慢,注意想办法优化,time变量用定时器中断处理,主函数不断地刷新显示函数,这样就不会有闪屏,当然在STM32平台毫无压力
#include
sbit SCK = P3^2; // 数据输入时钟线,脉冲
sbit SI = P3^3; // 数据线
sbit RCK = P3^4; // 锁存
unsigned char code SMG[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
unsigned char Wei[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};//位选
unsigned char Val[8] = {0};
void Output(void)
{
RCK = 0;
RCK = 1;
}
void Write_Byte(unsigned char dat)
{
unsigned char i = 0;
//每次发送一位数据,8次发送完毕,先发送高位(切记)
for(i=0; i<8; i++)
{
SCK = 0;
SI = dat & 0x80;
SCK = 1;
dat <= 1;
}
}
void Display(unsigned char * p)
{
unsigned char * pt = Wei;
//注意,先发送位选,因为控制位选的595芯片在控制段选的595芯片下一级
Write_Byte(*(pt+0));//位选,
Write_Byte(SMG[*(p+0)]);//段选
Output();//发送到595 输出
Write_Byte(*(pt+1));
Write_Byte(SMG[*(p+1)]);
Output();
Write_Byte(*(pt+2));
Write_Byte(SMG[*(p+2)]);
Output();
Write_Byte(*(pt+3));
Write_Byte(SMG[*(p+3)]);
Output();
Write_Byte(*(pt+4));
Write_Byte(SMG[*(p+4)]);
Output();
Write_Byte(*(pt+5));
Write_Byte(SMG[*(p+5)]);
Output();
Write_Byte(*(pt+6));
Write_Byte(SMG[*(p+6)]);
Output();
Write_Byte(*(pt+7));
Write_Byte(SMG[*(p+7)]);
Output();
}
int main(void)
{
unsigned long int time = 0;
unsigned int t = 0;
while(1)
{
t++;
if(100==t)
{
t= 0;
time++;
Val[0] = time / 10000000 % 10;
Val[1] = time / 1000000 % 10;
Val[2] = time / 100000 % 10;
Val[3] = time / 10000 % 10;
Val[4] = time / 1000 % 10;
Val[5] = time / 100 % 10;
Val[6] = time / 10 % 10;
Val[7] = time % 10;
}
Display(Val);
}
return 0;
}