#include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit we = P2^7; sbit du = P2^6; sbit LED1=P1^0; uchar counter,time_counter,n; uchar code leddata[]={ 0x3F, //"0" 0x06, //"1" 0x5B, //"2" 0x4F, //"3" 0x66, //"4" 0x6D, //"5" 0x7D, //"6" 0x07, //"7" 0x7F, //"8" 0x6F, //"9" 0x77, //"A" 0x7C, //"B" 0x39, //"C" 0x5E, //"D" 0x79, //"E" 0x71, //"F" 0x76, //"H" 0x38, //"L" 0x37, //"n" 0x3E, //"u" 0x73, //"P" 0x5C, //"o" 0x40, //"-" 0x00, //熄灭 0x00 //自定义 }; void delay(uint z) { uint x,y; for(x = z; x > 0; x--) for(y = 114; y > 0 ; y--); } void display(uchar i) { uchar ge,shi; ge=i%10; shi=i/10; P0 = 0xff; we = 1; P0 = 0xfe;//点亮第一位数码管 we = 0; du = 1; P0 = leddata[shi]; du = 0; delay(1); P0=0xff; we = 1; P0 = 0xfd;//点亮第二位数码管 we = 0; du = 1; P0 = leddata[ge]; du = 0; delay(1); } void dingshi() { TMOD=0x11; //定时器0,1; 工作模式1; TH0=TH1=0x4b; TL0=TL1=0xfd; //50ms TR0=TR1=1; //启动定时器 0,1 ET0=ET1=1; //开定时器中断 EA=1; //开总中断 } void main() { dingshi(); while(1) { if(time_counter==60) time_counter=0; display(time_counter); } } /*定时器0中断程序*/ void dingshi0() interrupt 1 { TH0=0x4b; TL0=0xfd; counter++; if(counter==4) { LED1=~LED1; counter=0; } } /*定时器1中断程序*/ void dingshi1() interrupt 3 { TH1=0x4b; TL1=0xfd; n++; if(n==20) { n=0; time_counter++; } } |