-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTM1621.cpp
More file actions
330 lines (328 loc) · 10.7 KB
/
TM1621.cpp
File metadata and controls
330 lines (328 loc) · 10.7 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
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
/*Begining of Auto generated code by Atmel studio */
/*
This code is not clean and also is not perfect, this is only
a reference to extract ideas and adapte to your solution.
*/
void send_char(unsigned int a);
#define LCD_in 7 // This is the pin number 7 on Arduino UNO
#define LCD_wr 8 // This is the pin number 8 on Arduino UNO
#define LCD_cs 9 // This is the pin number 9 on Arduino UNO
/*
#define BIN(x) \
( ((0x##x##L & 0x00000001L) ? 0x01 : 0) \
| ((0x##x##L & 0x00000010L) ? 0x02 : 0) \
| ((0x##x##L & 0x00000100L) ? 0x04 : 0) \
| ((0x##x##L & 0x00001000L) ? 0x08 : 0) \
| ((0x##x##L & 0x00010000L) ? 0x10 : 0) \
| ((0x##x##L & 0x00100000L) ? 0x20 : 0) \
| ((0x##x##L & 0x01000000L) ? 0x40 : 0) \
| ((0x##x##L & 0x10000000L) ? 0x80 : 0))
*/
//ATT: On the Uno and other ATMEGA based boards, unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value.
//Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.
//*************************************************//
void setup() {
pinMode(LCD_wr, OUTPUT);
pinMode(LCD_in, OUTPUT);
pinMode(LCD_cs, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
digitalWrite(LCD_cs, LOW);
delayMicroseconds(5);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
//
initLCD();
delay(100);
//testLCD();
//delay(1000);
}
void send_char(unsigned int a)
{
//
unsigned int data = 0;
unsigned int mask = 1; //our bitmask
data=a;
digitalWrite(LCD_cs, LOW); // When strobe is low, all output is enable. If high, all output will be set to low.
delayMicroseconds(2);
// I define the size of mask to send only 12 bits
for (mask = 000000000001; mask<4096; mask <<= 1) { //iterate through bit mask
// Serial.println(mask, BIN);
digitalWrite(LCD_wr,LOW);// need invert the signal to allow 8 bits is is low only send 7 bits
delayMicroseconds(2);
if (data & mask){ // if bitwise AND resolves to true
digitalWrite(LCD_in, HIGH);
// Serial.print(1);
}
else{ //if bitwise and resolves to false
digitalWrite(LCD_in, LOW);
// Serial.print(0);
}
digitalWrite(LCD_wr, HIGH);// need invert the signal to allow 8 bits is is low only send 7 bits
delayMicroseconds(2);
}
digitalWrite(LCD_cs, HIGH); // When strobe is low, all output is enable. If high, all output will be set to low.
delayMicroseconds(2);
}
void send_cmd(unsigned int a)
// Attention: The "cmd" use 13 bits, the send_char use only 12
{
//
unsigned int data = 0;
unsigned int mask = 1; //our bitmask
data=a;
digitalWrite(LCD_cs, LOW); // When strobe is low, all output is enable. If high, all output will be set to low.
delayMicroseconds(2);
// I define the size of mask to send only 12 bits
for (mask = 0000000000001; mask<8192; mask <<= 1) { //iterate through bit mask
//Serial.println(mask, BIN);
digitalWrite(LCD_wr,LOW);// need invert the signal to allow 8 bits is is low only send 7 bits
delayMicroseconds(2);
if (data & mask){ // if bitwise AND resolves to true
digitalWrite(LCD_in, HIGH);
//Serial.print(1);
}
else{ //if bitwise and resolves to false
digitalWrite(LCD_in, LOW);
//Serial.print(0);
}
digitalWrite(LCD_wr, HIGH);// need invert the signal to allow 8 bits is is low only send 7 bits
delayMicroseconds(2);
}
digitalWrite(LCD_cs, HIGH); // When strobe is low, all output is enable. If high, all output will be set to low.
delayMicroseconds(2);
}
//READ 110 A5A4A3A2A1A0D0D1D2D3 D Read data from the RAM
//WRITE 101 A5A4A3A2A1A0D0D1D2D3 D Write data to the RAM
//READMODIFYWRITE 101 A5A4A3A2A1A0D0D1D2D3 D READ and WRITE to the RAM
//SYS DIS 100 0000-0000-X C Turn off both system oscillator and LCD bias generator Yes
//SYS EN 100 0000-0001-X C Turn on system oscillator LCD, OFF 100 0000-0010-X C
//Turn off LCD bias generator Yes LCD ON 100 0000-0011-X C Turn on LCD bias generator
void initLCD(){
//*****************************************************************//
// LCD OFF 10000000010X Turn off LCD outputs
// LCD ON 10000000011X Turn on LCD outputs
//
// BIAS & COM 1000010abXcX
// c=0: 1/2 bias option
// c=1: 1/3 bias option
// ab=00: 2 commons option
// ab=01: 3 commons option
// ab=10: 4 commons option
//****************************************************************//
// X0Xba0100001
//send_char(0b000100100001); //100 0010-abX0-X BIAS 1/2 LCD 1/2 bias option ab=00: 2 commons option ab=01: 3 commons option ab=10: 4 commons option
//send_char(0b010100100001); //100 0010-abX0-X BIAS 1/2 LCD 1/2 bias option ab=00: 2 commons option ab=01: 3 commons option ab=10: 4 commons option
// The LCD I'm use, have 4 common pins. I let the position of "a" with "1" to select this option!
send_char(0b000010100001); //100 0010-abX1-X BIAS 1/3 LCD 1/3 bias option ab=00: 2 commons option ab=01: 3 commons option ab=10: 4 commons option
send_char(0b001100000001);// TIMER EN 100 0000-0110-X Enable time base output
//send_char(0b000000000001); //SYS DIS Turn off both system oscillator and LCD bias generator, stay off
//delay(2000);
//send_char(0B100000000010); // SYS EN
send_char(0B010000000001); // SYS EN
//send_char(0B100000000110); // LCD ON
send_char(0B011000000001); // LCD ON
//send_char(0B001000000001); // LCD OFF
}
void testLCD(void){
send_char(0b000000111001); //Mode Test
delay(2000);
send_char(0b011000111001); //Normal Mode Test
delay(2000);
segmentClear();
}
void fourBitsZero(void){
// tudos 4 bits a 0000;
send_cmd(0B0000000000101);
send_cmd(0B0000100000101);
send_cmd(0B0000010000101);
send_cmd(0B0000110000101);
send_cmd(0B0000001000101);
send_cmd(0B0000101000101);
send_cmd(0B0000011000101);
send_cmd(0B0000111000101);
send_cmd(0B0000000100101);
send_cmd(0B0000100100101);
send_cmd(0B0000010100101);
send_cmd(0B0000110100101);
send_cmd(0B0000001100101);
send_cmd(0B0000101100101);
send_cmd(0B0000011100101);
send_cmd(0B0000111100101);
//a0-a15 ... d0-d15
send_cmd(0B0000000010101);
send_cmd(0B0000100010101);
send_cmd(0B0000010010101);
send_cmd(0B0000110010101);
send_cmd(0B0000001010101);
send_cmd(0B0000101010101);
send_cmd(0B0000011010101);
send_cmd(0B0000111010101);
send_cmd(0B0000000110101);
send_cmd(0B0000100110101);
send_cmd(0B0000010110101);
send_cmd(0B0000110110101);
send_cmd(0B0000001110101);
send_cmd(0B0000101110101);
send_cmd(0B0000011110101);
send_cmd(0B0001111110101);
}
void letters(void){
// 1/0-1/15 ... 4/0-4/15
send_cmd(0B1101000000101);
send_cmd(0B0000100000101);
send_cmd(0B1010010000101);
send_cmd(0B0001110000101);
send_cmd(0B1101001000101);
send_cmd(0B1001101000101);
send_cmd(0B0010011000101);
send_cmd(0B0000111000101);
//
send_cmd(0B1001000100101);
send_cmd(0B1000100100101);
send_cmd(0B0000010100101);
send_cmd(0B0000110100101);
send_cmd(0B1001001100101);
send_cmd(0B1000101100101);
send_cmd(0B0000011100101);
send_cmd(0B0000111100101);
//a0-a15 ... d0-d15
send_cmd(0B1001000010101);
send_cmd(0B1001100010101);
send_cmd(0B1000010010101);
send_cmd(0B0001110010101);
send_cmd(0B0000001010101);
send_cmd(0B0000101010101);
send_cmd(0B0000011010101);
send_cmd(0B0000111010101);
//
send_cmd(0B0000000110101);
send_cmd(0B0000100110101);
send_cmd(0B0000010110101);
send_cmd(0B0000110110101);
send_cmd(0B0000001110101);
send_cmd(0B0000101110101);
send_cmd(0B0000011110101);
send_cmd(0B0000111110101);
}
void simbols1(void){
send_cmd(0B0000000110101);
send_cmd(0B0000100110101);
send_cmd(0B0000010110101);
send_cmd(0B0000110110101);
send_cmd(0B0000001110101);
send_cmd(0B0000101110101);
send_cmd(0B0001011110101);
send_cmd(0B0001111110101);
}
void simbols2(void){
send_cmd(0B0000000110101);
send_cmd(0B0000100110101);
send_cmd(0B0000010110101);
send_cmd(0B0000110110101);
send_cmd(0B0000001110101);
send_cmd(0B0000101110101);
send_cmd(0B0011011110101);
send_cmd(0B0011111110101);
}
void simbols3(void){
send_cmd(0B0000000110101);
send_cmd(0B0000100110101);
send_cmd(0B0000010110101);
send_cmd(0B0000110110101);
send_cmd(0B0000001110101);
send_cmd(0B0000101110101);
send_cmd(0B0111011110101);
send_cmd(0B0111111110101);
}
void simbols4(void){
send_cmd(0B0000000110101);
send_cmd(0B0000100110101);
send_cmd(0B0000010110101);
send_cmd(0B0000110110101);
send_cmd(0B0000001110101);
send_cmd(0B0000101110101);
send_cmd(0B1111011110101);
send_cmd(0B1111111110101);
}
void segmentClear(void){
unsigned int common=0;
unsigned int address=0;
unsigned int cmd=0b101;
unsigned int mem=0;
unsigned int aux=0;
unsigned int var=0;
unsigned int data=0;
unsigned int temp=0;
//
for(common=0; common<16; common++){
temp=0;
var=(common << 9);
for (mem=0; mem< 64; mem++){
data=0;
data=(data | cmd);
//Serial.print("Data 1: "); Serial.println(data, BIN); // Only for Debug
aux=mem << 3;
//Serial.print("aux: "); Serial.print(aux, BIN); Serial.print("; mem="); Serial.println(mem, DEC); // Only for Debug
data=(data | aux);
//Serial.print("Data 2: "); Serial.println(data, BIN); // Only for Debug
temp=(data | var);
send_cmd(temp);
//Serial.println(temp, BIN);
delayMicroseconds(2);
}
}
}
void segmentTest(void){
unsigned int common=0;
unsigned int address=0;
unsigned int cmd=0b101;
unsigned int mem=0;
unsigned int aux=0;
unsigned int var=0;
unsigned int data=0;
unsigned int temp=0;
//
for(common=0; common<16; common++){
temp=0;
var=(common << 9);
//Serial.print("var: "); Serial.print(var, BIN); Serial.print("; var=");Serial.println(var, DEC);// Only for Debug
//
for (mem=0; mem< 64; mem++){
data=0;
data=(data | cmd);
//Serial.print("Data 1: "); Serial.println(data, BIN); // Only for Debug
aux=mem << 3;
//Serial.print("aux: "); Serial.print(aux, BIN); Serial.print("; mem="); Serial.println(mem, DEC); // Only for Debug
data=(data | aux);
//Serial.print("Data 2: "); Serial.println(data, BIN); // Only for Debug
temp=(data | var);
send_cmd(temp);
//Serial.println(temp, BIN); // Only for Debug
delay(30);
}
}
}
void loop() {
segmentClear();
delay(50);
segmentTest();
delay(50);
while(1){
letters();
simbols1();
delay(50);
simbols2();
delay(50);
simbols3();
delay(50);
simbols4();
delay(50);
}
}