初学
单片机,编个秒表,原本的初值是00-00-00,但数码管显示是70-00-00 。
#include<reg52.h>
#include<intrins.h>
unsigned int a = 40;
unsigned char dis[7];
unsigned char code
time_b[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned char time_buf[] = {0,0,10,0,0,10,0,0};
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
void Delay_xMs(unsigned int x) //延时
{
unsigned int i,j;
for( i =0;i < x;i++ )
{
for( j =0;j<50;j++ );
}
}
void display(void)//数码管显示
{
unsigned char i,temp1,duan,wei;
wei=0xff;
for (i = 0;i < 8;i ++)
{
temp1=dis[i];
duan=time_b[time_buf[temp1]] ;
wei ++;
P2=_crol_(wei,2);
P0=duan;
Delay_xMs(1);
}
}
timer0( ) interrupt 1 using 1 //中断
{
a --;
if(a == 0)
{
a = 40;
time_buf[7] += 1;
if(time_buf[7] >= 10)
{
time_buf[7] = 0;
time_buf[6] += 1;
if(time_buf[6] >= 10)
{
time_buf[6] = 0;
time_buf[4] += 1;
if(time_buf[4] >= 10)
{
time_buf[4] = 0;
time_buf[3] += 1;
if(time_buf[3] >= 6)
{
time_buf[3] =0;
time_buf[1] += 1;
if(time_buf[1] >= 10)
{
time_buf[1] = 0;
time_buf[0] += 1;
}
}
}
}
}
}
}
main()
{
TH0 = 0x06;
TL0 = 0x06;
TMOD = 0x02;
IE = 0x82;
TR0 = 1;
dis[0]=0;
dis[1]=1;
dis[2]=2;
dis[3]=3;
dis[4]=4;
dis[5]=5;
dis[6]=6;
dis[7]=7;
while(1)
{
display();
}
}
一周热门 更多>