Thiết kế của tớ như sau:
- Phần cứng: theo sơ đồ đính kèm
- Phần mềm:
Code:
Init_port:
clrf PORTD
movlw 0xF0 ; Row: output (RD0-3), Col: Input (RD4-7)
movwf TRISD
MainProg:
clrwdt
clrf key
call Chk_Keys
movf key, w
call WriteData ; Hiển thị lên LCD
goto MainProg
;Keypad subroutine
Chk_Keys
movlw .0 ;wait until no key pressed
movwf PORTD ;set all output pins (row) low
movf PORTD,0
andlw 0xF0
sublw 0xF0
skpnz ; test if any key pressed
goto Keys ; if none, read keys
movlw 0x14
call Delay1ms ; delay20ms
return
Keys
call Scan_Keys
movlw 0x10 ;check for no key pressed
subwf key, w
btfss STATUS, Z
goto Key_Found
movlw 0x14
call Delay1ms
goto Keys
Key_Found:
movf key,w
andlw 0x0f
movwf temp
call Key_Table ;lookup key in table
movwf key ;save back in key
return ;key pressed now in W
Scan_Keys
movlw 0x0F
movwf PORTD ;set all output lines high
movlw 0x04
movwf rows ;set number of rows
bcf STATUS, C ;put a 0 into carry
Scan
rrf PORTD,1
bsf STATUS, C ;follow the zero with ones
;comment out next two lines for 4x3 numeric keypad.
btfss Col4
return
incf key,1
btfss Col3
return
incf key,1
btfss Col2
return
incf key,1
btfss Col1
return
incf key,1
decfsz rows,1
goto Scan
return
Key_Table
movlw high(Key_Table)
movwf PCLATH
movf temp,0
addwf PCL,1
retlw 'C'
retlw 'B'
retlw '0'
retlw 'A'
retlw 'D'
retlw '9'
retlw '8'
retlw '7'
retlw 'E'
retlw '6'
retlw '5'
retlw '4'
retlw 'F'
retlw '3'
retlw '2'
retlw '1'
Mục đích chương trình của tớ như sau: Khi có phím bất kỳ được ấn thì sẽ hiển thị lên màn hình LCD. Phần điều khiển hiển thị LCD tớ đã làm và chạy tốt. Tuy nhiên phần keypad này thì không chạy đúng với ý tưởng của tớ. Đầu tiên cắm điện thì nó không hiện gì. Bấm 1 nút bất kỳ thì nó hiện khoảng 4 ký tự C, sau đó là khoảng 6 ký tự ô vuông đen xì, sau đó lại 1 loạt ký tự C nữa. Vậy chương trình của tớ sai ở chỗ nào? Cần phải sửa những gì để nó chạy đúng ý tưởng của tớ? Rất mong được các bạn giúp đỡ