Large diffs are not rendered by default.

@@ -1,5 +1,5 @@

AVRASM ver. 2.2.6 C:\Users\Jason P\Desktop\uni\comp2121\labs\comp2121ShashJason\Ass3\VendingMachine\VendingMachine\main.asm Mon May 29 15:55:13 2017
AVRASM ver. 2.2.6 C:\Users\Jason P\Desktop\uni\comp2121\labs\comp2121ShashJason\Ass3\VendingMachine\VendingMachine\main.asm Tue May 30 17:17:03 2017


EQU SIGNATURE_000 0000001e
@@ -1360,62 +1360,86 @@ EQU UDRE3addr 0000006e
EQU UTXC3addr 00000070
EQU INT_VECTORS_SIZE 00000072
DEF lcd r16
DEF inventory_value r17
DEF boolean r18
DEF flag r19
DEF row r20
DEF col r21
DEF rmask r22
DEF cmask r23
DEF temp1 r24
DEF temp2 r25
DEF temp r17
DEF cost r18
DEF number r19
DEF temp2 r21
DEF boolean r22
DEF flag r23
DEF row r16
DEF col r17
DEF rmask r18
DEF cmask r19
DEF temp1 r20
EQU PORTLDIR 000000f0
EQU INITCOLMASK 000000ef
EQU INITROWMASK 00000001
EQU ROWMASK 0000000f
DSEG TempCounter 00000200
DSEG SecondCounter 00000202
DSEG CounterFlag 00000203
DSEG Cost 00000204
DSEG Stock 0000020e
DSEG ScreenState 00000204
DSEG InventState 00000205
DSEG One 00000206
DSEG Two 00000208
DSEG Three 0000020a
DSEG Four 0000020c
DSEG Five 0000020e
DSEG Six 00000210
DSEG Seven 00000212
DSEG Eight 00000214
DSEG Nine 00000216
DSEG Zero 00000218
CSEG RESET 00000033
CSEG EXT_INT1 000001b3
CSEG EXT_INT0 000001b5
CSEG DEFAULT 00000032
CSEG Timer0OVF 000000f6
CSEG INIT_INVENTORY 000000e4
CSEG lcd_command 0000019e
CSEG lcd_wait 000001ae
CSEG sleep_5ms 000001c8
CSEG sleep_1ms 000001bf
CSEG lcd_data 000001a5
CSEG Start_screen 000000d4
CSEG KEYBOARD 00000166
CSEG RetSS 000000db
CSEG RetSS_MAIN_MENU 000000e2
CSEG MAIN_MENU 0000011f
CSEG cost_loop 000000e7
CSEG stock_loop 000000f1
CSEG newSecond 000000fc
CSEG NotSecond 00000114
CSEG Cont 0000010e
CSEG EndIF 00000118
CSEG colloop 00000168
CSEG check 00000195
CSEG delay 0000016d
CSEG rowloop 00000176
CSEG nextcol 0000017f
CSEG convert 00000183
CSEG change 00000198
CSEG symbols 00000190
CSEG convert_end 0000019a
CSEG return 00000199
CSEG loop 0000019d
CSEG Timer0OVF 0000018a
CSEG lcd_command 0000031e
CSEG lcd_wait 0000032e
CSEG sleep_5ms 00000348
CSEG sleep_1ms 0000033f
CSEG lcd_data 00000325
CSEG Start_screen 0000017e
CSEG KEYBOARD 00000252
CSEG MAIN_MENU 000001b7
CSEG newSecond 00000190
CSEG NotSecond 000001a8
CSEG Cont 000001a2
CSEG EndIF 000001ac
CSEG delay255 00000200
CSEG Keypress 00000202
CSEG Inven1 00000211
CSEG Inven2 00000216
CSEG Inven3 0000021b
CSEG Inven4 00000220
CSEG Inven5 00000225
CSEG Part2 0000022a
CSEG STOCK 0000024e
CSEG Inven6 00000235
CSEG Inven7 0000023a
CSEG Inven8 0000023f
CSEG Inven9 00000244
CSEG Inven0 00000249
CSEG EMPTY 00000283
CSEG COIN 000002dd
CSEG colloop 00000254
CSEG return 00000281
CSEG delay 00000259
CSEG rowloop 00000262
CSEG nextcol 0000026b
CSEG convert 0000026f
CSEG symbols 0000027a
CSEG convert_end 0000027f
CSEG Remain 000002d2
CSEG Leave 000002dc
CSEG loop 0000031d
CSEG ITEM_SELECT 0000031e
EQU LCD_RS 00000007
EQU LCD_E 00000006
EQU LCD_RW 00000005
EQU LCD_BE 00000004
CSEG lcd_wait_loop 000001b3
CSEG lcd_wait_loop 00000333
EQU F_CPU 00f42400
EQU DELAY_1MS 00000f9c
CSEG delayloop_1ms 000001c3
CSEG inc_y 000001ce
CSEG inc_y_loop 000001d1
CSEG delayloop_1ms 00000343
Binary file not shown.

Large diffs are not rendered by default.

@@ -0,0 +1,90 @@
clear One
clear Two
clear Three
clear Four
clear Five
clear Six
clear Seven
clear Eight
clear Nine
clear Zero

lds temp, low(One)
ldi temp, 1
sts low(One), temp

lds temp, low(Two)
ldi temp, 2
sts low(Two), temp

lds temp, low(Three)
ldi temp, 3
sts low(Three), temp

lds temp, low(Four)
ldi temp, 4
sts low(Four), temp

lds temp, low(Five)
ldi temp, 5
sts low(Five), temp

lds temp, low(Six)
ldi temp, 6
sts low(Six), temp

lds temp, low(Seven)
ldi temp, 7
sts low(Seven), temp

lds temp, low(Eight)
ldi temp, 8
sts low(Eight), temp

lds temp, low(Nine)
ldi temp, 9
sts low(Nine), temp

lds temp, low(Zero)
ldi temp, 0
sts low(Zero), temp

lds temp, high(One)
ldi temp, 1
sts high(One), temp

lds temp, high(Two)
ldi temp, 2
sts high(Two), temp

lds temp, high(Three)
ldi temp, 1
sts high(Three), temp

lds temp, high(Four)
ldi temp, 2
sts high(Four), temp

lds temp, high(Five)
ldi temp, 1
sts high(Five), temp

lds temp, high(Six)
ldi temp, 2
sts high(Six), temp

lds temp, high(Seven)
ldi temp, 1
sts high(Seven), temp

lds temp, high(Eight)
ldi temp, 2
sts high(Eight), temp

lds temp, high(Nine)
ldi temp, 1
sts high(Nine), temp

lds temp, high(Zero)
ldi temp, 2
sts high(Zero), temp
@@ -0,0 +1,65 @@
KEYBOARD:
ldi cmask, INITCOLMASK ; initial column mask (1110 1111)
clr col ; initial column (0)

colloop:
cpi col, 4 ; compare current column # to total # columns
breq return ; if all keys are scanned, repeat

sts PORTL, cmask ; otherwise, scan a column
ldi temp1, 0xFF ; slow down the scan operation to debounce button press
delay:
dec temp1
brne delay

lds temp1, PINL ; read PORTL
andi temp1, ROWMASK ; get the keypad output value
cpi temp1, 0xF0 ; check if any row is low (0)
breq rowloop ; if yes, find which row is low
ldi rmask, INITROWMASK ; initialize rmask with 0000 0001 for row check
clr row

rowloop:
cpi row, 4 ; compare current value of row with total number of rows (4)
breq nextcol ; if theyre equal, the row scan is over.
mov temp2, temp1 ; temp1 is 0xF
and temp2, rmask ; check un-masked bit
breq convert ; if bit is clear, the key is pressed
inc row ; else move to the next row
lsl rmask ; shift row mask left by one
jmp rowloop

nextcol: ; if row scan is over
lsl cmask ; shift column mask left by one
inc col ; increase column value
jmp colloop ; go to the next column

convert:
cpi col, 3 ; if the pressed key is in col.3
breq return ; we have a letter, so ignore it and restart
cpi row, 3 ; if the key is not in col 3 and is in row3,
breq symbols ; we have a symbol or 0
mov temp1, row ; otherwise we have a number in 1-9
lsl temp1 ; multiply temp1 by 2
add temp1, row ; add row again to temp1 -> temp1 = row * 3
add temp1, col ; temp1 = col*3 + row
subi temp1, -1 ; add 1
; 1 row 0 col 0 -> temp1 = 0 + 0 + 1 = 1 = 0b 0000 0001
; 2 row 0 col 1 -> temp1 = 0 + 1 + 1 = 2 = 0b 0000 0010
; 3 row 0 col 2 -> temp1 = 0 + 2 + 1 = 3 = 0b 0000 0011
; 4 row 1 col 0 -> temp1 = 3 + 0 + 1 = 4 = 0b 0000 0100
jmp convert_end

symbols:
cpi col, 1 ; if its in column 1, it's a zero
brne return ; ignore * and #
ldi temp1, 255
jmp convert_end

convert_end:
mov flag, temp1
ret

return:
ldi flag, 0
ret
@@ -0,0 +1,77 @@
//--------LCD FUNCTIONS--------
.equ LCD_RS = 7
.equ LCD_E = 6
.equ LCD_RW = 5
.equ LCD_BE = 4

.macro lcd_set
sbi PORTA, @0
.endmacro
.macro lcd_clr
cbi PORTA, @0
.endmacro

;
; Send a command to the LCD (r16)
;

lcd_command:
out PORTF, lcd
rcall sleep_1ms
lcd_set LCD_E
rcall sleep_1ms
lcd_clr LCD_E
rcall sleep_1ms
ret
lcd_data:
out PORTF, lcd
lcd_set LCD_RS
rcall sleep_1ms
lcd_set LCD_E
rcall sleep_1ms
lcd_clr LCD_E
rcall sleep_1ms
lcd_clr LCD_RS
ret
lcd_wait:
push lcd
clr lcd
out DDRF, lcd
out PORTF, lcd
lcd_set LCD_RW
lcd_wait_loop:
rcall sleep_1ms
lcd_set LCD_E
rcall sleep_1ms
in lcd, PINF
lcd_clr LCD_E
sbrc lcd, 7
rjmp lcd_wait_loop
lcd_clr LCD_RW
ser lcd
out DDRF, lcd
pop lcd
ret

.equ F_CPU = 16000000
.equ DELAY_1MS = F_CPU / 4 / 1000 - 4
; 4 cycles per iteration - setup/call-return overhead

sleep_1ms:
push r24
push r25
ldi r25, high(DELAY_1MS)
ldi r24, low(DELAY_1MS)
delayloop_1ms:
sbiw r25:r24, 1
brne delayloop_1ms
pop r25
pop r24
ret
sleep_5ms:
rcall sleep_1ms
rcall sleep_1ms
rcall sleep_1ms
rcall sleep_1ms
rcall sleep_1ms
ret
@@ -0,0 +1,19 @@
; The macro clears a word (2 bytes) in a memory
; the parameter @0 is the memory address for that word
.macro clear
ldi YL, low(@0) ; load the memory address to Y
ldi YH, high(@0)
clr temp
st Y+, temp ; clear the two bytes at @0 in SRAM
st Y, temp
.endmacro
.macro do_lcd_command
ldi lcd, @0
rcall lcd_command
rcall lcd_wait
.endmacro
.macro do_lcd_data
ldi lcd, @0
rcall lcd_data
rcall lcd_wait
.endmacro

Large diffs are not rendered by default.

@@ -0,0 +1,174 @@
KEYBOARD:
ldi temp, 0b11 ;delete
out PORTC, temp ;delete

ldi cmask, INITCOLMASK ; initial column mask
clr col ; initial column

colloop:
cpi col, 4
breq cont ; If all keys are scanned, repeat.
sts PORTL, cmask ; Otherwise, scan a column.
delay:
ldi temp1, 0xFF ; Slow down the scan operation.
dec temp1
brne delay ; until temp1 is zero - delay

lds temp1, PINL ; Read PORTL
andi temp1, ROWMASK ; Get the keypad output value
cpi temp1, 0xF ; Check if any row is low
breq nextcol ; if not - switch to next column
; If yes, find which row is low
ldi rmask, INITROWMASK ; initialize for row check
clr row

rowloop: ; and going into the row loop
cpi row, 4 ; is row already 4?
breq nextcol ; the row scan is over - next column
mov temp2, temp1
and temp2, rmask ; check un-masked bit
breq convert ; if bit is clear, the key is pressed
inc row ; else move to the next row
lsl rmask
jmp rowloop
nextcol: ; if row scan is over
lsl cmask
inc col ; increase col value
jmp colloop ; go to the next column
convert:
ldi temp, 0b100 ;delete
out PORTC, temp ;delete
rjmp MAIN_MENU


//---------------------------------------------------
ldi flag, 0
ldi counter, 0
ldi boolean, 0

Start_screen:
push lcd
;push flag
push counter
ldi boolean, 1
rjmp KEYBOARD
RetSS:
pop counter
;pop flag
pop lcd
cpi boolean, 0
breq MAIN_MENU

Cont:
cpi flag, 1
breq Remain
rjmp Start_screen
Remain:
ldi flag, 0
inc counter
cpi counter, 3
breq Start_screen
rjmp MAIN_MENU


//------------------------------------------------------
KEYBOARD:
ldi cmask, INITCOLMASK ; initial column mask (1110 1111)
clr col ; initial column (0)

colloop:
cpi col, 4 ; compare current column # to total # columns
breq check ; if all keys are scanned, repeat

sts PORTL, cmask ; otherwise, scan a column
ldi temp1, 0xFF ; slow down the scan operation to debounce button press
delay:
dec temp1
brne delay

lds temp1, PINL ; read PORTL
andi temp1, ROWMASK ; get the keypad output value
cpi temp1, 0xF0 ; check if any row is low (0)
breq rowloop ; if yes, find which row is low
ldi rmask, INITROWMASK ; initialize rmask with 0000 0001 for row check
clr row

rowloop:
cpi row, 4 ; compare current value of row with total number of rows (4)
breq nextcol ; if theyre equal, the row scan is over.
mov temp2, temp1 ; temp1 is 0xF
and temp2, rmask ; check un-masked bit
breq convert ; if bit is clear, the key is pressed
inc row ; else move to the next row
lsl rmask ; shift row mask left by one
jmp rowloop

nextcol: ; if row scan is over
lsl cmask ; shift column mask left by one
inc col ; increase column value
jmp colloop ; go to the next column

convert:
cpi boolean, 1
breq change

cpi col, 3 ; if the pressed key is in col.3
breq KEYBOARD ; we have a letter, so ignore it and restart
cpi row, 3 ; if the key is not in col 3 and is in row3,
breq symbols ; we have a symbol or 0
mov temp1, row ; otherwise we have a number in 1-9
lsl temp1 ; multiply temp1 by 2
add temp1, row ; add row again to temp1 -> temp1 = row * 3
add temp1, col ; temp1 = col*3 + row
subi temp1, -1 ; add 1
; 1 row 0 col 0 -> temp1 = 0 + 0 + 1 = 1 = 0b 0000 0001
; 2 row 0 col 1 -> temp1 = 0 + 1 + 1 = 2 = 0b 0000 0010
; 3 row 0 col 2 -> temp1 = 0 + 2 + 1 = 3 = 0b 0000 0011
; 4 row 1 col 0 -> temp1 = 3 + 0 + 1 = 4 = 0b 0000 0100
jmp convert_end

symbols:
cpi col, 1 ; if its in column 1, it's a zero
brne KEYBOARD ; ignore * and #
clr temp1
jmp convert_end

check: ;check to see if KEYBOARD was jumped too from the start screen
cpi boolean, 1
breq return
rjmp KEYBOARD

change:
ldi boolean, 0 ;a key has been pressed

return: ;return from jmp
rjmp RetSS

convert_end:
out PORTC, temp1 ; write value to LEDs
cpi temp, 1
breq Inven1
cpi temp, 2
breq Inven2
cpi temp, 3
breq Inven3
cpi temp, 4
breq Inven4
cpi temp, 5
breq Inven5
cpi temp, 6
breq Inven6
cpi temp, 7
breq Inven7
cpi temp, 8
breq Inven8
cpi temp, 9
breq Inven9
cpi temp, 0
breq Inven0

jmp KEYBOARD ; restart main loop
BIN +22.4 KB VM.zip
Binary file not shown.
BIN +22.9 KB VendingMachine.zip
Binary file not shown.