基于c51功能最全的电子钟程序lcd1602.docx
《基于c51功能最全的电子钟程序lcd1602.docx》由会员分享,可在线阅读,更多相关《基于c51功能最全的电子钟程序lcd1602.docx(45页珍藏版)》请在冰豆网上搜索。
基于c51功能最全的电子钟程序lcd1602
功能最全的电子钟
【单片机】c51数字时钟(带年月日显示)
摘要:
本设计以单片机为核心,lcd1602显示。
采用独立键盘输入能任意修改当前时间日期和设定闹钟时间。
具有显示年月日(区分闰年和二月),闹钟报警和整点报时功能
主程序:
/**************************************************************************************************
***************************************************************************************************
**********************lcd1602电子钟***************************************************
***************************************************************************************************
***************************************************************************************************/
#include
#include"lcd16024.h"
sbitkey1=P2^0;//调整
sbitkey2=P2^1;//加1
sbitkey3=P2^2;//减1
sbitspeaker=P2^3;//蜂鸣器
sbitkey4=P2^4;//闹钟设计
bitcal_year=1;//进入判断闰年标志位
bitleap_year;//闰年标志位
bitcalculate=0;//日加一标记
bitrun=0;//闹钟标志
bitbeep=0;//整点报时标志
//uint8num=0;//调整是给的脉冲
uint8codestr1[]="D:
";
uint8codestr2[]="T:
";
uint8codestr3[]="Wek";
uint8daystr[]="2013-07-29";//年月日格式
uint8timestr[]="21:
30:
59N";//时分秒格式
uint8daystr1[]="2013-07-29";//闹钟年月日格式
uint8timestr1[]="21:
30:
59N";//闹钟时分秒格式
uint8numweek=0;//星期加1标记
charweek=1;//星期
charsec=53;//秒
charmin=50;//分
charhour=23;//时
uint8day=30;//日
uint8month=9;//月
uint16year=2013;//年
charweek1=1;//闹钟星期
charsec1=58;//闹钟秒
charmin1=50;//闹钟分
charhour1=23;//闹钟时
uint8day1=30;//闹钟日
uint8month1=9;//闹钟月
uint16year1=2013;//闹钟年
uint8WeekData1;//闹钟星期标记
uint8number=0;//定时
uint8WeekData;//星期标记
uint8speaker_num;//整点报时次数
uint8scan_key(void);//函数声名
/*************************************************************************************
**********************更新LCD时间分离读取************************************
**************************************************************************************/
voidTimeChange()
{
//时分秒
timestr[7]=sec%10+'0';
timestr[6]=sec/10+'0';
timestr[4]=min%10+'0';
timestr[3]=min/10+'0';
timestr[1]=hour%10+'0';
timestr[0]=hour/10+'0';
//年月日
daystr[9]=day%10+'0';
daystr[8]=day/10+'0';
daystr[6]=month%10+'0';
daystr[5]=month/10+'0';
daystr[3]=year%10+'0';
daystr[2]=year/10%10+'0';
daystr[1]=year/100%10+'0';
daystr[0]=year/1000+'0';
//星期
WeekData=week+'0';
}
/*************************************************************************************
**********************闹钟更新LCD时间分离读取************************************
**************************************************************************************/
voidTimeChange1()
{
//时分秒
timestr1[7]=sec1%10+'0';
timestr1[6]=sec1/10+'0';
timestr1[4]=min1%10+'0';
timestr1[3]=min1/10+'0';
timestr1[1]=hour1%10+'0';
timestr1[0]=hour1/10+'0';
//年月日
daystr1[9]=day1%10+'0';
daystr1[8]=day1/10+'0';
daystr1[6]=month1%10+'0';
daystr1[5]=month1/10+'0';
daystr1[3]=year1%10+'0';
daystr1[2]=year1/10%10+'0';
daystr1[1]=year1/100%10+'0';
daystr1[0]=year1/1000+'0';
//星期
WeekData1=week1+'0';
}
/*************************************************************************************
**********************初始化系统定时器0************************************
**************************************************************************************/
voidsystimer0_init(void)
{
TMOD|=0x01;//设置为1时用或(|)
TMOD&=0xfd;//设置为0时用与(&)
TH0=0xDC;//定时10ms
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
EX0=1;
}
/*************************************************************************************
**********************闹钟时间设置********************************
**************************************************************************************/
voidnaozhong()
{
uint8number=1;
uint8a=0;
uint8b=0;
if(0==key4)
{
Delay1Ms(5);
if(0==key4)
{
a=1;
LCD_write_command(0xc0+9);
LCD_write_command(0x0f);
}
while(!
key4);
}
while(a)
{
if(0==key4)
{
Delay1Ms(5);
if(0==key4)
{
a=0;
run=~run;
while(!
key4);
}
}
if(run)
{
timestr[9]='Y';
}
else
{
timestr[9]='N';
}
if(0==key1)
{
Delay1Ms(5);
if(0==key1)
{
b=1;
}
while(!
key1);
}
while(b)
{
if(0==key1)
{
Delay1Ms(5);
if(0==key1)
{
number++;
if(4==number)
{
b=0;
}
while(!
key1);
if(number==1)
{
LCD_write_command(0xc0+9);
LCD_write_command(0x0f);
}
if(number==2)
{
LCD_write_command(0xc0+6);
LCD_write_command(0x0f);
}
if(number==3)
{
LCD_write_command(0xc0+3);
LCD_write_command(0x0f);
}
}
}
switch(number)
{
case1:
if(0==key2)//闹钟秒加1的设置
{
Delay1Ms(5);
if(0==key2)
{
sec1++;
if(60==sec1)
{
sec1=0;
}
while(!
key2);
timestr1[7]=sec1%10+'0';
timestr1[6]=sec1/10+'0';
LCD_write_char(8,1,timestr1[6]);
LCD_write_char(9,1,timestr1[7]);
LCD_write_command(0xc0+9);
}
}
if(0==key3)//闹钟秒减1的设置
{
Delay1Ms(5);
if(0==key3)
{
sec1--;
if(sec1<0)
{
sec1=59;
}
while(!
key3);
timestr1[7]=sec1%10+'0';
timestr1[6]=sec1/10+'0';
LCD_write_char(8,1,timestr1[6]);
LCD_write_char(9,1,timestr1[7]);
LCD_write_command(0xc0+9);
}
}
break;
case2:
if(0==key2)//闹钟分加1的设置
{
Delay1Ms(5);
if(0==key2)
{
min1++;
if(60==min1)
{
min1=0;
}
while(!
key2);
timestr1[4]=min1%10+'0';
timestr1[3]=min1/10+'0';;
LCD_write_char(5,1,timestr1[3]);
LCD_write_char(6,1,timestr1[4]);
LCD_write_command(0xc0+6);
}
}
if(0==key3)//闹钟分减1的设置
{
Delay1Ms(5);
if(0==key3)
{
min1--;
if(min1<0)
{
min1=59;
}
while(!
key3);
timestr1[4]=min1%10+'0';
timestr1[3]=min1/10+'0';;
LCD_write_char(5,1,timestr1[3]);
LCD_write_char(6,1,timestr1[4]);
LCD_write_command(0xc0+6);
}
}
break;
case3:
if(0==key2)//闹钟时加1的设置
{
Delay1Ms(5);
if(0==key2)
{
hour1++;
if(24==hour1)
{
hour1=0;
}
while(!
key2);
timestr1[1]=hour1%10+'0';
timestr1[0]=hour1/10+'0';
LCD_write_char(2,1,timestr1[0]);
LCD_write_char(3,1,timestr1[1]);
LCD_write_command(0xc0+3);
}
}
if(0==key3)//闹钟时减1的设置
{
Delay1Ms(5);
if(0==key3)
{
hour1--;
if(hour1<0)
{
hour1=23;
}
while(!
key3);
timestr1[1]=hour1%10+'0';
timestr1[0]=hour1/10+'0';
LCD_write_char(2,1,timestr1[0]);
LCD_write_char(3,1,timestr1[1]);
LCD_write_command(0xc0+3);
}
}
break;
case4:
b=0;
LCD_write_command(0x0c);
break;
}
}
}
LCD_write_command(0x0c);
while(!
key4);
}
/*************************************************************************************
**********************判断按键进入时间调整********************************
**************************************************************************************/
uint8scan_key(void)
{
uint8number=1;
uint8a=0;
if(0==key1)
{
Delay1Ms(5);
if(0==key1)
{
while(!
key1);
a=1;
LCD_write_command(0xc0+9);
LCD_write_command(0x0f);
}
}
while(a)
{
if(0==key1)
{
Delay1Ms(5);
if(0==key1)
{
number++;
while(!
key1);
TR0=0;
if(number==2)
{
LCD_write_command(0xc0+6);
LCD_write_command(0x0f);
}
if(number==3)
{
LCD_write_command(0xc0+3);
LCD_write_command(0x0f);
}
if(number==4)
{
LCD_write_command(0x80+11);
LCD_write_command(0x0f);
}
if(number==5)
{
LCD_write_command(0x80+8);
LCD_write_command(0x0f);
}
if(number==6)
{
LCD_write_command(0x80+5);
LCD_write_command(0x0f);
}
if(number==7)
{
LCD_write_command(0xc0+14);
LCD_write_command(0x0f);
}
if(8==number)
{
LCD_write_command(0x0c);
a=0;
number=0;
}
}
}
switch(number)
{
case1:
if(0==key2)//秒加1的设置
{
Delay1Ms(5);
if(0==key2)
{
sec++;
if(60==sec)
{
sec=0;
}
timestr[7]=sec%10+'0';
timestr[6]=sec/10+'0';
LCD_write_char(8,1,timestr[6]);
LCD_write_char(9,1,timestr[7]);
LCD_write_command(0xc0+9);
while(!
key2);
}
}
if(0==key3)//秒减1的设置
{
Delay1Ms(5);
if(0==key3)
{
sec--;
if(sec<0)
{
sec=59;
}
timestr[7]=sec%10+'0';
timestr[6]=sec/10+'0';
LCD_write_char(8,1,timestr[6]);
LCD_write_char(9,1,timestr[7]);
LCD_write_command(0xc0+9);
while(!
key3);
}
}
break;
case2:
if(0==key2)//分加1的设置
{
Delay1Ms(5);
if(0==key2)
{
min++;
if(60==min)
{
min=0;
}