#include <reg52.h>
/*38译码器,使动态数码管全亮*/
unsigned char vector_38[8] = {0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0};
unsigned char led_display[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71}; // 数码管段选
unsigned char led_buffer[10] = {0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00}; // 数码管显示数字缓存,可以在Main里任意读
void Delay(float sec)
{
/*S:秒,最小支持输入0.02S*/
unsigned int base;
unsigned int conter;
TH0 = 0xFC;
TL0 = 0x67;
base = 1; //基本是1ms ,TH溢出
conter = sec * 1000 / base;
while(conter)
{
if (TF0 == 1)
{
conter--;
TH0 = 0xB8;
TL0 = 0x00;
}
}
}
void main()
{
unsigned int l; //控制数码管列表地址
unsigned int sec_1; //控制循环1S的次数
unsigned int show_sec_l4; //显示总秒数低四位9999
unsigned int show_sec_h4; //存储总秒数高四位,低到10000向高进位
TR0 = 1;
TMOD = 1;
P1 = 0x00; //默认低电平数码管不显示。初始都不显示
l = 0;
sec_1 = 0;
show_sec_l4 = 0;
show_sec_h4 = 0;
while(1)
{
P1 = led_buffer[l];
Delay(0.1);
/*控制数码管全亮*/
if (l > 7) {l = 0;}
P2 &= 0x1F; //清空P2口的5。6。7管脚,不影响其它管脚
P2 |= vector_38[l]; //给5。6。7管脚赋值,不影响其它管脚
if (sec_1 >= 100)
{
sec_1 = 0;
show_sec_l4++;
if (show_sec_l4 >= 9999)
{
show_sec_l4 = 0;
show_sec_h4++;
}
/*如果把led_fuffer顺序倒过来,数码管数字变动会变慢*/
led_buffer[7] = led_display[show_sec_l4%10];
led_buffer[6] = led_display[show_sec_l4/10%10];
led_buffer[5] = led_display[show_sec_l4/100%10];
led_buffer[4] = led_display[show_sec_l4/1000%10];
led_buffer[3] = led_display[show_sec_h4%10];
led_buffer[2] = led_display[show_sec_h4/10%10];
led_buffer[1] = led_display[show_sec_h4/100%10];
led_buffer[0] = led_display[show_sec_h4/1000%10];
}
sec_1++;
l++;
P1 = 0x00; //清除鬼影
}
}