- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
-
- void time(void);
- void digitron(void);
- void timemin(void);
- void DelayMs(uchar n);
-
- uchar I,MIN; //计时器的变量声明
- sfr P0M1=0X91;
- sfr P0M0=0X92;
- sbit P17=P1^7;
-
- uchar led[]={0x3f,0x05,0x5b,0x4f,0x65, //共阴极数码管,管脚链接顺序p1.7 p1.6 p1.5 p1.4 p1.3 p1.2 p1.1 p1.0
- 0x6e,0x7e,0x07,0x7f,0x6f}; //段码的编码 h g f e d c a b
- uchar led1[]={0xbf,0xef};
- uchar led2[2];
- void T0Isr()interrupt 1 //定时器0的中断
- {
-
- I++; // 50ms计数器,用于扩展计时值
- }
- void main(void)
- {
-
- P0M1=0X00;
- P0M0=0XFF;
- TH0=0X3c; //定时器的初值,高位
- TL0=0Xb0; //定时器低位初值,50ms初值为15536
- TMOD=0X00; //设置定时器工作模式,15W的工作模式0是16位自动重装
- TR0=1; // 定时器0工作设置
- IE=0X82; //开总中断,开T0中断
-
- while(1)
- {
- time();
- timemin();
- digitron();
- }
- }
- void time(void) //秒计时函数
- {
- uint SEC,MS;
- if (I==10)
- {
- MS++; //
- I=0;
- }
- if(MS==2)
- {
- SEC++;
- MS=0;
- }
- if(SEC==59)
- {
- MIN++;
- SEC=0;
- }
- }
- void digitron(void)
- {
- uchar i;
- for(i=0;i<2;i++)
- {
- P1=led[led2[i]];
- P3=led1[i];
- DelayMs(10);
- }
- }
- void timemin(void)
- {
- led2[1]=MIN/10;
- led2[0]=MIN%10;
- }
- void DelayMs(uchar n)
- {
- uchar j;
- while(n--)
- {
- for(j=0;j<113;j++);
- }
- }
复制代码 |