#include <reg51.h> #define uint unsigned int #define uchar unsigned char sbit wei=P2^7; sbit duan=P2^6; sbit key1=P3^4; sbit key2=P3^5; sbit key3=P3^6; uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//1~9 //0~9,0x77,0x7c,0x39,0x5e,0x79,0x71 uint number; void delay(uint xms)//延迟函数 { uint i;uchar j; for(i=xms;i>0;i--) for(j=110;j>0;j--); } void show(uint ge,uint shi) { ge=number%10; shi=number/10; wei=1; P0 = 0xfe; wei=0; duan=1; P0=table[shi]; duan=0; P0=0xff; delay(5); wei=1; P0=0xfd; wei=0; duan=1; P0=table[ge]; duan=0; P0=0xff; delay(5); } void initial_mcu() { TMOD=0x01; //定时器0和1工作在16位模式 TH0=(65536-45872)/256; TL0=(65536-45872)%256; EA=1; ET0=1; //TR0=1; } void buttons() { if(key1==0) { delay(10); //键盘消抖 if(key1==0) { number=0; } } if(key2==0) { delay(10); //键盘消抖 if(key2==0) { TR0=1; } } if(key3==0) { delay(10); //键盘消抖 if(key3==0) { TR0=0; } } } void main () { initial_mcu();//定时器初始化 while(1) { buttons(); show(); } } void timer0() interrupt 1 { uint num; TH0=(65536-45872)/256; TL0=(65536-45872)%256; num++; if(num==20) { num=0; number++; if(number==100) { number=0; } } }