Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * LCD1602.c
- *
- * Created on: Jan 22, 2023
- * Author: Psi
- */
- #include "LCD1602.h"
- #define LCD_ADDR (0x27 << 1)
- #define PIN_RS (1 << 0)
- #define PIN_EN (1 << 2)
- #define BACKLIGHT (1 << 3)
- #define LCD_DELAY_MS 5
- void I2C_Scan(I2C_HandleTypeDef *hi2c) {
- HAL_StatusTypeDef res;
- for(uint16_t i = 0; i < 128; i++) {
- res = HAL_I2C_IsDeviceReady(hi2c, i << 1, 1, 10);
- if(res == HAL_OK) {
- HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
- } else {
- }
- }
- }
- HAL_StatusTypeDef LCD_SendInternal(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, uint8_t data, uint8_t flags) {
- HAL_StatusTypeDef res;
- for(;;) {
- res = HAL_I2C_IsDeviceReady(hi2c, lcd_addr, 1, HAL_MAX_DELAY);
- if(res == HAL_OK)
- break;
- }
- uint8_t up = data & 0xF0;
- uint8_t lo = (data << 4) & 0xF0;
- uint8_t data_arr[4];
- data_arr[0] = up|flags|BACKLIGHT|PIN_EN;
- data_arr[1] = up|flags|BACKLIGHT;
- data_arr[2] = lo|flags|BACKLIGHT|PIN_EN;
- data_arr[3] = lo|flags|BACKLIGHT;
- res = HAL_I2C_Master_Transmit(hi2c, lcd_addr, data_arr, sizeof(data_arr), HAL_MAX_DELAY);
- HAL_Delay(LCD_DELAY_MS);
- return res;
- }
- void LCD_SendCommand(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, uint8_t cmd) {
- LCD_SendInternal(hi2c, lcd_addr, cmd, 0);
- }
- void LCD_SendData(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, uint8_t data) {
- LCD_SendInternal(hi2c, lcd_addr, data, PIN_RS);
- }
- void LCD_Init(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr) {
- // 4-bit mode, 2 lines, 5x7 format
- LCD_SendCommand(hi2c, lcd_addr, 0b00110000);
- // display & cursor home (keep this!)
- LCD_SendCommand(hi2c, lcd_addr, 0b00000010);
- // display on, right shift, underline off, blink off
- LCD_SendCommand(hi2c, lcd_addr, 0b00001100);
- // clear display (optional here)
- LCD_SendCommand(hi2c, lcd_addr, 0b00000001);
- }
- void LCD_SendString(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, char *str) {
- while(*str) {
- LCD_SendData(hi2c, lcd_addr, (uint8_t)(*str));
- str++;
- }
- }
- void init(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr) {
- I2C_Scan(hi2c);
- LCD_Init(hi2c, lcd_addr);
- // set address to 0x00
- LCD_SendCommand(hi2c, lcd_addr, 0b10000000);
- LCD_SendString(hi2c, lcd_addr, " Using 1602 LCD");
- // set address to 0x40
- LCD_SendCommand(hi2c, lcd_addr, 0b11000000);
- LCD_SendString(hi2c, lcd_addr, " over I2C bus");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement