模块简介
直接用lcd1602的话,至少占用8个gpio口,所以用的PCF8574P模块,只需要用I2C控制即可;
硬件原理图
手边只有一个5v的LCD1602,VDDIO口也要5V,我在处理I2C5v和3v的VDDIO电平连接时,使用n-mos管,稍微稳定一些,当然也可以上专门的电平互转IC;
软件部分
- main.c
- lcd1602_pcf8574.h
- lcd1602_pcf8574.c
- my_iic.h
- my_iic.c
- main.c部分:
/* main
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_err.h"
#include "driver/i2c.h"
#include "lcd1602_pcf8574.h"
void app_main(void)
{
char str1[41] = "Show text..";
i2c_master_init();
clear();
lcd_init_chip(I2C_EXAMPLE_MASTER_NUM);
isBacklight(1); //open Backlight
LCD_Write_String(0,0,str1);//从第一行第一个位置开始显示
i2c_driver_delete(I2C_EXAMPLE_MASTER_NUM);
}
- lcd1602_pcf8574.h:
/* lcd1602+pcf8574 display model
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_err.h"
#include "my_iic.h"
#include "my_iic.h"
/*
pcf8574 | lcd1602
p0 -> RS
p1 -> RW
p2 -> CS
p3 -> LED-K by PNP
p4 -> P4
p5 -> P5
p6 -> P6
p7 -> P7
VDD -> LED-A
*/
#define Rs 0x1 // Register select bit
#define Rw 0x2 // Read/Write bit
#define En 0x4 // Enable bit
// flags for backlight control
#define LCD_BACKLIGHT 0x01<<3
#define LCD_NOBACKLIGHT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for display entry mode
#define