1. 程式人生 > 其它 >微控制器:按秒計時,10秒響一次 每秒LED0 1亮 10進位制

微控制器:按秒計時,10秒響一次 每秒LED0 1亮 10進位制

#include<reg51.h>
#define uchar unsigned char;
#define uint unsigned int;
uchar position;           
uchar tt,bz1=0,bz2=0,bz3=0;             
uint 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; 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==10) { 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; 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/1000]; break; case 1: smg_q=1;smg_b=0; P0=table[(second-second/1000*1000)/100];break; case 2: smg_b=1;smg_s=0;P0=table[(second-second/100*100)/10];break; case 3: smg_s=1;smg_g=0;P0=table[(second-second/100*100)%10];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--); }