-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_3.5
560 lines (465 loc) · 9.68 KB
/
main_3.5
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
.INCLUDE "m328pdef.inc" ; Load addresses of (I/O) registers
.ORG 0x0000
RJMP init
.org 0x0012
RJMP TimerInterrupt
.ORG 0x0020
RJMP Timer0OverFlowInterrupt
.DEF DataLength = R4
.DEF Pattern = R5
.DEF char = R25 ; Register to store character data - button pressed
.DEF delay_reg = r20
; VARIABLE FOR TIMER RELAOD
.EQU FREQ_TCNT_RELOAD = 185
; VARIABLES FOR TIMING
.EQU DOT_LENGTH = 12
.EQU DASH_LENGTH = DOT_LENGTH*3
.EQU PAUSE_LENGTH = DOT_LENGTH*2
;-----
.EQU bufferstore=0x100
.equ buffer_end = bufferstore +16
;-----
init:
RCALL clearbuff
; Configure inputs(Joystick)
CBI DDRB,2
SBI PORTB,2
;------LCD_INIT-------
SBI DDRB,3 ; Pin PB3 is an output (Data)
CBI PORTB,3
SBI DDRB,4 ; Pin PB4 is an output (Latch Enable and Output Enable)
CBI PORTB,4
SBI DDRB,5 ; Pin PB5 is an output (Clock)
CBI PORTB,5
;-------LCD_END------------
LDI R23, 0b00000100 ;load Immediate 101 to R23(101: enable pre1024)
OUT TCCR0b, R23 ; timrt counter control register
; set the correct reload values 238
LDI R24, FREQ_TCNT_RELOAD ;256-(16MH/256)/880= 184.9
OUT TCNT0, R24 ; Timer counter register to initialization
;----Timer2------ Using for refreshing the screen so we can send code and screen at same time!
LDI R30, 0b00000101
STS TCCR2B, R30
SEI ; global interrupt enable
;---------Configuration-----
SBI DDRB,1
SBI PORTB,1
SBI DDRC,2
SBI PORTC,2
SBI DDRC,3
SBI PORTC,3
CBI DDRD,3 ;INPUT
CBI DDRD,2
CBI DDRD,1
CBI DDRD,0
SBI DDRD,7 ;OUTPUT
SBI DDRD,6
SBI DDRD,5
SBI DDRD,4
SBI PORTD,3 ;PULL RESITOR
SBI PORTD,2
SBI PORTD,1
SBI PORTD,0
RCALL clearbuff
LDI XH,high(bufferstore) ;init X to point to the address 0x0100
LDI XL,low(bufferstore)
RJMP main
clearbuff:
PUSH XH
PUSH XL
PUSH R17
PUSH R16
LDI XH,high(bufferstore) ;init X to point to the address 0x0100
LDI XL,low(bufferstore)
LDI R17,16
LDI R16, 16
sram_initloop:
ST X+,R17
DEC R16
BRNE sram_initloop
POP R16
POP R17
POP XL
POP XH
RET
JoyPressed:
RCALL clearbuff
RET
MAIN:
IN R3, PINB
BST R3,2
BRTC JoyPressed
push YL
push YH
LDI YL, Low(buffer_end)
LDI YH, High(buffer_end)
CP XL,YL ; Compare low byte
CPC XH,YH ; Compare high byte
pop YH
pop YL
brne keyboard
rcall clearbuff
LDI XH,high(bufferstore) ;init X to point to the address 0x0100
LDI XL,low(bufferstore)
;--------Keyboard-Configuration----------
keyboard:
CBI PORTD,7
SBI PORTD,6
SBI PORTD,5
SBI PORTD,4
nop
SBIS PIND,0
RJMP BTNF
SBIS PIND,1
RJMP BTN9
SBIS PIND,2
RJMP BTN8
SBIS PIND,3
RJMP BTN7
SBI PORTD,7
CBI PORTD,6
SBI PORTD,5
SBI PORTD,4
nop
SBIS PIND,0
RJMP BTNE
SBIS PIND,1
RJMP BTN6
SBIS PIND,2
RJMP BTN5
SBIS PIND,3
RJMP BTN4
SBI PORTD,7
SBI PORTD,6
CBI PORTD,5
SBI PORTD,4
nop
SBIS PIND,0
RJMP BTND
SBIS PIND,1
RJMP BTN3
SBIS PIND,2
RJMP BTN2
SBIS PIND,3
RJMP BTN1
SBI PORTD,7
SBI PORTD,6
SBI PORTD,5
CBI PORTD,4
nop
SBIS PIND,0
RJMP BTNC
SBIS PIND,1
RJMP BTNB
SBIS PIND,2
RJMP BTN0
SBIS PIND,3
RJMP BTNA
RJMP MAIN
;------Send the pattern to play the buzzer-------
SendMores:
PUSH R1
PUSH R2
LDI R31,1
STS TIMSK2, R31
LDI ZL, Low(CodeTable<<1)
LDI ZH, High(CodeTable<<1)
;------OFFSET----------
LDI R16,2
MUL char,R16
ADD ZL,R0
ADC ZH,R1
LPM DataLength,Z+
LPM Pattern, Z ;0x15 > 10101
PUSH R26
PUSH R28
PUSH R29
dotDashLoop:
RCALL SendDD
LDI delay_reg, PAUSE_LENGTH
RCALL VariableDelay
DEC DataLength
BRNE dotDashLoop
POP R29
POP R28
POP R26
POP R2
POP R1
RET
SendDD:
ROR Pattern
BRCC SendDot
RJMP SendDash
RET
SendDot:
CBI PORTC,2 ; Turn ON LED2
LDI R23, 0x01 ;
STS TIMSK0, R23 ; Turn on Buzzer
LDI delay_reg,DOT_LENGTH
RCALL VariableDelay
SBI PORTC,2 ; Turn OFF LED2
LDI R23, 0x00 ;
STS TIMSK0, R23 ; Turn off Buzzer
RET
SendDash:
CBI PORTC,3 ; Turn ON LED3
LDI R23, 0x01 ;
STS TIMSK0, R23 ; Turn on Buzzer
LDI delay_reg,DASH_LENGTH
RCALL VariableDelay
LDI R23, 0x00 ;
STS TIMSK0, R23 ; Turn Off Buzzer
SBI PORTC,3 ; Turn OFF LED3
RET
;----------------END-OFF-BUZZER-PLAY--------------
BigDelay:
PUSH delay_reg
LDI delay_reg, 100
RCALL VariableDelay
POP delay_reg
RET
VariableDelay:
PUSH r28
PUSH r29
delay_innerloop1:
LDI R28, 255
delay_innerloop2:
LDI R29, 255
delay_innerloop3:
DEC R29
BRNE delay_innerloop3
DEC R28
BRNE delay_innerloop2
DEC delay_reg
BRNE delay_innerloop1
POP R29
POP R28
RET
;-----BTN--------
BTN0:
LDI R17,0x00
ST X+,R17
LDI char, 0x00
RCALL SendMores
RJMP MAIN
BTN1:
LDI char, 0x01
ST X+,char
RCALL SendMores
RJMP MAIN
BTN2:
LDI char, 0x02
ST X+,char
RCALL SendMores
RJMP MAIN
BTN3:
LDI R17,3
ST X+,R17
LDI char, 0x03
RCALL SendMores
RJMP MAIN
BTN4:
LDI char, 0x04
ST X+,char
RCALL SendMores
RJMP MAIN
BTN5:
LDI char, 0x05
ST X+,char
RCALL SendMores
RJMP MAIN
BTN6:
LDI char, 0x06
ST X+,char
RCALL SendMores
RJMP MAIN
BTN7:
LDI R17,7
ST X+,R17
LDI char, 0x07
RCALL SendMores
RJMP MAIN
BTN8:
LDI char, 0x08
ST X+,char
RCALL SendMores
RJMP MAIN
BTN9:
LDI char, 0x09
ST X+,char
RCALL SendMores
RJMP MAIN
BTNA:
LDI char, 0x0A
ST X+,char
RCALL SendMores
RJMP MAIN
BTNB:
LDI char, 0x0B
ST X+,char
RCALL SendMores
RJMP MAIN
BTNC:
LDI char, 0x0C
ST X+,char
RCALL SendMores
RJMP MAIN
BTND:
LDI char, 0x0D
ST X+,char
RCALL SendMores
RJMP MAIN
BTNE:
LDI char, 0x0E
ST X+,char
RCALL SendMores
RJMP MAIN
BTNF:
LDI char, 0x0F
ST X+,char
RCALL SendMores
RJMP MAIN
;---------BTN-END--------------
Timer0OverflowInterrupt:
SBI PINB,1 ; Toggle BUZZER
RETI ; Return to Calling location from Interrupt
DELAY_LCD:
PUSH R16
LDI R16,200
LBL_DELAY:
dec r16
brne lbl_delay
pop r16
ret
TimerInterrupt:
PUSH R1
PUSH R0
push R18
push r19
push r20
push r22
push r17
push zh
push zl
push r6
push r4
PUSH YH
PUSH YL
PUSH R16
PUSH R21
IN R4, sreg
LDI R18,0b00000001 ; First row
LDI R19,8
Rows:
/* LDI YH,0x01 ;init Y to point to the address 0x0100
LDI YL,0x0F ;load addresses to Y
*/ LDI YH,high(buffer_end) ;init X to point to the address 0x0100
LDI YL,low(buffer_end)
//LD R16,Y+ ;auto-increment the address after reading
LDI R20,16 ;number of blocks
Block_Loop:
;calculate offset in table depending on charvalue
LD R22,-Y ; pre-decrement Y and load char value pointed by Y (from data memory/SRAM)
LDI ZH,high(CharTable<<1) ; load address table of chars into Z
LDI ZL,low(CharTable<<1) ; load address table of chars into Z
;Z=Z+8X+(rownumber-1)==> Z=Z+R22*8+(R19-1):
LDI R17,8
MUL R22,R17 ;X*8
ADD ZL,R0
ADC ZH,R1
MOV R16,R19
DEC R16 ;rownumber-1
CLR R6
ADD ZL,R16
ADC ZH,R6
LPM R22,Z ;Load Program Memory (loads data to send to the screen pointed by Z)
CLC ;Clear Carry
LDI R21,5 ;number of shifts for column data per block=number of columns per display
Columns:
CBI PORTB,3 ;Clear First Bit
ROR R22
BRCC Carry_Is_0_Col ;Branch if Carry is Cleared (BRCC)
SBI PORTB,3 ;Set PINB3 if the carry is set, then execute next line
Carry_Is_0_Col:
CBI PORTB,5
SBI PORTB,5 ;create rising edge of PB5 to shift
DEC R21
BRNE Columns
DEC R20
BRNE Block_Loop
CLC
LDI R17,8
ROR_loop:
CBI PORTB,3 ;Clear First Bit
ROR R18
BRCC Carry_Is_0_Row ;Branch if Carry is Cleared (BRCC)
SBI PORTB,3 ;Set PINB3 if the carry is set, then execute next line
Carry_Is_0_Row:
CBI PORTB,5
SBI PORTB,5 ;create rising edge of PB5 to shift
DEC R17
BRNE ROR_loop
//RCALL DELAY_LCD
CBI PORTB,4
RCALL DELAY_LCD
SBI PORTB,4; rising edge at PB4 to Latch the data in the output register
//CBI PORTB,4 ;enable the output of the shift register
DEC R19
TST R18 ; Test if Zero or Minus; Register will be zero if All Rows Sent
BRNE Rows ; Branch back to Column sending loop. Send to Row 7>6..>1
CLC
ROL R18 ; Otherwise just use ROL to reset
LDI R19,8 ; Reinitialize row index to 8
OUT sreg, r4
POP R21
POP R16
POP YL
POP YH
pop r4
pop r6
pop zl
pop zh
pop r17
pop r22
pop r20
pop r19
pop r18
POP R0
POP R1
RETI
CodeTable:
.db 5, 0b11111 ; [0]
.db 5, 0b11110 ; [1]
.db 5, 0b11100 ; [2]
.db 5, 0b11000 ; [3]
.db 5, 0b00001 ; [4]
.db 5, 0b00000 ; [5]
.db 5, 0b00001 ; [6]
.db 5, 0b00011 ; [7]
.db 5, 0b00111 ; [8]
.db 5, 0b01111; [9]
.db 2, 0b10 ; [A]
.db 4, 0b0001 ; [B]
.db 4, 0b0101 ; [C]
.db 3, 0b001 ; [D]
.db 1, 0b0 ; [E]
.db 4, 0b0100 ; [F]
CharTable:
.db 0b01111, 0b01001, 0b01001, 0b01001, 0b01001, 0b01001, 0b01111, 0b00000 ; 8
.db 0b00010, 0b00110, 0b01010, 0b00010, 0b00010, 0b00010, 0b00010, 0b00000 ; 1
.db 0b01111, 0b00001, 0b00001, 0b01111, 0b01000, 0b01000, 0b01111, 0b00000 ; 2
.db 0b01111, 0b00001, 0b00001, 0b01111, 0b00001, 0b00001, 0b01111, 0b00000 ; 3
.db 0b01001, 0b01001, 0b01001, 0b01111, 0b00001, 0b00001, 0b00001, 0b00000 ; 4
.db 0b01111, 0b01000, 0b01000, 0b01111, 0b00001, 0b00001, 0b01111, 0b00000 ; 5
.db 0b01111, 0b01000, 0b01000, 0b01111, 0b01001, 0b01001, 0b01111, 0b00000 ; 6
.db 0b01111, 0b00001, 0b00001, 0b00010, 0b00100, 0b00100, 0b00100, 0b00000 ; 7
.db 0b01111, 0b01001, 0b01001, 0b01111, 0b01001, 0b01001, 0b01111, 0b00000 ; 8
.db 0b01111, 0b01001, 0b01001, 0b01111, 0b00001, 0b00001, 0b01111, 0b00000 ; 9
.db 0b00110, 0b01001, 0b01001, 0b01001, 0b01111, 0b01001, 0b01001, 0b00000 ; A
.db 0b01110, 0b01001, 0b01001, 0b01110, 0b01001, 0b01001, 0b01110, 0b00000 ; B
.db 0b00110, 0b01001, 0b01000, 0b01000, 0b01000, 0b01001, 0b00110, 0b00000 ; C
.db 0b01110, 0b01001, 0b01001, 0b01001, 0b01001, 0b01001, 0b01110, 0b00000 ; D
.db 0b01111, 0b01000, 0b01000, 0b01111, 0b01000, 0b01000, 0b01111, 0b00000 ; E
.db 0b01111, 0b01000, 0b01000, 0b01110, 0b01000, 0b01000, 0b01000, 0b00000 ; F
.db 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 ; 0
.db 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000 ; Blank