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.
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
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.
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
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.
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?
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.
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
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.
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.
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.
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
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.
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.