|||
原理图未变...
参考代码:
//**********************************************************
//Deion:using mode 1 of timer 0 to delay 50 ms
//Coder:Benjamin Lau
//Time:2009-11-01
//**********************************************************
#include <reg51.h>
typedef unsigned char uchar;
uchar ct_1s = 0;
uchar decimal = 2;
uchar unit = 4;
uchar code sevenseg[10] = {0x0c0,0x0f9,0x0a4,0x0b0,0x99,0x92,0x82,0x0f8,0x80,0x90};
//Timer 0 interrupt service program
void display(void);
void compute(void);
void timer_0(void) interrupt 1 using 1
{
TH0 = 0X3C;
TL0 = 0X0B0;
ct_1s ++;
}
void main(void)
{
TMOD = 0X01;//TIMER0,MODE 1
TH0 = 0x3c;
TL0 = 0X0B0;//the preset number of 50ms
ET0 = 1;
EA = 1;//Enable the interrupt
TR0 = 1;//turn the timer0 on
while(1)
{
compute();
display();
}
}
//compute the result of decimal and the unit
void compute(void)
{
if(ct_1s == 20)
{
unit -- ;
ct_1s = 0;
}
if(decimal != 0)
{
if(unit == 255)
{
decimal --;
unit = 9;
}
}
else
{
if(unit == 255)
{
unit = 0;
TR0 = 0;
}
}
}
//display the result
void display(void)
{
P0 = sevenseg[decimal];
P2 = sevenseg[unit];
}