forked from nimaltd/LCD-Character
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD.c
270 lines (265 loc) · 9.13 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
#include "LCD.h"
#include "LCD_Config.h"
#if _LCD_USE_FREERTOS==1
#include "cmsis_os.h"
#endif
//############################################################################################
typedef struct {
uint8_t DisplayControl;
uint8_t DisplayFunction;
uint8_t DisplayMode;
uint8_t currentX;
uint8_t currentY;
} LCD_Options_t;
//############################################################################################
/* Private functions */
static void LCD_Cmd(uint8_t cmd);
static void LCD_Cmd4bit(uint8_t cmd);
static void LCD_Data(uint8_t data);
static void LCD_CursorSet(uint8_t col, uint8_t row);
//############################################################################################
/* Private variable */
static LCD_Options_t LCD_Opts;
//############################################################################################
/* Pin definitions */
#define LCD_RS_LOW HAL_GPIO_WritePin(_LCD_RS_PORT, _LCD_RS_PIN,GPIO_PIN_RESET)
#define LCD_RS_HIGH HAL_GPIO_WritePin(_LCD_RS_PORT, _LCD_RS_PIN,GPIO_PIN_SET)
#define LCD_E_LOW HAL_GPIO_WritePin(_LCD_E_PORT, _LCD_E_PIN,GPIO_PIN_RESET)
#define LCD_E_HIGH HAL_GPIO_WritePin(_LCD_E_PORT, _LCD_E_PIN,GPIO_PIN_SET)
#define LCD_E_BLINK LCD_E_HIGH; LCD_Delay_us(50); LCD_E_LOW; LCD_Delay_us(50)
//############################################################################################
/* Commands*/
#define LCD_CLEARDISPLAY 0x01
#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 */
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
/* Flags for display on/off control */
#define LCD_DISPLAYON 0x04
#define LCD_CURSORON 0x02
#define LCD_BLINKON 0x01
/* Flags for display/cursor shift */
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
/* Flags for function set */
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
//############################################################################################
void LCD_Delay_us(uint16_t us)
{
uint32_t Div = (SysTick->LOAD+1)/1000;
uint32_t StartMicros = HAL_GetTick()*1000 + (1000- SysTick->VAL/Div);
while((HAL_GetTick()*1000 + (1000-SysTick->VAL/Div)-StartMicros < us));
}
//############################################################################################
void LCD_Delay_ms(uint8_t ms)
{
#if _LCD_USE_FREERTOS==1
osDelay(ms);
#else
HAL_Delay(ms);
#endif
}
//############################################################################################
void LCD_Init(void)
{
GPIO_InitTypeDef gpio;
gpio.Mode = GPIO_MODE_OUTPUT_PP;
gpio.Speed = GPIO_SPEED_FREQ_HIGH;
gpio.Pull = GPIO_NOPULL;
gpio.Pin = _LCD_RS_PIN;
HAL_GPIO_Init(_LCD_RS_PORT,&gpio);
gpio.Pin = _LCD_E_PIN;
HAL_GPIO_Init(_LCD_E_PORT,&gpio);
gpio.Pin = _LCD_RW_PIN;
HAL_GPIO_Init(_LCD_RW_PORT,&gpio);
gpio.Pin = _LCD_D4_PIN;
HAL_GPIO_Init(_LCD_D4_PORT,&gpio);
gpio.Pin = _LCD_D5_PIN;
HAL_GPIO_Init(_LCD_D5_PORT,&gpio);
gpio.Pin = _LCD_D6_PIN;
HAL_GPIO_Init(_LCD_D6_PORT,&gpio);
gpio.Pin = _LCD_D7_PIN;
HAL_GPIO_Init(_LCD_D7_PORT,&gpio);
while(HAL_GetTick()<200)
LCD_Delay_ms(1);
/* Set cursor pointer to beginning for LCD */
LCD_Opts.currentX = 0;
LCD_Opts.currentY = 0;
LCD_Opts.DisplayFunction = LCD_4BITMODE | LCD_5x8DOTS | LCD_1LINE;
if (_LCD_ROWS > 1)
LCD_Opts.DisplayFunction |= LCD_2LINE;
/* Try to set 4bit mode */
LCD_Cmd4bit(0x03);
LCD_Delay_ms(5);
/* Second try */
LCD_Cmd4bit(0x03);
LCD_Delay_ms(5);
/* Third goo! */
LCD_Cmd4bit(0x03);
LCD_Delay_ms(5);
/* Set 4-bit interface */
LCD_Cmd4bit(0x02);
LCD_Delay_ms(5);
/* Set # lines, font size, etc. */
LCD_Cmd(LCD_FUNCTIONSET | LCD_Opts.DisplayFunction);
/* Turn the display on with no cursor or blinking default */
LCD_Opts.DisplayControl = LCD_DISPLAYON;
LCD_DisplayOn();
LCD_Clear();
/* Default font directions */
LCD_Opts.DisplayMode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
LCD_Cmd(LCD_ENTRYMODESET | LCD_Opts.DisplayMode);
LCD_Delay_ms(5);
}
//############################################################################################
void LCD_Clear(void)
{
LCD_Cmd(LCD_CLEARDISPLAY);
LCD_Delay_ms(5);
}
//############################################################################################
void LCD_Puts(uint8_t x, uint8_t y, char* str)
{
LCD_CursorSet(x, y);
while (*str)
{
if (LCD_Opts.currentX >= _LCD_COLS)
{
LCD_Opts.currentX = 0;
LCD_Opts.currentY++;
LCD_CursorSet(LCD_Opts.currentX, LCD_Opts.currentY);
}
if (*str == '\n')
{
LCD_Opts.currentY++;
LCD_CursorSet(LCD_Opts.currentX, LCD_Opts.currentY);
}
else if (*str == '\r')
{
LCD_CursorSet(0, LCD_Opts.currentY);
}
else
{
LCD_Data(*str);
LCD_Opts.currentX++;
}
str++;
}
}
//############################################################################################
void LCD_DisplayOn(void)
{
LCD_Opts.DisplayControl |= LCD_DISPLAYON;
LCD_Cmd(LCD_DISPLAYCONTROL | LCD_Opts.DisplayControl);
}
//############################################################################################
void LCD_DisplayOff(void)
{
LCD_Opts.DisplayControl &= ~LCD_DISPLAYON;
LCD_Cmd(LCD_DISPLAYCONTROL | LCD_Opts.DisplayControl);
}
//############################################################################################
void LCD_BlinkOn(void)
{
LCD_Opts.DisplayControl |= LCD_BLINKON;
LCD_Cmd(LCD_DISPLAYCONTROL | LCD_Opts.DisplayControl);
}
//############################################################################################
void LCD_BlinkOff(void)
{
LCD_Opts.DisplayControl &= ~LCD_BLINKON;
LCD_Cmd(LCD_DISPLAYCONTROL | LCD_Opts.DisplayControl);
}
//############################################################################################
void LCD_CursorOn(void)
{
LCD_Opts.DisplayControl |= LCD_CURSORON;
LCD_Cmd(LCD_DISPLAYCONTROL | LCD_Opts.DisplayControl);
}
//############################################################################################
void LCD_CursorOff(void)
{
LCD_Opts.DisplayControl &= ~LCD_CURSORON;
LCD_Cmd(LCD_DISPLAYCONTROL | LCD_Opts.DisplayControl);
}
//############################################################################################
void LCD_ScrollLeft(void)
{
LCD_Cmd(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
}
//############################################################################################
void LCD_ScrollRight(void)
{
LCD_Cmd(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
}
//############################################################################################
void LCD_CreateChar(uint8_t location, uint8_t *data)
{
uint8_t i;
/* We have 8 locations available for custom characters */
location &= 0x07;
LCD_Cmd(LCD_SETCGRAMADDR | (location << 3));
for (i = 0; i < 8; i++) {
LCD_Data(data[i]);
}
}
//############################################################################################
void LCD_PutCustom(uint8_t x, uint8_t y, uint8_t location)
{
LCD_CursorSet(x, y);
LCD_Data(location);
}
//############################################################################################
static void LCD_Cmd(uint8_t cmd)
{
LCD_RS_LOW;
LCD_Cmd4bit(cmd >> 4);
LCD_Cmd4bit(cmd & 0x0F);
}
//############################################################################################
static void LCD_Data(uint8_t data)
{
LCD_RS_HIGH;
LCD_Cmd4bit(data >> 4);
LCD_Cmd4bit(data & 0x0F);
}
//############################################################################################
static void LCD_Cmd4bit(uint8_t cmd)
{
HAL_GPIO_WritePin(_LCD_D7_PORT, _LCD_D7_PIN, (GPIO_PinState)(cmd & 0x08));
HAL_GPIO_WritePin(_LCD_D6_PORT, _LCD_D6_PIN, (GPIO_PinState)(cmd & 0x04));
HAL_GPIO_WritePin(_LCD_D5_PORT, _LCD_D5_PIN, (GPIO_PinState)(cmd & 0x02));
HAL_GPIO_WritePin(_LCD_D4_PORT, _LCD_D4_PIN, (GPIO_PinState)(cmd & 0x01));
LCD_E_BLINK;
}
//############################################################################################
static void LCD_CursorSet(uint8_t col, uint8_t row)
{
uint8_t row_offsets[] = {0x00, 0x40, 0x14, 0x54};
if (row >= _LCD_ROWS)
row = 0;
LCD_Opts.currentX = col;
LCD_Opts.currentY = row;
LCD_Cmd(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}
//############################################################################################
void LCD_Put(uint8_t Data)
{
LCD_Data(Data);
}
//############################################################################################