5501 views|11 replies

56

Posts

2

Resources
The OP
 

Detecting the keystrokes and outputting them to the digital tube does not display correctly [Copy link]

 
This post was last edited by JLennon on 2018-2-2 08:35 The program is as follows, and the proteus code is attached. The buttons K8, K12, and K16 cannot be displayed. What went wrong? I really can't find the reason. Thanks for your advice.
  1. LIST P=18F4520 ;--- Device model definition--- #include<p18f4520.inc>                            ;--- The device register header file contains--- ;-------------------------------------------------------------------------------- ;--- MCU configuration word configuration area--- CONFIG OSC = INTIO67 ;--- Configure the clock source to be the internal RC oscillator--- CONFIG WDT = OFF ;--- Disable the watchdog function--- CONFIG DEBUG = OFF ;--- Disable the debug function--- CONFIG LVP = OFF ;--- Disable the low voltage programming function--- CONFIG PBADEN = OFF ;--- Configure all RB ports as digital IO pins--- ;-------------------------------------------------------------------------------- ;--- Global variable definition area--- CBLOCK 0x10 KEYTEMP KEY DELAY_A DELAY_B DELAY_C ENDC ;-------------------------------------------------------------------------------- ;--- The start of the program execution--- ORG 0 ;--- MCU reset address--- GOTO START ;--- Jump to main program--- ORG 0x30 ;--- Main program entry address--- START: NOP ;-------------------------------------------------------------------------------- ;--- Initialize internal clock source--- MOVLW 0x70 MOVWF OSCCON ;--- Select internal 8MHZ RC oscillator--- BCF OSCTUNE,PLLEN ;--- Disable internal 4x PLL function--- ;-------------------------------------------------------------------------------- ;--- Initialize GPIO pins--- CLRF WREG MOVWF TRISD ;--- Configure RD port as output direction--- MOVWF LATD ;--- Let RD port output all low level--- BCF INTCON2,RBPU ;--- Enable internal weak pull-up function of RB port--- ;-------------------------------------------------------------------------------- ;--- Initialize related variables--- MOVLW 0x0F MOVWF TRISB ;--- Configure RB7..4 as output pins, RB3..0 as input pins--- MOVWF LATB ;--- RB7..4 outputs low level--- ;-------------------------------------------------------------------------------- ;--- Main program: Implement 4X4 matrix keyboard, send key values to LED digital tube for display--- MAIN: NOP MOVF PORTB,W ;--- Check if any key is pressed--- ANDLW 0x0F MOVWF KEYTEMP MOVLW 0x0F CPFSEQ KEYTEMP GOTO KEYA ;--- If any key is pressed, jump to the specified location--- GOTO MAIN ;--- If no key is pressed, continue to detect--- KEYA: CALL DELAY ;--- Remove key jitter--- MOVF PORTB,W ;--- Check if any key is really pressed--- ANDLW 0x0F MOVWF KEYTEMP MOVLW 0x0F CPFSEQ KEYTEMP GOTO KEYB ;--- If any key is really pressed, jump to the specified location--- GOTO MAIN ;--- Indicates that no key is pressed--- KEYB: MOVLW 0xF0 ;--- After reading the current value, swap the row and column directions--- MOVWF TRISB MOVWF LATB MOVF PORTB,W ;--- Read the code value of the current key--- ANDLW 0xF0 IORWF KEYTEMP ;--- Combine with the key code value read last time--- CLRF KEY KEYD: MOVF KEY,W ;--- Compare the key code value read with the value in the KEYTAB table--- ADDWF WREG,W CALL KEYTAB CPFSEQ KEYTEMP GOTO KEYC ;--- If not found, find the next key code value--- GOTO FIND ;--- Find the key code and jump to the specified location--- KEYC: INCF KEY MOVLW 0x10 CPFSLT KEY GOTO NOFIND ;--- If not found in the key code value, jump to the specified location--- GOTO KEYD FIND: MOVF KEY,W ;--- Display the found key value by searching the digital tube code value--- ADDWF WREG,W CALL LEDSEG MOVWF LATD ;--- Send it out for display--- NOFIND: NOP WAIT: MOVF PORTB,W ;--- Wait for the key to be released--- ANDLW 0xF0 MOVWF KEYTEMP MOVLW 0xF0 CPFSEQ KEYTEMP GOTO WAIT MOVLW 0x0F ;--- Exchange the row and column directions--- MOVWF TRISB MOVWF LATB GOTO MAIN ;--- Jump to the beginning of the main program--- ;-------------------------------------------------------------------------------- ;--- 4x4 matrix keyboard code value--- KEYTAB: ADDWF PCL,F DT 0xEE,0xED,0xEB,0xE7 DT 0xDE,0xDD,0xDB,0xD7 DT 0xBE,0xBD,0xBB,0xB7 DT 0x7E,0x7D,0x7B,0x77 ;-------------------------------------------------------------------------------- ;--- Define the segment code of digital tube display 0-9,AF--- LEDSEG: ADDWF PCL,F DT 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F DT 0x77,0x7C,0x39,0x5E,0x79,0x71 ;-------------------------------------------------------------------------------- ;--- Delay subroutine--- DELAY: MOVLW 0x01 MOVWF DELAY_A DLY_A: MOVLW 0x10 MOVWF DELAY_B DLY_B: MOVLW 0xFF MOVWF DELAY_C DLY_C: DECFSZ DELAY_C GOTO DLY_C DECFSZ DELAY_B GOTO DLY_B DECFSZ DELAY_A GOTO DLY_A RETURN END
复制代码



KEY_4X4_ASM.zip

56.73 KB, downloads: 0

This post is from Microchip MCU

Latest reply

Simulation is still very useful. You can see in detail where the program has problems.  Details Published on 2018-2-5 11:50
 

1234

Posts

4

Resources
2
 
I don't understand why you added that NOP. Is it just for self-consumption?
This post is from Microchip MCU
 
Personal signature天地庄周马;江湖范蠡船。
个性签名还是放QQ号吧,2060347305,添加说明EEworld好友
 
 

3471

Posts

11

Resources
3
 
The buttons K8, K12, and K16 cannot be displayed. What is the phenomenon? What is used for display? The high and low levels of io can be used to indicate the button status. See if the correct button status can be obtained.
This post is from Microchip MCU

Comments

The 7-segment digital tube displays that when K8, K12, and K16 are pressed, LATD=0X0, and the displays of the remaining buttons are correct.  Details Published on 2018-2-1 19:38
 
 
 

56

Posts

2

Resources
4
 
ienglgge posted on 2018-2-1 18:21 The buttons K8, K12, and K16 cannot be displayed. What is the phenomenon? What is used for display? You can use io high and low levels to represent the button status. See if you can get the correct button status...
The 7-segment digital tube displays that when K8, K12, and K16 are pressed, LATD=0X0, and the displays of the other buttons are correct.
This post is from Microchip MCU
 
 
 

1305

Posts

0

Resources
5
 
When K8, K12, K16 are pressed, LATD=0X0------------Is there no display on the digital tube? Or is the digital tube displaying incorrectly? What does the digital tube display?
This post is from Microchip MCU

Comments

There is no display on the digital tube, and a common cathode digital tube is used.  Details Published on 2018-2-2 08:36
 
 
 

1305

Posts

0

Resources
6
 
What is the hardware connection relationship of the K8, K12, and K16 keys? Maybe your hardware has a problem (for example, the three keys K8, K12, and K16 are on the same row or column, the row or column lines are not in good contact, or similar problems). Is the problem in the simulation or the actual product? If it is a simulation, check the connection. If it is a real product, use a multimeter to measure the resistance between the pins.
This post is from Microchip MCU

Comments

For simulation, the attachment of the main post is the simulation file of proteus8.6, and the cof file can be compiled with the code of the main post.  Details Published on 2018-2-2 08:33
 
 
 

56

Posts

2

Resources
7
 
yang_alex posted on 2018-2-1 20:48 What is the hardware connection relationship of the K8, K12, and K16 keys? Maybe your hardware has a problem (for example, the three keys K8, K12, and K16 are on the same row or column, and the row line...
Simulation, the main post attachment is the proteus8.6 simulation file, and the cof file can be compiled with the code in the main post.
This post is from Microchip MCU
 
 
 

56

Posts

2

Resources
8
 
yang_alex posted on 2018-2-1 20:42 When K8, K12, K16 are pressed, LATD=0X0------------Is there no display on the digital tube? Or is the digital tube displaying incorrectly? What does the digital tube display?
There is no display on the digital tube, and a common cathode digital tube is used.
This post is from Microchip MCU
 
 
 

1305

Posts

0

Resources
9
 
Proteus simulation can set breakpoints, set breakpoints where the key values are determined, and see what the K8, K12, and K16 keys are recognized as, and then conduct targeted analysis.
This post is from Microchip MCU

Comments

Got it, thanks for the tip. The error is located at LINE101, the segment code of DEF is stored at 0x100, which exceeds the default address range of BSR (0x00~0xFF). Move the code up to within address 0xFF and the problem is solved.  Details Published on 2018-2-2 14:34
 
 
 

56

Posts

2

Resources
10
 
yang_alex posted on 2018-2-2 11:27 Proteus simulation can set breakpoints, set breakpoints where the key values are determined, and see what the K8, K12, and K16 keys are recognized as, and then conduct targeted analysis.
Done, thanks for the tip, the error is located in LINE101, the segment code of DEF is stored at 0x100, which exceeds the default address range of BSR (0x00~0xFF), move the code up to within the address 0xFF, and the problem is solved.
This post is from Microchip MCU
 
 
 

1305

Posts

0

Resources
11
 
Solved. It's good. If you can explain how the problem was solved, it's even better! Well done! This problem can't be seen just by looking at the code. (An expert may be able to do it, he is very familiar with assembly and knows how instructions take up space) Simulation debugging is very helpful for locating errors. Learning it will save a lot of time.
This post is from Microchip MCU
 
 
 

6366

Posts

4929

Resources
12
 
Simulation is still very useful. You can see in detail where the program has problems.
This post is from Microchip MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list