51单片机入门(五)
用LCD1602来显示一个二十四小时的时钟和以0.1秒计时的秒表
1.时钟代码:
//use Lcd1602 to make a clock
#include<reg52.h>
#define uchar unsigned char
sbit K1=P3^1; //select hour /minute /second
sbit K2=P3^0; //add one
sbit K3=P3^2; //into clock configuration
sbit E=P2^7;
sbit RW=P2^5;
sbit RS=P2^6;
uchar setplace;
uchar time;
//************declare function
void delayms(uchar);
void LcdInit();
void LcdWriteCom(uchar);
void LcdWriteData(uchar);
void TimerConfig();
void Int0Config();
//************main()
void main()
{
uchar hour=12,minute=0,second=0;
LcdInit();
TimerConfig();
Int0Config();
LcdWriteData('0'+hour/10);
LcdWriteData('0'+hour%10);
LcdWriteData('-');
LcdWriteData('0'+minute/10);
LcdWriteData('0'+minute%10);
LcdWriteData('-');
LcdWriteData('0'+second/10);
LcdWriteData('0'+second%10);
while(1)
{
if(TR0==0)
{
if(K1==0)
{
delayms(10);
if(K1==0)
{
setplace++;
if(setplace>=3) setplace=0;
}
while(~K1);
}
if(K2==0)
{
delayms(10);
if(K2==0)
{
if(setplace==0)
{
second++;
if(second>=60) second=0;
}else if(setplace==1)
{
minute++;
if(minute>=60) minute=0;
}else hour++;
if(hour>=24) hour=0;
}
while(~K2);
}
}
//timer add one
if(time==20)
{
time=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
//show time on the lcd1602
LcdWriteCom(0x80);
LcdWriteData('0'+hour/10);
LcdWriteData('0'+hour%10);
LcdWriteCom(0x83);
LcdWriteData('0'+minute/10);
LcdWriteData('0'+minute%10);
LcdWriteCom(0x86);
LcdWriteData('0'+second/10);
LcdWriteData('0'+second%10);
}
}
//*************delayms(uchar)
void delayms(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//************LcdInit()
void LcdInit()
{
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
LcdWriteCom(0x01);
LcdWriteCom(0x80);
}
//****************LcdWriteCom(uchar)
void LcdWriteCom(uchar com)
{
RW=0;
RS=0;
P0=com;
delayms(1);
E=1;
delayms(5);
E=0;
}
//***************LcdWriteData(uchar)
void LcdWriteData(uchar dat)
{
RW=0;
RS=1;
P0=dat;
delayms(1);
E=1;
delayms(5);
E=0;
}
//******************TimerConfig()
void TimerConfig()
{
TMOD=0x01;
TH0=(65535-49539)/256;
TL0=(65535-49539)%256;
EA=1;
ET0=1;
TR0=1;
}
//**************Timer service
void Timer() interrupt 1
{
TH0=(65535-49539)/256;
TL0=(65535-49539)%256;
time++; //add one per 50ms
}
//****************Int0Config()
void Int0Config()
{
EA=1;
EX0=1;
IT0=1;
}
//***************Int0 service
void Int0() interrupt 0
{
delayms(10);
if(K3==0)
{
TR0=~TR0; //timer not count time
setplace=0;
}
}
效果图:
2.秒表代码:
//use timer to make a counter (add one per 0.1s)
#include<reg52.h>
#define uchar unsigned char
sbit K3=P3^2;
sbit K4=P3^3;
sbit E=P2^7;
sbit RS=P2^6;
sbit RW=P2^5;
uchar time;
uchar setplace;
//************************declare function
void delayms(uchar);
void TimerConfig();
void LcdInit();
void LcdWriteCom(uchar);
void LcdWriteData(uchar);
void Int0Config();
//********main()
void main()
{
uchar hour=0,minute=0,second=0,tenth=0;
LcdInit();
TimerConfig();
Int0Config();
LcdWriteData('0'+hour/10);
LcdWriteData('0'+hour%10);
LcdWriteData('-');
LcdWriteData('0'+minute/10);
LcdWriteData('0'+minute%10);
LcdWriteData('-');
LcdWriteData('0'+second/10);
LcdWriteData('0'+second%10);
LcdWriteData('.');
LcdWriteData('0'+tenth);
while(1)
{
//timer add one
if(time==2)
{
time=0;
tenth++;
if(tenth==10)
{
second++;
tenth=0;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
hour++;
minute=0;
if(hour==99)
{
hour=0;
}
}
}
}
}
//show outcom on the Lcd
LcdWriteCom(0x80);
LcdWriteData('0'+hour/10);
LcdWriteData('0'+hour%10);
LcdWriteCom(0x83);
LcdWriteData('0'+minute/10);
LcdWriteData('0'+minute%10);
LcdWriteCom(0x86);
LcdWriteData('0'+second/10);
LcdWriteData('0'+second%10);
LcdWriteCom(0x89);
LcdWriteData('0'+tenth);
}
}
//************delayms(uchar)
void delayms(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//***************TimerConfig()
void TimerConfig()
{
TMOD=0x01;
TH0=(65535-49539)/256;
TL0=(65535-49539)%256;
EA=1;
ET0=1;
TR0=1;
}
//**************Timer service
void Timer() interrupt 1
{
TH0=(65535-49539)/256;
TL0=(65535-49539)%256;
time++;
}
//********LcdInit()
void LcdInit()
{
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
LcdWriteCom(0x01);
LcdWriteCom(0x80);
}
//*********LcdWriteCom(uchar)
void LcdWriteCom(uchar com)
{
RW=0;
RS=0;
P0=com;
delayms(1);
E=1;
delayms(5);
E=0;
}
//*************LcdWriteData(uchar)
void LcdWriteData(uchar dat)
{
RW=0;
RS=1;
P0=dat;
delayms(1);
E=1;
delayms(5);
E=0;
}
//****************Int0Config()
void Int0Config()
{
EA=1;
EX0=1;
IT0=1;
}
//***************Int0 service
void Int0() interrupt 0
{
delayms(10);
if(K3==0)
{
TR0=~TR0; //timer not count time
setplace=0;
}
}
效果图: