User | Revision | Line number | New contents of line |
faker |
0:8e3468376286
|
1
|
/* draft mbed TextLCD
|
faker |
0:8e3468376286
|
2
|
* (c) 2007/8, sford
|
faker |
0:8e3468376286
|
3
|
*/
|
faker |
0:8e3468376286
|
4
|
|
faker |
0:8e3468376286
|
5
|
#include "TextLCD.h"
|
faker |
0:8e3468376286
|
6
|
|
faker |
0:8e3468376286
|
7
|
#include "mbed.h"
|
faker |
0:8e3468376286
|
8
|
|
faker |
0:8e3468376286
|
9
|
using namespace mbed;
|
faker |
0:8e3468376286
|
10
|
|
faker |
0:8e3468376286
|
11
|
/*
|
faker |
0:8e3468376286
|
12
|
* useful info found at http://www.a-netz.de/lcd.en.php
|
faker |
0:8e3468376286
|
13
|
*
|
faker |
0:8e3468376286
|
14
|
*
|
faker |
0:8e3468376286
|
15
|
* Initialisation
|
faker |
0:8e3468376286
|
16
|
* ==============
|
faker |
0:8e3468376286
|
17
|
*
|
faker |
0:8e3468376286
|
18
|
* After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
|
faker |
0:8e3468376286
|
19
|
*
|
faker |
0:8e3468376286
|
20
|
* - wait approximately 15 ms so the display is ready to execute commands
|
faker |
0:8e3468376286
|
21
|
* - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
|
faker |
0:8e3468376286
|
22
|
* - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command.
|
faker |
0:8e3468376286
|
23
|
* - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
|
faker |
0:8e3468376286
|
24
|
* - Execute the "clear display" command
|
faker |
0:8e3468376286
|
25
|
*
|
faker |
0:8e3468376286
|
26
|
* Timing
|
faker |
0:8e3468376286
|
27
|
* ======
|
faker |
0:8e3468376286
|
28
|
*
|
faker |
0:8e3468376286
|
29
|
* Nearly all commands transmitted to the display need 40us for execution.
|
faker |
0:8e3468376286
|
30
|
* Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
|
faker |
0:8e3468376286
|
31
|
* These commands need 1.64ms for execution. These timings are valid for all displays working with an
|
faker |
0:8e3468376286
|
32
|
* internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
|
faker |
0:8e3468376286
|
33
|
* can use the busy flag to test if the display is ready to accept the next command.
|
faker |
0:8e3468376286
|
34
|
*
|
faker |
0:8e3468376286
|
35
|
* _e is kept high apart from calling clock
|
faker |
0:8e3468376286
|
36
|
* _rw is kept 0 (write) apart from actions that uyse it differently
|
faker |
0:8e3468376286
|
37
|
* _rs is set by the data/command writes
|
faker |
0:8e3468376286
|
38
|
*/
|
faker |
0:8e3468376286
|
39
|
|
faker |
0:8e3468376286
|
40
|
TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
|
faker |
0:8e3468376286
|
41
|
PinName d2, PinName d3) : _rw(rw), _rs(rs),
|
faker |
0:8e3468376286
|
42
|
_e(e), _d(d0, d1, d2, d3){
|
faker |
0:8e3468376286
|
43
|
|
faker |
0:8e3468376286
|
44
|
_rows = 4;
|
faker |
0:8e3468376286
|
45
|
_columns = 20;
|
faker |
0:8e3468376286
|
46
|
|
faker |
0:8e3468376286
|
47
|
_rw = 0;
|
faker |
0:8e3468376286
|
48
|
_e = 1;
|
faker |
0:8e3468376286
|
49
|
_rs = 0; // command mode
|
faker |
0:8e3468376286
|
50
|
|
faker |
0:8e3468376286
|
51
|
// Should theoretically wait 15ms, but most things will be powered up pre-reset
|
faker |
0:8e3468376286
|
52
|
// so i'll disable that for the minute. If implemented, could wait 15ms post reset
|
faker |
0:8e3468376286
|
53
|
// instead
|
faker |
0:8e3468376286
|
54
|
// wait(0.015);
|
faker |
0:8e3468376286
|
55
|
|
faker |
0:8e3468376286
|
56
|
// send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
|
faker |
0:8e3468376286
|
57
|
for(int i=0; i<3; i++) {
|
faker |
0:8e3468376286
|
58
|
writeNibble(0x3);
|
faker |
0:8e3468376286
|
59
|
wait(0.00164); // this command takes 1.64ms, so wait for it
|
faker |
0:8e3468376286
|
60
|
}
|
faker |
0:8e3468376286
|
61
|
writeNibble(0x2); // 4-bit mode
|
faker |
0:8e3468376286
|
62
|
|
faker |
0:8e3468376286
|
63
|
writeCommand(0x28); // Function set 001 BW N F - -
|
faker |
0:8e3468376286
|
64
|
writeCommand(0x0C);
|
faker |
0:8e3468376286
|
65
|
writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
|
faker |
0:8e3468376286
|
66
|
|
faker |
0:8e3468376286
|
67
|
cls();
|
faker |
0:8e3468376286
|
68
|
}
|
faker |
0:8e3468376286
|
69
|
|
faker |
0:8e3468376286
|
70
|
int TextLCD::_putc(int value) {
|
faker |
0:8e3468376286
|
71
|
if(value == '\n') {
|
faker |
0:8e3468376286
|
72
|
newline();
|
faker |
0:8e3468376286
|
73
|
} else {
|
faker |
0:8e3468376286
|
74
|
writeData(value);
|
faker |
0:8e3468376286
|
75
|
}
|
faker |
0:8e3468376286
|
76
|
return value;
|
faker |
0:8e3468376286
|
77
|
}
|
faker |
0:8e3468376286
|
78
|
|
faker |
0:8e3468376286
|
79
|
int TextLCD::_getc() {
|
faker |
0:8e3468376286
|
80
|
return 0;
|
faker |
0:8e3468376286
|
81
|
}
|
faker |
0:8e3468376286
|
82
|
|
faker |
0:8e3468376286
|
83
|
void TextLCD::newline() {
|
faker |
0:8e3468376286
|
84
|
_column = 0;
|
faker |
0:8e3468376286
|
85
|
_row++;
|
faker |
0:8e3468376286
|
86
|
if(_row >= _rows) {
|
faker |
0:8e3468376286
|
87
|
_row = 0;
|
faker |
0:8e3468376286
|
88
|
}
|
faker |
0:8e3468376286
|
89
|
locate(_column, _row);
|
faker |
0:8e3468376286
|
90
|
}
|
faker |
0:8e3468376286
|
91
|
|
faker |
0:8e3468376286
|
92
|
void TextLCD::locate(int column, int row) {
|
faker |
0:8e3468376286
|
93
|
if(column < 0 || column >= _columns || row < 0 || row >= _rows) {
|
faker |
0:8e3468376286
|
94
|
error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
|
faker |
0:8e3468376286
|
95
|
return;
|
faker |
0:8e3468376286
|
96
|
}
|
faker |
0:8e3468376286
|
97
|
|
faker |
0:8e3468376286
|
98
|
_row = row;
|
faker |
0:8e3468376286
|
99
|
_column = column;
|
faker |
0:8e3468376286
|
100
|
int address=0;
|
faker |
0:8e3468376286
|
101
|
// row 0 : 0x0->0x13
|
faker |
0:8e3468376286
|
102
|
// row 1 : 0x40->0x53
|
faker |
0:8e3468376286
|
103
|
// row 2 : 0x14->0x27
|
faker |
0:8e3468376286
|
104
|
// row 3 : 0x54->0x67
|
faker |
0:8e3468376286
|
105
|
|
faker |
0:8e3468376286
|
106
|
switch (_row) {
|
faker |
0:8e3468376286
|
107
|
case (0) : address = 0x00 + _column;
|
faker |
0:8e3468376286
|
108
|
break;
|
faker |
0:8e3468376286
|
109
|
case (1) : address = 0x40 + _column;
|
faker |
0:8e3468376286
|
110
|
break;
|
faker |
0:8e3468376286
|
111
|
case (2) : address = 0x14 + _column;
|
faker |
0:8e3468376286
|
112
|
break;
|
faker |
0:8e3468376286
|
113
|
case (3) : address = 0x54 + _column;
|
faker |
0:8e3468376286
|
114
|
break;
|
faker |
0:8e3468376286
|
115
|
}
|
faker |
0:8e3468376286
|
116
|
|
faker |
0:8e3468376286
|
117
|
address += 0x80;
|
faker |
0:8e3468376286
|
118
|
|
faker |
0:8e3468376286
|
119
|
// }
|
faker |
0:8e3468376286
|
120
|
// else {
|
faker |
0:8e3468376286
|
121
|
// memory starts at 0x80, and is 40 chars long per row
|
faker |
0:8e3468376286
|
122
|
// address = 0x80 + (_row * 40) + _column;
|
faker |
0:8e3468376286
|
123
|
// }
|
faker |
0:8e3468376286
|
124
|
|
faker |
0:8e3468376286
|
125
|
writeCommand(address);
|
faker |
0:8e3468376286
|
126
|
}
|
faker |
0:8e3468376286
|
127
|
|
faker |
0:8e3468376286
|
128
|
|
faker |
0:8e3468376286
|
129
|
void TextLCD::rows(int rows) {
|
faker |
0:8e3468376286
|
130
|
_rows = rows;
|
faker |
0:8e3468376286
|
131
|
}
|
faker |
0:8e3468376286
|
132
|
|
faker |
0:8e3468376286
|
133
|
|
faker |
0:8e3468376286
|
134
|
void TextLCD::columns(int columns) {
|
faker |
0:8e3468376286
|
135
|
_columns = columns;
|
faker |
0:8e3468376286
|
136
|
}
|
faker |
0:8e3468376286
|
137
|
|
faker |
0:8e3468376286
|
138
|
|
faker |
0:8e3468376286
|
139
|
|
faker |
0:8e3468376286
|
140
|
|
faker |
0:8e3468376286
|
141
|
void TextLCD::cls() {
|
faker |
0:8e3468376286
|
142
|
writeCommand(0x01); // Clear Display
|
faker |
0:8e3468376286
|
143
|
wait(0.00164f); // This command takes 1.64 ms
|
faker |
0:8e3468376286
|
144
|
locate(0, 0);
|
faker |
0:8e3468376286
|
145
|
}
|
faker |
0:8e3468376286
|
146
|
|
faker |
0:8e3468376286
|
147
|
void TextLCD::reset() {
|
faker |
0:8e3468376286
|
148
|
cls();
|
faker |
0:8e3468376286
|
149
|
}
|
faker |
0:8e3468376286
|
150
|
|
faker |
0:8e3468376286
|
151
|
void TextLCD::clock() {
|
faker |
0:8e3468376286
|
152
|
wait(0.000040f);
|
faker |
0:8e3468376286
|
153
|
_e = 0;
|
faker |
0:8e3468376286
|
154
|
wait(0.000040f); // most instructions take 40us
|
faker |
0:8e3468376286
|
155
|
_e = 1;
|
faker |
0:8e3468376286
|
156
|
}
|
faker |
0:8e3468376286
|
157
|
|
faker |
0:8e3468376286
|
158
|
void TextLCD::writeNibble(int value) {
|
faker |
0:8e3468376286
|
159
|
_d = value;
|
faker |
0:8e3468376286
|
160
|
clock();
|
faker |
0:8e3468376286
|
161
|
}
|
faker |
0:8e3468376286
|
162
|
|
faker |
0:8e3468376286
|
163
|
void TextLCD::writeByte(int value) {
|
faker |
0:8e3468376286
|
164
|
writeNibble(value >> 4);
|
faker |
0:8e3468376286
|
165
|
writeNibble(value >> 0);
|
faker |
0:8e3468376286
|
166
|
}
|
faker |
0:8e3468376286
|
167
|
|
faker |
0:8e3468376286
|
168
|
void TextLCD::writeCommand(int command) {
|
faker |
0:8e3468376286
|
169
|
_rs = 0;
|
faker |
0:8e3468376286
|
170
|
writeByte(command);
|
faker |
0:8e3468376286
|
171
|
}
|
faker |
0:8e3468376286
|
172
|
|
faker |
0:8e3468376286
|
173
|
void TextLCD::writeData(int data) {
|
faker |
0:8e3468376286
|
174
|
_rs = 1;
|
faker |
0:8e3468376286
|
175
|
writeByte(data);
|
faker |
0:8e3468376286
|
176
|
_column++;
|
faker |
0:8e3468376286
|
177
|
if(_column >= _columns) {
|
faker |
0:8e3468376286
|
178
|
newline();
|
faker |
0:8e3468376286
|
179
|
}
|
faker |
0:8e3468376286
|
180
|
}
|