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

LAB1_3

The document outlines a lab experiment focused on interfacing buttons and matrix keyboards with an AVR microcontroller, including understanding key debouncing and LCD communication. It provides detailed instructions for connecting components, writing initialization and display programs for an LCD, and implementing a button press counter with debouncing features. The lab aims to enhance practical skills in embedded systems programming and hardware interfacing.

Uploaded by

longpham201105
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

LAB1_3

The document outlines a lab experiment focused on interfacing buttons and matrix keyboards with an AVR microcontroller, including understanding key debouncing and LCD communication. It provides detailed instructions for connecting components, writing initialization and display programs for an LCD, and implementing a button press counter with debouncing features. The lab aims to enhance practical skills in embedded systems programming and hardware interfacing.

Uploaded by

longpham201105
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

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

.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
.equ LCDPORT = PORTA
.equ LCDPORTDIR = DDRA
; Set signal port reg to PORTA
; 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
.def LCDData = r16
;***************************** Program ID *******************************
.org INT_VECTORS_SIZE
course_name:
.db "TN Vi Xu Ly-AVR",0
course_group:
.db "L2023",0
;********************************MAIN******************************
reset_handler:
call LCD_Init

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

; display the first line of information


ldi ZH, high(course_name) ; point to the information that is to be displayed
ldi ZL, low(course_name)
call LCD_Send_String
ldi r16,1
ldi r17,0
call LCD_Move_Cursor
ldi ZH, high(course_group) ; point to the information that is to be displayed
ldi ZL, low(course_group)
call
LCD_Send_String
start:
rjmp start
;*******************************FUNCTION*****************************
*********
;************INIT************;
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
;*******************SEND CMD******************;
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

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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
;**************SEND DATA****************;
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

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

; Pulse enable pin


sbi LCDPORT, LCD_EN
nop
cbi LCDPORT, LCD_EN
pop r17
ret
;*************************SET_CURSOR*******************;
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80
mov r16,r17
; Send command to LCD
call LCD_Send_Command
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
;*******************SEND STRING****************;
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

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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
;***************LCD_WAIT_BUSY****************;
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
;*******************DELAY10MS******************;
DELAY_10MS:
LDI R16,10
LOOP2:
LDI R17,250
LOOP1:
NOP

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

DEC R17
BRNE LOOP1
DEC R16
BRNE LOOP2
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)
e) Thêm tính năng chống rung phím vào chương trình
.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
.equ LCDPORT =
; Set signal port reg to PORTA
PORTA
; Set signal port dir reg to
.equ LCDPORTDIR =
PORTA
DDRA
.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
.def LCDData = r16
.def COUNTPress = R20
;button sw -> PB0
;bar_led -> PORTC
;******************************* Program ID
*********************************
;********************************MAIN******************************
reset_handler:
call LCD_Init
CLR R20
cbi DDRB, 0
SBI PORTB, 0
ldi r16,0xFF
out DDRC,R16
ldi r16, $30
mov r10, r16

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

;START UP DISPLAY LCD


LDI R16, $30
ldi ZL, $0
ldi ZH, $7
ST Z+, R16
ST Z+, R16
ST Z+, R16
ldi ZL, $0
ldi ZH, $7
CLR R16
LDI R17, 13
CALL LCD_Move_Cursor
CALL LCD_Send_String
; display the first line of information
start:
RCALL BUTTON
push R20
LDI R21, 10
CLR R22
L1: INC R22
SUB R20, R21
BRCC L1
DEC R22
ADD R20, R21
ADD r20, r10
MOV R19, R20
MOV R20, R22
CLR R22
L2: INC R22
SUB R20, R21
BRCC L2
DEC R22
ADD R20, R21
ADD r20, r10
ADD r22, r10
// 3 DIGIT IN DISPLAY R19, R20, R22
ST Z+, R22
ST Z+, R20
ST Z+, R19
clr R20
ST Z+, R20
ldi ZL, $0
ldi ZH, $7

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

CLR R16
LDI R17, 13
CALL LCD_Move_Cursor
call LCD_Send_String
POP R20
rjmp start
;*******************************LCD**********************************
****
BUTTON:
LDI R16, 50
DEBOUCING_0:
SBIC PINB, 0
RJMP BUTTON
DEC R16
BRNE DEBOUCING_0
INC R20
RELEASE:
LDI R16, 50
DEBOUCING_1:
SBIS PINB, 0
RJMP RELEASE
DEC R16
BRNE DEBOUCING_1
RET
;************INIT************;
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, 0x0C ; Display Control: Display OFF, Cursor OFF
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; Clear Display
call LCD_Send_Command

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

ret
;*******************SEND CMD******************;
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
;**************SEND DATA****************;
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

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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
;*************************SET_CURSOR*******************;
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80
mov r16,r17
; Send command to LCD
call LCD_Send_Command
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
;*******************SEND STRING****************;
LCD_Send_String:

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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:
LD 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
;***************LCD_WAIT_BUSY****************;
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

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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
;*******************DELAY10MS******************;
DELAY_10MS:
LDI R16,10
LOOP2:
LDI R17,250
LOOP1:
NOP
DEC R17
BRNE LOOP1
DEC R16
BRNE LOOP2
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.

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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

KEY_PAD_SCAN:
;PD_0 -> PD_3: OUTPUT, COL
;PD_4 -> PD_7: INPUT, ROW
LDI R16, $0F
OUT DDRD, R16
LDI R16, $F0
OUT PORTD, R16
CALL BUTTON

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

LDI R22, 0B11110111 ;INITIAL COLUMN MASK


LDI R24, 0 ;INITIAL PRESSED ROW VALUE
LDI R23, 3 ;SCANNING COLUMN INDEX
KEYPAD_SCAN_LOOP:
OUT PORTD, R22
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 R24, 1 ;ROW 1 IS PRESSED
RJMP KEYPAD_SCAN_FOUND
KEYPAD_SCAN_CHECK_COL3:
SBIC PIND, 6 ;CHECK ROW 2
RJMP KEYPAD_SCAN_CHECK_COL4
LDI R24, 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 R24, 3 ;ROW 3 IS PRESSED
RJMP KEYPAD_SCAN_FOUND
KEYPAD_SCAN_NEXT_ROW:
CPI R23, 0
BREQ KEYPAD_SCAN_NOT_FOUND
ROR R22
DEC R23
RJMP KEYPAD_SCAN_LOOP
KEYPAD_SCAN_FOUND:
; combine row and column to get key value (0-15)
;key code = row*4 + col
LSL R24 ; shift row value 4 bits to the left
LSL R24
ADD R24, R23 ; add row value to column value
RET
KEYPAD_SCAN_NOT_FOUND:
LDI R24, 0XFF ;NO KEY PRESSED

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

RET
BUTTON:
LDI R17, 50
DEBOUNCING_1:
IN R16, PIND
CPI R16, $FF ;DETECTE STATUS OF BUTTON
BREQ BUTTON
DEC R17
BRNE DEBOUNCING_1
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.
.include "m324padef.inc" ; Include Atmega324pa definitions
.org 0x0000 ; interrupt vector table
rjmp reset_handler ; reset
.equ LCDPORT = ; Set signal port reg to
PORTA PORTA
.equ LCDPORTDIR = ; Set signal port dir reg
DDRA 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
.def LCDData = r16
;******************************* Program ID
********************************
;PORTD -> CONTROL KEYPAD
;PORTC -> BAR LED
;********************************MAIN******************************
reset_handler:
CALL LCD_Init
SER R16
OUT DDRC, R16
LDI ZL, 0
LDI ZH, 7
LDI R16, $30
MOV R10, R16 ;DIGIT -> ASCII
LDI R16, $37
MOV R11, R16 ;ALPHA -> ASCII

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

CLR R15
; display the first line of information
start:
CALL KEY_PAD_SCAN
MOV R23, R24
OUT PORTC, R23
CPI R23, 0XFF
BREQ CLEAR
CPI R23, 10
BRCC ALPHA
ADD R23, R10 ;ASCII -> DIGIT
ST Z+, R23 ;DATA
ST Z, R15 ;END LINE
LDI ZL, 0
LDI ZH, 7
CLR R16
CLR R17
CALL LCD_Move_Cursor
CALL LCD_Send_String
RJMP start
ALPHA:
ADD R23, R11 ;ASCII -> ALPHA
ST Z+, R23 ;DATA
ST Z, R15 ;END LINE
LDI ZL, 0
LDI ZH, 7
CLR R16
CLR R17
CALL LCD_Move_Cursor
CALL LCD_Send_String
rjmp start
CLEAR:
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
rjmp start
;*******************************FUNCTION****************************
**********
;************INIT************;
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

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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, 0x0C ; Display Control: Display OFF, Cursor OFF
call LCD_Send_Command
ldi r16, 0x01 ; Clear Display
call LCD_Send_Command
ldi r16, 0x80 ; Clear Display
call LCD_Send_Command
ret
;*******************SEND CMD******************;
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
;**************SEND DATA****************;

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_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
;*************************SET_CURSOR*******************;
LCD_Move_Cursor:
cpi r16,0 ;check if first row
brne LCD_Move_Cursor_Second
andi r17, 0x0F
ori r17,0x80
mov r16,r17
; Send command to LCD
call LCD_Send_Command
ret

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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
;*******************SEND STRING****************;
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:
LD 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
;***************LCD_WAIT_BUSY****************;
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:

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

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
;*******************DELAY10MS******************;
DELAY_10MS:
LDI R16,10
LOOP2:
LDI R17,250
LOOP1:
NOP
DEC R17
BRNE LOOP1
DEC R16
BRNE LOOP2
RET
KEY_PAD_SCAN:
;PD_0 -> PD_3: OUTPUT, COL
;PD_4 -> PD_7: INPUT, ROW
LDI R16, $0F
OUT DDRD, R16
LDI R16, $F0
OUT PORTD, R16
CALL BUTTON
LDI R22, 0B11110111 ;INITIAL COLUMN MASK
LDI R24, 0 ;INITIAL PRESSED ROW VALUE
LDI R23, 3 ;SCANNING COLUMN INDEX

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

KEYPAD_SCAN_LOOP:
OUT PORTD, R22
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 R24, 1 ;ROW 1 IS PRESSED
RJMP KEYPAD_SCAN_FOUND
KEYPAD_SCAN_CHECK_COL3:
SBIC PIND, 6 ;CHECK ROW 2
RJMP KEYPAD_SCAN_CHECK_COL4
LDI R24, 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 R24, 3 ;ROW 3 IS PRESSED
RJMP KEYPAD_SCAN_FOUND
KEYPAD_SCAN_NEXT_ROW:
CPI R23, 0
BREQ KEYPAD_SCAN_NOT_FOUND
ROR R22
DEC R23
RJMP KEYPAD_SCAN_LOOP
KEYPAD_SCAN_FOUND:
; combine row and column to get key value (0-15)
;key code = row*4 + col
LSL R24 ; shift row value 4 bits to the left
LSL R24
ADD R24, R23 ; add row value to column value
RET
KEYPAD_SCAN_NOT_FOUND:
LDI R24, 0XFF ;NO KEY PRESSED
RET
BUTTON:
LDI R17, 50
DEBOUNCING_1:
IN R16, PIND
CPI R16, $FF ;DETECT STATUS OF BUTTON
BREQ BUTTON
DEC R17

https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN

BRNE DEBOUNCING_1
RET

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?
Dựa vào chân RS
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?
Delay cho mỗi lần thực hiện lệnh
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
.INCLUDE "M324PADEF.INC"
.EQU RS = 0
.EQU RW = 1
.EQU EN = 2
.EQU CR = $0D ;Enter
.EQU NULL = $00 ;End string
.ORG 0
RJMP MAIN
.ORG 0X40
MAIN:
LDI R16, HIGH(RAMEND)
OUT SPH, R16
LDI R16, LOW(RAMEND)
OUT SPL, R16
LDI R16, $FF
OUT DDRA, R16
;PA7,6,5,4,2,1,0 is output
CBI PORTA, RS
;Recieve command
CBI PORTA, RW ;Write data
CBI PORTA, EN
;Unenable LCD
CALL RESET_LCD
CALL INIT_LCD4
START:
CBI PORTA, RS
LDI R17, $01 ;Clear
display before

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

RCALL OUT_LCD4_2
LDI R16, 20 ;Delay
20ms after clearing
RCALL DELAY_US
CBI PORTA, RS
LDI R17, $80
;Pointer start at line1, pos1
RCALL OUT_LCD4_2
LDI ZH, HIGH(TAB<<1)
LDI ZL, LOW(TAB<<1)
LINE1:
LPM R17, Z+
CPI R17, CR ;Check enter code
BREQ DOWN
SBI PORTA, RS ;Display on LCD
RCALL OUT_LCD4_2
RJMP LINE1
DOWN:
LDI R16, 1 ;Xuong dong
phai doi 100us
RCALL DELAY_US
CBI PORTA, RS

LDI R17, $C0 ;Set pointer to line2 pos1


RCALL OUT_LCD4_2
LINE2:
LPM R17, Z+
CPI R17, NULL
BREQ DONE
SBI PORTA, RS
RCALL OUT_LCD4_2
RJMP LINE2
DONE:
RJMP DONE
OUT_LCD4:
OUT PORTA, R17
SBI PORTA, EN
CBI PORTA, EN
RET
OUT_LCD4_2:
LDI R16, 1 ;Delay 1us

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

RCALL DELAY_US
IN R16, PORTA
ANDI R16, (1<<RS)
PUSH R16
PUSH R17
ANDI R17, $F0
OR R17, R16
RCALL OUT_LCD4
LDI R16, 1 ;Delay 1us between first and second access
RCALL DELAY_US

POP R17
POP R16
SWAP R17
ANDI R17, $F0
OR R17, R16
RCALL OUT_LCD4
RET
INIT_LCD4:
LDI R18, $28
;Function set
LDI R19, $01 ;Clear
display
LDI R20, $0C
;Display on, pointer off
LDI R21, $06
CBI PORTA, RS
MOV R17, R18
RCALL OUT_LCD4_2
MOV R17, R19 ;Clear
display
RCALL OUT_LCD4_2
LDI R16, 20 ;Delay 20ms after clearing display
RCALL DELAY_US
MOV R17, R20
RCALL OUT_LCD4_2
MOV R17, R21
RCALL OUT_LCD4_2
RET
RESET_LCD:

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

LDI R16, 250 ;Delay


25ms
RCALL DELAY_US
LDI R16, 250 ;Delay
25ms
RCALL DELAY_US
CBI PORTA, RS
LDI R17, $30 ;Lan 1
RCALL OUT_LCD4
LDI R16, 250 ;Delay
25ms
RCALL DELAY_US
LDI R16, 170 ;Delay
17ms
RCALL DELAY_US
CBI PORTA, RS
LDI R17, $30 ;Lan 2
RCALL OUT_LCD4
LDI R16, 2 ;Delay
200us
RCALL DELAY_US
CBI PORTA, RS
LDI R17, $32 ;Lan 3
RCALL OUT_LCD4_2
RET
DELAY_US:
MOV R15, R16
LDI R16, 200
LP1:

MOV R14, R16


LP2:
NOP
DEC R14
BRNE LP2
DEC R15
BRNE LP1
RET
TAB:
.DB "TN VXL-AVR",
$0D,"To: ","01",$00

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 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
Khi bạn nhấn công tắc, ban đầu nó sẽ tiếp xúc với nhũng vùng kim loại khác
(có thể gọi là tiếp xúc chưa hoàn toàn), nhưng chỉ trong một khoảng thời gian
cực ngắn, cỡ vài micro giây. Quá trình diễn ra dần dần cho đến khi tiếp xúc
hoàn toàn.
Do phần cứng phản xạ cực nhanh với các tiếp xúc, nên khi trong quá trình
nhấn nút như trên thì phần cứng nó hiểu rằng bạn nhấn công tắc nhiều lần.
Đây chính là hiện tượng rung phím bấ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


.ORG 0
RJMP MAIN
.ORG 0X40
MAIN :
CBI DDRA,0
CBI DDRA,1

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

SBI PORTA,0
SBI PORTA,1
LDI R16,0XFF
OUT DDRD,R16
STACK :
LDI R16,HIGH (RAMEND)
OUT SPH,R16
LDI R16,LOW (RAMEND )
OUT SPL,R16
START: ; RESET
LDI R23,0X00
OUT PORTD,R23
RCALL XUATLCD
NHAN_PHIM :
SBIS PINA,1 ; KIEM TRA RESET
RJMP START
SBIC PINA,0 ; NEU PHIM NHAN
RJMP NHAN_PHIM
SBIC PINA,0
RJMP NHAN_PHIM
NHA_PHIM:
SBIS PINA,0
RJMP NHA_PHIM
SBIS PINA,0
RJMP NHA_PHIM
INC R22 ; TANG BIEN DEM
RJMP NHAN_PHIM

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


.ORG 0
.EQU LCD=PORTC ;PORTC giao tiếp bus LCD 16 x 2
.EQU LCD_DR=DDRC
.EQU LCD_IN=PINC
.EQU RS=0 ;bit RS
.EQU RW=1 ;bit RW
.EQU E=2 ;bit E
.EQU CR=$0D ;mã xuống dòng
.EQU NULL=$00 ;mã kết thúc
RJMP MAIN
.ORG 0X40

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

MAIN :
CBI DDRA,0
CBI DDRA,1
SBI PORTA,0
SBI PORTA,1
LDI R16,0XFF
OUT DDRD,R16
STACK :
LDI R16,HIGH (RAMEND)
OUT SPH,R16

LDI R16,LOW (RAMEND )


OUT SPL,R16
START: ; RESET
LDI R23,0X00
OUT PORTD,R23
RCALL XUATLCD
NHAN_PHIM :
SBIS PINA,1 ; KIEM TRA RESET
RJMP START
SBIC PINA,0 ; NEU PHIM NHAN
RJMP NHAN_PHIM
RCALL DELAY20MS ; CHONG RUN PHIM
SBIC PINA,0
RJMP NHAN_PHIM
NHA_PHIM:
SBIS PINA,0
RJMP NHA_PHIM
RCALL DELAY20MS ; CHONG RUN KIEM TRA NHA PHIM
SBIS PINA,0
RJMP NHA_PHIM
INC R22 ; TANG BIEN DEM
OUT PORTD,R22
RCALL XUATLCD
RJMP NHAN_PHIM
DELAY20MS:
LDI R21,160 ;1MC
Lp1: LDI R20,250 ;1MC
Lp2: DEC R20 ;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 R21 ;1MC


BRNE Lp1 ;2/1MC
RET ;4MC
XUATLCD:
PUSH R16
LDI R16,HIGH(RAMEND) ;đưa stack lên vùng địa chỉ cao
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
LDI R16,0XFF
OUT LCD_DR,R16 ;khai báo PORTC là output
CBI LCD,RS ;RS=PC0=0
CBI LCD,RW ;RW=PC1=0 truy xuất ghi
CBI LCD,E ;E=PC2=0 cấm LCD
;--------------------------------
;khởi tạo LCD gồm:
;reset cấp nguồn
;cấu hình LCD hoạt động chế độ 4 bit
;--------------------------------
RCALL POWER_RESET_LCD4
;reset cấp nguồn LCD 4 bit
RCALL INIT_LCD4
;ctc khởi động LCD 4 bit
;--------------------------------
CBI LCD,RS
;RS=0 ghi lệnh
LDI R17,$83
;con trỏ bắt đầu ở dòng 1 vị trí thứ 3
RCALL OUT_LCD4_2
LDI R16,1
;chờ 100μs
RCALL DELAY_US

LDI ZH,HIGH(TAB<<1)
;Z trỏ đầu bảng tra ký tự
LDI ZL,LOW(TAB<<1)
LINE1: LPM R17,Z+
;lấy mã ASCII ký tự từ Flash ROM
CPI R17,CR
;kiểm tra ký tự xuống dòng ?
BREQ DOWN

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

;ký tự CR xuống dòng


SBI LCD,RS
;RS=1 ghi data hiển thị LCD
RCALL OUT_LCD4_2
;ghi mã ASCII ký tự ra LCD
LDI R16,1
;chờ 100μs
RCALL DELAY_US
RJMP LINE1
;tiếp tục hiển thị dòng 1
DOWN: CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$C7 ;con trỏ bắt đầu ở dòng 2 vị trí thứ 7
RCALL OUT_LCD4_2
LDI R16,1 ;chờ 100μs
RCALL DELAY_US
RCALL HEXTOASCII
LDI YH,0X08
LDI YL,0X00
LINE2:
XUATTRAM:
LD R17,Y+
SBI LCD,RS
RCALL OUT_LCD4_2
LDI R16,1
RCALL DELAY_US
XUATCHUC:
LD R17,Y+
SBI LCD,RS
RCALL OUT_LCD4_2
LDI R16,1
RCALL DELAY_US
XUATDONVI :
LD R17,Y+
SBI LCD,RS
RCALL OUT_LCD4_2
LDI R16,1
RCALL DELAY_US
HERE: JMP NHAN_PHIM
;-----------------------------------------------------------------
;Các lệnh reset cấp nguồn LCD 4 bit
;Chờ hơn 15ms

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

;Ghi 4 bit mã lệnh 30H lần 1, chờ ít nhất 4.1ms


;Ghi 4 bit mã lệnh 30H lần 2, chờ ít nhất 100μs
;Ghi byte mã lệnh 32H, chờ ít nhất 100μs sau mỗi lần ghi 4
bit
;-----------------------------------------------------------------
POWER_RESET_LCD4:
LDI R16,200 ;delay 20ms
RCALL DELAY_US ;ctc delay 100μsxR16
;Ghi 4 bit cao mã lệnh 30H lần 1, chờ 4.2ms
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$30 ;mã lệnh=$30 lần 1,RS=RW=E=0

RCALL OUT_LCD4 ;ctc ghi ra LCD 4 bit cao


LDI R16,42 ;delay 4.2ms
RCALL DELAY_US
;Ghi 4 bit cao mã lệnh 30H lần 2, chờ 200μs
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$30 ;mã lệnh=$30 lần 2
RCALL OUT_LCD4 ;ctc ghi ra LCD 4 bit cao
LDI R16,2 ;delay 200μs
RCALL DELAY_US
;Ghi byte mã lệnh 32H
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$32
RCALL OUT_LCD4_2; ctc ghi 1 byte, mỗi lần 4 bit
RET
;INIT_LCD4 khởi động LCD ghi 4 byte mã lệnh
;Function set: 0x28: 8 bit, 2 dòng font 5x8
;Clear display: 0x01: xóa màn hình
;Display on/off control: 0x0C: màn hình on, con trỏ off
;Entry mode set: 0x06: dịch phải con trỏ, địa chỉ DDRAM tăng 1 khi ghi
data
;----------------------------------------------------------------
INIT_LCD4: CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x28 ;chế độ giao tiếp 8 bit, 2 dòng font 5x8
RCALL OUT_LCD4_2
;----------------------------------------------------------------
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x01 ;xóa màn hình
RCALL OUT_LCD4_2
LDI R16,20 ;chờ 2ms sau lệnh Clear display

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

RCALL DELAY_US
;----------------------------------------------------------------
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x0C ;màn hình on, con trỏ off
RCALL OUT_LCD4_2
;----------------------------------------------------------------
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x06 ;dịch phải con trỏ, địa chỉ DDRAM tăng 1 khi ghi data
RCALL OUT_LCD4_2
;----------------------------------------------------------------
RET
;--------------------------------------------------
;OUT_LCD4_2 ghi 1 byte mã lệnh/data ra LCD
;chia làm 2 lần ghi 4bit: 4 bit cao trước, 4 bit thấp sau
;Input: R17 chứa mã lệnh/data,R16
;bit RS=0/1:lệnh/data,bit RW=0:ghi
;Sử dụng ctc OUT_LCD4
;--------------------------------------------------
OUT_LCD4_2:
IN R16,LCD ;đọc PORT LCD
ANDI R16,(1<<RS) ;lọc bit RS
PUSH R16 ;cất R16
PUSH R17 ;cất R17
ANDI R17,$F0 ;lấy 4 bit cao
OR R17,R16 ;ghép bit RS
RCALL OUT_LCD4 ;ghi ra LCD
LDI R16,1 ;chờ 100us
RCALL DELAY_US
POP R17 ;phục hồi R17
POP R16 ;phục hồi R16
SWAP R17 ;đảo 4 bit

;lấy 4 bit thấp chuyển thành cao


ANDI R17,$F0
OR R17,R16 ;ghép bit RS
RCALL OUT_LCD4;ghi ra LCD
LDI R16,1 ;chờ 100us
RCALL DELAY_US
RET
;--------------------------------------------------
;OUT_LCD4 ghi mã lệnh/data ra LCD

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

;Input: R17 chứa mã lệnh/data 4 bit cao


;--------------------------------------------------
OUT_LCD4: OUT LCD,R17
SBI LCD,E
CBI LCD,E
RET
;-------------------------------------------------------
;DELAY_US tạo thời gian trễ =R16x100μs(Fosc=8MHz, CKDIV8 =
1)
;Input:R16 hệ số nhân thời gian trễ 1 đến 255
;-------------------------------------------------------
DELAY_US: MOV R15,R16 ;1MC nạp data cho R15
LDI R16,200 ;1MC sử dụng R16
L1: MOV R14,R16 ;1MC nạp data cho R14
L2: DEC R14 ;1MC
NOP ;1MC
BRNE L2 ;2/1MC
DEC R15 ;1MC
BRNE L1 ;2/1MC
RET ;4MC
.ORG 0X0200
;-------------------------------------------------------------
TAB: .DB "KET QUA LA:" ,$0D
RET
HEXTOASCII:
push r22
push r21
push r20
push r18
mov r18,R22
ldi r20,0
ldi r21,0
ldi r22,0
loop1:
cpi r18,10
brlo a
subi r18,10
inc r21
rjmp loop1
a: mov r22,r18
loop2:

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

cpi r21,10
brlo b
subi r21,10
inc r20
rjmp loop2
b:
mov r17,r20
rcall HEX_ASC
sts 0x800,r18

mov r17,r21
rcall HEX_ASC
sts 0x801,r18
mov r17,r22
rcall HEX_ASC
sts 0x802,r18
LDI R18,0X00
STS 0X803,R18
pop r18
pop r20
pop r21
pop r22
ret
;HEX_ASC chuyển từ mã Hex sang mã ASCII
;Input R17=mã Hex,Output R18=mã ASCII
;------------------------------------------
HEX_ASC:
CPI R17,0X0A
BRCS NUM
LDI R18,0X37
RJMP CHAR
NUM: LDI R18,0X30
CHAR: ADD R18,R17
RET

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

https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn 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.

;-----------------------------------------
;SCANKEY đọc trạng thái các phím,
;Trả về R24= mã phím và C=1 nếu có phím nhấn
;Trả về C=0 nếu phím chưa nhấn
;------------------------------------------
SCANKEY:
LDI R24,4 ;R24 đếm số lần quét cột
LDI R20,0XFE ;bắt đầu quét cột 0
SCAN_COL:
OUT PORTA,R20
IN R19,PINA ;đọc trạng thái hàng
IN R19,PINA ;đọc lại trạng thái hàng
ANDI R19,0XF0 ;che 4 bit cao lấy mã hàng
CPI R19,0XF0 ;xem có phím nhấn?
BRNE CHK_KEY ;R19 khác F0H: có phím nhấn
LSL R20 ;quét cột kế tiếp
INC R20 ;đặt LSB=1
DEC R24
BRNE SCAN_COL ;tiếp tục quét hết số cột
CLC ;phím chưa nhấn,C=0
RJMP EXIT ;thoát
CHK_KEY:
SUBI R24,4 ;tính vị trí cột
NEG R24 ;bù 2 lấy số dương

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

SWAP R19 ;đảo sang 4 bit thấp mã hàng


LDI R20,4 ;quét 4 hàng tìm vị trí hàng…
SCAN_ROW: ; tìm vị trí hàng có phím được nhấn
ROR R19 ;quay phải mã hàng qua C tìm bit 0
BRCC SET_FLG ;C=0 đúng vị trí hàng có phím nhấn
INC R24 ;không đúng hàng, tăng vị trí hàng thêm 4
INC R24
INC R24
INC R24
DEC R20
BRNE SCAN_ROW ;quét hết 4 hàng
CLC ;không có phím nhấn C=0
LDI R24,0XFF
RJMP EXIT ;thoát
SET_FLG: SEC ;có phím nhấn C=1
EXIT: RET

.EQU OUTPORT=PORTD
.ORG 0
RJMP MAIN
.ORG 0X40
MAIN: LDI R16,HIGH(RAMEND);đưa stack lên vùng địa chỉ cao
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
LDI R16,0X0F
OUT DDRA,R16
LDI R16,0XFF
OUT PORTA,R16
LDI R16,0XFF
OUT DDRD,R16

LDI R16,0X00
OUT PORTD,R16
START:
RCALL KEY_RD ;ctc đọc phím
CLR R16
OUT OUTPORT,R16
OUT OUTPORT,R17 ;hiển thị mã phím ra bar LED
RCALL XUATLCD

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

RJMP START ;lặp vòng từ đầU


;------------------------------------------
;KEY_RD đọc trạng thái phím
;Chống rung phím khi nhấn/nhả 50 lần
;Sử dụng SCANKEY nhận dạng phím nhấn: R17 = mã phím, C = 0: chưa
nhấn, C =
1: có nhấn
;Chỉ thoát khi có phím nhấn!!!
;-------------------------------------------
KEY_RD:
LDI R16,50 ;số lần nhận dạng phím nhấn
BACK1: RCALL SCANKEY ;gọi ctc nhận dạng phím
BRCC KEY_RD ;C=0 phím chưa nhấn lặp lại
DEC R16 ;đếm số lần nhận dạng phím
BRNE BACK1 ;lặp vòng cho đủ số lần đếm
PUSH R17 ;xác nhận phím nhấn,cất mã phím
WAIT_1: LDI R16,50 ;số lần nhận dạng phím nhả
BACK2: RCALL SCANKEY ;gọi ctc nhận dạng phím
BRCS WAIT_1 ;C=1 phím chưa nhả
DEC R16 ;đếm số lần nhận dạng phím
BRNE BACK2 ;lặp vòng cho đủ số lần đếm
POP R17 ;xác nhận phím nhả lấy lại mã phím
MOV R24,R17
RET
;SCANKEY đọc trạng thái các phím,
;Trả về R24= mã phím và C=1 nếu có phím nhấn
;Trả về C=0 nếu phím chưa nhấn
;------------------------------------------
SCANKEY:
LDI R17,4 ;R17 đếm số lần quét cột
LDI R20,0XFE ;bắt đầu quét cột 0
SCAN_COL:
OUT PORTA,R20
IN R19,PINA ;đọc trạng thái hàng
IN R19,PINA ;đọc lại trạng thái hàng
ANDI R19,0XF0 ;che 4 bit cao lấy mã hàng
CPI R19,0XF0 ;xem có phím nhấn?
BRNE CHK_KEY ;R19 khác F0H: có phím nhấn
LSL R20 ;quét cột kế tiếp
INC R20 ;đặt LSB=1
DEC R17

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

BRNE SCAN_COL ;tiếp tục quét hết số cột


CLC ;phím chưa nhấn,C=0
MOV R24,R17
RJMP EXIT ;thoát
CHK_KEY:
SUBI R17,4 ;tính vị trí cột
NEG R17 ;bù 2 lấy số dương
SWAP R19 ;đảo sang 4 bit thấp mã hàng
LDI R20,4 ;quét 4 hàng tìm vị trí hàng…
SCAN_ROW: ; tìm vị trí hàng có phím được nhấn
ROR R19 ;quay phải mã hàng qua C tìm bit 0
BRCC SET_FLG ;C=0 đúng vị trí hàng có phím nhấn
INC R17 ;không đúng hàng, tăng vị trí hàng thêm 4
INC R17
INC R17
INC R17
DEC R20
BRNE SCAN_ROW ;quét hết 4 hàng
CLC ;không có phím nhấn C=0
RJMP EXIT ;thoát
SET_FLG: SEC ;có phím nhấn C=1
EXIT:
MOV R24,R17
RET
XUATLCD:
PUSH R16
.EQU LCD=PORTC ;PORTC giao tiếp bus LCD 16 x 2
.EQU LCD_DR=DDRC
.EQU LCD_IN=PINC
.EQU RS=0 ;bit RS
.EQU RW=1 ;bit RW
.EQU E=2 ;bit E
.EQU CR=$0D ;mã xuống dòng
.EQU NULL=$00 ;mã kết thúc
LDI R16,HIGH(RAMEND) ;đưa stack lên vùng địa chỉ cao
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
LDI R16,0XFF
OUT LCD_DR,R16 ;khai báo PORTC là output
CBI LCD,RS ;RS=PC0=0

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

CBI LCD,RW ;RW=PC1=0 truy xuất ghi


CBI LCD,E ;E=PC2=0 cấm LCD
;-------------------------------- ;khởi tạo LCD gồm:
;reset cấp nguồn ;cấu hình LCD hoạt động chế độ 4 bit ;---------------------
-----------
RCALL POWER_RESET_LCD4
;reset cấp nguồn LCD 4 bit
RCALL INIT_LCD4
;ctc khởi động LCD 4 bit
;--------------------------------
CBI LCD,RS
;RS=0 ghi lệnh
LDI R17,$83
;con trỏ bắt đầu ở dòng 1 vị trí thứ 3
RCALL OUT_LCD4_2
LDI R16,1
;chờ 100μs
RCALL DELAY_US
LDI ZH,HIGH(TAB<<1)
;Z trỏ đầu bảng tra ký tự
LDI ZL,LOW(TAB<<1)
LINE1: LPM R17,Z+
;lấy mã ASCII ký tự từ Flash ROM
CPI R17,CR
;kiểm tra ký tự xuống dòng ?
BREQ DOWN
;ký tự CR xuống dòng
SBI LCD,RS
;RS=1 ghi data hiển thị LCD
RCALL OUT_LCD4_2
;ghi mã ASCII ký tự ra LCD
LDI R16,1
;chờ 100μs
RCALL DELAY_US
RJMP LINE1
;tiếp tục hiển thị dòng 1
DOWN: CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$C5 ;con trỏ bắt đầu ở dòng 2 vị trí thứ 5
RCALL OUT_LCD4_2
LDI R16,1 ;chờ 100μs
RCALL DELAY_US

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

LINE2:
RJMP XUANCANH
TT: MOV R17,R24
RCALL HEX_ASC
MOV R17,R18
SBI LCD,RS
RCALL OUT_LCD4_2
LDI R16,1
RCALL DELAY_US
RJMP START
XUANCANH:
LPM R17,Z+ ;lấy mã ASCII ký tự từ Flash ROM
CPI R17,NULL ;kiểm tra ký tự kết thúc
BREQ TT ;ký tự NULL thoát
SBI LCD,RS ;RS=1 ghi data hiển thị LCD
RCALL OUT_LCD4_2 ;ghi mã ASCII ký tự ra LCD
LDI R16,1 ;chờ 100μs
RCALL DELAY_US
RJMP XUANCANH ;tiếp tục hiển thị dòng 2
;-----------------------------------------------------------------
;Các lệnh reset cấp nguồn LCD 4 bit
;Chờ hơn 15ms
;Ghi 4 bit mã lệnh 30H lần 1, chờ ít nhất 4.1ms
;Ghi 4 bit mã lệnh 30H lần 2, chờ ít nhất 100μs
;Ghi byte mã lệnh 32H, chờ ít nhất 100μs sau mỗi lần ghi 4 bit
;-----------------------------------------------------------------
POWER_RESET_LCD4:
LDI R16,200 ;delay 20ms
RCALL DELAY_US ;ctc delay 100μsxR16
;Ghi 4 bit cao mã lệnh 30H lần 1, chờ 4.2ms
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$30 ;mã lệnh=$30 lần 1,RS=RW=E=0
RCALL OUT_LCD4 ;ctc ghi ra LCD 4 bit cao
LDI R16,42 ;delay 4.2ms
RCALL DELAY_US
;Ghi 4 bit cao mã lệnh 30H lần 2, chờ 200μs
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$30 ;mã lệnh=$30 lần 2
RCALL OUT_LCD4 ;ctc ghi ra LCD 4 bit cao
LDI R16,2 ;delay 200μs
RCALL DELAY_US

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

;Ghi byte mã lệnh 32H


CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,$32
RCALL OUT_LCD4_2; ctc ghi 1 byte, mỗi lần 4 bit
RET
;INIT_LCD4 khởi động LCD ghi 4 byte mã lệnh
;Function set: 0x28: 8 bit, 2 dòng font 5x8
;Clear display: 0x01: xóa màn hình
;Display on/off control: 0x0C: màn hình on, con trỏ off
;Entry mode set: 0x06: dịch phải con trỏ, địa chỉ DDRAM tăng 1 khi ghi
data
;----------------------------------------------------------------
INIT_LCD4: CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x28 ;chế độ giao tiếp 8 bit, 2 dòng font 5x8
RCALL OUT_LCD4_2
;----------------------------------------------------------------
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x01 ;xóa màn hình
RCALL OUT_LCD4_2
LDI R16,20 ;chờ 2ms sau lệnh Clear display
RCALL DELAY_US
;----------------------------------------------------------------
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x0C ;màn hình on, con trỏ off
RCALL OUT_LCD4_2
;----------------------------------------------------------------
CBI LCD,RS ;RS=0 ghi lệnh
LDI R17,0x06 ;dịch phải con trỏ, địa chỉ DDRAM tăng 1 khi ghi data
RCALL OUT_LCD4_2
;----------------------------------------------------------------
RET
;--------------------------------------------------
;OUT_LCD4_2 ghi 1 byte mã lệnh/data ra LCD
;chia làm 2 lần ghi 4bit: 4 bit cao trước, 4 bit thấp sau
;Input: R17 chứa mã lệnh/data,R16
;bit RS=0/1:lệnh/data,bit RW=0:ghi
;Sử dụng ctc OUT_LCD4
;--------------------------------------------------
OUT_LCD4_2:
IN R16,LCD ;đọc PORT LCD
ANDI R16,(1<<RS) ;lọc bit RS

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

PUSH R16 ;cất R16


PUSH R17 ;cất R17
ANDI R17,$F0 ;lấy 4 bit cao
OR R17,R16 ;ghép bit RS
RCALL OUT_LCD4 ;ghi ra LCD
LDI R16,1 ;chờ 100us
RCALL DELAY_US
POP R17 ;phục hồi R17
POP R16 ;phục hồi R16
SWAP R17 ;đảo 4 bit
;lấy 4 bit thấp chuyển thành cao
ANDI R17,$F0
OR R17,R16 ;ghép bit RS
RCALL OUT_LCD4;ghi ra LCD
LDI R16,1 ;chờ 100us
RCALL DELAY_US
RET
;--------------------------------------------------
;OUT_LCD4 ghi mã lệnh/data ra LCD
;Input: R17 chứa mã lệnh/data 4 bit cao ;-----------------------------------

OUT_LCD4: OUT LCD,R17


SBI LCD,E
CBI LCD,E
RET
;------------------------------------------------------
;DELAY_US tạo thời gian trễ =R16x100μs(Fosc=8MHz, CKDIV8 = 1)
;Input:R16 hệ số nhân thời gian trễ 1 đến 255 ;-----------------------------
--------------------------
DELAY_US: MOV R15,R16 ;1MC nạp data cho R15
LDI R16,200 ;1MC sử dụng R16
L1: MOV R14,R16 ;1MC nạp data cho R14
L2: DEC R14 ;1MC
NOP ;1MC
BRNE L2 ;2/1MC
DEC R15 ;1MC
BRNE L1 ;2/1MC
RET ;4MC
.ORG 0X0200
;-------------------------------------------------------------
TAB: .DB "KET QUA LA :" ,$0D,"PHIM ",0X00

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

RET
;HEX_ASC chuyển từ mã Hex sang mã ASCII
;Input R17=mã Hex,Output R18=mã ASCII
;------------------------------------------
HEX_ASC:
CPI R17,0X0A
BRCS NUM
LDI R18,0X37
RJMP CHAR
NUM: LDI R18,0X30
CHAR: ADD R18,R17
RET

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

You might also like