给你一个仿真示例,如果实物效果不好就按10楼建议处理,以留给主函数足够时间运行其它代码。
- #include <reg51.H>
- #define uint unsigned int
- #define uchar unsigned char
- #define ulong unsigned long
- sbit P_HC595_SER = P2^0;
- sbit P_HC595_SRCLK = P2^1;
- sbit P_HC595_RCLK = P2^2;
- sbit A0=P2^3;
- sbit B0=P2^4;
- sbit C0=P2^5;
- uchar code table[]={
- // 0 1 2 3 4 5 6 7 8 9 A B C D E F
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
- // G H J K L M N o P Q r t
- 0x3d,0x76,0x1E,0x70,0x38,0x37,0x54,0x5C,0x73,0x67,0x50,0x78};
- ulong num=0xfffffffe;
- bit flag;//标志
- /**************** 向HC595发送一个字节函数 ******************/
- void Send_595(uchar dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- dat <<= 1;
- P_HC595_SER = CY; //溢出位赋值数据输出端
- P_HC595_SRCLK = 1; //移位时钟
- P_HC595_SRCLK = 0;
- }
- }
- /**************** 向HC595发送5个字节驱动码 ******************/
- void DisplayScan()
- {
- static uchar i;
- Send_595(num>>24);//向HC595发送位码
- Send_595(num>>16);//向HC595发送位码
- Send_595(num>>8);//向HC595发送位码
- Send_595(num); //向HC595发送位码
- Send_595(table[i]);//向HC595发送段码
- P_HC595_RCLK = 1;//锁存输出数据
- P_HC595_RCLK = 0;
- num=num<<1|0x01;
- if(++i>=27)
- {
- num=0xFFFFFFFE;
- i=0;
- }
- }
- void InitTimer0(void)//740us
- {
- TMOD = 0x01;
- TH0 = 0x0FD;
- TL0 = 0x1C;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void main()
- {
- P_HC595_SRCLK = 0;//初始化移位时钟端
- P_HC595_RCLK = 0;//初始化锁存输出端
- InitTimer0();
- while(1)
- {
- if(flag)
- {
- flag=0;
- DisplayScan();
- //其它程序
- }
- }
- }
- void Timer0Interrupt(void) interrupt 1
- {
- TH0 = 0xFD;
- TL0 = 0x1C;
- flag=1;
- }
复制代码
|