MSP430 MCU simulation example based on Proteus 12-4X4 matrix keyboard key value display
[Copy link]
This post was last edited by tiankai001 on 2019-1-7 08:19 This content is originally created by EEWORLD forum user tiankai001. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source 1. Task RequirementsNumber the 4X4 matrix keyboard. If one of the keys is closed, the corresponding key number will be displayed on the LED digital tube. II. Analysis and DescriptionThe 4X4 matrix keyboard only needs to occupy an 8-bit I/O port. The design of the hardware circuit is relatively simple. The key lies in how to determine the key position of the matrix keyboard in the program. III. Hardware CircuitThe hardware circuit is shown in the figure below. Select the P4 port of MSP430F247 microcontroller to connect the digital tube to display the key number. The 8 pins of P5 port are respectively connected to the row and column lines of the matrix keyboard. The hardware circuit is shown in the figure below. Select the P4 port of the MSP430F247 microcontroller to connect the digital tube to display the key number. The 8 pins of the P5 port are respectively connected to the row and column lines of the matrix keyboard. The hardware circuit is shown in the figure below. In the circuit diagram, the column lines P5.4~P5.7 are connected to the power supply through pull-up resistors and are in the input state; the row lines P5.0~P5.3 are in the output state. When no key on the keyboard is closed, the inputs of all column lines P5.4~P5.7 are all high level. When a key on the keyboard is closed, the corresponding row and column lines are short-circuited. When detecting whether a key is closed, first make all 4 row lines output low level, and then read the status of 4 column lines. If all are high level, it means that no key is closed. If any key is closed, due to the pull-up resistor on the column line, the value read on the row line will be a non-full "1". Fourth, Program Design To determine which key on the matrix keyboard is closed, the row scanning method is usually used, also known as the row (or column) scanning query method, and its software is mainly based on the scanning method. The procedure of keyboard scanning query method can be roughly divided into the following steps: 1. Detect whether there is a key closed at present. First, look at the input column line. Assume that all four row lines output low level and all four column lines have pull-up resistors. When no key is closed, the inputs of the four column lines are all 1. However, when any one of the four keys connected to a row line is closed, the column line will input a low level. That is, when a column line inputs a low level, it must be that a key connected to this crack line is closed. 34)]2. Remove key jitter. When a key is detected to be closed, extend the time before making the next step of detection and judgment. 3. If a key is closed, detect which key is closed Line-by-line scanning method: output 0 signal on 4 row lines respectively. The first time, output low level on the first row line, and high level on other row lines. The second time, output low level on the second row line, and high level on other row lines. The third time, output low level on the third row line, and high level on other row lines. The fourth time, output low level on the fourth row line, and high level on other row lines. When a row line outputs low level, if a key is pressed on this row, then the column line of the corresponding key will read 0, so it can be determined that the key is pressed. - //main.c #include "msp430f247.h" #include "stdlib.h" #include "string.h" /************************************************Software delay, main frequency 1M*******************/ #define CPU_F1 ((double)1000000) #define delay_us1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000000.0)) #define delay_ms1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000.0)) /************************************************************************/ //Common anode digital tube segment code table unsigned char const Led_Tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80, 0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //Common cathode digital tube segment code table unsigned char const Led_Tab2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f, 0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; static unsigned char key; unsigned char KeyScan(void);// unsigned char GetKeyValue(unsigned char keycode);// /**************************************************** Function name: main function Function function: matrix keyboard number display Entry parameter: none Exit parameter: none Description: Author: Lao Ma Shi Tu MCU Date: January 7, 2018 **************************************************/ main() { unsigned char key; WDTCTL = WDTPW + WDTHOLD; //Turn off the watchdog P4DIR=0xff; //Port initialization P4OUT=0x00; //Port initialization //Lines P5.0~P5.3 are in output state. Column lines P5.4~P5.7 are input P5DIR=0x0f;// while(1) { key=GetKeyValue(KeyScan());//Display scan if(key != 255) { P4OUT=Led_Tab2[key]; } } } unsigned char KeyScan(void) { unsigned char ScCode,ReCode; P5OUT=0x00; if((P5IN & 0xf0) != 0xf0)//Judge whether a key is closed { delay_ms1M(40); if((P5IN & 0xf0) != 0xf0)//Judge whether a key is closed again after delay, { ScCode=0xfe;//Scan the initial value line by line, scan the first line first while((ScCode&0x0f) != 0x0f)//Row scan completed { P5OUT=ScCode;//Output row scan codeif((P5IN & 0xf0) != 0xf0)//A key is closed on the current row{ ReCode=(P5IN & 0xf0) | 0x0f;//Read the high 4-bit column value and set the low 4 bits to 1 key=(ScCode & ReCode);//Combining row and column to get the keyboard codereturn key; } else//If no key is closed on the scanned row, scan the next row{ ScCode=(ScCode<<1)|0x01;//Shift the row scan code left by one bit} } } return 0xff;//No key is pressed} return 0xff;//No key is pressed} unsigned char GetKeyValue(unsigned char keycode) { unsigned char keyval; switch(keycode) { case 0x77: keyval=0; break; case 0x7b: keyval=1; break; case 0x7d: keyval=2; break; case 0x7e: keyval=3; break; case 0xb7: keyval=4; break; case 0xbb: keyval=5; break; case 0xbd: keyval=6; break; case 0xbe: keyval=7; break; case 0xd7: keyval=8; break; case 0xdb: keyval=9; keyval=11; break; case 0xe7: keyval=12; break; case 0xeb: keyval=13; break; case 0xed: keyval=14; break; case 0xee: keyval=15; break; default: keyval=255; } return keyval; }
复制代码 5. Program DescriptionThe main program obtains the key value by calling the keyboard scanning program and displays the keyboard number through the digital tube. The keyboard scan first reads the column line input. If not all are 1, it will delay for a certain period of time and then determine whether all the column lines are 1 again. If still not all are 1, it can be determined that there is a stable key action, and the position of the key is obtained by scanning line by line. There are still two problems in the program. First, it takes a certain amount of time to delay the debounce in the key scan, which wastes the computing resources of the single-chip microcomputer; second, if the key is closed and does not move after the key is scanned, the main program will get multiple identical key values, that is, repeated key presses. This situation can be solved by judging the action of the key popping up Six, simulation results and analysis After drawing the circuit diagram in proteus, double-click the single-chip microcomputer to load the executable file into the single-chip microcomputer, click Run, and observe the display of the digital tube. As shown in the figure below. Matrix keyboard number display Entry parameters: None Exit parameters: None Description: Author: Lao Ma Shi Tu Microcontroller Date: January 7, 2018 ******************************************************/ main() { unsigned char key; WDTCTL = WDTPW + WDTHOLD; // Turn off watchdog P4DIR = 0xff; // Port initialization P4OUT = 0x00; // Port initialization // Row lines P5.0 ~ P5.3 are in output state. Column lines P5.4~P5.7 are input P5DIR=0x0f;// while(1) { key=GetKeyValue(KeyScan());//Display scan if(key != 255) { P4OUT=Led_Tab2[key]; } } } unsigned char KeyScan(void) { unsigned char ScCode,ReCode; P5OUT=0x00; if((P5IN & 0xf0) != 0xf0)//Judge whether a key is closed { delay_ms1M(40); if((P5IN & 0xf0) != 0xf0)//Judge whether a key is closed again after delay, { ScCode=0xfe;//Scan the initial value line by line, scan the first line first while((ScCode&0x0f) != 0x0f)//Row scan completed { P5OUT=ScCode;//Output row scan codeif((P5IN & 0xf0) != 0xf0)//A key is closed on the current row{ ReCode=(P5IN & 0xf0) | 0x0f;//Read the high 4-bit column value and set the low 4 bits to 1 key=(ScCode & ReCode);//Combining row and column to get the keyboard codereturn key; } else//If no key is closed on the scanned row, scan the next row{ ScCode=(ScCode<<1)|0x01;//Shift the row scan code left by one bit} } } return 0xff;//No key is pressed} return 0xff;//No key is pressed} unsigned char GetKeyValue(unsigned char keycode) { unsigned char keyval; switch(keycode) { case 0x77: keyval=0; break; case 0x7b: keyval=1; break; case 0x7d: keyval=2; break; case 0x7e: keyval=3; break; case 0xb7: keyval=4; break; case 0xbb: keyval=5; break; case 0xbd: keyval=6; break; case 0xbe: keyval=7; break; case 0xd7: keyval=8; break; case 0xdb: keyval=9; keyval=11; break; case 0xe7: keyval=12; break; case 0xeb: keyval=13; break; case 0xed: keyval=14; break; case 0xee: keyval=15; break; default: keyval=255; } return keyval; }[/code] 5. Program DescriptionThe main program obtains the key value by calling the keyboard scanning program and displays the keyboard number through the digital tube. The keyboard scan first reads the column line input. If not all are 1, it will delay for a certain period of time and then determine whether all the column lines are 1 again. If still not all are 1, it can be determined that there is a stable key action, and the position of the key is obtained by scanning line by line. There are still two problems in the program. First, it takes a certain amount of time to delay the debounce in the key scan, which wastes the computing resources of the single-chip microcomputer; second, if the key is closed and does not move after the key is scanned, the main program will get multiple identical key values, that is, repeated key presses. This situation can be solved by judging the action of the key popping up Six, simulation results and analysis After drawing the circuit diagram in proteus, double-click the single-chip microcomputer to load the executable file into the single-chip microcomputer, click Run, and observe the display of the digital tube. As shown in the figure below. Matrix keyboard number display Entry parameters: None Exit parameters: None Description: Author: Lao Ma Shi Tu Microcontroller Date: January 7, 2018 ******************************************************/ main() { unsigned char key; WDTCTL = WDTPW + WDTHOLD; // Turn off watchdog P4DIR = 0xff; // Port initialization P4OUT = 0x00; // Port initialization // Row lines P5.0 ~ P5.3 are in output state. Column lines P5.4~P5.7 are input P5DIR=0x0f;// while(1) { key=GetKeyValue(KeyScan());//Display scan if(key != 255) { P4OUT=Led_Tab2[key]; } } } unsigned char KeyScan(void) { unsigned char ScCode,ReCode; P5OUT=0x00; if((P5IN & 0xf0) != 0xf0)//Judge whether a key is closed { delay_ms1M(40); if((P5IN & 0xf0) != 0xf0)//Judge whether a key is closed again after delay, { ScCode=0xfe;//Scan the initial value line by line, scan the first line first while((ScCode&0x0f) != 0x0f)//Row scan completed { P5OUT=ScCode;//Output row scan codeif((P5IN & 0xf0) != 0xf0)//A key is closed on the current row{ ReCode=(P5IN & 0xf0) | 0x0f;//Read the high 4-bit column value and set the low 4 bits to 1 key=(ScCode & ReCode);//Combining row and column to get the keyboard codereturn key; } else//If no key is closed on the scanned row, scan the next row{ ScCode=(ScCode<<1)|0x01;//Shift the row scan code left by one bit} } } return 0xff;//No key is pressed} return 0xff;//No key is pressed} unsigned char GetKeyValue(unsigned char keycode) { unsigned char keyval; switch(keycode) { case 0x77: keyval=0; break; case 0x7b: keyval=1; break; case 0x7d: keyval=2; break; case 0x7e: keyval=3; break; case 0xb7: keyval=4; break; case 0xbb: keyval=5; break; case 0xbd: keyval=6; break; case 0xbe: keyval=7; break; case 0xd7: keyval=8; break; case 0xdb: keyval=9; keyval=11; break; case 0xe7: keyval=12; break; case 0xeb: keyval=13; break; case 0xed: keyval=14; break; case 0xee: keyval=15; break; default: keyval=255; } return keyval; }[/code] 5. Program DescriptionThe main program obtains the key value by calling the keyboard scanning program and displays the keyboard number through the digital tube. The keyboard scan first reads the column line input. If not all are 1, it will delay for a certain period of time and then determine whether all the column lines are 1 again. If still not all are 1, it can be determined that there is a stable key action, and the position of the key is obtained by scanning line by line. There are still two problems in the program. First, it takes a certain amount of time to delay the debounce in the key scan, which wastes the computing resources of the single-chip microcomputer; second, if the key is closed and does not move after the key is scanned, the main program will get multiple identical key values, that is, repeated key presses. This situation can be solved by judging the action of the key popping up Six, simulation results and analysis After drawing the circuit diagram in proteus, double-click the single-chip microcomputer to load the executable file into the single-chip microcomputer, click Run, and observe the display of the digital tube. As shown in the figure below. = 0xf0)//The current row has a key closed { ReCode=(P5IN & 0xf0) | 0x0f;//Read the high 4-bit column value and set the low 4 bits to 1 key=(ScCode & ReCode);//The row and column are combined to get the keyboard code return key; } else//If there is no key closed in the scanned row, scan the next row { ScCode=(ScCode<<1)|0x01;//The row scan code is shifted left by one bit } } } return 0xff;//No key is pressed } return 0xff;//No key is pressed } unsigned char GetKeyValue(unsigned char keycode) { unsigned char keyval; switch(keycode) { case 0x77: keyval=0; break; case 0x7b: keyval=1; break; case 0x7d: keyval=2; break; case 0x7e: keyval=3; break; case 0xb7: keyval=4; break; case 0xbb: keyval=5; break; case 0xbd: keyval=6; break; case 0xbe: keyval=7; break; case 0xd7: keyval=8; break; case 0xdb: keyval=9; break; case 0xdd: keyval=10; break; case 0xde: keyval=11; break; case 0xe7: keyval=12; keyval=13; break; case 0xed: keyval=14; break; case 0xee: keyval=15; break; default: keyval=255; } return keyval; }[/code] 5. Program DescriptionThe main program obtains the key value by calling the keyboard scanning program and displays the keyboard number through the digital tube. The keyboard scan first reads the column line input. If not all are 1, it will delay for a certain period of time and then determine whether all the column lines are 1. If still not all are 1, it can be determined that there is a stable key action, and the position of the key is obtained by scanning line by line. There are still two problems in the program. One is that the delay de-bounce in the key scanning takes a certain amount of time, which wastes the computing resources of the single-chip microcomputer; the other is that if the key is closed and does not move after the key is scanned, the main program will get multiple identical key values, that is, repeated key presses. This situation can be solved by judging the action of the key popping up VI. Simulation results and analysisAfter drawing the circuit diagram in proteus, double-click the microcontroller to load the executable file into the microcontroller, click run, and observe the display of the digital tube. As shown in the figure below. = 0xf0)//The current row has a key closed { ReCode=(P5IN & 0xf0) | 0x0f;//Read the high 4-bit column value and set the low 4 bits to 1 key=(ScCode & ReCode);//The row and column are combined to get the keyboard code return key; } else//If there is no key closed in the scanned row, scan the next row { ScCode=(ScCode<<1)|0x01;//The row scan code is shifted left by one bit } } } return 0xff;//No key is pressed } return 0xff;//No key is pressed } unsigned char GetKeyValue(unsigned char keycode) { unsigned char keyval; switch(keycode) { case 0x77: keyval=0; break; case 0x7b: keyval=1; break; case 0x7d: keyval=2; break; case 0x7e: keyval=3; break; case 0xb7: keyval=4; break; case 0xbb: keyval=5; break; case 0xbd: keyval=6; break; case 0xbe: keyval=7; break; case 0xd7: keyval=8; break; case 0xdb: keyval=9; break; case 0xdd: keyval=10; break; case 0xde: keyval=11; break; case 0xe7: keyval=12; keyval=13; break; case 0xed: keyval=14; break; case 0xee: keyval=15; break; default: keyval=255; } return keyval; }[/code] 5. Program DescriptionThe main program obtains the key value by calling the keyboard scanning program and displays the keyboard number through the digital tube. The keyboard scan first reads the column line input. If not all are 1, it will delay for a certain period of time and then determine whether all the column lines are 1. If still not all are 1, it can be determined that there is a stable key action, and the position of the key is obtained by scanning line by line. There are still two problems in the program. One is that the delay de-bounce in the key scanning takes a certain amount of time, which wastes the computing resources of the single-chip microcomputer; the other is that if the key is closed and does not move after the key is scanned, the main program will get multiple identical key values, that is, repeated key presses. This situation can be solved by judging the action of the key popping up VI. Simulation results and analysisAfter drawing the circuit diagram in proteus, double-click the microcontroller to load the executable file into the microcontroller, click run, and observe the display of the digital tube. As shown in the figure below. com/large/5682000276f08c835f12[/img]com/large/5682000276f08c835f12[/img]
|