单片机实例
&运用138译码器
基本实现1毫秒累加1
存在误差,可通过计算单片机循环以及程序步骤运行时间增减定时中断中的TH0/TL0
最高位因与其他位等待时间不同导致最高位亮度高于其它位
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint t;
long x,p,o;
uchar z,y,a;
uchar w,d;
uchar code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uchar code table2[]={0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c,0x20,0x24};
void print(w,d);
void main()
{
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
p=x;
o=x;
a=0x01;
while(o)
{
o=o/10;
if(o>0)
{
a++;
}
}
z=0x00;
while(a--)
{
y=p%10;
p=p/10;
print(z,y);
z++;
}
z=0x00;
}
}
void print(w,d)
{
P2=table2[w];
P0=table[d];
}
void rupt() interrupt 1
{
t++;
if(t==1)
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
x++;
t=0;
}
}
|