其他分享
首页 > 其他分享> > LCD1602A的学习指南

LCD1602A的学习指南

作者:互联网

最近,意外获得了一块LCD1602A的显示屏,外加PCF8574T模块使用IIC协议。如下如所示;

在开发的过程中,遇到了很多挫折,记录下来,帮助正在有困惑的你。

  1. 寻找LCD1602A的驱动
    在这个网站上,可以找到很多库文件及示例教程,不过都是英文,慢慢看也是可以学会的,我也会把lcd的库函数传到本博客的下方,供各位免费下载。
  2. 创建工程,导入i2c-lcd.h,i2c-lcd.c到工程中。
  3. 调用.h文件的函数编写函数。
    下面是一个简单的示例。
	lcd_init();
	
	lcd_send_string("hello word!");
	
	HAL_Delay(1000);
	
	lcd_put_cur(1,0);
	lcd_send_string("nice a story!");
	HAL_Delay(1000);
  1. 烧写代码到开发版。
    我使用的是STM32最小开发版---STM32F103C8T6

    因为我的ST-LINK下载器在学校忘记拿了,只能使用USB-TTL,借助flymc进行下载。在下载时,将BOOT0与3V通过跳线帽相连。下载结束后,将跳线帽取下。
    这时候,这个实验效果你就可以看到了,喜欢的给点个赞哦。

下面是LCD1602A的驱动代码,喜欢的朋友可以直接复制。

i2c-lcd.c


/** Put this in the src folder **/

#include "i2c-lcd.h"
extern I2C_HandleTypeDef hi2c1;  // change your handler here accordingly

#define SLAVE_ADDRESS_LCD 0x4E // change this according to ur setup

void lcd_send_cmd (char cmd)
{
  char data_u, data_l;
	uint8_t data_t[4];
	data_u = (cmd&0xf0);
	data_l = ((cmd<<4)&0xf0);
	data_t[0] = data_u|0x0C;  //en=1, rs=0
	data_t[1] = data_u|0x08;  //en=0, rs=0
	data_t[2] = data_l|0x0C;  //en=1, rs=0
	data_t[3] = data_l|0x08;  //en=0, rs=0
	HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
}

void lcd_send_data (char data)
{
	char data_u, data_l;
	uint8_t data_t[4];
	data_u = (data&0xf0);
	data_l = ((data<<4)&0xf0);
	data_t[0] = data_u|0x0D;  //en=1, rs=0
	data_t[1] = data_u|0x09;  //en=0, rs=0
	data_t[2] = data_l|0x0D;  //en=1, rs=0
	data_t[3] = data_l|0x09;  //en=0, rs=0
	HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
}

void lcd_clear (void)
{
	lcd_send_cmd (0x80);
	for (int i=0; i<70; i++)
	{
		lcd_send_data (' ');
	}
}

void lcd_put_cur(int row, int col)
{
    switch (row)
    {
        case 0:
            col |= 0x80;
            break;
        case 1:
            col |= 0xC0;
            break;
    }

    lcd_send_cmd (col);
}


void lcd_init (void)
{
	// 4 bit initialisation
	HAL_Delay(50);  // wait for >40ms
	lcd_send_cmd (0x30);
	HAL_Delay(5);  // wait for >4.1ms
	lcd_send_cmd (0x30);
	HAL_Delay(1);  // wait for >100us
	lcd_send_cmd (0x30);
	HAL_Delay(10);
	lcd_send_cmd (0x20);  // 4bit mode
	HAL_Delay(10);

  // dislay initialisation
	lcd_send_cmd (0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
	HAL_Delay(1);
	lcd_send_cmd (0x08); //Display on/off control --> D=0,C=0, B=0  ---> display off
	HAL_Delay(1);
	lcd_send_cmd (0x01);  // clear display
	HAL_Delay(1);
	HAL_Delay(1);
	lcd_send_cmd (0x06); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)
	HAL_Delay(1);
	lcd_send_cmd (0x0C); //Display on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)
}

void lcd_send_string (char *str)
{
	while (*str) lcd_send_data (*str++);
}

i2c-lcd.h

#include "stm32f1xx_hal.h"

void lcd_init (void);   // initialize lcd

void lcd_send_cmd (char cmd);  // send command to the lcd

void lcd_send_data (char data);  // send data to the lcd

void lcd_send_string (char *str);  // send string to the lcd

void lcd_put_cur(int row, int col);  // put cursor at the entered position row (0 or 1), col (0-15);

void lcd_clear (void);

标签:学习指南,HAL,void,cmd,send,Delay,lcd,LCD1602A
来源: https://www.cnblogs.com/fanlulin/p/16342793.html