LAB1_3
LAB1_3
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
https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN
https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN
https://doe.dee.hcmut.edu.vn/
Lab 1-3
Giao tiếp nút nhấn, BÀN PHÍM MA TRẬN
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
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
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
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
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
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
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:
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
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
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
https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn thí nghiệm:
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:
https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn 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:
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
https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn 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:
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:
.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:
https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn 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:
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:
https://doe.dee.hcmut.edu.vn/
BÁO CÁO
Nhóm:
Nhóm môn học: Môn 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:
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/