单片机:按秒计时 用16进制表示 15秒1响 1秒LED0 1闪
#include<reg51.h> #define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt,bz1=0,bz2=0,bz3=0; char buffer [3]; uint second,n; uchar minute; uchar hour; uchar code table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9 uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//0-F 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; sbit beep=P2^5; void keyscan(); void display(unsigned int second); void delay(unsigned int timer); void init(); void main() { init(); while(1) { if(tt==20) { led1=~led1;led2=~led2; if (bz1==15) { beep=0; bz1=0;} bz1=bz1+1; tt=0; second++; } keyscan(); display(second); //delay(1); beep=1; } } void keyscan() { if(key1==0) { TR0=1; } if(key2==0) { TR0=1; } if(key3==0) { TR0=1; } if(key4==0) { TR0=1; } } void init() { tt=0; n=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 int second) { P0=0XFF; switch(position) { case 0: smg_g=1;smg_q=0; P0=table[second/4096]; break; case 1: smg_q=1;smg_b=0; P0=table[(second-second/(16*16*16)*16*16*16)/(16*16)];break; case 2: smg_b=1;smg_s=0;P0=table[(second-second/(16*16)*16*16)/16];break; case 3: smg_s=1;smg_g=0;P0=table[(second-(second/(16*16)*16*16))%16];if(tt>=10) P0&=0x7f; 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--); }
#include<reg51.h> #define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt,bz1=0,bz2=0,bz3=0; char buffer [3]; uint second,n; uchar minute; uchar hour; uchar code table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9 uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//0-F 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; sbit beep=P2^5; void keyscan(); void display(unsigned int second); void delay(unsigned int timer); void init(); void main() { init(); while(1) { if(tt==20) { led1=~led1;led2=~led2; if (bz1==15) { beep=0; bz1=0;} bz1=bz1+1; tt=0; second++; } keyscan(); display(second); //delay(1); beep=1; } } void keyscan() { if(key1==0) { TR0=1; } if(key2==0) { TR0=1; } if(key3==0) { TR0=1; } if(key4==0) { TR0=1; } } void init() { tt=0; n=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 int second) { P0=0XFF; switch(position) { case 0: smg_g=1;smg_q=0; P0=table[second%65536/4096]; break; case 1: smg_q=1;smg_b=0; P0=table[(second%4096)/(256)];break; case 2: smg_b=1;smg_s=0;P0=table[second%256/16];break; case 3: smg_s=1;smg_g=0;P0=table[second%16];if(tt>=10) P0&=0x7f; 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--); }