This repository has been archived by the owner on Oct 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptrexLCD.cpp
371 lines (322 loc) · 10.2 KB
/
OptrexLCD.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <OptrexLCD.h>
#include <MsTimer2.h>
/*
* Written on September 22, 2008 by Warren W. Gay VE3WWG.
* ve3wwg@gmail.com
*
* Software License: Public Domain
*/
namespace OptrexLCD {
/*
* Pin Assignments : Alter these prior to calling OptrexLCD::initialize()
*/
static byte LCD_M = 2; // LCD clock (30-90 Hz) pin #
static byte LCD_Shift = 3; // Shift clock pin #
static byte LCD_Latch = 7; // Data latch pin #
static byte LCD_Din = 10; // LCD Data in pin #
static byte LCD_Shift_ms = 3; // Time in milliseconds for each shift clock change
static volatile byte LCD_bits = 40; // Bits to shift out
static volatile byte bitbuf[5]; // LCD bit buffer (40 bits of segment select)
static volatile byte M_State = 0; // M state
static volatile byte Shift_State = 0; // Shift line state
static volatile byte Latch_State = 0; // Current latch state
static volatile void (*latchcb)(void) = 0; // Optional user callback after each latch
/*
* Mapping table for ASCII to 7-segment display, starting
* with '0' :
*/
static byte segmap[] = {
0x7E, // '0'
0x0C, // '1'
0xB6, // '2'
0x9E, // '3'
0xCC, // '4'
0xDA, // '5'
0xFA, // '6'
0x0E, // '7'
0xFE, // '8'
0xDE, // '9'
0xEE, // 'A'
0xF8, // 'b'
0x72, // 'C'
0xBC, // 'D'
0xF2, // 'E'
0xE2, // 'F'
0x80, // '-' OPTREX_SEG_MINUS
0x10, // '_' (underscore) OPTREX_SEG_UNDERSCORE
0x02, // Clear (digit 0 only) OPTREX_SEG_CLEAR
0x04 // Secure(digit 0 only) OPTREX_SEG_SECURE
};
# define OPTREX_SEG_MINUS 16
# define OPTREX_SEG_UNDERSCORE 17
# define OPTREX_SEG_CLEAR 18
# define OPTREX_SEG_SECURE 19
static byte data[] = { 0x02, 0x00, 0x00, 0x00, 0x00 };
static void timer_routine(void);
static void show_overflow(void);
static byte ascii2seg(byte ascii_ch);
/*
* Establish which ATmega168 I/O pin is used for the
* M clock. Default = 2.
*/
void
set_M_pin(byte pin_M) {
LCD_M = pin_M;
}
/*
* Establish which ATmega168 I/O pin is used for the data
* shift clock (M freq X 2). AKA CLK2. Default = 3.
*/
void
set_Shift_pin(byte pin_Shift) {
LCD_Shift = pin_Shift;
}
/*
* Establish which ATmega168 I/O pin is used for the
* latch signal. Default = 7.
*/
void
set_Latch_pin(byte pin_Latch) {
LCD_Latch = pin_Latch;
}
/*
* Establish which ATmega168 I/O pin is used for the
* data in (Din) line. Default = 10.
*/
void
set_Din_pin(byte pin_Din) {
LCD_Din = pin_Din;
}
/*
* Alter the timer value in milliseconds.
* Default = 3 msec.
*/
void
set_timer_ms(byte msec) {
if ( msec > 0 )
LCD_Shift_ms = msec;
}
/*
* This routine initializes the timer and starts the
* LCD refresh routine. Make sure that you assign the
* correct pin numbers first, or accept the defaults.
*/
void
initialize(void) {
pinMode(OptrexLCD::LCD_M,OUTPUT);
pinMode(OptrexLCD::LCD_Shift,OUTPUT);
pinMode(OptrexLCD::LCD_Latch,OUTPUT);
pinMode(OptrexLCD::LCD_Din,OUTPUT);
digitalWrite(OptrexLCD::LCD_M,LOW);
digitalWrite(OptrexLCD::LCD_Shift,LOW);
digitalWrite(OptrexLCD::LCD_Latch,LOW);
digitalWrite(OptrexLCD::LCD_Din,LOW);
MsTimer2::set(OptrexLCD::LCD_Shift_ms,timer_routine);
MsTimer2::start();
}
/*
* Install an optional user callback. When established,
* the callback function usercb() is invoked after
* every latching of information in the display.
*
* For example, this can be used to increment a
* user counter.
*/
void
set_latch_callback(volatile void (*usercb)(void)) {
latchcb = usercb;
}
/*
* Clear the entire display (make it blank):
*/
void
clear(void) {
cli();
for ( byte x = 0; x < 5; ++x )
data[x] = bitbuf[x] = 0;
LCD_bits = 40;
sei();
}
/*
* Display all minus signs to represent a numeric
* overflow (value too large to fit in four digit
* display).
*/
void
show_overflow(void) {
write("----");
}
/*
* Write up to four text characters to the display.
* Note that hexadecimal, '-', '_', and blank are
* the only characters supported.
*/
void
write(char text[4]) {
cli();
for ( byte x = 0; x<4; ++x )
data[1+3-x] = ascii2seg(text[x]);
sei();
}
/*
* Write a four digit unsigned value into the
* LCD display (with leading zeros). The range
* of values supported are 0000 to 9999.
*/
void
write_uint(unsigned v) {
byte r;
char buf[4];
if ( v > 9999 ) {
show_overflow();
return;
}
for ( byte x=4; x-- > 0; ) {
r = v % 10;
v /= 10;
buf[x] = '0' + r;
}
write(buf);
}
/*
* Write a four digit signed value into the
* LCD display. Range of values supported are
* -999 to 9999.
*/
void
write_int(int v) {
byte r;
byte c = 0;
char buf[4];
if ( v < -999 || v > 9999 ) {
show_overflow();
return;
}
if ( v < 0 ) {
buf[0] = '-';
c = 1;
v = -v;
}
for ( byte x=4; x-- > c; ) {
r = v % 10;
v /= 10;
buf[x] = '0' + r;
}
write(buf);
}
/*
* Internal routine to toggle bits in data[0];
*/
static void
set_by_mask(boolean b,byte selector) {
byte mask = segmap[selector];
cli();
if ( b != false )
data[0] |= mask;
else
data[0] &= ~mask;
sei();
}
/*
* Set or reset the "Secure" word display :
*/
void
set_secure(boolean b) {
set_by_mask(b,OPTREX_SEG_SECURE);
}
/*
* Set or reset the "Clear" word display :
*/
void
set_clear(boolean b) {
set_by_mask(b,OPTREX_SEG_CLEAR);
}
/*
* Convert one ASCII character into 7-segment display :
*/
byte
ascii2seg(byte ascii_ch) {
byte selector = 0;
if ( ascii_ch >= '0' && ascii_ch <= '9' )
return segmap[ascii_ch - '0'];
if ( ascii_ch >= 'A' && ascii_ch <= 'F' )
return segmap[ascii_ch - 'A' + 10];
if ( ascii_ch >= 'a' && ascii_ch <= 'f' )
return segmap[ascii_ch - 'a' + 10];
switch ( ascii_ch ) {
case ' ' :
return 0x00;
case '-' :
selector = OPTREX_SEG_MINUS;
break;
case '_' :
selector = OPTREX_SEG_UNDERSCORE;
break;
default :
selector = OPTREX_SEG_UNDERSCORE;
}
return segmap[selector];
}
/*
* LCD Timer service routine :
*
* Here we must :
* 1. Change the state of the data shift clock
* 2. Change the M clock (at half the rate)
* 3. Shift out one data bit
* 4. Activate the latch when the last bit is clocked out
* 5. Re-start the next refresh, after the latch has been clocked
*/
void
timer_routine() {
byte x; // General index
byte b; // Current data bit to LCD display
byte t; // Carry bit in shift operation
boolean cb_flag = false; // Callback flag
Shift_State ^= 1; // Toggle shift pin status
digitalWrite(LCD_Shift,Shift_State); // Send to shift pin
if ( Shift_State != 0 ) { // On shift rise, toggle M state
M_State ^= 1;
digitalWrite(LCD_M,M_State); // Send new M state to M pin
}
if ( Latch_State != 0 ) { // Is latch pin active?
Latch_State = 0; // Yes, reset it low
digitalWrite(LCD_Latch,Latch_State); // Send new state to Latch pin
cb_flag = true; // Signal for possible user callback
} else if ( LCD_bits > 0 ) { // else any Data bits to send?
if ( Shift_State != 0 ) { // Yes, but is shift state rising?
b = bitbuf[0] & 0x80; // Yes, extract next data bit to send
digitalWrite(LCD_Din,b != 0 ? HIGH : LOW); // Send it to the Din pin
for ( x=0; x<5; ++x ) { // Shift the 40 bit buffer left
if ( x+1 < 5 ) // Extract carry bit from next byte
t = ( bitbuf[x+1] & 0x80 ) ? 1 : 0;
else t = 0; // else just use zero
bitbuf[x] = (bitbuf[x] << 1) | t; // Shift left 1 bit, + or in carry bit
}
} else { // Shift pin has gone LOW
--LCD_bits; // Decrement # of data bits remaining
if ( LCD_bits == 0 ) { // Was this the last bit?
Latch_State = 1; // Yes, then set latch high to clock in data
digitalWrite(LCD_Latch,Latch_State); // Latch the shifted data into HD44100
}
}
} else if ( Latch_State == 0 ) { // If no data, and latch is LOW...
for ( x = 0; x < 5; ++x ) // Refresh bitbuf[] with current data
bitbuf[x] = data[x]; // All 5 bytes
LCD_bits = 40; // Start first bit on next timer interrupt
}
if ( cb_flag == true && latchcb != 0 ) // If we have a user callback, invoke it..
latchcb(); // User routine: do not tarry too long
}
/*
* Return control, only after a latch event occurs :
*/
void
wait_latched(void) {
while ( Latch_State == 0 )
;
while ( Latch_State != 0 )
;
}
}
/* End OptrexLCD.cpp */