LCD 1602 Driver with ESP32 I2C 1.1
This is a complete driver for LCD 1602 using I2C with the ESP32
Loading...
Searching...
No Matches
lcd_1602.c
Go to the documentation of this file.
1
9
11#include "internal/lcd_i2c.h"
12
22static uint8_t write_nibble(i2c_master_dev_handle_t handle, uint8_t nibble, bool rs) {
23 uint8_t data = (nibble & 0xF0);
24
25 if(rs) data |= LCD_1602_RS;
26
27 data |= LCD_1602_BACKLIGHT;
28
29 uint8_t sequence[2];
30 sequence[0] = data | LCD_1602_ENABLE;
31 sequence[1] = data & ~LCD_1602_ENABLE;
32
33 return i2c_master_transmit(handle, sequence, sizeof(sequence), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
34}
35
44static uint8_t send_command(i2c_master_dev_handle_t handle, uint8_t cmd) {
45 uint8_t err = 0;
46
47 uint8_t high = cmd & 0xF0;
48 uint8_t low = (cmd << 4) & 0xF0;
49
50 err = write_nibble(handle, high, false);
51 vTaskDelay(pdMS_TO_TICKS(2));
52 err = write_nibble(handle, low, false);
53 vTaskDelay(pdMS_TO_TICKS(2));
54
55 return err;
56}
57
66static uint8_t send_char(i2c_master_dev_handle_t handle, char c) {
67 uint8_t err = 0;
68
69 uint8_t high = c & 0xF0;
70 uint8_t low = (c << 4) & 0xF0;
71
72 err = write_nibble(handle, high, true);
73 vTaskDelay(pdMS_TO_TICKS(2));
74 err = write_nibble(handle, low, true);
75 vTaskDelay(pdMS_TO_TICKS(2));
76
77 return err;
78}
79
87static uint8_t clear_screen(i2c_master_dev_handle_t handle) {
88 uint8_t err = send_command(handle, LCD_1602_CLEAR_SCREEN);
89 vTaskDelay(pdMS_TO_TICKS(10));
90 return err;
91}
92
102static uint8_t lcd_goto(i2c_master_dev_handle_t handle, uint8_t x, uint8_t y) {
103 static const uint8_t row_offsets[] = { 0x00, 0x40 };
104 if (y > 1) y = 1;
105 uint8_t err = send_command(handle, 0x80 | (row_offsets[y] + x));
106
107 vTaskDelay(pdMS_TO_TICKS(10));
108 return err;
109}
110
111LCD_WRITE_STATUS lcd_1602_send_string(i2c_master_dev_handle_t handle, char *str) {
112 clear_screen(handle);
113
114 uint8_t char_len = 0;
115 uint8_t row = 0;
116
117 while(*str != '\0') {
118 if(*str == '\n' || char_len >= 16) {
119 if(row < LCD_1602_MAX_ROWS) {
120 lcd_goto(handle, 0,1);
121 row++;
122 char_len = 0;
123 str++;
124 continue;
125 }
126 else return LCD_WRITE_INTERRUPTED;
127 }
128
129 if(*str == '\0') {
130 return LCD_WRITE_FINISHED;
131 }
132
133 send_char(handle, *str);
134 char_len++;
135 str++;
136 }
137
138 return LCD_WRITE_FINISHED;
139}
140
141uint8_t lcd_1602_init(i2c_master_dev_handle_t handle) {
142/*
143This function sets up the standard mode of the LCD and follows
144a specific start up sequence described by the manufacturer.
145*/
146 // Manufacturer specific order
147 vTaskDelay(pdMS_TO_TICKS(15));
148 write_nibble(handle, (0x03 << 4), 0);
149 vTaskDelay(pdMS_TO_TICKS(5));
150 write_nibble(handle, (0x03 << 4), 0);
151 vTaskDelay(pdMS_TO_TICKS(2));
152 write_nibble(handle, (0x03 << 4), 0);
153 vTaskDelay(pdMS_TO_TICKS(2));
154 write_nibble(handle, (0x02 << 4), 0);
155 // End of manufacturer specific order
156
159 clear_screen(handle);
161
162 return 0;
163}
164
#define LCD_1602_MAX_ROWS
Definition lcd_1602.h:51
LCD_WRITE_STATUS lcd_1602_send_string(i2c_master_dev_handle_t handle, char *str)
Writes out data to the LCD.
Definition lcd_1602.c:111
uint8_t lcd_1602_init(i2c_master_dev_handle_t handle)
initializes the LCD 1602 correctly according to the datasheet.
Definition lcd_1602.c:141
#define LCD_1602_FUNCTION_SET(dl, r, f)
Macro for configuring the lcd screens function. Use together with:
#define LCD_1602_CONFIG_INPUT_SET(dir, mov)
Macro for setting the correct movement of the cursor. Use together with:
#define LCD_1602_CONFIG_DISPLAY_SWITCH(disp, curs, blink)
Macro for configuring the display settings. Use together with;.
LCD_WRITE_STATUS
Enum for returning the result of writing to the LCD.
Definition lcd_1602.h:58
@ LCD_WRITE_INTERRUPTED
Definition lcd_1602.h:61
@ LCD_WRITE_FINISHED
Definition lcd_1602.h:59
#define LCD_1602_2_ROWS
#define LCD_1602_N_BLINK_DISPLAY
#define LCD_1602_DISPLAY_ON
#define LCD_1602_CLEAR_SCREEN
#define LCD_1602_CURSOR_OFF
#define LCD_1602_BACKLIGHT
#define LCD_1602_ENABLE
#define LCD_1602_INCREMENT_MODE
#define LCD_1602_FONT_5X10
#define LCD_1602_CURSOR_N_MOVE
#define LCD_1602_DATA_LEN_4_BIT
#define LCD_1602_RS
: Internal implementation of the LCD1602
#define I2C_MASTER_TIMEOUT_MS
Definition lcd_i2c.h:21