一个51单片机电子钟程序,求详细的解释啊急急急!!!
#include<AT89X51.H>unsignedcharcodedispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x0...
#include <AT89X51.H>
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char dispbitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char dispbuf[8]={0,0,16,0,0,16,0,0};
unsigned char dispbitcnt;
unsigned char second;
unsigned char minite;
unsigned char hour;
unsigned int tcnt;
unsigned char mstcnt;
unsigned char i,j;
void main(void)
{
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(P0_0==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if (P0_0==0)
{
second++;
if(second==60)
{
second=0;
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
while(P0_0==0);
}
}
if(P0_1==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_1==0)
{
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(P0_1==0);
}
}
if(P0_2==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(P0_2==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
mstcnt++;
if(mstcnt==8)
{
mstcnt=0;
P1=dispcode[dispbuf[dispbitcnt]];
P3=dispbitcode[dispbitcnt];
dispbitcnt++;
if(dispbitcnt==8)
{
dispbitcnt=0;
}
}
tcnt++;
if(tcnt==4000)
{
tcnt=0;
second++;
if(second==60)
{
second=0;
minite++;
if(minite==60)
{
minite=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
} 展开
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
unsigned char dispbitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char dispbuf[8]={0,0,16,0,0,16,0,0};
unsigned char dispbitcnt;
unsigned char second;
unsigned char minite;
unsigned char hour;
unsigned int tcnt;
unsigned char mstcnt;
unsigned char i,j;
void main(void)
{
TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(P0_0==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if (P0_0==0)
{
second++;
if(second==60)
{
second=0;
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
while(P0_0==0);
}
}
if(P0_1==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_1==0)
{
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(P0_1==0);
}
}
if(P0_2==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(P0_2==0);
}
}
}
}
void t0(void) interrupt 1 using 0
{
mstcnt++;
if(mstcnt==8)
{
mstcnt=0;
P1=dispcode[dispbuf[dispbitcnt]];
P3=dispbitcode[dispbitcnt];
dispbitcnt++;
if(dispbitcnt==8)
{
dispbitcnt=0;
}
}
tcnt++;
if(tcnt==4000)
{
tcnt=0;
second++;
if(second==60)
{
second=0;
minite++;
if(minite==60)
{
minite=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
} 展开
2个回答
展开全部
#include <AT89X51.H>
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; //定义显示用的段码
unsigned char dispbitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //定义显示用的位码
unsigned char dispbuf[8]={0,0,16,0,0,16,0,0}; //定义显示缓冲区
unsigned char dispbitcnt; //定义显示的位
unsigned char second; //定义秒
unsigned char minite; //定义分钟
unsigned char hour;//定义小时
unsigned int tcnt;//定义定时累加器
unsigned char mstcnt;//定义毫秒累加器
unsigned char i,j;
void main(void) //主函数
{
TMOD=0x02; //设置定时器工作模式
TH0=0x06; //设置初值
TL0=0x06;
TR0=1; //开定时器
ET0=1; //开定时器中断
EA=1; //开总中断
while(1)
{
if(P0_0==0) //判断P00口的按键是否按下
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);//延时消抖
if (P0_0==0) //判断按键真的按下了
{
second++; //秒加1
if(second==60) //秒加到60?
{
second=0; //秒清零
}
dispbuf[0]=second%10; //取秒的个位放在显示缓冲
dispbuf[1]=second/10; //取秒的十位放在显示缓冲
while(P0_0==0); //等待,知道按键松开
}
}
if(P0_1==0) //p01的按键按下,设置分钟,请参考秒的程序,一样的
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_1==0)
{
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(P0_1==0);
}
}
if(P0_2==0) //p02的按键按下,设置小时,请参考秒的程序,一样的
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(P0_2==0);
}
}
}
}
void t0(void) interrupt 1 using 0 //定时器中断函数
{
mstcnt++; //累加器加一
if(mstcnt==8) //计时满8毫秒?
{
mstcnt=0; //累加器清零
P1=dispcode[dispbuf[dispbitcnt]]; //送显示段码
P3=dispbitcode[dispbitcnt]; //送显示位码
dispbitcnt++; //显示位加1
if(dispbitcnt==8) //8位都显示完了?
{
dispbitcnt=0; //从第一位开始显示
}
}
tcnt++; //累加器加一
if(tcnt==4000) //记满1秒
{
tcnt=0; //累加器清零
second++; //秒加一
if(second==60) //秒满60?
{
second=0; //秒清零
minite++; //分钟加一
if(minite==60) //分钟满60
{
minite=0; //分钟清零
hour++; //小时加1
if(hour==24) //小时满24
{
hour=0; //小时清零
}
}
}
dispbuf[0]=second%10; //将时分秒的个位与十位分开,装在显示区
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
}
其实这个程序不难,仔细想想,有不懂的再问我吧
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; //定义显示用的段码
unsigned char dispbitcode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //定义显示用的位码
unsigned char dispbuf[8]={0,0,16,0,0,16,0,0}; //定义显示缓冲区
unsigned char dispbitcnt; //定义显示的位
unsigned char second; //定义秒
unsigned char minite; //定义分钟
unsigned char hour;//定义小时
unsigned int tcnt;//定义定时累加器
unsigned char mstcnt;//定义毫秒累加器
unsigned char i,j;
void main(void) //主函数
{
TMOD=0x02; //设置定时器工作模式
TH0=0x06; //设置初值
TL0=0x06;
TR0=1; //开定时器
ET0=1; //开定时器中断
EA=1; //开总中断
while(1)
{
if(P0_0==0) //判断P00口的按键是否按下
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);//延时消抖
if (P0_0==0) //判断按键真的按下了
{
second++; //秒加1
if(second==60) //秒加到60?
{
second=0; //秒清零
}
dispbuf[0]=second%10; //取秒的个位放在显示缓冲
dispbuf[1]=second/10; //取秒的十位放在显示缓冲
while(P0_0==0); //等待,知道按键松开
}
}
if(P0_1==0) //p01的按键按下,设置分钟,请参考秒的程序,一样的
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_1==0)
{
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(P0_1==0);
}
}
if(P0_2==0) //p02的按键按下,设置小时,请参考秒的程序,一样的
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P0_2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(P0_2==0);
}
}
}
}
void t0(void) interrupt 1 using 0 //定时器中断函数
{
mstcnt++; //累加器加一
if(mstcnt==8) //计时满8毫秒?
{
mstcnt=0; //累加器清零
P1=dispcode[dispbuf[dispbitcnt]]; //送显示段码
P3=dispbitcode[dispbitcnt]; //送显示位码
dispbitcnt++; //显示位加1
if(dispbitcnt==8) //8位都显示完了?
{
dispbitcnt=0; //从第一位开始显示
}
}
tcnt++; //累加器加一
if(tcnt==4000) //记满1秒
{
tcnt=0; //累加器清零
second++; //秒加一
if(second==60) //秒满60?
{
second=0; //秒清零
minite++; //分钟加一
if(minite==60) //分钟满60
{
minite=0; //分钟清零
hour++; //小时加1
if(hour==24) //小时满24
{
hour=0; //小时清零
}
}
}
dispbuf[0]=second%10; //将时分秒的个位与十位分开,装在显示区
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
}
其实这个程序不难,仔细想想,有不懂的再问我吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询