基于51单片机个LCD1602的万年历程序

小白 第一次跟新博客

基于51单片机和LCD1602的万年历程序

可实现走时和调时功能 有简单的1602菜单制作

欢迎大家交流

LCD1602和51单片机的连接方法

RS = P3^5; //数据/命令选择端(H/L)
RW = P3^4; //数/写选择端(H/L)
EN = P3^3; //使能信号
P0 //LCD1602的数据口D0-D7

 

#include<reg52.h>

typedef unsigned char uChar8;
typedef unsigned int uInt16;

uChar8 TAB1[]="^_^2016-12-27^_^";
uChar8 TAB2[]=" 23:59:45 ";
uChar8 year=2016,month=12,day=27,hour=23,minute=59,second=45,count;

sbit RS = P3^5; //数据/命令选择端(H/L)
sbit RW = P3^4; //数/写选择端(H/L)
sbit EN = P3^3; //使能信号
sbit SEG_SELECT = P1^7;
sbit BIT_SELECT = P1^6;

sbit LED1 = P2^0;
sbit Key2 = P3^6;
sbit Key1 = P3^7;
sbit Key3 = P3^0;

void DelayMS(uInt16 ValMS)
{
uInt16 uiVal,ujVal;
for(uiVal = 0; uiVal < ValMS; uiVal++)
for(ujVal = 0; ujVal < 113; ujVal++);
}

void DectectBusyBit(void)
{
P0 = 0xff; // 读状态值时,先赋高电平
RS = 0;
RW = 1;
EN = 1;
DelayMS(1);
while(P0 & 0x80); // 若LCD忙,停止到这里,否则走起
EN = 0; // 之后将EN初始化为低电平
}

void WrComLCD(uChar8 ComVal)
{
DectectBusyBit();
RS = 0;
RW = 0;
EN = 1;
P0 = ComVal;
DelayMS(1);
EN = 0;
}

void WrDatLCD(uChar8 DatVal)
{
DectectBusyBit();
RS = 1;
RW = 0;
EN = 1;
P0 = DatVal;
DelayMS(1);
EN = 0;
}

void LCD_Init(void)
{
uChar8 ucVal,i;

WrComLCD(0x38); // 16*2行显示、5*7点阵、8位数据接口
WrComLCD(0x38);
WrComLCD(0x38); // 重新设置一遍
WrComLCD(0x01); // 显示清屏
WrComLCD(0x06); // 光标自增、画面不动
DelayMS(1); // 稍作延时
WrComLCD(0x0c); // 开显示、关光标并不闪烁

for(i=0;i<16;i++)
{
WrComLCD(0x80+i);
WrDatLCD(TAB1[ucVal+i]);

WrComLCD(0xC0+i);
WrDatLCD(TAB2[ucVal+i]);
}
}

void Timer_Init()
{
TMOD=0x01;
TH0=0xDC;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}

void DisplayYear()
{
WrComLCD(0x80+5);
WrDatLCD(0x30+year/10);
WrComLCD(0x80+6);
WrDatLCD(0x30+year%10);
}

void DisplayMonth()
{
WrComLCD(0x80+8);
WrDatLCD(0x30+month/10);
WrComLCD(0x80+9);
WrDatLCD(0x30+month%10);
}

void DisplayDay()
{
WrComLCD(0x80+11);
WrDatLCD(0x30+day/10);
WrComLCD(0x80+12);
WrDatLCD(0x30+day%10);
}

void DisplayHour()
{
WrComLCD(0xc0+4);
WrDatLCD(0x30+hour/10);
WrComLCD(0xc0+5);
WrDatLCD(0x30+hour%10);
}

void DisplayMinute()
{
WrComLCD(0xc0+7);
WrDatLCD(0x30+minute/10);
WrComLCD(0xc0+8);
WrDatLCD(0x30+minute%10);
}

void DisplaySecond()
{
WrComLCD(0xc0+10);
WrDatLCD(0x30+second/10);
WrComLCD(0xc0+11);
WrDatLCD(0x30+second%10);
}

void CloseDigTube()
{
BIT_SELECT=1;
P0=0xff;
BIT_SELECT=0;
SEG_SELECT=1;
P0=0x00;
SEG_SELECT=0;

}

void Keyscan()
{
static uChar8 i=0,j=0; //static静态变量 每按下一次Key1(停止计时键)i++; 每按下一次Key2(调位键)j++;
if(Key1==0) //每按下一次Key1(停止计时键)i++;
{
DelayMS(5); //消抖
if(Key1==0)
i++;
}
if(i%2==1) //判断i的奇偶性决定TR的状态
{
LED1=0;
TR0=0;
}
if(i%2==0)
{
LED1=1;
TR0=1;
WrComLCD(0xc0+5); //确定位置 第二行第五位
WrComLCD(0xc0+8); //确定位置 第二行第八位
WrComLCD(0xc0+11); //确定位置 第二行第十一位
WrComLCD(0x0c); //光标不闪烁
}
if(Key2==0) //每按下一次Key2(调位键)j++;
{
DelayMS(5); //消抖
if(Key2==0)
j++;
}
if(j%7==1)
{
WrComLCD(0x0f); //光标闪烁
WrComLCD(0xc0+11); //哪一位光标闪烁
if(Key3==0)
{
second++;
if(second==60)
second=0;
DisplaySecond();
}
}
if(j%7==2)
{
WrComLCD(0x0f); //光标闪烁
WrComLCD(0xc0+8); //哪一位光标闪烁
if(Key3==0)
{
minute++;
if(minute==60)
minute=0;
DisplayMinute();
}
}
if(j%7==3)
{
WrComLCD(0x0f); //光标闪烁
WrComLCD(0xc0+5); //哪一位光标闪烁
if(Key3==0)
DelayMS(5);
if(Key3==0)
{
hour++;
if(hour==24)
hour=0;
DisplayHour();
}
}
if(j%7==4)
{
WrComLCD(0x0f); //光标闪烁
WrComLCD(0x80+12); //哪一位光标闪烁
if(Key3==0)
DelayMS(5);
if(Key3==0)
{
day++;
if(day==31)
day=1;
DisplayDay();
}
}
if(j%7==5)
{
WrComLCD(0x0f); //光标闪烁
WrComLCD(0x80+9); //哪一位光标闪烁
if(Key3==0)
DelayMS(5);
if(Key3==0)
{
month++;
if(month==12)
month=1;
DisplayMonth();
}
}
if(j%7==6)
{
WrComLCD(0x0f); //光标闪烁
WrComLCD(0x80+6); //哪一位光标闪烁
if(Key3==0)
DelayMS(5);
if(Key3==0)
{
year++;
if(year==99)
year=0;
DisplayYear();
}
}
if(j%7==0)
{
WrComLCD(0x0c); //光标不闪烁
}



}

void main(void)
{
LCD_Init();
Timer_Init();
CloseDigTube();
DelayMS(5);
while(1)
{
Keyscan(); //按键扫描 菜单制作
}

}

void Timer0(void) interrupt 1
{

TH0=0xDC;
TL0=0x00;
count++;
if(count==100)
{
count=0;
second++;

if(second==60)
{

second=0;
minute++;

if(minute==60)
{

minute=0;
hour++;
if(hour==24)
{
hour=0;
}
DisplayHour();
}
DisplayMinute();
}
DisplaySecond();
}

}

 

posted @ 2017-02-03 17:44  零下十一度.C  阅读(2230)  评论(0编辑  收藏  举报