C52单片机6位数码管时钟
发布时间:2024-10-30
发布时间:2024-10-30
#include <reg52.h>
# define uchar unsigned char
# define uint unsigned int
sbit wela =P2^7; //数码管位选信号
sbit dula =P2^6; //数码管段选信号
int time;
int W1,W2,W3,W4,W5,W6;
/*延时子函数*/
void delayms(uint time)
{
uint i;
for(;time>0;time--)
{
for(i=0;i<124;i++)
{;}
}
}
/*数码管显示子函数--不带小数点*/
void display (int num1,int num2) // num1为显示的位置,num2为要显示的数字0-9 {
uchar code wei[]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管各位的码表,为低时选中某一位
uchar code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,}; //段选0-9的码表,为低时某一段亮
P0=wei[num1-1];
wela=1;
wela=0;
switch(num2)
{
case 0: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 1: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 2: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 3: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 4: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 5: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 6: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 7: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 8: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
case 9: P0=duan[num2]; dula=1; dula=0; delayms(2); break;
}
}
/*数码管显示子函数--带小数点*/
void display2 (int a,int b) // a为显示的位置,b为要显示的数字0.-9.
{
uchar code wei[]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管各位的码表,为低时选中某一位
uchar code duan[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,}; //段选0.-9.的码表,为低时某一段亮
P0=wei[a-1];
wela=1;
wela=0;
switch(b)
{
case 0: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 1: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 2: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 3: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 4: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 5: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 6: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 7: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 8: P0=duan[b]; dula=1; dula=0; delayms(2); break;
case 9: P0=duan[b]; dula=1; dula=0; delayms(2); break;
}
}
/*定时器T0初始化子函数*/
void initTimer0 (void)
{
TMOD = 0x01; //模式1,16位T0,定时10ms
TH0 = 0xD8; //初值
TL0 = 0xF0;
EA = 1; //开总中断
ET0 = 1; //允许T0中断
TR0 = 1; //启动T0
}
/*定时器T0中断子程序*/
void Timer0 (void) interrupt 1
{
int a;
time++;
a=time;
W1=a%10;a/=10;
W2=a%10;a/=10;
W3=a%6;a/=6;
W4=a%10;a/=10;
W5=a%6;a/=6;
W6=a%10;a/=10;
}
/*主函数*/
void main()
{
initTimer0(); //初始化
while(1) {
display (1,W1) ; //数码管显示 display2 (2,W2) ;
display (3,W3) ;
display2 (4,W4) ;
display (5,W5) ;
display2 (6,W6) ;
}
}