今天继续调试板子,写了一个简单的数字钟视频。
C代码如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit LS138A = P2^0; //定义138译码器的输入A脚由P2.0控制
sbit LS138B = P2^1; //定义138译码器的输入脚B由P2.1控制
sbit LS138C = P2^2; //定义138译码器的输入脚C由P2.2控制
uchar code seven_seg[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
uchar code dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(uint i)
{ char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
typedef struct{
char hour;
char minute;
char second;
}time;
time now;
uint ledout[10];
void main()
{ uchar i,delaycnt=0;
EA=1;
EX0=1;
EX1=1;
IT0=1;
IT1=1;
now.second=0;
now.minute=58;
now.hour=23;
while(1)
{ if(++delaycnt==20)
{ delaycnt=0;
now.second++;
if(now.second==60)
{ now.second=0;
now.minute++;
if(now.minute==60)
{ now.minute=0;
now.hour++;
if(now.hour==24)
{ now.hour=0;
}
}
}
}
ledout[7]=seven_seg[now.second%10];
ledout[6]=seven_seg[now.second/10];
ledout[5]=0x40;
ledout[4]=seven_seg[now.minute%10];
ledout[3]=seven_seg[now.minute/10];
ledout[2]=0x40;
ledout[1]=seven_seg[now.hour%10];
ledout[0]=seven_seg[now.hour/10];
for(i=0;i<8;i++)
{ P0=ledout[i];
switch(i) //使用switch 语句控制位选
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;
}
delay(250);
}
}
}
void int0() interrupt 0
{ now.hour++;
if(now.hour==24)
now.hour=0;
}
void int1() interrupt 2
{ now.minute++;
if(now.minute==60)
now.minute=0;
}