📄 lcd1602.txt
字号:
#include "at89x51.h"
//***********************************
#ifndef __LCD1602_H__
#define _LCD1602_H__
#define MSB 0x80
#define LSB 0x00
#define TRUE 1
#define FALSE 0
#define HIGH 1
#define LOW 0
//*****************************************
#define LINE1 0
#define LINE2 1
#define LINE1_HEAD 0x80
#define LINE2_HEAD 0xC0
#define DATA_MODE 0x38
#define OPEN_SCREEN 0x0C
#define DISPLAY_ADDRESS 0x80
#define CLEARSCREEN LCD_en_com(0x01)
//*************************************************************
#define LCDIO P0
sbit LCD1602_RS=P2^0; //data command select 1 data 0 command pin 4
sbit LCD1602_RW=P2^1; //read write select 1 read 0 write pin 5
sbit LCD1602_EN=P2^2; //LCD enable signal pin 6
//**********************************************************************
//*****************************************************************
//define time
unsigned char hour_set=00,min_set=00,sec_set=59;
unsigned char hour_backup,min_backup,sec_backup;
unsigned char day;
unsigned char p=0;
unsigned char key_p;
#endif
void LCD_Read_BF(void)
{
unsigned char read=0;
unsigned int i=0;
LCD1602_RS = LOW ;//RS 0
LCD1602_RW = HIGH; //RW 1
LCD1602_EN = HIGH; //EN 1 Read BF
LCDIO = 0xFF;
LCDIO = 0xFF;
do{i++;
read = LCDIO;
if(i>1000)break;
}while(read&MSB);
}
//*************************************
void LCD_en_com(unsigned char command)
{
LCD_Read_BF();
LCD1602_RS = LOW; //RS 0
LCD1602_RW = LOW; //RW 0
LCD1602_EN = HIGH; //EN Write command
LCDIO = command;
LCD1602_EN = LOW;
}
//*********************************
void LCD_en_dat(unsigned char dat)
{
LCD_Read_BF();
LCD1602_RS = HIGH; //RS 1
LCD1602_RW = LOW; //RW 0
LCD1602_EN = HIGH; //EN __ Write data
LCDIO = dat;
LCD1602_EN = LOW;
}
//**********************************************
void LCD_set_xy(unsigned char x,unsigned char y)
{
unsigned char address;
if(x == LINE1)
address = LINE1_HEAD + y;
else
address = LINE2_HEAD + y;
LCD_en_com(address);
}
//****************************************************************
//*********************************************************************
void LCD_write_string(unsigned char x,unsigned char y,unsigned char *s)
{
LCD_set_xy(x,y);
while(*s)
{
//LCDIO = *s;
LCD_en_dat(*s);
s++;
}
}
//****************/*********************
void LCD_init(void)
{
LCD_en_com(DATA_MODE);
//set 8 bit data transmission mode
LCD_en_com(OPEN_SCREEN);
//open display (enable lcd display)
LCD_en_com(DISPLAY_ADDRESS);
//set lcd first display address
CLEARSCREEN;
//clear screen
}
//**************************************
void Delay(unsigned int count)
{
unsigned char i;
while(--count != 0)
{
for(i = 0; i < 125; i++);
}
}
void time(unsigned char hour,unsigned min,unsigned sec)
{
LCD_set_xy(LINE2,5);
LCD_en_dat(hour/10+48);
LCD_en_dat(hour%10+48);
LCD_en_dat(58);
LCD_en_dat(min/10+48);
LCD_en_dat(min%10+48);
LCD_en_dat(58);
LCD_en_dat(sec/10+48);
LCD_en_dat(sec%10+48);
}
void disp_backup_set_time(unsigned char hour,unsigned min,unsigned sec)
{
LCD_set_xy(LINE1,0);
LCD_en_dat(0);
LCD_en_dat(0);
LCD_en_dat(0);
LCD_en_dat(0);
LCD_en_dat(0);
LCD_en_dat(hour/10+48);
LCD_en_dat(hour%10+48);
LCD_en_dat(58);
LCD_en_dat(min/10+48);
LCD_en_dat(min%10+48);
LCD_en_dat(58);
LCD_en_dat(sec/10+48);
LCD_en_dat(sec%10+48);
LCD_en_dat(0);
LCD_en_dat(0);
LCD_en_dat(0);
}
void time_int_EA()
{
EA = 1;
ET0 = 1;
TMOD = 0x1;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;;
TR0 = 1;
}
//
void delayus(unsigned char i)
{
while(--i);
}
void arlm()
{
unsigned int k=0;
P1_7=0;
Delay(1000);
P1_7=1;
//*****************************************
for(k=0;k<=5000;k++){P1_6=~P1_6;delayus(25);}
hour_set=hour_backup;
min_set=min_backup;
sec_set=sec_backup;
}
//********************************************************
void key()
{
if(P1_0==0){Delay(500);key_p++;if(key_p>=5)key_p=0;}
if(P1_1==0&&key_p==1){hour_set++;if(hour_set>23)hour_set=0;Delay(200);}
if(P1_1==0&&key_p==2){min_set++;if(min_set>=59)min_set=0;Delay(200);}
if(P1_1==0&&key_p==3){sec_set++;if(sec_set>=59)sec_set=0;Delay(200);}
if(P1_2==0&&key_p==1){hour_set--;if(hour_set>23)hour_set=0;Delay(200);}
if(P1_2==0&&key_p==2){min_set--;if(min_set<=0)min_set=59;Delay(200);}
if(P1_2==0&&key_p==3){sec_set--;if(sec_set<=0)sec_set=59;Delay(200);}
}
void main(void)
{
time_int_EA();
Delay(1000);
LCD_init();
hour_backup=hour_set;
min_backup=min_set;
sec_backup=sec_set;
P1_7=1;
while(TRUE )
{
if(key_p==0)
{
if(!(sec_set%9))LCD_write_string(LINE1,0,"Welcome pourtime" );
if(!(sec_set%7)){disp_backup_set_time(hour_backup,min_backup,sec_backup);}
LCD_write_string(LINE2,0,"Run:");
}
else
{
LCD_write_string(LINE2,0,"Paus");
if(key_p==1){LCD_write_string(LINE1,0," MM " );}
if(key_p==2){LCD_write_string(LINE1,0," MM " );}
if(key_p==3){LCD_write_string(LINE1,0," MM " );}
if(key_p==4)
{
LCD_write_string(LINE1,0,"SAVE:TIME " );
hour_backup=hour_set;
min_backup=min_set;
sec_backup=sec_set;
}
}
time(hour_set,min_set,sec_set);
}
}
void disp_Time0(void) interrupt 1 using 3
{
TH0=(65536-5000)/256;
DUFAULT;;
p++;
if(key_p==0){
if(p>=200){p=0;sec_set--;}
if(!(hour_set==0&&min_set==0&&sec_set==0))if(sec_set<=0){sec_set=59;if(!(hour_set==0&&min_set==0))min_set--;}
if(!(hour_set==0))if(min_set<=0){min_set=59;hour_set--;}
if(hour_set==0&&min_set==0&&sec_set==0)arlm();
}
key();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -