两片 74HC595级联动态驱动 8位数码管 51单片机 #include sbit SCK = P1^1; // 数据输入时钟线,脉冲 sbit SI = P1^0; // 数据线 sbit RCK = P1^2; // 锁存 unsigned char code SMG[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; // 段码 unsigned char code Wei[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; // 位选 unsigned char Val[8] = {0}; // 要显示的数据 ************************ 函数声明 ************************ void interrupt_init(void); void timer_init(void); 控制74HC595 输出数据 void Output(void) { RCK = 0; RCK = 1; } 向74HC595 中写入一字节数据 void Write_Byte(unsigned char dat) { unsigned char i = 0; for(i=0; i<8; i++) { SCK = 0; SI = dat & 0x80; SCK = 1; dat <<= 1; } } 显示函数 void Display(unsigned char * p) { unsigned char * pt = Wei; Write_Byte(*(pt+0)); Write_Byte(SMG[*(p+7)]); Output(); Write_Byte(*(pt+1)); Write_Byte(SMG[*(p+6)]); Output(); Write_Byte(*(pt+2)); Write_Byte(SMG[*(p+5)]); Output(); Write_Byte(*(pt+3)); Write_Byte(SMG[*(p+4)]); Output(); Write_Byte(*(pt+4)); Write_Byte(SMG[*(p+3)]); Output(); Write_Byte(*(pt+5)); Write_Byte(SMG[*(p+2)]); Output(); Write_Byte(*(pt+6)); Write_Byte(SMG[*(p+1)]); Output(); Write_Byte(*(pt+7)); Write_Byte(SMG[*(p+0)]); Output(); } int main(void) { timer_init(); interrupt_init(); while(1) { Display(Val); } return 0; } void interrupt_init(void) { EA = 1; //开总中断 ET0 = 1; //开定时器 0 中断 ET1 = 1; //开定时器 1 中断 } void timer_init(void) { TMOD = TMOD | 0x01; //定时器 0 工作方式 1 TMOD = TMOD & 0xFD; TH0 = 0x4B; //装初值,50ms 计数 TL0 = 0xFF; TR0 = 1; //开启定时器 0 } void timer0() interrupt 1 { static unsigned char counter0 = 0; counter0++; TH0 = 0x4B; //重新装入初值,定时器 0 从头开始计数,计数 50ms TL0 = 0xFF; if(2 == counter0) //2*50 ms = 100ms = 0.1s { counter0 = 0; //counter0 置零,定时器0 从头开始计数 Val[0]++; if(10==Val[0]) { Val[0] = 0; Val[1]++; if(10==Val[1]) { Val[1] = 0; Val[2]++; if(10==Val[2]) { Val[2] = 0; Val[3]++; if(10==Val[3]) { Val[3] = 0; Val[4]++; if(10==Val[4]) { Val[4] = 0; Val[5]++; if(10==Val[5]) { Val[5] = 0; Val[6]++; if(10==Val[6]) { Val[6] = 0; Val[7]++; if(10==Val[7]) { Val[7] = 0; } } } } } } } } } }