MCU through PCF8574T module driver 1602/2004LCD

Publisher:anluranLatest update time:2020-02-05 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The microcontroller drives the 1602/2004 LCD through the PCF8574T module. Friends in need can take a look. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int 


//#define L1 0x80 // The first line write address
//#define L2 0xc0 // The second line write address
        
sbit SCL = P2^0;
sbit SDA = P2^1;


//char ADDR = 0x4E; // The address code of the PCF8574 T module
  char ADDR = 0x7e; // The address code of the PCF8574 AT module


//***************************** Delay y ms ***************************************************


void delay1(int y) // 
{
         ;
        while(y--)
        {
        unsigned char a,b,c;
        for(c=1;c>0;c--)
        for(b=142;b>0;b--)
        for(a=2;a>0;a--);
        }
}


//******************************** IIC serial port starts********************************************


void IIC_start(void)
{
        SDA=1;
        _nop_();
        SCL=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        SDA=0;
        _nop_();
        _nop_ (
        );
        _nop_(); _nop_()
        ;
        SCL=0;
}




//********************************** IIC serial port writes 1 byte******************************************


void IIC_writeByte(char temp)
{
        char i;
        for(i=0;i<8;i++)
        {
                SDA=(bit)(temp & 0x80); // According to the regulations, the highest bit of 1602 data must be 1  
                temp <<=1;
                _nop_();
                _nop_();
                SCL=1;                 _nop_
                ();                 _nop_(); _nop_();                 _nop_();                 _nop_();                 SCL=0;         }         _nop_();          _nop_();         _nop_();         _nop_();         SDA=1;         _nop_();          _nop_();          _nop_();         _nop_();         SCL=1;          _nop_();         _nop_();         _nop_();         while(SDA);         _nop_();         SCL=0; }



























//******************************** 1602 write command********************************************


void LCD_write_command(char comm)
{
        char tmp;
        IIC_start(); // Serial port starts
        IIC_writeByte(ADDR); // Select the address of PCF 8574T first (it should be equivalent to the meaning of selection)
        
        tmp = comm & 0xF0; // and 0xf0 should mean taking the fourth bit
        tmp |= 0x0C; //Retain the upper 4 bits as the upper 4 bits of the instruction, and the lower 4 bits are RS = 0, RW = 0, EN = 1 IIC_writeByte  
        (tmp); //Send
        delay1(20) from the serial port;
        tmp &= 0xFB; //Make EN = 0
        IIC_writeByte(tmp); 
        
        tmp = (comm & 0x0F) << 4 ; //Send the lower 4 bits of the instruction to the high position for storage
        tmp |= 0x0C; //RS = 0, RW = 0, EN = 1
        IIC_writeByte(tmp);
        delay1(20);
        tmp &= 0xFB; // Make EN = 0
        IIC_writeByte(tmp);
        
}
//******************************** 1602 write data********************************************


void LCD_write_data(char data1)
{
        char tmp;
        IIC_start();
        IIC_writeByte(ADDR); // Select the address of PCF 8574T first (it should be equivalent to the meaning of selection)
        
        tmp = data1 & 0xF0;
        tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
        IIC_writeByte(tmp);
        delay1(20);
        tmp &= 0xFB; //Make EN = 0
        IIC_writeByte(tmp); 
        
        tmp = (data1 & 0x0F) << 4 ;
        tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
        IIC_writeByte(tmp);
        delay1(20);
        tmp &= 0xFB ; // Make EN = 0
        IIC_writeByte(tmp);
}


//******************************** 1602 Initialization********************************************


void Init_Lcd(void)
{
        LCD_write_command(0x33); //Convert 8-bit bus to 4-bit bus
        delay1(50);
        LCD_write_command(0x32); //
        delay1(50); 
        LCD_write_command(0x28); // 4-bit data line, display 2 lines, 5*7 dot matrix characters! If it is 0x38, it is 8-bit data line, display 2 lines, 5*7 dot matrix characters
        delay1(50); 
        LCD_write_command(0x0C); // Turn on display, turn off cursor, no blinking
        delay1(50);  
        LCD_write_command(0x06); // Set input mode, increment without displacement
        delay1(50); 
        LCD_write_command(0x01); // Clear screen
        delay1(50);
}








//*************************************** Display the string at the specified position ******************************************


void Write_LCD(int x, int y, char *str)
{
        char addr;
        if(x < 0)
        {
                x = 0;
        }
        if(x > 15)
        {
                x = 15;
        }
        if(y<0)
        {
                s++;
        } }                 //********************************* Display a character at the specified position         ***************************************          /         *
        void         Print_Char (unsigned char line ,unsigned char num,unsigned char date)         {         LCD_write_command         (         line                 + num )         ;                 LCD_write_data                  (                 date);                 }         * / // Display a character at the specified position ( for 1602 LCD ) -used in temperature displayvoid DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData) { Y &= 0x1; X &= 0xF; //Limit X not to be greater than 15, Y not to be greater than 1 if (Y) X |= 0x40; //When the second line is to be displayed, the address code + 0x40; X |= 0x80; //Calculate the command code LCD_write_command(X); //Do not detect the busy signal here, send the address code LCD_write_data(DData); }



        

















































Keywords:MCU Reference address:MCU through PCF8574T module driver 1602/2004LCD

Previous article:Design of DHT11 temperature and humidity detection control system based on 51 single chip microcomputer
Next article:Analysis of the working selection mode of the register TMOD of the MCS-51 single-chip microcomputer

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号