57 lines
1.3 KiB
C
57 lines
1.3 KiB
C
#include "lcd_i2c.h"
|
|
#include <string.h>
|
|
|
|
extern I2C_HandleTypeDef hi2c1;
|
|
#define LCD_ADDR (0x27 << 1)
|
|
#define LCD_BACKLIGHT 0x08
|
|
#define LCD_ENABLE 0x04
|
|
|
|
void lcd_send_cmd(uint8_t cmd);
|
|
void lcd_send_data(uint8_t data);
|
|
void lcd_send(uint8_t data, uint8_t mode);
|
|
|
|
void lcd_init(void) {
|
|
HAL_Delay(50);
|
|
lcd_send_cmd(0x33);
|
|
lcd_send_cmd(0x32);
|
|
lcd_send_cmd(0x28);
|
|
lcd_send_cmd(0x0C);
|
|
lcd_send_cmd(0x06);
|
|
lcd_send_cmd(0x01);
|
|
HAL_Delay(5);
|
|
}
|
|
|
|
void lcd_clear(void) {
|
|
lcd_send_cmd(0x01);
|
|
HAL_Delay(2);
|
|
}
|
|
|
|
void lcd_set_cursor(uint8_t row, uint8_t col) {
|
|
const uint8_t row_offsets[] = {0x00, 0x40, 0x14, 0x54};
|
|
lcd_send_cmd(0x80 | (col + row_offsets[row]));
|
|
}
|
|
|
|
void lcd_print(const char *str) {
|
|
while (*str) {
|
|
lcd_send_data((uint8_t)(*str++));
|
|
}
|
|
}
|
|
|
|
void lcd_send_cmd(uint8_t cmd) {
|
|
lcd_send(cmd, 0);
|
|
}
|
|
|
|
void lcd_send_data(uint8_t data) {
|
|
lcd_send(data, 1);
|
|
}
|
|
|
|
void lcd_send(uint8_t data, uint8_t mode) {
|
|
uint8_t high = (data & 0xF0) | LCD_BACKLIGHT | (mode ? 0x01 : 0);
|
|
uint8_t low = ((data << 4) & 0xF0) | LCD_BACKLIGHT | (mode ? 0x01 : 0);
|
|
|
|
uint8_t data_arr[4] = {
|
|
high | LCD_ENABLE, high,
|
|
low | LCD_ENABLE, low
|
|
};
|
|
HAL_I2C_Master_Transmit(&hi2c1, LCD_ADDR, data_arr, 4, HAL_MAX_DELAY);
|
|
} |