利用51单片机,4个数码管设计一个计时器,要求在数码管上显示的数据从0开始每1秒钟加1。
电路图与程序,c语言和汇编都行 展开
现成的有与之差不多的,功能是倒计时,到一定值后停止
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77,
0x7C, 0x39, 0x5E, 0x79, 0x71, 0x76, 0x38, };
sbit duan=P2^0;
sbit wei=P2^1;
uchar temp,tem,flag,chu;
uint dsec;
void init ();
void delay (uchar );
void display ();
void main ()
{
init ();
while (1)
{
display();
}
}
void init ()
{
flag=0;
temp=0xfe;
tem=0x01;
chu=0xff;
dsec=432;
TMOD=0x11;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
P1=0x00;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
}
void delay (uchar x)
{
uchar y;
for ( ;x>0;x--)
for (y=50;y>0;y--);
}
void display ()
{
uchar shu ,j,zi;
wei=1;
P0=temp;
wei=0;
duan=1;
P0=table[zi];
delay(2);
duan=0;
P0=0xff;
shu++;
if (shu==6) {shu=0 ;}
switch (shu)
{
case 0 : zi=7; break;
case 1 : zi=6 ;break ;
case 2 : zi=5; break;
case 3 : zi=dsec/100;break ;
case 4 :zi=dsec%100/10; break ;
case 5 :zi=dsec%10 ;break;
default : break ;
}
temp=_crol_(temp,1);
j++;
if(j==6)
{
j=0;
temp=0xfe;
}
}
void tr0 () interrupt 1
{
uchar ms;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
ms++;
if (ms==10)
{
ms=0;
dsec--;
if (dsec==398)
{
TR0=0;
flag=1;
}
}
}
void tr1 () interrupt 3
{
uchar tt,time;
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
tt++;
if(tt==10)
{
tt=0;
if (flag==0)
{
P1=tem;
tem=_crol_(tem,1);
}
else
{
P1=chu;
chu=~chu;
time++;
if(time==6)
{
TR1=0;
}
}
}
}
2024-12-13 广告