#include<reg51.h> #define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt; uint second; uchar minute; uchar hour; uint bz=0,bza=0; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//?--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; void keyscan(); //检查是否按键,开启 暂停 K1继续 K2暂停 K3 重新开始 void display(unsigned int second);//显示时分 void delay(unsigned int timer);//延时 void init(); //初始化 void main() { tt=0; init(); while(1) { if(tt==20) { tt=0; second++; } keyscan(); display(second); delay(1); } } void keyscan() { if(key1==0) //如果按键1 { //TR0=0; TR0=1; } if(key2==0) // ??? { TR0=0; //TR0=1; } if(key3==0) // ??? { tt=0; init(); TR0=1; //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//中断服务程序:0为外部中 0 ,1定时器0,2外部中断1,3定时器1,4串行中断 { TH0=0x4c; TL0=0x00; //TH0=(65536-50000)/256; //定时器T0重新赋值:0X3C //TL0=(65536-50000)%256;//0XB0 15536转成16进制为:3CB0 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]; //if(tt>=10) //P0&=0xff; //第二数码管右下角的点(0x7f) 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; //第二数码管右下角的点(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--); }