LCD1602 Driver

Publisher:梦想启航Latest update time:2019-05-13 Source: eefocusKeywords:LCD1602 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

LCD1602driver_inside.c



/*This driver depends on the reg51.h header file*/
#include


/*Modify macro definitions and sbit statements according to actual circuit connections*/
#define D0_D7 P3 //Data communication bus, used to write or read instructions, display addresses and display data
sbit RS = P2^5; //Register selection pin, set 0 to select instruction and display address registers, set 1 to select display data register
sbit RW = P2^6; //Read and write pin, set 0 to write, set 1 to read
sbit E = P2^7; //Signal enable pin, set 0 to allow writing, set 1 to allow reading
sbit BF = P3^7; //Busy signal, busy is 1, not busy is 0


/*Modify macro definitions and sbit statements according to actual circuit connections*/


/*Internal function declaration*/


bit Busy_Check(); //1602 busy signal detection, busy returns 1, not busy returns 0 (this function needs to be called before all write operations
void Data_Write(char Data); //Write 8-bit display data (supports direct reception of characters in the 1602 font library or corresponding ASCII code)
void Addr_Write(unsigned char Addr); //Write 8-bit display address
void Command_Write(unsigned char Command); //Write various commands of 1602
void Cursor_Reset(); //Reset the cursor position to the upper left corner
unsigned char Coordinate_To_Addr(unsigned char X_Intercept,unsigned char Y_Intercept); //Convert the input display coordinates to the corresponding display address and return
/*For single-line display, 0 <= X_Intercept <= 79, Y_Interce == 0; for double-line display, 0 <= X_Intercept <= 39, 0 <= Y_Interce <= 1; */
void DelayMS(unsigned int MS_Num); //Delay function MS
void Open_Screen_Inside(); //Screen display opening function
void Close_Screen_Inside(); //Screen display closing function
void Move_Screen_Left_Inside(); //Screen left movement function
void Move_Screen_Right_Inside(); //Screen right movement function
void Reset_D0_D7(); //Set the communication bus to low level
/*Internal function declaration*/


/*void main() //For syntax testing, uncomment the statements when using
{
    Reset_D0_D7();
    Command_Write(0x38);
    DelayMS(5);
    Command_Write(0x06);
    Open_Screen_Inside();
    DelayMS(5);
    Command_Write(0x01);
    DelayMS(5);
    Addr_Write(Coordinate_To_Addr(0,0)); 
    DelayMS(1);
    Data_Write('5');
    P3 = 0;
    Close_Screen_Inside();
    Open_Screen_Inside();
    Data_Write('6');
    Data_Write('7');
    Data_Write('7');
    Move_Screen_Inside(1);
    Move_Screen_Inside(1);
    Cursor_Reset();
    Data_Write('7');
    Data_Write('7');
    Move_Screen_Inside(1);
    while(1){P1 = 0;}
} */


/*Internal function definition*/
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()
{
// bit result;
// RS = 0;
// RW = 1;
// E = 1;
    DelayMS(1); // Pseudo busy check
// result = (bit)(P2&0x80);
// return result;
}


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();     //1602忙信号检测,忙返回1,不忙返回0(所有写操作前均需要调用此函数
void Data_Write(char Data);     //写入8位显示数据(支持直接接收1602字库内字符或对应ASCII码)
void Addr_Write(unsigned char Addr);        //写入8位显示地址
void Command_Write(unsigned char Command);      //写入1602的各种命令
void Cursor_Reset();        //光标位置重置为左上角
unsigned char Coordinate_To_Addr(unsigned char X_Intercept,unsigned char Y_Intercept); //Convert the input display coordinates to the corresponding display address and return
/*When displaying in single line, 0 <= X_Intercept <= 79, Y_Interce == 0; when displaying in double line, 0 <= X_Intercept <= 39, 0 <= Y_Interce <= 1; */
void DelayMS(unsigned int MS_Num);
void Open_Screen_Inside(); //Screen display opening function
void Close_Screen_Inside(); //Screen display closing function
void Move_Screen_Left_Inside(); //Screen left movement function
void Move_Screen_Right_Inside(); //Screen right movement function
void Reset_D0_D7(); //Set the communication bus to low level


#endif
 
LCD1602.c


/*


unsigned char Busy_Check(); //1602 busy signal detection, busy returns 1, not busy returns 0 (this function needs to be called before all write operations
void Data_Write(unsigned char Data); //Write 8-bit display data (supports direct reception of characters in the 1602 font or corresponding ASCII code)
void Addr_Write(unsigned char Addr); //Write 8-bit display address
void Command_Write(unsigned char Command); //Write various commands of 1602
void Cursor_Reset(); //Reset the cursor position to the upper left corner
unsigned char Coordinate_To_Addr(unsigned char X_Intercept,unsigned char Y_Intercept); //Convert the input display coordinates to the corresponding display address and return
/For double-line display, 0 <= X_Intercept <= 47, Y_Interce == 0; for single-line display, 0 <= X_Intercept <= 79, 0 <= Y_Interce <= 1;/


void DelayMS(unsigned int MS_Num);
void Open_Screen_Inside(); //Screen display opening function
void Close_Screen_Inside(); //Screen display closing function
void Move_Screen_Left_Inside(); //Screen left movement function
void Move_Screen_Right_Inside(); //Screen right movement function
void Reset_D0_D7(); //Set the communication bus to low level


*/
#include"LCD1602driver_inside.h"
#include       
//Includes internal functions. For specific contents, please refer to LCD1602driver_inside.h and LCD1602driver internal functions.c. To understand the function functions, you can also refer to the large comment at the beginning of this file.


/*Callable function declaration*/


void LCD1602_Init(unsigned char Line_Num,unsigned char Char_Type); //Initialization function, receiving the number of lines (1 or 2) and the dot matrix character type (1 or 2, corresponding to 5x7 and 5x10 dot matrix characters)
void LCD1602_Clear_Screen();
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept); //Single character output function; When displaying in a single line, 0 <= X_Intercept <= 79, Y_Interce == 0; When displaying in two lines, 0 <= X_Intercept <= 39, 0 <= Y_Interce <= 1; The screen will display the characters before the X coordinate of 15, and the other characters will be displayed by moving the cursor
void LCD1602_Write_Line_Screen(char Line_Data[28],unsigned char Y_Intercept); //Entire line output function; Need to receive 16 to 28 characters, 0 <= Y_Interce <= 1; The screen will display the first 16 characters, and the other characters will be displayed by moving the cursor 
void LCD1602_Open_Screen(); //Screen display opening function
void LCD1602_Close_Screen(); //Screen display closing function
void LCD1602_Move_Left_Screen(); //Screen moves one position to the left
void LCD1602_Move_Right_Screen(); //Screen moves one position to the right


/*Callable function declaration*/


/*void main() //For syntax testing, uncomment the statement when using it
{
    LCD1602_Init(2,1);
    LCD1602_Write_Line_Screen("123456",0);
    LCD1602_Write_Line_Screen("123456",1);
    LCD1602_Move_Screen(1);
    LCD1602_Move_Screen(0);
    LCD1602_Move_Screen(1);
    LCD1602_Move_Screen(1);


    while(1);
}       */


/*可调用函数定义*/


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] != '';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);      //初始化函数,接收行数(1或2)和点阵字符类型(1或2,对应5x7和5x10点阵字符)
void LCD1602_Clear_Screen();
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept);        //单字符输出函数;单行显示时,0 <= X_Intercept <= 79,Y_Interce == 0;双行显示时,0 <= X_Intercept <= 39,0 <= Y_Interce <= 1;; 屏幕会显示X坐标在15之前的字符,其他字符通过光标移动显示
void LCD1602_Write_Line_Screen(char Line_Data[16],unsigned char Y_Intercept); //Full line output function; receive up to 28 characters, 0 <= Y_Intercept <= 1; the screen will display the first 16 characters, and the rest of the characters will be displayed by moving the screen 
void LCD1602_Open_Screen(); //Screen display opening function
void LCD1602_Close_Screen(); //Screen display closing function
void LCD1602_Move_Left_Screen(); //Screen moves left one position
void LCD1602_Move_Right_Screen(); //Screen moves right one position
#endif
 
driver_test.c


#include
#include"LCD1602driver_inside.h"
#include
#include
/*Header file contains function
void LCD1602_Init(unsigned char Line_Num,unsigned char Char_Type); //Initialization function, receive line number (1 or 2) and dot matrix character type (1 or 2, corresponding to 5x7 and 5x10 dot matrix characters)
void LCD1602_Clear_Screen();
void LCD1602_Write_Char_Screen(char ASCII_Data,unsigned char X_Intercept,unsigned char Y_Intercept); //Single character output function; when single line display, 0 <= X_Intercept <= 79, Y_Interce == 0; when double line display, 0 <= X_Intercept <= 39, 0 <= Y_Interce <= 1;; The screen will display the characters with X coordinate before 15, and other characters will be displayed by moving the cursor
void LCD1602_Write_Line_Screen(char Line_Data[16],unsigned char Y_Intercept); //Entire line output function; receive up to 16 characters, 0 <= Y_Interce <= 1; The screen will display the first 16 characters, and the other characters will be displayed by moving the cursor 
void LCD1602_Open_Screen(); //Screen display opening function
void LCD1602_Close_Screen(); //Screen display closing function
void LCD1602_Move_Left_Screen(); //Screen moves one position to the left
void LCD1602_Move_Right_Screen(); //Screen moves one position to the right
*/
void delay(void) //Delay 0.25S
{
    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();
        }    
    }
}

[1] [2]
Keywords:LCD1602 Reference address:LCD1602 Driver

Previous article:SAA3010T infrared decoding
Next article:51 MCU simplest LED driver

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号