LCD1602driver_inside.c
#include<reg51.h>
#define D0_D7 P3
sbit RS = P2^5;
sbit RW = P2^6;
sbit E = P2^7;
sbit BF = P3^7;
bit Busy_Check();
void Data_Write(char Data);
void Addr_Write(unsigned char Addr);
void Command_Write(unsigned char Command);
void Cursor_Reset();
unsigned char Coordinate_To_Addr(unsigned char X_Intercept,unsigned char Y_Intercept);
void DelayMS(unsigned int MS_Num);
void Open_Screen_Inside();
void Close_Screen_Inside();
void Move_Screen_Left_Inside();
void Move_Screen_Right_Inside();
void Reset_D0_D7();
void Reset_D0_D7()
{
D0_D7 = 0;
}
void DelayMS(unsigned int MS_Num)
{
unsigned char a,b;
for(;MS_Num>0;MS_Num--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
bit Busy_Check()
{
DelayMS(1);
}
void Data_Write(char Data)
{
RS = 1;
RW = 0;
E = 0;
DelayMS(1);
D0_D7 = Data;
E = 1;
DelayMS(1);
E = 0;
}
void Addr_Write(unsigned char Addr)
{
RS = 0;
RW = 0;
E = 0;
DelayMS(1);
D0_D7 = Addr;
E = 1;
DelayMS(1);
E = 0;
}
void Command_Write(unsigned char Command)
{
RS = 0;
RW = 0;
E = 0;
DelayMS(1);
D0_D7 = Command;
E = 1;
DelayMS(1);
E = 0;
}
void Cursor_Reset()
{
RS = 0;
RW = 0;
E = 0;
DelayMS(1);
D0_D7 = 0x02;
E = 1;
DelayMS(1);
E = 0;
}
unsigned char Coordinate_To_Addr(unsigned char X_Intercept,unsigned char Y_Intercept)
{
unsigned char Addr;
if(Y_Intercept == 0)
{
Addr = 0x80 + X_Intercept;
}
if(Y_Intercept == 1)
{
Addr = 0xC0 + X_Intercept;
}
return Addr;
}
void Open_Screen_Inside()
{
RS = 0;
RW = 0;
E = 0;
Command_Write(0x0C);
E = 1;
DelayMS(1);
E = 0;
}
void Close_Screen_Inside()
{
RS = 0;
RW = 0;
E = 0;
Command_Write(0x08);
E = 1;
DelayMS(1);
E = 0;
}
void Move_Screen_Left_Inside()
{
RS = 0;
RW = 0;
E = 0;
Command_Write(0x18);
E = 1;
DelayMS(1);
E = 0;
}
void Move_Screen_Right_Inside()
{
RS = 0;
RW = 0;
E = 0;
Command_Write(0x1C);
E = 1;
DelayMS(1);
E = 0;
}
LCD1602driver_inside.h
#ifndef _LCD1602DRIVER_INSIDE_H_
#define _LCD1602DRIVER_INSIDE_H_
unsigned char Busy_Check();
void Data_Write(char Data);
void Addr_Write(unsigned char Addr);
void Command_Write(unsigned char Command);
void Cursor_Reset();
unsigned char Coordinate_To_Addr(unsigned char X_Intercept,unsigned char Y_Intercept);
void DelayMS(unsigned int MS_Num);
void Open_Screen_Inside();
void Close_Screen_Inside();
void Move_Screen_Left_Inside();
void Move_Screen_Right_Inside();
void Reset_D0_D7();
#endif
LCD1602.c
#include"LCD1602driver_inside.h"
#include<reg51.h>
void LCD1602_Init(unsigned char Line_Num,unsigned char Char_Type);
void LCD1602_Clear_Screen();
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept);
void LCD1602_Write_Line_Screen(char Line_Data[28],unsigned char Y_Intercept);
void LCD1602_Open_Screen();
void LCD1602_Close_Screen();
void LCD1602_Move_Left_Screen();
void LCD1602_Move_Right_Screen();
void LCD1602_Init(unsigned char Line_Num,unsigned char Char_Type)
{
Reset_D0_D7();
Command_Write(0x06);
if(Line_Num == 1)
{
if(Char_Type == 2)
{
Command_Write(0x34);
}
else
{
Command_Write(0x30);
}
}
else
{
if(Char_Type == 2)
{
Command_Write(0x3C);
}
else
{
Command_Write(0x38);
}
}
DelayMS(5);
LCD1602_Open_Screen();
DelayMS(5);
LCD1602_Clear_Screen();
DelayMS(5);
}
void LCD1602_Clear_Screen()
{
while(Busy_Check());
Command_Write(0x01);
}
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept)
{
while(Busy_Check());
Addr_Write(Coordinate_To_Addr(X_Intercept,Y_Intercept));
DelayMS(1);
Data_Write(ASCII_Data);
}
void LCD1602_Write_Line_Screen(char Line_Data[28],unsigned char Y_Intercept)
{
char Count;
while(Busy_Check());
Addr_Write(Coordinate_To_Addr(0,Y_Intercept));
for(Count = 0;Line_Data[Count] != '\0';Count++)
{
Data_Write(Line_Data[Count]);
}
}
void LCD1602_Open_Screen()
{
Open_Screen_Inside();
}
void LCD1602_Close_Screen()
{
Close_Screen_Inside();
}
void LCD1602_Move_Left_Screen()
{
Move_Screen_Left_Inside();
}
void LCD1602_Move_Right_Screen()
{
Move_Screen_Right_Inside();
}
LCD1602.H
#ifndef _LCD1602_H_
#define _LCD1602_H_
void LCD1602_Init(unsigned char Line_Num,unsigned char Char_Type);
void LCD1602_Clear_Screen();
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept);
void LCD1602_Write_Line_Screen(char Line_Data[16],unsigned char Y_Intercept);
void LCD1602_Open_Screen();
void LCD1602_Close_Screen();
void LCD1602_Move_Left_Screen();
void LCD1602_Move_Right_Screen();
#endif
driver_test.c
#include<LCD1602.h>
#include"LCD1602driver_inside.h"
#include<reg51.h>
#include<intrins.h>
/*头文件包含函数
void LCD1602_Init(unsigned char Line_Num,unsigned char Char_Type);
void LCD1602_Clear_Screen();
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept);
void LCD1602_Write_Line_Screen(char Line_Data[16],unsigned char Y_Intercept);
void LCD1602_Open_Screen();
void LCD1602_Close_Screen();
void LCD1602_Move_Left_Screen();
void LCD1602_Move_Right_Screen();
*/
void delay(void)
{
unsigned char a,b,c;
for(c=3;c>0;c--)
for(b=247;b>0;b--)
for(a=142;a>0;a--);
_nop_;
}
void main()
{
char Count;
LCD1602_Init(2,1);
while(1)
{
LCD1602_Clear_Screen();
LCD1602_Write_Line_Screen(" A1411ZDK",0);
LCD1602_Write_Line_Screen(" 2016*11*18",1);
for(Count = 12;Count>=0;Count--)
{
LCD1602_Move_Right_Screen();
delay();
}
}
}