-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
448 lines (399 loc) · 12.5 KB
/
main.asm
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
;---------------------------------------------------------
; LASERPROJECT.ASM
; DESCRIPTION : A project for Computer Systems
; AUTHOR : Andrew Siemer
; VERSION : 3.6.20
;---------------------------------------------------------
; Registers:
; R16 - LCD buffer
; R17 - general use
; R18 - shift flag
; R19 - general use
; R20 - mode flag
;---------------------------------------------------------
.org 0x00 ; Write next command at 0x00 (Reset)
jmp START ; Wakeup/reset
.org 0x02 ; Write next command at 0x02 (INT0)
jmp INT0ROUTINE ; Call interrupt service routine for INT0
.org 0x300 ; go to 0x300 in program memory
data1:.DB 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
;---------------------------------------------------------
START: ; start of program
;---------------------------------------------------------
; STACK_CONFIG : Establishes stack variables
;---------------------------------------------------------
STACK_CONFIG:
LDI R17, HIGH(RAMEND)
OUT SPH, R17 ; set up stack
LDI R21, LOW(RAMEND)
OUT SPL, R17
;---------------------------------------------------------
; INT_CONFIG : Establishes interrupt variables
;---------------------------------------------------------
INT_CONFIG:
LDI R19, 0x0A ; preload binary 00001010 into R19
LDI R17, 0x00
STS EICRA, R19 ; set eicra to 00001010 (both interrupts trigger on active low)
LDI R19, 0x03 ; preload binary 00000011 into R19
OUT EIMSK, R19 ; set eimsk to 00000011 (enable both interrupts)
LDI R19, 0x00 ; preload binary 00000000 into R19
OUT DDRD, R19 ; set ddrd to 00000000 (all pins of portd are input pins, note you only need pins 2 and 3 for the interrupts)
LDI R19, 0x0C ; preload binary 00001100 into R19
OUT PORTD, R19 ; set portd to 00001100 (portd pins 2 and 3 are internally hooked to pull up resistors)
SEI ; set enable interrupts
;---------------------------------------------------------
; KEYPAD_CONFIG : Establishes keypad variables
;---------------------------------------------------------
KEYPAD_CONFIG:
.EQU KPD_PRT = PORTC ; keypad data port
.EQU KPD_DDR = DDRC ; keypad ddr
.EQU KPD_PIN = PINC ; keypad data pin
LDI R17, 0x00
OUT KPD_DDR, R17
OUT KPD_PRT, R17
;---------------------------------------------------------
; LCD_CONFIG : Establishes LCD variables
;---------------------------------------------------------
LCD_CONFIG:
.EQU LCD_PRT = PORTB ; LCD data port
.EQU LCD_DDR = DDRB ; LCD ddr
.EQU LCD_PIN = PINB ; LCD data pin
.EQU LCD_RS = 0 ; LCD RS
.EQU LCD_RW = 1 ; LCD RW
.EQU LCD_EN = 2 ; LCD EN
.EQU LCD_BUF1 = 0x200 ; LCD char buffer 1
.EQU LCD_BUF2 = 0x208 ; LCD char buffer 2
.EQU LCD_BUF3 = 0x210 ; LCD char buffer 3
.EQU LCD_BUF4 = 0x218 ; LCD char buffer 4
;---------------------------------------------------------
; KEYPAD_INIT : Initializes the keypad periphrials
;---------------------------------------------------------
KEYPAD_INIT:
LDI R20, '0' ; set mode flag to 0
;---------------------------------------------------------
; LCD_INIT : Initializes the LCD periphrials
;---------------------------------------------------------
LCD_INIT:
LDI R17, 0xFF
OUT LCD_DDR, R17 ; LCD data port is output
OUT LCD_DDR, R17 ; LCD command port is output
LDI R16, 0x33 ; init. LCD for 4-bit data
CALL LCD_CMD ; call command function
CALL DELAY_2ms ; init. hold
LDI R16, 0x32 ; init. LCD for 4-bit data
CALL LCD_CMD ; call command function
CALL DELAY_2ms ; init. hold
LDI R16, 0x28 ; init. LCD 2 lines, 5x7 matrix
CALL LCD_CMD ; call command function
CALL DELAY_2ms ; init. hold
LDI R16, 0x0E ; display on, cursor on
CALL LCD_CMD ; call command function
LDI R16, 0x01 ; clear LCD
CALL LCD_CMD ; call command function
CALL DELAY_2ms ; delay 2 ms for clear LCD
LDI R16, 0x06 ; shift cursor right
CALL LCD_CMD ; call command function
LDI R18, 0 ; set current state of shift to lower
LDI R17, ' '
STS LCD_BUF1, R17 ; init. buffer with spaces
STS LCD_BUF2, R17 ; init. buffer with spaces
STS LCD_BUF3, R17 ; init. buffer with spaces
STS LCD_BUF4, R17 ; init. buffer with spaces
CALL LCD_UPDATE
RJMP MAIN ; setup complete wait for interrupt
;---------------------------------------------------------
;---------------------------------------------------------
; MAIN : Wait in loop when idle
;---------------------------------------------------------
MAIN:
RJMP MAIN ; stay here
;---------------------------------------------------------
; INT0ROUTINE : Action on button press
;---------------------------------------------------------
INT0ROUTINE:
CLI ; clear interrupt
CALL KEYPAD_LOAD ; load in keypad buffer
CPI R16, 0x12 ; look for shift key
BREQ SHIFT_JMP
CPI R16, 0x13 ; look for mode key
BREQ MODE_JMP
CALL LOADZREGISTER1 ; load Z Register with database value
CALL KEYBUFF_LOAD ; load LCD character buffer
INT_BREAK:
CALL LCD_UPDATE ; write diplay buffer to LCD
SEI ; set enable interrupts
RETI
;---------------------------------------------------------
; LOADZREGISTER1 : Load Z register with database values
;---------------------------------------------------------
LOADZREGISTER1:
LDI ZL, LOW(2*data1)
LDI ZH, HIGH(2*data1)
LDI R21, 0
LDI R22, 18
CPSE R18, R21
ADD R16, R22
ADD ZL, R16
LPM R16, Z
RET
;---------------------------------------------------------
; KEYPAD_LOAD : Load R16 with keypad buffer
;---------------------------------------------------------
KEYPAD_LOAD:
LDI R16, 0x00
OUT KPD_DDR, R16 ; set keypad port as output
LDI R17, 10
LOOP:
CALL DELAY_2ms
DEC R17
BRNE LOOP
IN R16, KPD_PIN ; read in buffer from keypad
ANDI R16, 0x1F ; keypad buffer mask
RET
;---------------------------------------------------------
; SHIFT_JMP : JMP to SHIFT since its outside of relative range
;---------------------------------------------------------
SHIFT_JMP:
JMP SHIFT
;---------------------------------------------------------
; MODE_JMP : JMP to MODE since its outside of relative range
;---------------------------------------------------------
MODE_JMP:
JMP MODE
;---------------------------------------------------------
; KEYBUFF_LOAD : Load new keypad value into buffer
;---------------------------------------------------------
KEYBUFF_LOAD:
LDS R17, LCD_BUF3 ; load buffer value into R17
STS LCD_BUF4, R17 ; write R17 value to buffer memory
LDS R17, LCD_BUF2 ; load buffer value into R17
STS LCD_BUF3, R17 ; write R17 value to buffer memory
LDS R17, LCD_BUF1 ; load buffer value into R17
STS LCD_BUF2, R17 ; write R17 value to buffer memory
STS LCD_BUF1, R16 ; write R17 value to buffer memory
RET
;---------------------------------------------------------
; LCD_UPDATE : Updates LCD with new char buffer
;---------------------------------------------------------
LCD_UPDATE:
CALL LCD_CLR
LDI R16, 'M'
CALL LCD_WRT ; write R16 to LCD
LDI R16, 'o'
CALL LCD_WRT ; write R16 to LCD
LDI R16, 'd'
CALL LCD_WRT ; write R16 to LCD
LDI R16, 'e'
CALL LCD_WRT ; write R16 to LCD
LDI R16, ':'
CALL LCD_WRT ; write R16 to LCD
LDI R16, ' '
CALL LCD_WRT ; write R16 to LCD
MOV R16, R20
CALL LCD_WRT ; write R16 to LCD
LDI R16, 0x14 ; shift cursor right
LDI R17, 10
MOVE_CURSOR:
CALL LCD_CMD
DEC R17
BRNE MOVE_CURSOR
LDS R16, LCD_BUF4 ; load buffer value into R16
CALL LCD_WRT ; write R16 to LCD
LDS R16, LCD_BUF3 ; load buffer value into R16
CALL LCD_WRT ; write R16 to LCD
LDS R16, LCD_BUF2 ; load buffer value into R16
CALL LCD_WRT ; write R16 to LCD
LDS R16, LCD_BUF1 ; load buffer value into R16
CALL LCD_WRT ; write R16 to LCD
RET
;---------------------------------------------------------
; SHIFT : Toggle uppercase/lowercase
;---------------------------------------------------------
Shift:
CPI R18, 1 ; compare shift flag
BREQ LOWERCASE ; if uppercase call lowercase
BRNE UPPERCASE ; if lowercase call uppercase
;---------------------------------------------------------
; LOWERCASE : Toggle on lowercase
;---------------------------------------------------------
LOWERCASE:
LDI R16, 0x0E ; set LCD cursor to not blinking
CALL LCD_CMD ; write command to LCD
LDI R18, 0 ; set shift flag = 0
RJMP INT_BREAK
;---------------------------------------------------------
; LOWERCASE : Toggle on uppercase
;---------------------------------------------------------
UPPERCASE:
LDI R16, 0x0F ; set LCD cursor to blinking
CALL LCD_CMD ; write command to LCD
LDI R18, 1 ; set shift flag = 1
RJMP INT_BREAK
;---------------------------------------------------------
; MODE : Toggle mode state
;---------------------------------------------------------
MODE:
CPI R20, '0'
BREQ MODE_1
CPI R20, '1'
BREQ MODE_2
CPI R20, '2'
BREQ MODE_3
CPI R20, '3'
BREQ MODE_0
MODE_0:
LDI R20, '0'
RJMP INT_BREAK
MODE_1:
LDI R20, '1'
RJMP INT_BREAK
MODE_2:
LDI R20, '2'
CALL LASER_WRT
RJMP INT_BREAK
MODE_3:
LDI R20, '3'
RJMP INT_BREAK
;---------------------------------------------------------
; LCD_CLR : Clear LCD Screen
;---------------------------------------------------------
LCD_CLR:
LDI R16, 0x01 ; LCD clear command
CALL LCD_CMD ; LCD command write
CALL DELAY_2ms
RET
;---------------------------------------------------------
; LCD_CMD : Write data in R16 to LCD as command
;---------------------------------------------------------
LCD_CMD:
MOV R27, R16
ANDI R27, 0xF0
IN R26, LCD_PRT
ANDI R26, 0x0F
OR R26, R27
OUT Lcd_PRT, R26 ; LCD data port = R16
CBI LCD_PRT, LCD_RS ; RS = 0 for command
CBI LCD_PRT, LCD_RW ; RW = 0 for write
SBI LCD_PRT, LCD_EN ; EN = 1 for high pulse
CALL SDELAY ; make a wide EN pulse
CBI LCD_PRT, LCD_EN ; EN = 0 for H-to-L pulse
CALL DELAY_100us ; make a wide EN pulse
MOV R27, R16
SWAP R27
ANDI R27, 0xF0
IN R26, LCD_PRT
ANDI R26, 0x0F
OR R26, R27
OUT LCD_PRT, R26 ; LCD data port = R16
SBI LCD_PRT, LCD_EN ; EN = 1 for high pulse
CALL SDELAY ; make a wide EN pulse
CBI LCD_PRT, LCD_EN ; EN = 0 for H-to-L pulse
CALL DELAY_100us ; wait 100 us
RET
;---------------------------------------------------------
; LCD_WRT : Write data in R16 to LCD
;---------------------------------------------------------
LCD_WRT:
MOV R27, R16
ANDI R27, 0xF0
IN R26, LCD_PRT
ANDI R26, 0x0F
OR R26, R27
OUT LCD_PRT, R26 ; LCD data port = R16
SBI LCD_PRT, LCD_RS ; RS = 1 for data
CBI LCD_PRT, LCD_RW ; RW = 0 for write
SBI LCD_PRT, LCD_EN ; EN = 1 for high pulse
CALL SDELAY ; make a wide EN pulse
CBI LCD_PRT, LCD_EN ; EN = 0 for H-to-L pulse
MOV R27, R16
SWAP R27
ANDI R27, 0xF0
IN R26, LCD_PRT
ANDI R26, 0x0F
OR R26, R27
OUT LCD_PRT, R26 ; LCD data port = R16
SBI LCD_PRT, LCD_EN ; EN = 1 for high pulse
CALL SDELAY ; make a wide EN pulse
CBI LCD_PRT, LCD_EN ; EN = 0 for H-to-L pulse
CALL DELAY_100us ; wait 100 us
RET
;---------------------------------------------------------
; SDELAY : Pulse delay for LCD
;---------------------------------------------------------
SDELAY:
NOP
NOP
RET
;---------------------------------------------------------
; DELAY_100us : 100 microsecond delay
;---------------------------------------------------------
DELAY_100us:
PUSH R17
LDI R17, 60
DR0: CALL SDELAY
DEC R17
BRNE DR0
POP R17
RET
;---------------------------------------------------------
; DELAY_2ms : 2 millisecond delay
;---------------------------------------------------------
DELAY_2ms:
PUSH R17
LDI R17, 20
LDR0: CALL DELAY_100us
DEC R17
BRNE LDR0
POP R17
RET
;---------------------------------------------------------
LASER_WRT:
LDI R21, 0xFF
OUT DDRC, R21
LDI R21, 0x00 ;Initialization, Port is set to all 0's
OUT PortC, R21
LDI R21, 0xFF ;Set PortB as output
OUT DDRB, R21
LDI R21, 0b00000011 ;Set X and Y -Buffer
OUT PortC, R21
LDI R21, 0x00 ;Clear X and Y -Buffer
OUT PortB, R21
LDI R21, 0b00000000 ;Finish setting X and Y
OUT PortC, R21
LOP:
LDI R16, 0x00
CALL XBUFF_SET
CALL LASER_UPDATE
LDI R16, 0x00
CALL YBUFF_SET
CALL LASER_UPDATE
CALL DELAY_100us
CALL DELAY_100us
CALL DELAY_100us
CALL DELAY_100us
LDI R16, 0x00
CALL XBUFF_SET
CALL LASER_UPDATE
LDI R16, 0xFF
CALL YBUFF_SET
CALL LASER_UPDATE
CALL DELAY_100us
CALL DELAY_100us
CALL DELAY_100us
CALL DELAY_100us
RJMP LOP
XBUFF_SET:
LDI R21, 0b00000001
OUT PortC, R21
CALL DELAY_100us
RET
YBUFF_SET:
LDI R21, 0b00000010
OUT PortC, R21
CALL DELAY_100us
RET
LASER_UPDATE: ;Sets the position of the one of the buffers
OUT PortB, R16
CALL DELAY_100us
CALL DELAY_100us
RET