-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm1638cc.inc
479 lines (404 loc) · 10.9 KB
/
tm1638cc.inc
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
;=======================================================================
; TM1638cc.inc
; Library to drive the special circuit for LED control TM1638
; from Titan Micro Electronics (TM)
;
; written by Ralf Jardon (cosmicos at gmx dot net), May-July 2017
;
; Comments related to the datasheet refer to version 1.3 (en)
;
; License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007
;
; Version: 1.0beta-1
;=======================================================================
;=======================================================================
; TM1638_INIT:
;=======================================================================
TM1638_INIT:
; initialize Ports
ldi AKKU, (1<<STB_PIN) | (1<<CLK_PIN) | (1<<DATA_PIN)
out DDR_TM1638, AKKU
ldi AKKU, (1<<STB_PIN) | (1<<CLK_PIN)
out PORT_TM1638, AKKU
rcall delay1us
#if !defined(BITBANGING) ; initialize SPI-engine
ldi AKKU, (0<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<DORD)|(1<<CPOL)|(1<<CPHA)|(1<<SPR1)|(1<<SPR0)
out SPCR, AKKU
ldi AKKU, (1<<SPI2X)
out SPSR, AKKU
#endif
; initialize TM1638
ldi TM1638_DATA_BYTE, DATA_CMD + WRITE_DATA
rcall TM1638_SEND_COMMAND
ldi TM1638_DATA_BYTE, DISP_CTRL_CMD + DISP_ON + DISP_PWM_MASK
rcall TM1638_SEND_COMMAND
rcall TM1638_CLEAR ; clear Display Memory
ret
;=======================================================================
; TM1638_SEND: generates CLOCK signal and send DATA (bit-banging).
;=======================================================================
#ifdef BITBANGING ; BITBANGING
TM1638_SEND:
push COUNT
push AKKU
push AKKU2
ldi COUNT, 8 ; loop bit 0-7
loop:
ror TM1638_DATA_BYTE ; put lowest bit into carry flag
brcs high_bit ; if carry set -> DATA pin HIGH
rcall delay1us
TM1638_CLK_LOW_DATA_LOW ; carry is not set -> DATA pin LOW
rjmp next_cycle ; go to the next clk cycle
high_bit:
rcall delay1us
TM1638_CLK_LOW_DATA_HIGH
next_cycle:
rcall delay1us
TM1638_CLK_HIGH ; CLOCK pin HIGH
dec COUNT ; next bit
brne loop
pop AKKU2
pop AKKU
pop COUNT
ret
#else ; HARDWARE-SPI
TM1638_SEND: ; start SPI transfer
out SPDR, TM1638_DATA_BYTE ; write Data into SPI-engine
busy:
sbis SPSR, SPIF ; Wait for transmission complete
rjmp busy
ret
#endif
;=======================================================================
; TM1638_SEND_COMMAND: Sends Command to TM1638
;=======================================================================
TM1638_SEND_COMMAND:
TM1638_STB_LOW ; set STROBE-output LOW. Ready to
; send commands or data
rcall TM1638_SEND ; send 1 Byte command
TM1638_STB_HIGH ; set STROBE-output HIGH. Command
; received
rcall delay1us
ret
;=======================================================================
; TM1638_SEND_DATA: Sends DATA to TM1638
; (page 10 at datasheet)
;=======================================================================
TM1638_SEND_DATA:
ldi TM1638_DATA_BYTE, DATA_CMD + FIXED_ADDR
rcall TM1638_SEND_COMMAND ; send Command 1
rcall delay1us
TM1638_STB_LOW
ori TM1638_GRID_BYTE, ADDR_CMD ; build segment address
mov TM1638_DATA_BYTE, TM1638_GRID_BYTE
rcall TM1638_SEND ; send Command 2 + address
mov TM1638_DATA_BYTE, TM1638_SEGM_BYTE
rcall TM1638_SEND ; send data
TM1638_STB_HIGH
ret
;=======================================================================
; TM1638_CLEAR: clears the Display memory and the Grid-memory to prevent
; garbage after startup
;=======================================================================
TM1638_CLEAR:
push COUNT
clr COUNT
clear_loop:
mov TM1638_GRID_BYTE, COUNT ; Grid
ldi TM1638_SEGM_BYTE, 0x00 ; Segment
rcall TM1638_SEND_DATA
subi COUNT, -2 ; next address
cpi COUNT, REG_MAX+1 ; REG_MAX = 0x0F
brne clear_loop
pop COUNT
ret
;=======================================================================
; TM1638_PRINT_DEC 16 bit
; AKKU2 (low Byte) / AKKU3 (high Byte)
;=======================================================================
TM1638_PRINT_DEC:
push AKKU
push AKKU2
push AKKU3
; ** 10000 **
ldi AKKU, '0'
DEC_SEGM5:
inc AKKU
subi AKKU2, low(10000)
sbci AKKU3, high(10000)
brcc DEC_SEGM5
subi AKKU2, low(-10000)
sbci AKKU3, high(-10000)
dec AKKU
clt ; clr t-flag: decimal place was 1-9
cpi AKKU, '0'
breq DEC_SEGM4
mov TM1638_SEGM_BYTE, AKKU
ldi TM1638_GRID_BYTE, 0x06
rcall TM1638_PRINT_CHAR
set ; set t-flag: decimal place was 0
; ** 1000 **
ldi AKKU, '0'
DEC_SEGM4:
inc AKKU
subi AKKU2, low(1000)
sbci AKKU3, high(1000)
brcc DEC_SEGM4
subi AKKU2, low(-1000)
sbci AKKU3, high(-1000)
dec AKKU
brts segm4 ; skip zero test because previous
; decimal place was 1-9
cpi AKKU, '0'
breq DEC_SEGM3
segm4:
mov TM1638_SEGM_BYTE, AKKU
ldi TM1638_GRID_BYTE, 0x08
rcall TM1638_PRINT_CHAR
set
; ** 100 **
ldi AKKU, '0'
DEC_SEGM3:
inc AKKU
subi AKKU2, low(100)
sbci AKKU3, high(100)
brcc DEC_SEGM3
subi AKKU2, -100
dec AKKU
brts segm3 ; skip zero test because previous
; decimal place was 1-9
cpi AKKU, '0'
breq DEC_SEGM2
segm3:
mov TM1638_SEGM_BYTE, AKKU
ldi TM1638_GRID_BYTE, 0x0A
rcall TM1638_PRINT_CHAR
set
; ** 10 **
ldi AKKU, '0'
DEC_SEGM2:
inc AKKU
subi AKKU2, 10
brcc DEC_SEGM2
subi AKKU2, -10
dec AKKU
brts segm2 ; skip zero test because previous
; decimal place was 1-9
cpi AKKU, '0'
breq DEC_SEGM1
segm2:
mov TM1638_SEGM_BYTE, AKKU
ldi TM1638_GRID_BYTE, 0x0C
rcall TM1638_PRINT_CHAR
set
; ** 1 **
DEC_SEGM1:
ldi AKKU, '0'
add AKKU, AKKU2
mov TM1638_SEGM_BYTE, AKKU
ldi TM1638_GRID_BYTE, 0x0E
rcall TM1638_PRINT_CHAR
set
pop AKKU3
pop AKKU2
pop AKKU
ret
;=======================================================================
; TM1638_PRINT_HEX 16 bit
; AKKU2 (low Byte) / AKKU3 (high Byte)
; improved version by Eberhard Haug
;=======================================================================
TM1638_PRINT_HEX:
push AKKU3
push AKKU2
push AKKU
swap AKKU3 ; higher nibble
ldi TM1638_GRID_BYTE, 0x08 ; digit position 4
rcall hex_digith
swap AKKU3 ; lower nibble
ldi TM1638_GRID_BYTE, 0x0A ; digit position 3
rcall hex_digith
swap AKKU2 ; higher nibble
ldi TM1638_GRID_BYTE, 0x0C ; digit position 2
rcall hex_digit
swap AKKU2 ; lower nibble
ldi TM1638_GRID_BYTE, 0x0E ; digit position 1
rcall hex_digit
pop AKKU
pop AKKU2
pop AKKU3
ret
hex_digith:
push AKKU3
andi AKKU3, $0F ; mask off nibble
CPI AKKU3,10
BRLO _I47
subi AKKU3, -('A'-'9'-1)
_I47:
subi AKKU3, -'0'
mov TM1638_SEGM_BYTE, AKKU3
rcall TM1638_PRINT_CHAR
pop AKKU3
ret
hex_digit:
push AKKU2
andi AKKU2, $0F ; mask off nibble
CPI AKKU2,10
BRLO _I50
subi AKKU2, -('A'-'9'-1)
_I50:
subi AKKU2, -'0'
mov TM1638_SEGM_BYTE, AKKU2
rcall TM1638_PRINT_CHAR
pop AKKU2
ret
;=======================================================================
; TM1638_PRINT_BIN 8 bit from AKKU
;=======================================================================
TM1638_PRINT_BIN:
push AKKU
push AKKU2
push AKKU3
push TM1638_GRID_BYTE
mov AKKU2, AKKU;
ldi TM1638_GRID_BYTE, 0x00 ; Segment start-address
ldi AKKU3, 8;
bin_number_loop:
dec AKKU3;
rol AKKU2;
brcc bin_number_bit_not_set;
brcs bin_number_bit_set;
rjmp bin_number_loop;
bin_number_bit_not_set:
ldi AKKU, '0'
rjmp bin_out
bin_number_bit_set:
ldi AKKU, '1'
bin_out:
mov TM1638_SEGM_BYTE, AKKU
rcall TM1638_PRINT_CHAR
subi TM1638_GRID_BYTE, -2 ; next segment address
tst AKKU3;
breq bin_number_ende;
rjmp bin_number_loop;
bin_number_ende:
pop TM1638_GRID_BYTE
pop AKKU3
pop AKKU2
pop AKKU
ret
;=======================================================================
; M1638_PRINT_CHAR: prints only one character from font table.
; Char is given by TM1638_SEGM_BYTE, Position is given by TM1638_GRID_BYTE
;=======================================================================
TM1638_PRINT_CHAR:
push ZH
push ZL
push ZCODE
push AKKU
push TM1638_SEGM_BYTE
ldi ZL, LOW(2*FONTS) ; start address fonts
ldi ZH, HIGH(2*FONTS) ;
subi TM1638_SEGM_BYTE, ASCII_OFFSET; - 0x30
cpi TM1638_SEGM_BYTE, 0x1A ; check, if number or character
brlo number ; if char, sub 7
subi TM1638_SEGM_BYTE, CHAR_OFFSET
number:
add ZL, TM1638_SEGM_BYTE ; select char address
brcc load_char
inc ZH
load_char:
lpm ; load r0=ZCODE with char
mov TM1638_SEGM_BYTE, ZCODE
rcall TM1638_SEND_DATA ; send data
pop TM1638_SEGM_BYTE
pop AKKU
pop ZCODE
pop ZL
pop ZH
ret
;=======================================================================
; TM1638_PRINT_TEXT
; AKKU3 = Textblock Number
;=======================================================================
TM1638_PRINT_TEXT:
push AKKU3
push AKKU
ldi ZL, LOW(PRINTTEXT*2) ; load text start address
ldi ZH, HIGH(PRINTTEXT*2)
ldi TM1638_GRID_BYTE, 0x00 ; first segment
select_textblock_print:
cpi AKKU3, 0
breq print_text_loop
adiw ZL, 10, ; each TEXT_BLOCK has 10 chars
; calculate address of current block
dec AKKU3
rjmp select_textblock_print
print_text_loop:
lpm AKKU, Z+ ; load first char of current block +
; post-increment pointer
cpi AKKU, 0 ; test if zero (block end)
breq text_end
mov TM1638_SEGM_BYTE, AKKU
rcall TM1638_PRINT_CHAR ; print char
subi TM1638_GRID_BYTE, -2 ; go to next segment address
rjmp print_text_loop
text_end:
pop AKKU
pop AKKU3
ret
;=======================================================================
; TM1638_PRINT_MOVETEXT
; AKKU3 = Textblock Number
;=======================================================================
TM1638_PRINT_MOVETEXT:
push COUNT
push AKKU3
push AKKU2
push AKKU
ldi ZL, LOW(MOVETEXT*2) ; load text start address
ldi ZH, HIGH(MOVETEXT*2)
select_textblock_move:
cpi AKKU3, 0
breq start_movement
adiw ZL, TEXT_BLOCK ; each TEXT_BLOCK has 36 chars
; calculate address of current block
dec AKKU3
rjmp select_textblock_move
start_movement:
clr TM1638_GRID_BYTE
clr COUNT
clr AKKU2
move_display_loop:
inc COUNT ; display loop
lpm AKKU, Z+ ; load first char of current block +
; post-increment pointer
mov TM1638_SEGM_BYTE, AKKU
rcall TM1638_PRINT_CHAR ; print current char
subi TM1638_GRID_BYTE, -2 ; go to next segment address
cpi COUNT, 8 ; test, if display full
brne move_display_loop ; if not, print next char
rcall Delay100ms ; text loop
rcall Delay100ms
rcall TM1638_POLL_KEYPAD
clr COUNT
sbiw ZL, 7 ; sub 7 (8-1) to shift display
inc AKKU2
cpi AKKU2, TEXT_BLOCK-8
breq move_text_end
rjmp move_display_loop
move_text_end:
pop AKKU
pop AKKU2
pop AKKU3
pop COUNT
ret
;=======================================================================
; TM1638_BRIGHTNESS
; AKKU = Value between 0-7
;=======================================================================
TM1638_BRIGHTNESS:
ldi TM1638_DATA_BYTE, DISP_CTRL_CMD + DISP_ON
add TM1638_DATA_BYTE, AKKU
rcall TM1638_SEND_COMMAND
ret