Code VXL LAB1
Code VXL LAB1
ldi r19,0xf0 ;
and r19,r17 ;Lay 2 nibble cao
swap r19 ;Doi 2 nibble trong r19 cho nhau
sbrc r17,7 ;Neu bit so 7 cua r17 la 0 thi bo qua lenh duoi
or r19,r20 ;Chuyen r18 ve so co dau
loop:
cbi porta,1 ;Led off
sbis pina,0 ;Neu sw dang nha thi bo qua lenh duoi
sbi porta,1 ;Led on
rcall delay_50ms ;Delay de on dinh
rjmp loop ;Vong lap loop
lab1_2
2._b .include"m324PAdef.inc"
.org 00
ldi r16,0x01
out DDRA, r16
start:
sbi PORTA,PINA0
call delay05ms
cbi PORTA, PINA0
call delay05ms
rjmp start
delay05ms: ;4*4*250
ldi r16,4
l1: ldi r17,250
l2: nop
dec r17
brne l2
dec r16
brne l1
ret
delay10ms:
ldi r18,20
l10: call delay05ms
dec r18
brne l10
ret
delay100ms:
ldi r18,200
l100: call delay05ms
dec r18
brne l100
ret
delay1s: ;8*250
ldi r18,8
l1000: ldi r19,250
l2000: call delay05ms
dec r19
brne l2000
dec r18
brne l1000
ret
lab1_3:
1....include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
;******************************* Program ID *********************************
.org INT_VECTORS_SIZE
dong1:
.db "TN VXL-AVR",0
dong2:
.db "Nhom: XX",0
reset_handler:
call LCD_Init
; display the first line of information
ldi ZH, high(dong1) ; point to the information that is to be displayed
ldi ZL, low(dong1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
ldi ZH, high(dong2) ; point to the information that is to be displayed
ldi ZL, low(dong2)
call LCD_Send_String
start:
rjmp start
LCD_Send_Data:
push r17
call LCD_wait_busy ;check if LCD is busy
mov r17,r16 ;save the command
; Set RS high to select data register
; Set RW low to write to LCD
andi r17,0xF0
ori r17,0x01
; Send data to LCD
out LCDPORT, r17
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
; Delay for command execution
;send the lower nibble
nop
swap r16
andi r16,0xF0
; Set RS high to select data register
; Set RW low to write to LCD
andi r16,0xF0
ori r16,0x01
; Send command to LCD
out LCDPORT, r16
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
pop r17
ret
LCD_wait_busy:
push r16
ldi r16, 0b00000111 ; set PA7-PA4 as input, PA2-PA0 as output
out LCDPORTDIR, r16
ldi r16,0b11110010 ; set RS=0, RW=1 for read the busy flag
out LCDPORT, r16
nop
LCD_wait_busy_loop:
sbi LCDPORT, LCD_EN
nop
nop
in r16, LCDPORTPIN
cbi LCDPORT, LCD_EN
nop
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
nop
andi r16,0x80
cpi r16,0x80
breq LCD_wait_busy_loop
ldi r16, 0b11110111 ; set PA7-PA4 as output, PA2-PA0 as output
out LCDPORTDIR, r16
ldi r16,0b00000000 ; set RS=0, RW=1 for read the busy flag
out LCDPORT, r16
pop r16
ret
DELAY_10MS: ;
push r16
push r17
ldi r16,106
l1:
ldi r17,250
;1MC
;1MC
l2: dec r17 ;1MC
brne l2 ;2/1MC
dec r16
;1MC
brne l1
pop r17
pop r16
ret
;2/1MC
;4MC
call LCD_Init
call SW_Init
call BARLED_Init
start:
call checkSWnoDB
call disp_BL
call disp_LCD
rjmp start
;////////////////////////LCD DISP///////////////////////
.org INT_VECTORS_SIZE
dong1:
.db "So Lan Bam:",0
disp_LCD:
push r16
push r17
push r18
push solanBam
; display the first line of information
ldi r16,0
ldi r17,0
call LCD_Move_Cursor
ldi ZH, high(dong1) ; point to the information that is to be displayed
ldi ZL, low(dong1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
ldi r17,0
ldi r18,0
hangtram:
cpi solanBam,100
brsh lonhontram
hangchuc:
cpi solanBam,10
brsh lonhonchuc
rjmp inraLCD
lonhonchuc:
subi solanBam,10
inc r17
rjmp hangchuc
lonhontram:
subi solanBam,100
inc r18
rjmp hangtram
inraLCD:
ldi r16,48
add r16,r18
call LCD_Send_Data
ldi r16,48
add r16,r17
call LCD_Send_Data
ldi r16,48
add r16,solanBam
call LCD_Send_Data
pop solanBam
pop r18
pop r17
pop r16
ret
;////////////////////////SW/////////////////////////////
SW_Init:
cbi ddrd, 0
sbi portd, 0
ret
checkSW:
rcall debounce
brts else
waitSW:
rcall debounce
brts pressed
rjmp waitSW
pressed:
inc solanBam
ret
else:
rjmp checkSW
debounce:
sbic pind,0 ;bit is clear=button is pressed
rjmp bitSet
call DELAY_10MS
sbic pind,0 ;button still pressed?
rjmp bitSet
set
ret
bitSet:
clt
ret
checkSWnoDB:
sbic pind,0
rjmp else1
waitSW1:
sbic pind,0
rjmp pressed1
rjmp waitSW1
pressed1:
inc solanBam
ret
else1:
rjmp checkSWnoDB
;////////////////////////BARLED//////////////////////////
BARLED_Init:
ldi r16,0xff
out ddrb,r16
ret
disp_BL:
out portb,solanBam
ret
;////////////////////////LCD/////////////////////////////
;init the LCD
;LCD_D7..LCD_D4 connect to PA7..PA4
;LCD_RS connect to PA0
;LCD_RW connect to PA1
;LCD_EN connect to PA2
.equ LCDPORT = PORTA ; Set signal port reg to PORTA
.equ LCDPORTDIR = DDRA ; Set signal port dir reg to PORTA
.equ LCDPORTPIN = PINA ; Set clear signal port pin reg to PORTA
.equ LCD_RS = PINA0
.equ LCD_RW = PINA1
.equ LCD_EN = PINA2
.equ LCD_D7 = PINA7
.equ LCD_D6 = PINA6
.equ LCD_D5 = PINA5
.equ LCD_D4 = PINA4
LCD_Init:
; Set up data direction register for Port A
ldi r16, 0b11110111 ; set PA7-PA4 as outputs, PA2-PA0 as output
out LCDPORTDIR, r16
; Wait for LCD to power up
call DELAY_10MS
call DELAY_10MS
; Send initialization sequence
ldi r16, 0x02 ; Function Set: 4-bit interface
call LCD_Send_Command
ldi r16, 0x28 ; Function Set: enable 5x7 mode for chars
call LCD_Send_Command
ldi r16, 0x0E ; Display Control: Display OFF, Cursor ON
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; Clear Display
call LCD_Send_Command
ret
LCD_Send_Data:
push r17
call LCD_wait_busy ;check if LCD is busy
mov r17,r16 ;save the command
; Set RS high to select data register
; Set RW low to write to LCD
andi r17,0xF0
ori r17,0x01
; Send data to LCD
out LCDPORT, r17
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
; Delay for command execution
;send the lower nibble
nop
swap r16
andi r16,0xF0
; Set RS high to select data register
; Set RW low to write to LCD
andi r16,0xF0
ori r16,0x01
; Send command to LCD
out LCDPORT, r16
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
pop r17
ret
LCD_wait_busy:
push r16
ldi r16, 0b00000111 ; set PA7-PA4 as input, PA2-PA0 as output
out LCDPORTDIR, r16
ldi r16,0b11110010 ; set RS=0, RW=1 for read the busy flag
out LCDPORT, r16
nop
LCD_wait_busy_loop:
sbi LCDPORT, LCD_EN
nop
nop
in r16, LCDPORTPIN
cbi LCDPORT, LCD_EN
nop
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
nop
andi r16,0x80
cpi r16,0x80
breq LCD_wait_busy_loop
ldi r16, 0b11110111 ; set PA7-PA4 as output, PA2-PA0 as output
out LCDPORTDIR, r16
ldi r16,0b00000000 ; set RS=0, RW=1 for read the busy flag
out LCDPORT, r16
pop r16
ret
DELAY_10MS: ;
push r16
push r17
ldi r16,106 ;1MC
l1: ldi r17,250 ;1MC
l2: dec r17 ;1MC
brne l2 ;2/1MC
dec r16 ;1MC
brne l1 ;2/1MC
pop r17
pop r16
ret ;4MC
3.
.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
jmp reset ; reset
.org 0x000E ; pin change interrupt vector
rjmp pinchange ; Chuong trinh ngat
;******************************* Program ID *********************************
.def sttphimBam = r24
.org 0x0020
reset:
ldi sttphimBam, 0
call LCD_Init
call SW_Init
call BARLED_Init
call INT_Init
start:
rjmp start
pinchange:
push r16
in r16, PIND
cpi r16, 0xf0
breq pinchange_handler_exit;
call scankey ;scan and shiftout keycode
cpi r24, 0xff
breq pinchange_handler_exit;
call disp_LCD
call disp_BL
pinchange_handler_exit:
pop r16
call SW_Init
reti ; return from interrupt
;////////////////////////LCD DISP///////////////////////
.org INT_VECTORS_SIZE
dong1:
.db "Da bam phim:",0
disp_LCD:
push r16
push r17
push r18
push sttphimBam
; display the first line of information
ldi r16,0
ldi r17,0
call LCD_Move_Cursor
ldi ZH, high(dong1) ; point to the information that is to be displayed
ldi ZL, low(dong1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
ldi r17,0
ldi r18,0
hangtram:
cpi sttphimBam,100
brsh lonhontram
hangchuc:
cpi sttphimBam,10
brsh lonhonchuc
rjmp inraLCD
lonhonchuc:
subi sttphimBam,10
inc r17
rjmp hangchuc
lonhontram:
subi sttphimBam,100
inc r18
rjmp hangtram
inraLCD:
ldi r16,48
add r16,r18
call LCD_Send_Data
ldi r16,48
add r16,r17
call LCD_Send_Data
ldi r16,48
add r16,sttphimBam
call LCD_Send_Data
pop sttphimBam
pop r18
pop r17
pop r16
ret
;////////////////////////INTERUPT PinChange////////////////////
INT_Init:
;PinChange cho PortC 7-4
ldi r16, 0xf0
sts PCMSK3, r16
ldi r16, (1<<PCIE3)
sts PCICR, r16
sei
ret
;////////////////////////Matrix SW/////////////////////////////
SW_Init:
ldi r16,0x0f ; PC7-4 input row 3-0, 3-0 output col 3-0
out ddrd, r16
ldi r16,0xf0
out portd,r16 ; Dien tro keo len
ret
scanKey:
push r22
push r23
;////////////////////////BARLED//////////////////////////
BARLED_Init:
ldi r16,0xff
out ddrb,r16
ret
disp_BL:
out portb,sttphimBam
ret
;////////////////////////LCD/////////////////////////////
;init the LCD
;LCD_D7..LCD_D4 connect to PA7..PA4
;LCD_RS connect to PA0
;LCD_RW connect to PA1
;LCD_EN connect to PA2
.equ LCDPORT = PORTA ; Set signal port reg to PORTA
.equ LCDPORTDIR = DDRA ; Set signal port dir reg to PORTA
.equ LCDPORTPIN = PINA ; Set clear signal port pin reg to PORTA
.equ LCD_RS = PINA0
.equ LCD_RW = PINA1
.equ LCD_EN = PINA2
.equ LCD_D7 = PINA7
.equ LCD_D6 = PINA6
.equ LCD_D5 = PINA5
.equ LCD_D4 = PINA4
LCD_Init:
; Set up data direction register for Port A
ldi r16, 0b11110111 ; set PA7-PA4 as outputs, PA2-PA0 as output
out LCDPORTDIR, r16
; Wait for LCD to power up
call DELAY_10MS
call DELAY_10MS
; Send initialization sequence
ldi r16, 0x02 ; Function Set: 4-bit interface
call LCD_Send_Command
ldi r16, 0x28 ; Function Set: enable 5x7 mode for chars
call LCD_Send_Command
ldi r16, 0x0E ; Display Control: Display OFF, Cursor ON
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; Clear Display
call LCD_Send_Command
ret
LCD_Send_Data:
push r17
call LCD_wait_busy ;check if LCD is busy
mov r17,r16 ;save the command
; Set RS high to select data register
; Set RW low to write to LCD
andi r17,0xF0
ori r17,0x01
; Send data to LCD
out LCDPORT, r17
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
; Delay for command execution
;send the lower nibble
nop
swap r16
andi r16,0xF0
; Set RS high to select data register
; Set RW low to write to LCD
andi r16,0xF0
ori r16,0x01
; Send command to LCD
out LCDPORT, r16
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
pop r17
ret
LCD_wait_busy:
push r16
ldi r16, 0b00000111 ; set PA7-PA4 as input, PA2-PA0 as output
out LCDPORTDIR, r16
ldi r16,0b11110010 ; set RS=0, RW=1 for read the busy flag
out LCDPORT, r16
nop
LCD_wait_busy_loop:
sbi LCDPORT, LCD_EN
nop
nop
in r16, LCDPORTPIN
cbi LCDPORT, LCD_EN
nop
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
nop
andi r16,0x80
cpi r16,0x80
breq LCD_wait_busy_loop
ldi r16, 0b11110111 ; set PA7-PA4 as output, PA2-PA0 as output
out LCDPORTDIR, r16
ldi r16,0b00000000 ; set RS=0, RW=1 for read the busy flag
out LCDPORT, r16
pop r16
ret
DELAY_1MS: ;
push r16
push r17
ldi r16,10 ;1MC
l11: ldi r17,250 ;1MC
l22: dec r17 ;1MC
brne l22 ;2/1MC
dec r16 ;1MC
brne l11 ;2/1MC
pop r17
pop r16
ret ;4MC
DELAY_10MS: ;
push r16
push r17
ldi r16,106 ;1MC
l1: ldi r17,250 ;1MC
l2: dec r17 ;1MC
brne l2 ;2/1MC
dec r16 ;1MC
brne l1 ;2/1MC
pop r17
pop r16
ret ;4MC