#include "reg52.h"
#include "intrins.h"
sbit LED1 = P1^1; // LED灯
sbit SLED1 = P2^4; // 数码管
sbit SLED2 = P2^5;
sbit SLED3 = P2^6;
sbit SLED4 = P2^7;
unsigned char counter;
unsigned char time_counter;
// 数码管显示的数字
typedef unsigned char uchar;
uchar code dataArr[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
// ms级延时函数
void delay(unsigned int xms)
{
unsigned int i,j;
for(i = xms; i > 0; i--)
for(j = 114; j > 0; j--);
}
// 数码管计时
void display(unsigned char time_counter)
{
unsigned char shi, ge;
shi = time_counter / 10;
ge = time_counter % 10;
// 只让数码管1亮
SLED1 = 1;
SLED2 = 0;
SLED3 = 0;
SLED4 = 0;
P0 = dataArr[shi]; // 十位
delay(2);
// 只让数码管2亮
SLED1 = 0;
SLED2 = 1;
SLED3 = 0;
SLED4 = 0;
P0 = dataArr[ge]; // 个位
delay(2);
}
void main()
{
// 定时器
/* 定时50ms
11.0592晶振的机器周期为 1/11.0592 * 12 = 1.085ns
50000/1.085 = 46082
65536-46082 = 19454 = 4bfe
*/
TMOD = 0x01; // 选择定时器模式1 定时器0
TH0 = (65536 - 46082)/256; // 定时50ms
TL0 = (65536 - 46082)%256;
TR0 = 1; // 启动定时器0
LED1 = 0;
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0x4b;
TL0 = 0xfe;
counter++;
}
if(counter == 20)
{
LED1 = ~LED1;
counter = 0;
time_counter++;
}
if(time_counter == 60)
{
time_counter = 0; // 到60s计时器清零
}
display(time_counter);
}
}