16x02 LCD via I2C (PCF8574) using STM32F103C8Tx microcontroller.
By: Nachiketh V Boin
STM32F103C8Tx <->
PCF8574 <-> LCD 16×
-GitHub link for code files: nachikethboin/16x02-LCD-via-I2C-PCF8574-using-STM32F103C8Tx-microcontroller. (github.com)
-Hardware Components and
PC Requirements
>STM32F103C8Tx Board. Amazon Buy Link
> LCD
16X02 MODULE WITH SERIAL I2C INTERFACE MODULE.
Amazon Buy Link
>PC with
STM32CubeIde (CubeIDE Link)
, Bread Board and connectors.
-Pinout and Configuration
> A_>Z
·
RCC > Mode
> High Speed Clock (HSE) >
Crystal/Ceramic Resonator (clock from external crystal).
>Low Speed Clock (LSE) >
Disable.
·
SYS > Mode
> Debug > Serial Wire (select
serial wire debug or else the ST link will not work after the first flash and
to make it work again you have to erase it).
> Timebase Source > SysTick.
·
I2C1 > Mode
> I2C > I2C (Note: Pin PB6 >
I2C SCL, Pin PB7 > I2C SDA)
-Clock Configuration
-i2c-lcd.h file
/** Put this in the Inc folder **/
#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);
-i2c-lcd.c file
/** 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
initialization
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);
// display
initialization
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++);
}
thank you for reading my blog.
Comments
Post a Comment