-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm_stm32f4_hd44780.c
executable file
·285 lines (236 loc) · 9.05 KB
/
tm_stm32f4_hd44780.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
/**
* |----------------------------------------------------------------------
* | Copyright (C) Tilen Majerle, 2014
* |
* | This program is free software: you can redistribute it and/or modify
* | it under the terms of the GNU General Public License as published by
* | the Free Software Foundation, either version 3 of the License, or
* | any later version.
* |
* | This program is distributed in the hope that it will be useful,
* | but WITHOUT ANY WARRANTY; without even the implied warranty of
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* | GNU General Public License for more details.
* |
* | You should have received a copy of the GNU General Public License
* | along with this program. If not, see <http://www.gnu.org/licenses/>.
* |----------------------------------------------------------------------
*/
#include "tm_stm32f4_hd44780.h"
/* Private HD44780 structure */
typedef struct {
uint8_t DisplayControl;
uint8_t DisplayFunction;
uint8_t DisplayMode;
uint8_t Rows;
uint8_t Cols;
uint8_t currentX;
uint8_t currentY;
} HD44780_Options_t;
/* Private functions */
static void TM_HD44780_InitPins(void);
static void TM_HD44780_Cmd(uint8_t cmd);
static void TM_HD44780_Cmd4bit(uint8_t cmd);
static void TM_HD44780_Data(uint8_t data);
static void TM_HD44780_CursorSet(uint8_t col, uint8_t row);
/* Private variable */
static HD44780_Options_t HD44780_Opts;
/* Pin definitions */
#define HD44780_RS_LOW TM_GPIO_SetPinLow(HD44780_RS_PORT, HD44780_RS_PIN)
#define HD44780_RS_HIGH TM_GPIO_SetPinHigh(HD44780_RS_PORT, HD44780_RS_PIN)
#define HD44780_E_LOW TM_GPIO_SetPinLow(HD44780_E_PORT, HD44780_E_PIN)
#define HD44780_E_HIGH TM_GPIO_SetPinHigh(HD44780_E_PORT, HD44780_E_PIN)
#define HD44780_E_BLINK HD44780_E_HIGH; HD44780_Delay(20); HD44780_E_LOW; HD44780_Delay(20)
#define HD44780_Delay(x) Delay(x)
/* Commands*/
#define HD44780_CLEARDISPLAY 0x01
#define HD44780_RETURNHOME 0x02
#define HD44780_ENTRYMODESET 0x04
#define HD44780_DISPLAYCONTROL 0x08
#define HD44780_CURSORSHIFT 0x10
#define HD44780_FUNCTIONSET 0x20
#define HD44780_SETCGRAMADDR 0x40
#define HD44780_SETDDRAMADDR 0x80
/* Flags for display entry mode */
#define HD44780_ENTRYRIGHT 0x00
#define HD44780_ENTRYLEFT 0x02
#define HD44780_ENTRYSHIFTINCREMENT 0x01
#define HD44780_ENTRYSHIFTDECREMENT 0x00
/* Flags for display on/off control */
#define HD44780_DISPLAYON 0x04
#define HD44780_CURSORON 0x02
#define HD44780_BLINKON 0x01
/* Flags for display/cursor shift */
#define HD44780_DISPLAYMOVE 0x08
#define HD44780_CURSORMOVE 0x00
#define HD44780_MOVERIGHT 0x04
#define HD44780_MOVELEFT 0x00
/* Flags for function set */
#define HD44780_8BITMODE 0x10
#define HD44780_4BITMODE 0x00
#define HD44780_2LINE 0x08
#define HD44780_1LINE 0x00
#define HD44780_5x10DOTS 0x04
#define HD44780_5x8DOTS 0x00
void TM_HD44780_Init(uint8_t cols, uint8_t rows) {
/* Initialize delay */
TM_DELAY_Init();
/* Init pinout */
TM_HD44780_InitPins();
/* At least 40ms */
HD44780_Delay(45000);
/* Set LCD width and height */
HD44780_Opts.Rows = rows;
HD44780_Opts.Cols = cols;
/* Set cursor pointer to beginning for LCD */
HD44780_Opts.currentX = 0;
HD44780_Opts.currentY = 0;
HD44780_Opts.DisplayFunction = HD44780_4BITMODE | HD44780_5x8DOTS | HD44780_1LINE;
if (rows > 1) {
HD44780_Opts.DisplayFunction |= HD44780_2LINE;
}
/* Try to set 4bit mode */
TM_HD44780_Cmd4bit(0x03);
HD44780_Delay(4500);
/* Second try */
TM_HD44780_Cmd4bit(0x03);
HD44780_Delay(4500);
/* Third goo! */
TM_HD44780_Cmd4bit(0x03);
HD44780_Delay(4500);
/* Set 4-bit interface */
TM_HD44780_Cmd4bit(0x02);
HD44780_Delay(100);
/* Set # lines, font size, etc. */
TM_HD44780_Cmd(HD44780_FUNCTIONSET | HD44780_Opts.DisplayFunction);
/* Turn the display on with no cursor or blinking default */
HD44780_Opts.DisplayControl = HD44780_DISPLAYON;
TM_HD44780_DisplayOn();
/* Clear lcd */
TM_HD44780_Clear();
/* Default font directions */
HD44780_Opts.DisplayMode = HD44780_ENTRYLEFT | HD44780_ENTRYSHIFTDECREMENT;
TM_HD44780_Cmd(HD44780_ENTRYMODESET | HD44780_Opts.DisplayMode);
/* Delay */
HD44780_Delay(4500);
}
void TM_HD44780_Clear(void) {
TM_HD44780_Cmd(HD44780_CLEARDISPLAY);
HD44780_Delay(3000);
}
void TM_HD44780_Puts(uint8_t x, uint8_t y, char* str) {
TM_HD44780_CursorSet(x, y);
while (*str) {
if (HD44780_Opts.currentX >= HD44780_Opts.Cols) {
HD44780_Opts.currentX = 0;
HD44780_Opts.currentY++;
TM_HD44780_CursorSet(HD44780_Opts.currentX, HD44780_Opts.currentY);
}
if (*str == '\n') {
HD44780_Opts.currentY++;
TM_HD44780_CursorSet(HD44780_Opts.currentX, HD44780_Opts.currentY);
} else if (*str == '\r') {
TM_HD44780_CursorSet(0, HD44780_Opts.currentY);
} else {
TM_HD44780_Data(*str);
HD44780_Opts.currentX++;
}
str++;
}
}
void TM_HD44780_DisplayOn(void) {
HD44780_Opts.DisplayControl |= HD44780_DISPLAYON;
TM_HD44780_Cmd(HD44780_DISPLAYCONTROL | HD44780_Opts.DisplayControl);
}
void TM_HD44780_DisplayOff(void) {
HD44780_Opts.DisplayControl &= ~HD44780_DISPLAYON;
TM_HD44780_Cmd(HD44780_DISPLAYCONTROL | HD44780_Opts.DisplayControl);
}
void TM_HD44780_BlinkOn(void) {
HD44780_Opts.DisplayControl |= HD44780_BLINKON;
TM_HD44780_Cmd(HD44780_DISPLAYCONTROL | HD44780_Opts.DisplayControl);
}
void TM_HD44780_BlinkOff(void) {
HD44780_Opts.DisplayControl &= ~HD44780_BLINKON;
TM_HD44780_Cmd(HD44780_DISPLAYCONTROL | HD44780_Opts.DisplayControl);
}
void TM_HD44780_CursorOn(void) {
HD44780_Opts.DisplayControl |= HD44780_CURSORON;
TM_HD44780_Cmd(HD44780_DISPLAYCONTROL | HD44780_Opts.DisplayControl);
}
void TM_HD44780_CursorOff(void) {
HD44780_Opts.DisplayControl &= ~HD44780_CURSORON;
TM_HD44780_Cmd(HD44780_DISPLAYCONTROL | HD44780_Opts.DisplayControl);
}
void TM_HD44780_ScrollLeft(void) {
TM_HD44780_Cmd(HD44780_CURSORSHIFT | HD44780_DISPLAYMOVE | HD44780_MOVELEFT);
}
void TM_HD44780_ScrollRight(void) {
TM_HD44780_Cmd(HD44780_CURSORSHIFT | HD44780_DISPLAYMOVE | HD44780_MOVERIGHT);
}
void TM_HD44780_CreateChar(uint8_t location, uint8_t *data) {
uint8_t i;
/* We have 8 locations available for custom characters */
location &= 0x07;
TM_HD44780_Cmd(HD44780_SETCGRAMADDR | (location << 3));
for (i = 0; i < 8; i++) {
TM_HD44780_Data(data[i]);
}
}
void TM_HD44780_PutCustom(uint8_t x, uint8_t y, uint8_t location) {
TM_HD44780_CursorSet(x, y);
TM_HD44780_Data(location);
}
/* Private functions */
static void TM_HD44780_Cmd(uint8_t cmd) {
/* Command mode */
HD44780_RS_LOW;
/* High nibble */
TM_HD44780_Cmd4bit(cmd >> 4);
/* Low nibble */
TM_HD44780_Cmd4bit(cmd & 0x0F);
}
static void TM_HD44780_Data(uint8_t data) {
/* Data mode */
HD44780_RS_HIGH;
/* High nibble */
TM_HD44780_Cmd4bit(data >> 4);
/* Low nibble */
TM_HD44780_Cmd4bit(data & 0x0F);
}
static void TM_HD44780_Cmd4bit(uint8_t cmd) {
/* Set output port */
TM_GPIO_SetPinValue(HD44780_D7_PORT, HD44780_D7_PIN, (cmd & 0x08));
TM_GPIO_SetPinValue(HD44780_D6_PORT, HD44780_D6_PIN, (cmd & 0x04));
TM_GPIO_SetPinValue(HD44780_D5_PORT, HD44780_D5_PIN, (cmd & 0x02));
TM_GPIO_SetPinValue(HD44780_D4_PORT, HD44780_D4_PIN, (cmd & 0x01));
HD44780_E_BLINK;
}
static void TM_HD44780_CursorSet(uint8_t col, uint8_t row) {
uint8_t row_offsets[] = {0x00, 0x40, 0x14, 0x54};
/* Go to beginning */
if (row >= HD44780_Opts.Rows) {
row = 0;
}
/* Set current column and row */
HD44780_Opts.currentX = col;
HD44780_Opts.currentY = row;
/* Set location address */
TM_HD44780_Cmd(HD44780_SETDDRAMADDR | (col + row_offsets[row]));
}
static void TM_HD44780_InitPins(void) {
/* Init all pins */
TM_GPIO_Init(HD44780_RS_PORT, HD44780_RS_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Low);
TM_GPIO_Init(HD44780_E_PORT, HD44780_E_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Low);
TM_GPIO_Init(HD44780_D4_PORT, HD44780_D4_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Low);
TM_GPIO_Init(HD44780_D5_PORT, HD44780_D5_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Low);
TM_GPIO_Init(HD44780_D6_PORT, HD44780_D6_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Low);
TM_GPIO_Init(HD44780_D7_PORT, HD44780_D7_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Low);
/* Set pins low */
TM_GPIO_SetPinLow(HD44780_RS_PORT, HD44780_RS_PIN);
TM_GPIO_SetPinLow(HD44780_E_PORT, HD44780_E_PIN);
TM_GPIO_SetPinLow(HD44780_D4_PORT, HD44780_D4_PIN);
TM_GPIO_SetPinLow(HD44780_D5_PORT, HD44780_D5_PIN);
TM_GPIO_SetPinLow(HD44780_D6_PORT, HD44780_D6_PIN);
TM_GPIO_SetPinLow(HD44780_D7_PORT, HD44780_D7_PIN);
}