#ifndef _LCD_H_
#define _LCD_H_
#include <reg51.h>
#include <intrins.h>
sbit RS = P2^6;
sbit RW = P2^5;
sbit EN = P2^7;
#define RS_CLR RS = 0
#define RS_SET RS = 1
#define RW_CLR RW = 0
#define RW_SET RW = 1
#define EN_CLR EN = 0
#define EN_SET EN = 1
#define dataport P0
#define uchar unsigned char
extern void DelayUs2x(uchar t);
extern void DelayMs(uchar t);
extern void LCD_Write_COM(uchar com); //???
extern void LCD_Write_Data(uchar Data);
extern void LCD_Clear();
extern void LCD_Write_String(uchar x, uchar y, uchar* s);
extern void LCD_Write_Char(uchar x, uchar y, uchar c);
extern void LCD_Init();
#endif
/*****lcd.c********/
#include "lcd.h"
void DelayUs2x(uchar t)
{
while(--t);
}
void DelayMs(uchar t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
/* bit LCD_Check_busy(void)
{
dataport = 0xff;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(dataport&0x80);
} */
//???
void LCD_Write_COM(uchar com)
{
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
dataport = com;
_nop_();
EN_CLR;
}
//???
void LCD_Write_Data(uchar Data)
{
DelayMs(5);
RS_SET;
RW_CLR;
EN_SET;
dataport = Data;
_nop_();
EN_CLR;
}
//??
void LCD_Clear()
{
LCD_Write_COM(0x01);
DelayMs(5);
}
//????,x????,y????
void LCD_Write_String(uchar x, uchar y, uchar* s)
{
if(y == 0)
{
LCD_Write_COM(0x80+x);
}
else
{
LCD_Write_COM(0xC0+x);
}
while(*s)
{
LCD_Write_Data(*s);
s++;
}
}
//????,x????,y????
void LCD_Write_Char(uchar x, uchar y, uchar c)
{
if(y == 0)
{
LCD_Write_COM(0x80+x);
}
else
{
LCD_Write_COM(0xC0+x);
}
LCD_Write_Data(c);
}
void LCD_Init() //LCD??????
{
EN_CLR;
LCD_Write_COM(0x38); //??????
DelayMs(5);
LCD_Write_COM(0x38); //??????
DelayMs(5);
LCD_Write_COM(0x38); //??????
DelayMs(5);
LCD_Write_COM(0x38); //??????
DelayMs(5);
LCD_Write_COM(0x0c); //????????,????
LCD_Write_COM(0x06); //???????
LCD_Write_COM(0x01); //??
LCD_Write_COM(0x80); //????????
}
/*****main.c*******/
#include "lcd.h"
uchar hour = 21, min = 43, sec = 0, count, date = 28, monse = 4;
unsigned int year = 2017;
void init_T0()
{
EA = 1;
ET0 = 1;
TMOD = 0X01;
TH0 = 0X3C;
TL0 = 0XB0;
TR0 = 1;
}
void main()
{
init_T0();
LCD_Init();
LCD_Clear();
while(1)
{
LCD_Write_String(0, 0, "Date:");
LCD_Write_Char(5, 0, year/1000+48);
LCD_Write_Char(6, 0, year%1000/100+48);
LCD_Write_Char(7, 0, year%100/10+48);
LCD_Write_Char(8, 0, year%10+48);
LCD_Write_Char(9, 0, '-');
LCD_Write_Char(10, 0, monse/10+48);
LCD_Write_Char(11, 0, monse%10+48);
LCD_Write_Char(12, 0, '-');
LCD_Write_Char(13, 0, date/10+48);
LCD_Write_Char(14, 0, date%10+48);
LCD_Write_String(0, 1, "Time:");
LCD_Write_Char(5, 1, hour/10+48);
LCD_Write_Char(6, 1, hour%10+48);
LCD_Write_Char(7, 1, '-');
LCD_Write_Char(8, 1, min/10+48);
LCD_Write_Char(9, 1, min%10+48);
LCD_Write_Char(0xa, 1, '-');
LCD_Write_Char(0xb, 1, sec/10+48);
LCD_Write_Char(0xc, 1, sec%10+48);
}
}
void inter() interrupt 1
{
TH0 = 0X3C;
TL0 = 0XB0;
count++;
if(count >= 20)
{
count = 0;
sec++;
}
if(sec >= 60)
{
sec = 0;
min++;
}
if(min >= 60)
{
min = 0;
hour++;
}
if(hour >= 24)
{
hour = 0;
date++;
}
if(date > 31)
{
date = 0;
monse++;
}
if(monse > 13)
{
monse = 0;
year++;
}
}
|