单片机:数码管显示计时,LED0 1 1秒一闪 LED2 3 1分一闪 LED5 LED6 1小时一闪 K1 K2调整时 K3 K4调整分钟
#include<reg51.h> #define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt,bz1=0,bz2=0; uchar second; uchar minute; uchar hour; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9 sbit smg_q = P2^0; sbit smg_b = P2^1; sbit smg_s = P2^2; sbit smg_g = P2^3; sbit key1=P3^2; sbit key2=P3^3; sbit key3=P3^4; sbit key4=P3^5; sbit led1=P1^0; sbit led2=P1^1; sbit led3=P1^2; sbit led4=P1^3; sbit led5=P1^4; sbit led6=P1^5; void keyscan(); void display(unsigned char hour,unsigned char minute); void delay(unsigned int timer); void init(); void main() { init(); while(1) { if(tt==20) { led1=~led1;led2=~led2; if (bz1==1 ) { led3=~led3;led4=~led4; bz1=0; } if (bz2==1 ) { led5=~led5;led6=~led6; bz2=0; } tt=0; second++; if(second==60) { led3=~led3;led4=~led4; bz1=1; second=0; minute++; if(minute==60) { led5=~led5;led6=~led6; bz2=1; minute=0; hour++; if(hour==24) hour=0; } } } keyscan(); display(hour,minute); delay(1); } } void keyscan() { if(key1==0) { TR0=0; hour++; if(hour==24) hour=0; delay(200) ; TR0=1; } if(key2==0) {TR0=0; hour--; if(hour<0) hour=23; delay(200) ; TR0=1; } if(key3==0) { TR0=0; minute++; if(minute==60) minute=0; delay(200) ; TR0=1; } if(key4==0) { TR0=0; minute--; if(minute<0) minute=59; delay(200) ; TR0=1; } } void init() { tt=0; position=0; second=0; minute=0; hour=0; smg_q=1; smg_b=1; smg_s=1; smg_g=1; key1=1; key2=1; TMOD=0X01; TH0=0x4c; TL0=0x00; EA=1; ET0=1; TR0=1; } void t0() interrupt 1 { TH0=0x4c; TL0=0x00; tt++; } void display(unsigned char hour,unsigned char minute) { P0=0XFF; switch(position) { case 0: smg_g=1;smg_q=0;P0=table[hour/10]; break; case 1: smg_q=1;smg_b=0; P0=table[hour%10];if(tt>=10) P0&=0x7f; break; case 2: smg_b=1;smg_s=0;P0=table[minute/10];break; case 3: smg_s=1;smg_g=0;P0=table[minute%10];break; } position++; if(position>3) position=0; } void delay(unsigned int time) { unsigned int x,y; for(x=time;x>0;x--) for(y=110;y>0;y--); }