0% found this document useful (0 votes)
5 views

PRELAB1_3

The document outlines a laboratory exercise focused on interfacing buttons and matrix keyboards with an AVR microcontroller, including LCD communication. It provides detailed instructions for connecting components, initializing the LCD, and programming to count button presses while displaying results on both a bar LED and an LCD. The document includes sample assembly code for both tasks, emphasizing the importance of debouncing and proper communication protocols.

Uploaded by

bach.mai3008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PRELAB1_3

The document outlines a laboratory exercise focused on interfacing buttons and matrix keyboards with an AVR microcontroller, including LCD communication. It provides detailed instructions for connecting components, initializing the LCD, and programming to count button presses while displaying results on both a bar LED and an LCD. The document includes sample assembly code for both tasks, emphasizing the importance of debouncing and proper communication protocols.

Uploaded by

bach.mai3008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

LAB 1-3

GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN

MỤC TIÊU:

 Hiểu cách chống rung phím


 Hiểu cách giao tiếp LCD
 Hiểu cách giao tiếp phím đơn
 Hiểu cách giao tiếp bàn phím ma trận

THAM KHẢO:

 Tài liệu hướng dẫn thí nghiệm, chương 1, 2 , 3 ,6

BÀI 1

a) Kết nối một PORT của AVR vào J33 (Header điều khiển LCD) trên kit thí nghiệm.
b) Dùng các chương trình mẫu trong tài liệu hướng dẫn thí nghiệm, viết chương trình khởi
động LCD và xuất lên LCD như sau. (XX là số nhóm)

TN VXL-AVR
Nhom: XX

;BAI1
.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
;******************************* Program ID *********************************
.org INT_VECTORS_SIZE
line1: .db "TN VXL-AVR ",0
line2: .db "Nhom: XX ",0
;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

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
.equ LCD_D5 = PINA5
.equ LCD_D4 = PINA4

reset_handler:
call LCD_Init
; display the first line of information
ldi ZH, high(line1) ; point to the information that is to be displayed
ldi ZL, low(line1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
ldi ZH, high(line2) ; point to the information that is to be displayed
ldi ZL, low(line2)
call LCD_Send_String
start:
rjmp start

; Function to move the cursor to a specific position on the LCD


; Assumes that the LCD is already initialized
; Input: Row number in R16 (0-based), Column number in R17 (0-based)
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80 ;CHUYEN CON TRO VE DONG 2, VI TRI TRONG BYTE THAP R17
mov r16,r17 ;CHUYEN LENH VAO R16 DE XUAT
; Send command to LCD
call LCD_Send_Command ;XUAT LENH TRONG R16
ret
LCD_Move_Cursor_Second:
cpi r16,1 ;check if second row
brne LCD_Move_Cursor_Exit ;else exit
andi r17, 0x0F
ori r17,0xC0
mov r16,r17
; Send command to LCD
call LCD_Send_Command
LCD_Move_Cursor_Exit:
; Return from function
ret

;Subroutine to send string to LCD


;address of the string on ZH-ZL
;string end with Null
.def LCDData = r16
LCD_Send_String:
push ZH ; preserve pointer registers
push ZL
push LCDData
; fix up the pointers for use with the 'lpm' instruction
lsl ZL ; shift the pointer one bit left for the lpm instruction
rol ZH
; write the string of characters

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
LCD_Send_String_01:
lpm LCDData, Z+ ; get a character
cpi LCDData, 0 ; check for end of string
breq LCD_Send_String_02 ; done
; arrive here if this is a valid character
call LCD_Send_Data ; display the character
rjmp LCD_Send_String_01 ; not done, send another character
; arrive here when all characters in the message have been sent to the LCD module
LCD_Send_String_02:
pop LCDData
pop ZL ; restore pointer registers
pop ZH
ret

; Subroutine to send command to LCD


;Command code in r16
;LCD_D7..LCD_D4 connect to PA7..PA4
;LCD_RS connect to PA0
;LCD_RW connect to PA1
;LCD_EN connect to PA2
LCD_Send_Command:
push r17
call LCD_wait_busy ; check if LCD is busy
mov r17,r16 ;save the command
; Set RS low to select command register
; Set RW low to write to LCD
andi r17,0xF0
; Send command to LCD
out LCDPORT, r17
nop
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
swap r16
andi r16,0xF0
; Send command to LCD
out LCDPORT, r16
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
pop r17
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

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
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

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
pop r16
ret

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 DELAY10MS
call DELAY10MS
; Send initialization sequence
ldi r16, 0x02 ; RETURN HOME
call LCD_Send_Command
ldi r16, 0x28 ; Function Set: GIAO TIẾP 4 BIT CAO, 2 DONG, 5X8 DOT
call LCD_Send_Command
ldi r16, 0x0E ; Display Control: MAN HINH BAT, CON TRO NHAP NHAY TAT
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; CHUYEN CON TRO VE DAU DONG 1
call LCD_Send_Command
ret

DELAY1MS: LDI R16, 8 ;1MC


LP1: LDI R17, 250 ;1MC
LP2: DEC R17 ;1MC
NOP ;1MC
BRNE LP2;2/1MC
DEC R16 ;1MC
BRNE LP1;2/1MC
RET ;4MC

DELAY10MS: LDI R18, 10


LP3: CALL DELAY1MS
DEC R18
BRNE LP3
RET

BÀI 2

c) Kết nối 1 switch đến 1 chân port của AVR, kết nối module BAR LED đến 1 port của
AVR, kết nối LCD đến 1 port của AVR

d) Viết chương trình đếm số lần nhấn nút và xuất kết quả ra barled, đồng thời xuất ra LCD
(không chống rung)

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
;BAI2
.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
;******************************* Program ID *********************************
.org INT_VECTORS_SIZE
line1: .db "SO LAN NHAN NUT: ",0
;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

reset_handler:
LDI R16, 0xFF
OUT DDRB, R16 ; PORTB - BAR LED
CBI DDRC, 0; PINC0 - NUT NHAN
SBI PORTC, 0; DIEN TRO KEO LEN
call LCD_Init
; display the first line of information
LDI R18, 0 ; BIEN DEM SO LAN NHAN
LOOP:
OUT PORTB, R18 ;XUAT RA BARLED
ldi ZH, high(line1) ; point to the information that is to be displayed
ldi ZL, low(line1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
MOV R17, R18
RCALL CHUYENTHANHBCD ;NIBBLE CAO R17 / THAP R16
MOV R19, R16
SUBI R17, -48 ; CHUYEN THANH ASCII
MOV R16, R17
RCALL LCD_Send_Data
SUBI R19, -48 ; CHUYEN THANH ASCII
MOV R16, R19
RCALL LCD_Send_Data

KIEMTRANUTNHAN:
SBIC PINC, 0
RJMP KIEMTRANUTNHAN
INC R18 ; TANG BIEN DEM
WAITBUT:

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
SBIS PINC, 0
RJMP WAITBUT
LDI R16, 0x01 ; XOA MAN HINH
CALL LCD_Send_Command
RJMP LOOP

; Function to move the cursor to a specific position on the LCD


; Assumes that the LCD is already initialized
; Input: Row number in R16 (0-based), Column number in R17 (0-based)
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80 ;CHUYEN CON TRO VE DONG 2, VI TRI TRONG BYTE THAP R17
mov r16,r17 ;CHUYEN LENH VAO R16 DE XUAT
; Send command to LCD
call LCD_Send_Command ;XUAT LENH TRONG R16
ret
LCD_Move_Cursor_Second:
cpi r16,1 ;check if second row
brne LCD_Move_Cursor_Exit ;else exit
andi r17, 0x0F
ori r17,0xC0
mov r16,r17
; Send command to LCD
call LCD_Send_Command
LCD_Move_Cursor_Exit:
; Return from function
ret

;Subroutine to send string to LCD


;address of the string on ZH-ZL
;string end with Null
.def LCDData = r16
LCD_Send_String:
push ZH ; preserve pointer registers
push ZL
push LCDData
; fix up the pointers for use with the 'lpm' instruction
lsl ZL ; shift the pointer one bit left for the lpm instruction
rol ZH
; write the string of characters
LCD_Send_String_01:
lpm LCDData, Z+ ; get a character
cpi LCDData, 0 ; check for end of string
breq LCD_Send_String_02 ; done
; arrive here if this is a valid character
call LCD_Send_Data ; display the character
rjmp LCD_Send_String_01 ; not done, send another character
; arrive here when all characters in the message have been sent to the LCD module
LCD_Send_String_02:
pop LCDData
pop ZL ; restore pointer registers
pop ZH

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
ret

; Subroutine to send command to LCD


;Command code in r16
;LCD_D7..LCD_D4 connect to PA7..PA4
;LCD_RS connect to PA0
;LCD_RW connect to PA1
;LCD_EN connect to PA2
LCD_Send_Command:
push r17
call LCD_wait_busy ; check if LCD is busy
mov r17,r16 ;save the command
; Set RS low to select command register
; Set RW low to write to LCD
andi r17,0xF0
; Send command to LCD
out LCDPORT, r17
nop
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
swap r16
andi r16,0xF0
; Send command to LCD
out LCDPORT, r16
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
pop r17
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

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
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

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 DELAY10MS
call DELAY10MS
; Send initialization sequence

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
ldi r16, 0x02 ; RETURN HOME
call LCD_Send_Command
ldi r16, 0x28 ; Function Set: GIAO TIẾP 4 BIT CAO, 2 DONG, 5X8 DOT
call LCD_Send_Command
ldi r16, 0x0E ; Display Control: MAN HINH BAT, CON TRO NHAP NHAY TAT
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; CHUYEN CON TRO VE DAU DONG 1
call LCD_Send_Command
ret

DELAY1MS: LDI R16, 8 ;1MC


LP1: LDI R17, 250 ;1MC
LP2: DEC R17 ;1MC
NOP ;1MC
BRNE LP2;2/1MC
DEC R16 ;1MC
BRNE LP1;2/1MC
RET ;4MC

DELAY10MS: LDI R18, 10


LP3: CALL DELAY1MS
DEC R18
BRNE LP3
RET

;SO CAN CHUYEN:R17


;NIBBLE CAO R17
;NIBBLE THAP R16
CHUYENTHANHBCD:
PUSH R15
CLR R15
LDI R16, 10
LP:
SUB R17, R16
BRCS FINAL
INC R15
RJMP LP
FINAL:
ADD R17, R16
MOV R16, R17
MOV R17, R15
POP R15
RET

e) Thêm tính năng chống rung phím vào chương trình


;BAI2E
.include "m324padef.inc" ; Include Atmega324pa definitions

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
;******************************* Program ID *********************************
.org INT_VECTORS_SIZE
line1: .db "SO LAN NHAN NUT: ",0
;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

reset_handler:
LDI R16, 0xFF
OUT DDRB, R16 ; PORTB - BAR LED
CBI DDRC, 0; PINC0 - NUT NHAN
SBI PORTC, 0; DIEN TRO KEO LEN
call LCD_Init
; display the first line of information
LDI R18, 0 ; BIEN DEM SO LAN NHAN
LOOP:
OUT PORTB, R18 ;XUAT RA BARLED
ldi ZH, high(line1) ; point to the information that is to be displayed
ldi ZL, low(line1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
MOV R17, R18
RCALL CHUYENTHANHBCD ;NIBBLE CAO R17 / THAP R16
MOV R19, R16
SUBI R17, -48 ; CHUYEN THANH ASCII
MOV R16, R17
RCALL LCD_Send_Data
SUBI R19, -48 ; CHUYEN THANH ASCII
MOV R16, R19
RCALL LCD_Send_Data

KIEMTRANUTNHAN:
SBIC PINC, 0
RJMP KIEMTRANUTNHAN
RCALL DELAY100MS
SBIC PINC, 0
RJMP KIEMTRANUTNHAN
INC R18 ; TANG BIEN DEM

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
WAITBUT:
SBIS PINC, 0
RJMP WAITBUT
LDI R16, 0x01 ; XOA MAN HINH
CALL LCD_Send_Command
RJMP LOOP

; Function to move the cursor to a specific position on the LCD


; Assumes that the LCD is already initialized
; Input: Row number in R16 (0-based), Column number in R17 (0-based)
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80 ;CHUYEN CON TRO VE DONG 2, VI TRI TRONG BYTE THAP R17
mov r16,r17 ;CHUYEN LENH VAO R16 DE XUAT
; Send command to LCD
call LCD_Send_Command ;XUAT LENH TRONG R16
ret
LCD_Move_Cursor_Second:
cpi r16,1 ;check if second row
brne LCD_Move_Cursor_Exit ;else exit
andi r17, 0x0F
ori r17,0xC0
mov r16,r17
; Send command to LCD
call LCD_Send_Command
LCD_Move_Cursor_Exit:
; Return from function
ret

;Subroutine to send string to LCD


;address of the string on ZH-ZL
;string end with Null
.def LCDData = r16
LCD_Send_String:
push ZH ; preserve pointer registers
push ZL
push LCDData
; fix up the pointers for use with the 'lpm' instruction
lsl ZL ; shift the pointer one bit left for the lpm instruction
rol ZH
; write the string of characters
LCD_Send_String_01:
lpm LCDData, Z+ ; get a character
cpi LCDData, 0 ; check for end of string
breq LCD_Send_String_02 ; done
; arrive here if this is a valid character
call LCD_Send_Data ; display the character
rjmp LCD_Send_String_01 ; not done, send another character
; arrive here when all characters in the message have been sent to the LCD module
LCD_Send_String_02:
pop LCDData
pop ZL ; restore pointer registers

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
pop ZH
ret

; Subroutine to send command to LCD


;Command code in r16
;LCD_D7..LCD_D4 connect to PA7..PA4
;LCD_RS connect to PA0
;LCD_RW connect to PA1
;LCD_EN connect to PA2
LCD_Send_Command:
push r17
call LCD_wait_busy ; check if LCD is busy
mov r17,r16 ;save the command
; Set RS low to select command register
; Set RW low to write to LCD
andi r17,0xF0
; Send command to LCD
out LCDPORT, r17
nop
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
swap r16
andi r16,0xF0
; Send command to LCD
out LCDPORT, r16
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
pop r17
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

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
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

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 DELAY10MS
call DELAY10MS

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
; Send initialization sequence
ldi r16, 0x02 ; RETURN HOME
call LCD_Send_Command
ldi r16, 0x28 ; Function Set: GIAO TIẾP 4 BIT CAO, 2 DONG, 5X8 DOT
call LCD_Send_Command
ldi r16, 0x0E ; Display Control: MAN HINH BAT, CON TRO NHAP NHAY TAT
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; CHUYEN CON TRO VE DAU DONG 1
call LCD_Send_Command
ret

DELAY1MS: LDI R16, 8 ;1MC


LP1: LDI R17, 250 ;1MC
LP2: DEC R17 ;1MC
NOP ;1MC
BRNE LP2;2/1MC
DEC R16 ;1MC
BRNE LP1;2/1MC
RET ;4MC

DELAY10MS:
PUSH R18
LDI R18, 10
LP3: CALL DELAY1MS
DEC R18
BRNE LP3
POP R18
RET

DELAY100MS:
PUSH R18
LDI R18, 100
LP4: CALL DELAY1MS
DEC R18
BRNE LP3
POP R18
RET

;SO CAN CHUYEN:R17


;NIBBLE CAO R17
;NIBBLE THAP R16
CHUYENTHANHBCD:
PUSH R15
CLR R15
LDI R16, 10
LP:
SUB R17, R16
BRCS FINAL
INC R15
RJMP LP
FINAL:

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
ADD R17, R16
MOV R16, R17
MOV R17, R15
POP R15
RET

f) Thực hiện chương trình, nhấn/nhả nút và quan sát kết quả

BÀI 3

a) Kết nối tín hiệu từ một port của AVR đến module bàn phím ma trận , kết nối module
BAR LED và LCD đến 2 port khác của AVR.

b) Viết chương trình con SCANKEY để quét bàn phím ma trận và trả về giá trị từ 0x0 đến
0xF ứng với mã của phím được nhấn. Nếu không có phím nào được nhấn trả về giá trị
0xFF. Giá trị trả về chứa trong R24
;BAI3B
keypad_scan:
ldi r20, 0b00001111 ; set upper 4 bits of PORTD as input with pull-up, lower 4 bits as output
out DDRD, r20
ldi r20, 0b11111111 ; enable pull up resistor
out PORTD, r20
ldi r22, 0b11110111 ; initial col mask
ldi r23, 0 ; initial pressed row value
ldi r24,3 ;scanning col index
keypad_scan_loop:
out PORTD, r22 ; scan current col
nop ;need to have 1us delay to stablize
sbic PIND, 4 ; check row 0
rjmp keypad_scan_check_col2
rjmp keypad_scan_found ; row 0 is pressed
keypad_scan_check_col2:
sbic PIND, 5 ; check row 1
rjmp keypad_scan_check_col3
ldi r23, 1 ; row1 is pressed
rjmp keypad_scan_found
keypad_scan_check_col3:
sbic PIND, 6 ; check row 2
rjmp keypad_scan_check_col4
ldi r23, 2 ; row 2 is pressed
rjmp keypad_scan_found
keypad_scan_check_col4:
sbic PIND, 7 ; check row 3
rjmp keypad_scan_next_row
ldi r23, 3 ; row 3 is pressed

https://doe.dee.hcmut.edu.vn/
LAB 1-3
GIAO TIẾP NÚT NHẤN, BÀN PHÍM MA TRẬN
rjmp keypad_scan_found
keypad_scan_next_row:
; check if all rows have been scanned
cpi r24,0
breq keypad_scan_not_found
; shift row mask to scan next row
ror r22
dec r24 ;increase row index
rjmp keypad_scan_loop
keypad_scan_found:
; combine row and column to get key value (0-15)
;key code = row*4 + col
lsl r23 ; shift row value 4 bits to the left
lsl r23
add r24, r23 ; add row value to column value
ret
keypad_scan_not_found:
ldi r24, 0xFF ; no key pressed
ret

c) Dùng chương trình con này, viết chương trình thực hiện việc quét phím và xuất giá trị
đọc được lên bar led và LCD.

d) Thực hiện chương trình, quan sát kết quả

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:

BÀI 1
1. Trả lời các câu hỏi
a. LCD phân biệt command và data bằng cách nào?
b. Ngoài cách đọc bit BUSY, còn cách nào để đảm bảo là LCD rảnh khi gửi dữ
liệu/command?
c. Mô tả kết nối trên kit thí nghiệm.
d. Mã nguồn chương trình với chú thích
a) Khi RS = 0 thì dữ liệu ghi vào LCD được hiểu là command và sẽ được
lưu vào thanh ghi lệnh. Khi RS = 0 thì dữ liệu ghi vào LCD được hiểu là
data và sẽ được lưu vào thanh ghi dữ liệu.
b) Ngoài cách đọc bit BUSY, còn cách làm chương trình chờ khoảng 2 ms
trước khi thực hiện lệnh kế tiếp để đảm bảo là LCD rảnh khi gửi dữ
liệu/command
c) Mô tả kết nối trên kit:
- PA0: RS
- PA1: RW
- PA2: E
- PA4-7: D4-7

;BAI1
.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
;******************************* Program ID *********************************
.org INT_VECTORS_SIZE
line1: .db "TN VXL-AVR ",0
line2: .db "Nhom: XX ",0
;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

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:

.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

reset_handler:
call LCD_Init
; display the first line of information
ldi ZH, high(line1) ; point to the information that is to be displayed
ldi ZL, low(line1)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
ldi ZH, high(line2) ; point to the information that is to be displayed
ldi ZL, low(line2)
call LCD_Send_String
start:
rjmp start

; Function to move the cursor to a specific position on the LCD


; Assumes that the LCD is already initialized
; Input: Row number in R16 (0-based), Column number in R17 (0-based)
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80 ;CHUYEN CON TRO VE DONG 2, VI TRI TRONG BYTE THAP R17
mov r16,r17 ;CHUYEN LENH VAO R16 DE XUAT
; Send command to LCD
call LCD_Send_Command ;XUAT LENH TRONG R16
ret
LCD_Move_Cursor_Second:
cpi r16,1 ;check if second row
brne LCD_Move_Cursor_Exit ;else exit
andi r17, 0x0F
ori r17,0xC0
mov r16,r17
; Send command to LCD
call LCD_Send_Command
LCD_Move_Cursor_Exit:
; Return from function
ret

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:

;Subroutine to send string to LCD


;address of the string on ZH-ZL
;string end with Null
.def LCDData = r16
LCD_Send_String:
push ZH ; preserve pointer registers
push ZL
push LCDData
; fix up the pointers for use with the 'lpm' instruction
lsl ZL ; shift the pointer one bit left for the lpm instruction
rol ZH
; write the string of characters
LCD_Send_String_01:
lpm LCDData, Z+ ; get a character
cpi LCDData, 0 ; check for end of string
breq LCD_Send_String_02 ; done
; arrive here if this is a valid character
call LCD_Send_Data ; display the character
rjmp LCD_Send_String_01 ; not done, send another character
; arrive here when all characters in the message have been sent to the LCD module
LCD_Send_String_02:
pop LCDData
pop ZL ; restore pointer registers
pop ZH
ret

; Subroutine to send command to LCD


;Command code in r16
;LCD_D7..LCD_D4 connect to PA7..PA4
;LCD_RS connect to PA0
;LCD_RW connect to PA1
;LCD_EN connect to PA2
LCD_Send_Command:
push r17
call LCD_wait_busy ; check if LCD is busy
mov r17,r16 ;save the command
; Set RS low to select command register
; Set RW low to write to LCD
andi r17,0xF0
; Send command to LCD
out LCDPORT, r17
nop
nop
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
swap r16
andi r16,0xF0

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:

; Send command to LCD


out LCDPORT, r16
; Pulse enable pin
sbi LCDPORT, LCD_EN
nop
nop
cbi LCDPORT, LCD_EN
pop r17
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

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:

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

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 DELAY10MS
call DELAY10MS
; Send initialization sequence
ldi r16, 0x02 ; RETURN HOME
call LCD_Send_Command
ldi r16, 0x28 ; Function Set: GIAO TIẾP 4 BIT CAO, 2 DONG, 5X8 DOT
call LCD_Send_Command
ldi r16, 0x0E ; Display Control: MAN HINH BAT, CON TRO NHAP NHAY TAT
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; CHUYEN CON TRO VE DAU DONG 1
call LCD_Send_Command
ret

DELAY1MS: LDI R16, 8 ;1MC


LP1: LDI R17, 250 ;1MC
LP2: DEC R17 ;1MC
NOP ;1MC
BRNE LP2;2/1MC

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:

DEC R16 ;1MC


BRNE LP1;2/1MC
RET ;4MC

DELAY10MS: LDI R18, 10


LP3: CALL DELAY1MS
DEC R18
BRNE LP3
RET

BÀI 2
1. Trả lời các câu hỏi
a. Hiện tượng gì xảy ra khi không chống rung phím
b. Mô tả cách kết nối trên kit thí nghiệm
c. Mã nguồn chương trình không chống rung phím và chú thích

d. Mã nguồn chương trình có chống rung và chú thích

BÀI 3
1. Trả lời các câu hỏi
a. Cách kết nối các module trên bài thí nghiệm
b. Có hiện tượng rung phím đối với bàn phím ma trận hay không? Nếu có thì xử
lý bằng cách nào?
c. Trình bày mã nguồn chương trình và chú thích.

https://doe.dee.hcmut.edu.vn/

You might also like