单片机最小系统,定时器,LED显示器做时钟!
/*p0控制显示器*/
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit LCDRS=P2^0;
sbit LCDEN=P2^1;
sbit K1=P3^0;
sbit K2=P3^1;
//uchar code p[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
unsigned long int flag=1;
uint second=58,minute=59,hour=12;
void delay(uint z)
{
uint i;
while(z--)
for(i=110;i>0;i--);
}
void write_com(uchar com)
{
LCDRS=0;
P0=com;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
void write_data(uchar datas)
{
LCDRS=1;
P0=datas;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
void init()
{
//P3=0x0f;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //开启总中断
ET0=1;//开启定时器1中断
TR0=1;//开启定时器1
LCDEN=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void main()
{
init();
write_data(0x3c);
delay(5);
write_data(*T*);
delay(5);
write_data(*i*);
delay(5);
write_data(*m*);
delay(5);
write_data(*e*);
delay(5);
write_data(0x3e);
write_com(0x80+0x06);
do
{
write_data(hour%24/10+48);
write_data(hour%24%10+48);
write_data(58);
write_data(minute%60/10+48);
write_data(minute%10+48);
write_data(58);
write_data(second%60/10+48);
write_data(second%10+48);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
if(K1==0)
{
TR0=0;
while(!K1);
TR0=1;
}
}while(1);
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
flag++;
if(flag%20==0)
second++;
if(second%60==0)
minute++;
if(minute%60==0)
hour++;
}
运行3s后,不是应该是13:00:00吗???分钟位怎么会出现这种情况,麻烦有经验的大神给点提示点拨,让我渡过难关,顺利毕业吧,小弟在此谢谢了!!!
/*p0控制显示器*/
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit LCDRS=P2^0;
sbit LCDEN=P2^1;
sbit K1=P3^0;
sbit K2=P3^1;
//uchar code p[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};
unsigned long int flag=1;
uint second=58,minute=59,hour=12;
void delay(uint z)
{
uint i;
while(z--)
for(i=110;i>0;i--);
}
void write_com(uchar com)
{
LCDRS=0;
P0=com;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
void write_data(uchar datas)
{
LCDRS=1;
P0=datas;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
void init()
{
//P3=0x0f;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //开启总中断
ET0=1;//开启定时器1中断
TR0=1;//开启定时器1
LCDEN=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void main()
{
init();
write_data(0x3c);
delay(5);
write_data(*T*);
delay(5);
write_data(*i*);
delay(5);
write_data(*m*);
delay(5);
write_data(*e*);
delay(5);
write_data(0x3e);
write_com(0x80+0x06);
do
{
write_data(hour%24/10+48);
write_data(hour%24%10+48);
write_data(58);
write_data(minute%60/10+48);
write_data(minute%10+48);
write_data(58);
write_data(second%60/10+48);
write_data(second%10+48);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
write_com(0x10);
if(K1==0)
{
TR0=0;
while(!K1);
TR0=1;
}
}while(1);
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
flag++;
if(flag%20==0)
second++;
if(second%60==0)
minute++;
if(minute%60==0)
hour++;
}
运行3s后,不是应该是13:00:00吗???分钟位怎么会出现这种情况,麻烦有经验的大神给点提示点拨,让我渡过难关,顺利毕业吧,小弟在此谢谢了!!!