第1个回答 2009-11-21
//===============================
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar int_cishu, shi, ge;
uchar code duan_table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; //共阴段码
//-------------------------------
void delayms(uint i) //延时
{
uint j;
for(; i > 0; i--)
for(j = 100; j > 0; j--);
}
//-------------------------------
void T0_time() interrupt 1 //T0中断函数
{
TH0 = (65536-50000)/256; //初值, 定时50ms @ 12MHz
TL0 = (65536-50000)%256;
int_cishu++; //中断次数加一
if(int_cishu == 20) { //到了20次,1秒
int_cishu = 0;
ge++;
if(ge == 10) { //到了10
ge = 0;
shi++;
if(shi == 10) shi = 0; //到了10
}
}
}
//-------------------------------
void xian_shi(uchar x, uchar y) //显示
{
//P0、P2口,各自外接一个共阴LED数码显示器
P0 = duan_table[x]; //输出十位段码
delayms(2);
P2 = duan_table[y]; //输出个位段码
delayms(2);
}
//-------------------------------
void init(void) //初始化
{
TMOD = 0x01; //定时器0 工作方式1
TH0 = (65536-50000)/256; //初值, 定时50ms @ 12MHz
TL0 = (65536-50000)%256;
TR0 = 1; //T0启动
ET0 = 1; //开T0中断
EA = 1; //开总中断
}
//-------------------------------
void main() //主函数
{
init();
while(1) { //无尽循环
xian_shi(shi, ge); //显示, 等待中断
}
}
//===============================