Advertisement
PsiAmp

lcd1602

Jan 21st, 2023
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. /*
  2.  * LCD1602.c
  3.  *
  4.  *  Created on: Jan 22, 2023
  5.  *      Author: Psi
  6.  */
  7.  
  8. #include "LCD1602.h"
  9.  
  10. #define LCD_ADDR (0x27 << 1)
  11.  
  12. #define PIN_RS    (1 << 0)
  13. #define PIN_EN    (1 << 2)
  14. #define BACKLIGHT (1 << 3)
  15.  
  16. #define LCD_DELAY_MS 5
  17.  
  18. void I2C_Scan(I2C_HandleTypeDef *hi2c) {
  19.     HAL_StatusTypeDef res;
  20.  
  21.     for(uint16_t i = 0; i < 128; i++) {
  22.         res = HAL_I2C_IsDeviceReady(hi2c, i << 1, 1, 10);
  23.  
  24.         if(res == HAL_OK) {
  25.             HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  26.         } else {
  27.         }
  28.     }
  29. }
  30.  
  31. HAL_StatusTypeDef LCD_SendInternal(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, uint8_t data, uint8_t flags) {
  32.     HAL_StatusTypeDef res;
  33.     for(;;) {
  34.         res = HAL_I2C_IsDeviceReady(hi2c, lcd_addr, 1, HAL_MAX_DELAY);
  35.         if(res == HAL_OK)
  36.             break;
  37.     }
  38.  
  39.     uint8_t up = data & 0xF0;
  40.     uint8_t lo = (data << 4) & 0xF0;
  41.  
  42.     uint8_t data_arr[4];
  43.     data_arr[0] = up|flags|BACKLIGHT|PIN_EN;
  44.     data_arr[1] = up|flags|BACKLIGHT;
  45.     data_arr[2] = lo|flags|BACKLIGHT|PIN_EN;
  46.     data_arr[3] = lo|flags|BACKLIGHT;
  47.  
  48.     res = HAL_I2C_Master_Transmit(hi2c, lcd_addr, data_arr, sizeof(data_arr), HAL_MAX_DELAY);
  49.     HAL_Delay(LCD_DELAY_MS);
  50.     return res;
  51. }
  52.  
  53. void LCD_SendCommand(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, uint8_t cmd) {
  54.     LCD_SendInternal(hi2c, lcd_addr, cmd, 0);
  55. }
  56.  
  57. void LCD_SendData(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, uint8_t data) {
  58.     LCD_SendInternal(hi2c, lcd_addr, data, PIN_RS);
  59. }
  60.  
  61. void LCD_Init(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr) {
  62.     // 4-bit mode, 2 lines, 5x7 format
  63.     LCD_SendCommand(hi2c, lcd_addr, 0b00110000);
  64.     // display & cursor home (keep this!)
  65.     LCD_SendCommand(hi2c, lcd_addr, 0b00000010);
  66.     // display on, right shift, underline off, blink off
  67.     LCD_SendCommand(hi2c, lcd_addr, 0b00001100);
  68.     // clear display (optional here)
  69.     LCD_SendCommand(hi2c, lcd_addr, 0b00000001);
  70. }
  71.  
  72. void LCD_SendString(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr, char *str) {
  73.     while(*str) {
  74.         LCD_SendData(hi2c, lcd_addr, (uint8_t)(*str));
  75.         str++;
  76.     }
  77. }
  78.  
  79. void init(I2C_HandleTypeDef *hi2c, uint8_t lcd_addr) {
  80.     I2C_Scan(hi2c);
  81.     LCD_Init(hi2c, lcd_addr);
  82.  
  83.     // set address to 0x00
  84.     LCD_SendCommand(hi2c, lcd_addr, 0b10000000);
  85.     LCD_SendString(hi2c, lcd_addr, " Using 1602 LCD");
  86.  
  87.     // set address to 0x40
  88.     LCD_SendCommand(hi2c, lcd_addr, 0b11000000);
  89.     LCD_SendString(hi2c, lcd_addr, "  over I2C bus");
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement