俺家小三开始学习单片机开发,昨天晚上写的一个钟表小程序,发出来纪念一下。呵呵
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1=P3^4;
- sbit key2=P3^5;
- sbit key3=P3^6;
- sbit key4=P3^7;
- uchar code smg_du[]=
- {
- 0x3f,0x06,0x5b,0x4f, //0 1 2 3
- 0x66,0x6d,0x7d,0x07, //4 5 6 7
- 0x7f,0x6f,0x77,0x7c, //8 9 a b
- 0x39,0x5e,0x79,0x71,0x00 //c d e f null
- };
- uchar smg_we[]=
- {
- 0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f
- };
- sbit LACTCH1=P2^6;
- sbit LACTCH2=P2^7;
- uchar count;
- uchar clickcnt;
- uchar second;
- uchar minute;
- uchar hour;
- uchar dispbuf[6]={1,2,0,0,0,0};
- void delay(uint m)
- {
- while(m--);
- }
- void main(void)
- {
- uchar j;
- count=0;
- second=0;
- hour=0;
- TMOD=0x01; //0000,0001 设置定时器0工作方式1
- TH0=(65536-50000)/256; //设置初值 定时50ms
- TL0=(65536-50000)%256;
- EA=1; //打开总中断
- ET0=1; //打开定时器0 中断
- while(1)
- {
- if(key3==0)
- {
- delay(100);
- if(key3==0)
- {
- second++;
- if(second==60)
- {
- second=0;
- }
- dispbuf[5]=second%10;
- dispbuf[4]=second/10;
- dispbuf[5]=second%10;
- dispbuf[4]=second/10;
- while(key3==0);
- }
- }
- if(key2==0)
- {
- delay(100);
- if(key2==0)
- {
- minute++;
- if(minute==60)
- minute=0;
- dispbuf[2]=minute/10;
- dispbuf[3]=minute%10;
- while(key2==0);
- }
- }
- if(key1==0)
- {
- delay(100);
- if(key1==0)
- {
- hour++;
- if(hour==24)
- hour=0;
- dispbuf[1]=hour%10;
- dispbuf[0]=hour/10;
- while(key1==0);
- }
- }
- if(key4==0)
- {
- delay(200);
- if(key4==0)
- {
- clickcnt++;
- if((clickcnt%2)==1)
- {
- TR0=1;
- }
- else
- {
- TR0=0;
- }
- }
- while(key4==0);
- }
- for(j=0;j<6;j++)
- {
- P0=smg_du[dispbuf[j]];
- LACTCH1=1;
- LACTCH1=0;
- P0=smg_we[j];
- LACTCH2=1;
- LACTCH2=0;
- delay(100);
- }
- }
- }
- void timer_int0(void) interrupt 1
- {
- TH0=(65536-50000)/256; //设置初值 定时50ms
- TL0=(65536-50000)%256;
- count++;
- if(count==20)
- {
- count=0;
- second++;
- if(second==60)
- {
- second=0;
- minute++;
- if(minute==60)
- {
- minute=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- dispbuf[1]=hour%10;
- dispbuf[0]=hour/10;
- }
- dispbuf[3]=minute%10;
- dispbuf[2]=minute/10;
- }
- dispbuf[5]=second%10;
- dispbuf[4]=second/10;
- }
- }
阅读(2642) | 评论(0) | 转发(0) |