-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdisplay.cpp
More file actions
280 lines (234 loc) · 10.3 KB
/
display.cpp
File metadata and controls
280 lines (234 loc) · 10.3 KB
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
//=====[Libraries]=============================================================
#include "mbed.h"
#include "arm_book_lib.h"
#include "display.h"
//=====[Declaration of private defines]========================================
#define DISPLAY_IR_CLEAR_DISPLAY 0b00000001
#define DISPLAY_IR_ENTRY_MODE_SET 0b00000100
#define DISPLAY_IR_DISPLAY_CONTROL 0b00001000
#define DISPLAY_IR_FUNCTION_SET 0b00100000
#define DISPLAY_IR_SET_DDRAM_ADDR 0b10000000
#define DISPLAY_IR_ENTRY_MODE_SET_INCREMENT 0b00000010
#define DISPLAY_IR_ENTRY_MODE_SET_DECREMENT 0b00000000
#define DISPLAY_IR_ENTRY_MODE_SET_SHIFT 0b00000001
#define DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT 0b00000000
#define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON 0b00000100
#define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF 0b00000000
#define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON 0b00000010
#define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF 0b00000000
#define DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON 0b00000001
#define DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF 0b00000000
#define DISPLAY_IR_FUNCTION_SET_8BITS 0b00010000
#define DISPLAY_IR_FUNCTION_SET_4BITS 0b00000000
#define DISPLAY_IR_FUNCTION_SET_2LINES 0b00001000
#define DISPLAY_IR_FUNCTION_SET_1LINE 0b00000000
#define DISPLAY_IR_FUNCTION_SET_5x10DOTS 0b00000100
#define DISPLAY_IR_FUNCTION_SET_5x8DOTS 0b00000000
#define DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS 0
#define DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS 64
#define DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS 20
#define DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS 84
#define DISPLAY_RS_INSTRUCTION 0
#define DISPLAY_RS_DATA 1
#define DISPLAY_RW_WRITE 0
#define DISPLAY_RW_READ 1
#define DISPLAY_PIN_RS 4
#define DISPLAY_PIN_RW 5
#define DISPLAY_PIN_EN 6
#define DISPLAY_PIN_D0 7
#define DISPLAY_PIN_D1 8
#define DISPLAY_PIN_D2 9
#define DISPLAY_PIN_D3 10
#define DISPLAY_PIN_D4 11
#define DISPLAY_PIN_D5 12
#define DISPLAY_PIN_D6 13
#define DISPLAY_PIN_D7 14
//=====[Declaration of private data types]=====================================
//=====[Declaration and initialization of public global objects]===============
DigitalOut displayD4( D4 );
DigitalOut displayD5( D5 );
DigitalOut displayD6( D6 );
DigitalOut displayD7( D7 );
DigitalOut displayRs( D8 );
DigitalOut displayEn( D9 );
//=====[Declaration of external public global variables]=======================
//=====[Declaration and initialization of public global variables]=============
//=====[Declaration and initialization of private global variables]============
static display_t display;
static bool initial8BitCommunicationIsCompleted;
//=====[Declarations (prototypes) of private functions]========================
static void displayPinWrite( uint8_t pinName, int value );
static void displayDataBusWrite( uint8_t dataByte );
static void displayCodeWrite( bool type, uint8_t dataBus );
//=====[Implementations of public functions]===================================
void displayInit( displayConnection_t connection )
{
display.connection = connection;
initial8BitCommunicationIsCompleted = false;
delay( 50 );
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_FUNCTION_SET |
DISPLAY_IR_FUNCTION_SET_8BITS );
delay( 5 );
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_FUNCTION_SET |
DISPLAY_IR_FUNCTION_SET_8BITS );
delay( 1 );
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_FUNCTION_SET |
DISPLAY_IR_FUNCTION_SET_8BITS );
delay( 1 );
switch( display.connection ) {
case DISPLAY_CONNECTION_GPIO_8BITS:
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_FUNCTION_SET |
DISPLAY_IR_FUNCTION_SET_8BITS |
DISPLAY_IR_FUNCTION_SET_2LINES |
DISPLAY_IR_FUNCTION_SET_5x8DOTS );
delay( 1 );
break;
case DISPLAY_CONNECTION_GPIO_4BITS:
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_FUNCTION_SET |
DISPLAY_IR_FUNCTION_SET_4BITS );
delay( 1 );
initial8BitCommunicationIsCompleted = true;
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_FUNCTION_SET |
DISPLAY_IR_FUNCTION_SET_4BITS |
DISPLAY_IR_FUNCTION_SET_2LINES |
DISPLAY_IR_FUNCTION_SET_5x8DOTS );
delay( 1 );
break;
}
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_DISPLAY_CONTROL |
DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF |
DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF |
DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF );
delay( 1 );
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_CLEAR_DISPLAY );
delay( 1 );
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_ENTRY_MODE_SET |
DISPLAY_IR_ENTRY_MODE_SET_INCREMENT |
DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT );
delay( 1 );
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_DISPLAY_CONTROL |
DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON |
DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF |
DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF );
delay( 1 );
}
void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY )
{
switch( charPositionY ) {
case 0:
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_SET_DDRAM_ADDR |
( DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS +
charPositionX ) );
delay( 1 );
break;
case 1:
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_SET_DDRAM_ADDR |
( DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS +
charPositionX ) );
delay( 1 );
break;
case 2:
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_SET_DDRAM_ADDR |
( DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS +
charPositionX ) );
delay( 1 );
break;
case 3:
displayCodeWrite( DISPLAY_RS_INSTRUCTION,
DISPLAY_IR_SET_DDRAM_ADDR |
( DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS +
charPositionX ) );
delay( 1 );
break;
}
}
void displayStringWrite( const char * str )
{
while (*str) {
displayCodeWrite(DISPLAY_RS_DATA, *str++);
}
}
//=====[Implementations of private functions]==================================
static void displayCodeWrite( bool type, uint8_t dataBus )
{
if ( type == DISPLAY_RS_INSTRUCTION )
displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_INSTRUCTION);
else
displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_DATA);
displayPinWrite( DISPLAY_PIN_RW, DISPLAY_RW_WRITE );
displayDataBusWrite( dataBus );
}
static void displayPinWrite( uint8_t pinName, int value )
{
switch( display.connection ) {
case DISPLAY_CONNECTION_GPIO_8BITS:
switch( pinName ) {
case DISPLAY_PIN_D4: displayD4 = value; break;
case DISPLAY_PIN_D5: displayD5 = value; break;
case DISPLAY_PIN_D6: displayD6 = value; break;
case DISPLAY_PIN_D7: displayD7 = value; break;
case DISPLAY_PIN_RS: displayRs = value; break;
case DISPLAY_PIN_EN: displayEn = value; break;
case DISPLAY_PIN_RW: break;
default: break;
}
break;
case DISPLAY_CONNECTION_GPIO_4BITS:
switch( pinName ) {
case DISPLAY_PIN_D4: displayD4 = value; break;
case DISPLAY_PIN_D5: displayD5 = value; break;
case DISPLAY_PIN_D6: displayD6 = value; break;
case DISPLAY_PIN_D7: displayD7 = value; break;
case DISPLAY_PIN_RS: displayRs = value; break;
case DISPLAY_PIN_EN: displayEn = value; break;
case DISPLAY_PIN_RW: break;
default: break;
}
break;
}
}
static void displayDataBusWrite( uint8_t dataBus )
{
displayPinWrite( DISPLAY_PIN_EN, OFF );
displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b10000000 );
displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b01000000 );
displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00100000 );
displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00010000 );
switch( display.connection ) {
case DISPLAY_CONNECTION_GPIO_8BITS:
displayPinWrite( DISPLAY_PIN_D3, dataBus & 0b00001000 );
displayPinWrite( DISPLAY_PIN_D2, dataBus & 0b00000100 );
displayPinWrite( DISPLAY_PIN_D1, dataBus & 0b00000010 );
displayPinWrite( DISPLAY_PIN_D0, dataBus & 0b00000001 );
break;
case DISPLAY_CONNECTION_GPIO_4BITS:
if ( initial8BitCommunicationIsCompleted == true) {
displayPinWrite( DISPLAY_PIN_EN, ON );
delay( 1 );
displayPinWrite( DISPLAY_PIN_EN, OFF );
delay( 1 );
displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b00001000 );
displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b00000100 );
displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00000010 );
displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00000001 );
}
break;
}
displayPinWrite( DISPLAY_PIN_EN, ON );
delay( 1 );
displayPinWrite( DISPLAY_PIN_EN, OFF );
delay( 1 );
}