#include
#define uchar unsigned char
#define uint unsigned int
sbit le=P1^0;
sbit key1=P3^7;
sbit key2=P3^6;
sbit key3=P3^5;
uchar hour,minute,second,t0,a8,a7,a5,a4,a2,a1;
void init();
void display(uchar a8,uchar a7,uchar a5,uchar a4,uchar a2,uchar a1);
uchar discode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
void delay(uint z)
{
uint x,y;
for(x=0;x for(y=0;y<120;y++); } void main() { init(); while(1) { if(key1==0) { delay(10); if(key1==0) { second++; if(second==60) { second=0; } a1=second%10; a2=second/10; }while(key1==0); } if(key2==0) { delay(10); if(key2==0) { minute++; if(minute==60) { minute=0; } a4=minute%10; a5=minute/10; }while(key2==0); } if(key3==0) { delay(10); if(key3==0) { hour++; if(hour==24) { hour=0; } a7=hour%10; a8=hour/10; }while(key3==0); } display(a8,a7,a5,a4,a2,a1); } } void init() { hour=12; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } void timer0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; t0++; if(t0==20) { t0=0; second++; if(second==60) { second=0; minute++; if(minute==60) { minute=0; hour++; if(hour==24) { hour=0; } } } a2=second/10; a1=second%10; a5=minute/10; a4=minute%10; a8=hour/10; a7=hour%10; } } void display(uchar a8,uchar a7,uchar a5,uchar a4,uchar a2,uchar a1) { P2=0x7f; le=1; P0=discode[a8]; le=0; delay(1); P2=0xbf; le=1; P0=discode[a7]; le=0; P2=0xdf; le=1; P0=0x40; le=0; delay(1); P2=0xef; le=1; P0=discode[a5]; le=0; delay(1); P2=0xf7; le=1; P0=discode[a4]; le=0; P2=0xfb; le=1; P0=0x40; le=0; P2=0xfd; le=1; P0=discode[a2]; le=0; delay(1); P2=0xfe; le=1; P0=discode[a1]; le=0; delay(1); } 注意:J16接单片机P2口,J12接单片机P0口。