每秒顯示的數字加1,並在數碼管上顯示出來。
程序要求:
定時器定時,數碼管顯示時間,並且數碼管數字顯示每1s數字加1,顯示0-9999。
使用74LS373控制I/O口,可以節省I/O口。
74LS373簡介:
74LS373是三態輸出的八D鎖存器,共有54S373和74LS373兩種線路。373 的輸出端 Q0~Q7 可直接與總線相連。當鎖存允許端LE爲高電平時,Q 隨數據D而變,當LE爲低電平時,D 被鎖存在已建立的數據電平。
真值表:
電路原理圖:
程序代碼:
#include<reg52.h>
sbit duan=P2^6;
sbit wei=P2^7;
#define uchar unsigned char
#define uint unsigned int
uint num,count;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//數碼管對應 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
uchar smwei[] = {0,0,0,0};
void delay(uint i)
{
while(i--); //i=1時,大約延時10us
}
void DigDisplay()
{
uint i;
for(i=0;i<4;i++)
{
smwei[0] = num%1000%100%10;
smwei[1] = num%1000%100/10;
smwei[2] = num%1000/100;
smwei[3] = num/1000;
switch(i) //位選,選擇點亮的數碼管,
{
case(0):
wei=1;P0=0xfe;wei=0; break;//顯示第0位
case(1):
wei=1;P0=0xfd;wei=0; break;//顯示第1位
case(2):
wei=1;P0=0xfb;wei=0; break;//顯示第2位
case(3):
wei=1;P0=0x07;wei=0; break;//顯示第3位
}
duan=1;
P0=table[smwei[i]];//發送段碼
duan=0;
delay(1000); //間隔一段時間掃描
//P0=0x00;//消隱
}
}
void main()
{ ET1=1;
EA=1;
TMOD=0x10;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TR1=1;
while(1)
{
if(count==20)
{
count=0;
num++;
if(num==10000)
{
num=0;
}
}
DigDisplay();
}
}
void time1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
count++;
}