forked from ArjanteMarvelde/uSDR-pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
301 lines (256 loc) · 8.58 KB
/
lcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* lcd.c
*
* Created: Mar 2021
* Author: Arjan te Marvelde
*
* --> Set I2C address below!
* --> Select LCD_TYPE below!
*
* LCD_1804:
* Grove 16x2 LCD HD44780, with integrated JHD1804 I2C bridge (@ 0x3E)
* 2 byte interface,
* byte0 contains coomand/data,
* byte1 contains 8-bit command or data word
*
* LCD_8574_ADA:
* Standard 16x2 LCD HD44780, with PCF8574 based Adafruit backpack I2C bridge (@ 0x27)
* Same registers, but interface uses 4-bit transfers for data/comand, in bits 3..6
* bit 0 is unused
* bit 1 is Register Select (0 for command, 1 for data)
* bit 2 is Enable, data/command is transferred on falling edge
* bit 3..6 data or command nibble (write high nibble first)
* bit 7 is backlight (1 for on)
*
* LCD_8574_GEN:
* Standard 16x2 LCD HD44780, with PCF8574 based Generic backpack I2C bridge (@ 0x27)
* Same registers, but interface uses 4-bit transfers for data/comand, in bits 4..7
* bit 0 is Register Select (0 for command, 1 for data)
* bit 1 is unused
* bit 2 is Enable, data/command is transferred on falling edge
* bit 3 is backlight (1 for on)
* bit 4..7 data or command nibble (write high nibble first)
*
* Note: There may be other bit-mappings, code needs to be adjusted to that...
*
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
#include "lcd.h"
/** User selectable definitions **/
// Set I2C address
//#define I2C_LCD 0x27
#define I2C_LCD 0x3E
// Select LCD type matching your HW
#define LCD_1804 0 // Seeed / Grove
#define LCD_8574_ADA 1 // Adafruit I2C backpack
#define LCD_8574_GEN 2 // Generic I2C backpack
#define LCD_TYPE LCD_1804
/** HD44780 interface **/
// commands
#define LCD_CLEARDISPLAY 0x01 // Note: LCD_ENTRYINC is set
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode: LCD_ENTRYMODESET
#define LCD_ENTRYSHIFT 0x01
#define LCD_ENTRYNOSHIFT 0x00
#define LCD_ENTRYINC 0x02 // Also applies to CGRAM writes
#define LCD_ENTRYDEC 0x00 // Also applies to CGRAM writes
// flags for display on/off control: LCD_DISPLAYCONTROL
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift: LCD_CURSORSHIFT
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set: LCD_FUNCTIONSET
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
/** I2C interface specific mappings **/
// 1804-based specific bitmasks
#define LCD_COMMAND 0x80
#define LCD_DATA 0x40
#define LCD_INIT_1804 (LCD_FUNCTIONSET | LCD_8BITMODE | LCD_2LINE | LCD_5x8DOTS)
// 8574-based specific bitmasks
#define LCD_COMMAND_ADA 0x00
#define LCD_DATA_ADA 0x02
#define LCD_BACKLIGHT_ADA 0x80
#define LCD_ENABLE_ADA 0x04
#define LCD_INIT_ADA (LCD_FUNCTIONSET | LCD_4BITMODE | LCD_2LINE | LCD_5x8DOTS)
#define LCD_COMMAND_GEN 0x00
#define LCD_DATA_GEN 0x01
#define LCD_ENABLE_GEN 0x04
#define LCD_BACKLIGHT_GEN 0x08
#define LCD_INIT_GEN (LCD_FUNCTIONSET | LCD_4BITMODE | LCD_2LINE | LCD_5x8DOTS)
#if (LCD_TYPE == LCD_1804)
#define LCD_START LCD_INIT_1804
#define LCD_INIT_FUNCTION LCD_INIT_1804
#elif (LCD_TYPE == LCD_8574_ADA)
#define LCD_START 0x30
#define LCD_INIT_FUNCTION LCD_INIT_ADA
#elif (LCD_TYPE == LCD_8574_GEN)
#define LCD_START 0x30
#define LCD_INIT_FUNCTION LCD_INIT_GEN
#endif
/** Other definitions **/
#define LCD_DELAY 100 // Delay for regular write
/*
* User defined (CGRAM) characters
* Display RAM addresses 0x00-0x1f for top row and 0x40-0x5f for bottom row
* Available character Generator addresses are 0x00-0x07
*/
uint8_t cgram[8][8] =
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 0x00: blank
{0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00}, // 0x01: Level 1
{0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x00, 0x00}, // 0x02: Level 2
{0x00, 0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x00, 0x00}, // 0x03: Level 3
{0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00}, // 0x04: Level 4
{0x00, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00}, // 0x05: Level 5
{0x00, 0x04, 0x04, 0x04, 0x1f, 0x0e, 0x04, 0x00}, // 0x06: Receive arrow down
{0x04, 0x0e, 0x1f, 0x04, 0x04, 0x04, 0x00, 0x00} // 0x07: Transmit arrow up
};
/*
* Transfer 1 byte to LCD
* --> this function is interface dependent
*/
void lcd_sendbyte(uint8_t command, uint8_t data)
{
#if LCD_TYPE == LCD_1804
uint8_t txdata[2];
// Write command/data flag and data byte
txdata[0] = (command?LCD_COMMAND:LCD_DATA);
txdata[1] = data;
i2c_write_blocking(i2c1, I2C_LCD, txdata, 2, false);
sleep_us(LCD_DELAY);
#endif
#if LCD_TYPE == LCD_8574_ADA
uint8_t high, low;
high = (command?LCD_COMMAND_ADA:LCD_DATA_ADA)|((data>>1)&0x78)|LCD_BACKLIGHT_ADA;
low = (command?LCD_COMMAND_ADA:LCD_DATA_ADA)|((data<<3)&0x78)|LCD_BACKLIGHT_ADA;
// Write high nibble
high |= LCD_ENABLE_ADA;
i2c_write_blocking(i2c1, I2C_LCD, &high, 1, false); sleep_us(LCD_DELAY);
high &= ~LCD_ENABLE_ADA;
i2c_write_blocking(i2c1, I2C_LCD, &high, 1, false); sleep_us(LCD_DELAY);
// Write low nibble
low |= LCD_ENABLE_ADA;
i2c_write_blocking(i2c1, I2C_LCD, &low, 1, false); sleep_us(LCD_DELAY);
low &= ~LCD_ENABLE_ADA;
i2c_write_blocking(i2c1, I2C_LCD, &low, 1, false); sleep_us(LCD_DELAY);
#endif
#if LCD_TYPE == LCD_8574_GEN
uint8_t high, low;
high = (command?LCD_COMMAND_GEN:LCD_DATA_GEN)|((data )&0xf0)|LCD_BACKLIGHT_GEN;
low = (command?LCD_COMMAND_GEN:LCD_DATA_GEN)|((data<<4)&0xf0)|LCD_BACKLIGHT_GEN;
// Write high nibble
high |= LCD_ENABLE_GEN;
i2c_write_blocking(i2c1, I2C_LCD, &high, 1, false); sleep_us(LCD_DELAY);
high &= ~LCD_ENABLE_GEN;
i2c_write_blocking(i2c1, I2C_LCD, &high, 1, false); sleep_us(LCD_DELAY);
// Write low nibble
low |= LCD_ENABLE_GEN;
i2c_write_blocking(i2c1, I2C_LCD, &low, 1, false); sleep_us(LCD_DELAY);
low &= ~LCD_ENABLE_GEN;
i2c_write_blocking(i2c1, I2C_LCD, &low, 1, false); sleep_us(LCD_DELAY);
#endif
}
/*
* It seems that there is too much in here, but it doesn't harm either.
*/
void lcd_init(void)
{
uint8_t i;
/* HD44780 start sequence */
sleep_ms(500);
i = LCD_START;
lcd_sendbyte(true, i);
sleep_us(4500);
lcd_sendbyte(true, i);
sleep_us(100);
lcd_sendbyte(true, i);
/* Initialize function set */
lcd_sendbyte(true, LCD_INIT_FUNCTION);
/* Initialize display control */
lcd_sendbyte(true, LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF);
/* Initialize entry mode set */
lcd_sendbyte(true, LCD_ENTRYMODESET | LCD_ENTRYINC | LCD_ENTRYNOSHIFT);
/* Initialize display */
// lcd_sendbyte(true, LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF);
lcd_sendbyte(true, LCD_CLEARDISPLAY);
sleep_ms(2);
lcd_sendbyte(true, LCD_RETURNHOME);
sleep_ms(2);
/* Load CGRAM */
for (i=0; i<8; i++)
{
lcd_sendbyte(true, LCD_SETCGRAMADDR | (i<<3)); //Set CGRAM address
for (int j=0; j<8; j++)
lcd_sendbyte(false, cgram[i][j]); // One byte at a time
}
}
void lcd_clear(void)
{
lcd_sendbyte(true, LCD_CLEARDISPLAY);
sleep_ms(2);
}
void lcd_curxy(uint8_t x, uint8_t y, bool on)
{
uint8_t txdata[3];
x &= 0x0f; // Clip range
y &= 0x01;
lcd_sendbyte(true, (x | 0x80 | (y==1?0x40:0x00)));
lcd_sendbyte(true, LCD_DISPLAYCONTROL | LCD_DISPLAYON | (on?LCD_CURSORON:LCD_CURSOROFF) | LCD_BLINKOFF);
}
void lcd_putxy(uint8_t x, uint8_t y, uint8_t c)
{
x &= 0x0f; // Clip range
y &= 0x01;
lcd_sendbyte(true, (x | 0x80 | (y==1?0x40:0x00)));
lcd_sendbyte(false, c);
}
void lcd_writexy(uint8_t x, uint8_t y, uint8_t *s)
{
uint8_t i, len;
x &= 0x0f; // Clip range
y &= 0x01;
lcd_sendbyte(true, (x | 0x80 | (y==1?0x40:0x00)));
len = strlen(s);
len = (len>(16-x))?(16-x):len; // Clip range
for(i=0; i<len; i++)
lcd_sendbyte(false, s[i]);
}
void lcd_test(void)
{
uint8_t chr[17];
int i, j;
chr[16] = 0;
lcd_clear();
for (i=0; i<16; i++)
{
for(j=0; j<16; j++)
chr[j] = (uint8_t)(16*i+j);
lcd_writexy(0, 0, chr);
sleep_ms(800);
lcd_writexy(0, 1, chr);
}
lcd_clear();
}