上班闲着没事,找来一个数码管的模块,编写了一段程序,可做秒表计数,程序大家有需要的自己下载!
单片机源程序如下:
- #include <STC8G.h>
- #include <intrins.h>
- #define u8 unsigned char
- #define u16 unsigned int
- sbit DIO = P1^0;
- sbit CLK = P1^1;
- //u8 num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7f,0x6F};
- u8 num[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFf,0xEF};
- u16 ms,second,min,hour;
- void Delay2us() //@11.0592MHz
- {
- unsigned char i;
- _nop_();
- i = 5;
- while (--i);
- }
- void Delay3us() //@11.0592MHz
- {
- unsigned char i;
- i = 9;
- while (--i);
- }
- void Delay5us() //@11.0592MHz
- {
- unsigned char i;
- _nop_();
- i = 16;
- while (--i);
- }
- void I2CStart()
- {
- CLK = 1;
- DIO = 1;
- Delay2us();
- DIO = 0;
- }
- void I2CAsk()
- {
- CLK = 0;
- Delay5us();
- while(DIO);
- CLK = 1;
- Delay2us();
- CLK = 0;
- }
- void I2CStop()
- {
- CLK = 0;
- Delay2us();
- DIO = 0;
- Delay2us();
- CLK = 1;
- Delay2us();
- DIO = 1;
- }
- void I2CWriteByte(u8 dat)
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- CLK = 0;
- if(dat&0x01)
- {
- DIO = 1;
- }
- else
- {
- DIO = 0;
- }
- Delay3us();
- dat>>=1;
- CLK = 1;
- Delay3us();
- }
- }
- void Display(u8 m,u8 s)
- {
- I2CStart();
- I2CWriteByte(0x40);
- I2CAsk();
- I2CStop();
- I2CStart();
- I2CWriteByte(0xC0);
- I2CAsk();
- I2CWriteByte(num[m/10]);
- I2CAsk();
- I2CWriteByte(num[m%10]);
- I2CAsk();
- I2CWriteByte(num[s/10]);
- I2CAsk();
- I2CWriteByte(num[s%10]);
- I2CAsk();
- I2CWriteByte(0xFF);
- I2CAsk();
- I2CWriteByte(0xFF);
- I2CAsk();
- I2CStop();
- I2CStart();
- I2CWriteByte(0x8F);
- I2CAsk();
- I2CStop();
- }
- void Timer0Init(void) //1毫秒@11.0592MHz
- {
- AUXR |= 0x80;
- TMOD &= 0xF0;
- TL0 = 0xCD;
- TH0 = 0xD4;
- ET0 = 1;
- EA = 1;
- TF0 = 0;
- TR0 = 1;
- }
- void TM0_Isr() interrupt 1
- {
- ms++;
- if(ms==1000)
- {
- ms=0;
- second++;
- if(second==60)
- {
- second=0;
- min++;
- if(min==60)
- {
- min=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- }
- }
- void main()
- {
- P1M0 = 0x00;
- P1M1 = 0x00;
- Timer0Init();
- while(1)
- {
- Display(min,second);
- }
- }
复制代码
所有代码51hei提供下载:
四位数码管显示.rar
(40.4 KB, 下载次数: 58)
|