11/6/2020 11 NGUYEN THANH NGHIA HCMC University of Technology and Education Faculty of Electrical & Electronic Engineering No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 1. Introduction The push button with active low and active high is presented as in bellow.
NGUYEN THANH NGHIA 4
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 1. Introduction 1.1 Switch Bounce Waveform The problem is that any electronic or digital circuit which the mechanical switch is input interfaced too could read these multiple switch operations as a series of ON and OFF signals lasting several milliseconds instead of just the one intended single and positive switching action.
NGUYEN THANH NGHIA 5
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 1. Introduction 1.1 Switch Bounce Waveform This multiple switch closing (or opening) action is called Switch Bounce in switches with the same action being called Contact Bounce in relays. Also, as switch and contact bounce occurs during both the opening and closing actions, the resultant bouncing and arcing across the contacts causes wear, increases contact resistance, and lowers the working life of the switch.
NGUYEN THANH NGHIA 6
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.1 Control 8 LEDs by two push button ON, OFF Schematic:
NGUYEN THANH NGHIA 7
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.1 Control 8 LEDs by two push button ON, OFF Operation: Normally, 2 push buttons is opened and 2 inputs have 2 pull-up resistors. So the logic level is 1. When the key is pressed, the input is conneted to ground. Therefore, the input logic level is 1. When the push button is released. The input logic level is returned to 1.
NGUYEN THANH NGHIA 8
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.1 Control 8 LEDs by two push button ON, OFF Flowchart:
NGUYEN THANH NGHIA 9
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.1 Control 8 LEDs by two push button ON, OFF Code: #include <TV_16F887.C> #DEFINE ON PIN_E0 #DEFINE OFF PIN_E1 VOID MAIN() { SET_TRIS_E(0xFF); SET_TRIS_D(0x00); OUTPUT_D(0X00); WHILE(TRUE) { WHILE (INPUT(ON)); //Check logic level 1 OUTPUT_D(0XFF); WHILE (INPUT(OFF)); //Check logic level 1 OUTPUT_D(0X00); } }
NGUYEN THANH NGHIA 10
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.2 Control 8 LEDs by three push button ON, OFF, INV Schematic:
NGUYEN THANH NGHIA 11
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.2 Control 8 LEDs by three push button ON, OFF, INV Operation: When power is on, 8 LEDs is turned off. When button ON is pressed, 4 low LEDs (D<3:0>) turn ON and 4 high LEDs (D<7:4>) continous OFF. When button INV is pressed, the status of 8 LEDs is reversed.
NGUYEN THANH NGHIA 12
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.2 Control 8 LEDs by three push button ON, OFF, INV Flowchart:
NGUYEN THANH NGHIA 13
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.2 Control 8 LEDs by three push button ON, OFF, INV #INCLUDE<TV_16F887.C> Code: #DEFINE ON PIN_E0 #DEFINE OFF PIN_E1 #DEFINE INV PIN_E2 UNSIGNED INT8 X=0; VOID MAIN() { SET_TRIS_E(0xFF); SET_TRIS_D(0x00); WHILE(TRUE) { X=0; OUTPUT_D(X); WHILE (INPUT(ON)); X= 0X0F; OUTPUT_D(X); DO { IF(!INPUT(INV)) //! comparition { X= ~ X; OUTPUT_D(X); } } WHILE(INPUT(OFF)); } }
NGUYEN THANH NGHIA 14
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.3 Control 8 LEDs by ON, OFF, INV with debounce Schematic:
NGUYEN THANH NGHIA 15
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.3 Control 8 LEDs by ON, OFF, INV with debounce Flowchart:
NGUYEN THANH NGHIA 16
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.3 Control 8 LEDs by ON, OFF, INV with debounce #INCLUDE<TV_16F887.C> Code:#DEFINE ON PIN_E0 #DEFINE OFF PIN_E1 VOID MAIN() #DEFINE INV PIN_E2 { UNSIGNED INT8 X=0; SET_TRIS_E(0xFF); VOID CHONGDOI_INV () SET_TRIS_D(0x00); { WHILE(TRUE) IF (!INPUT(INV)) { { X=0; OUTPUT_D(X); DELAY_MS (20); WHILE (INPUT(ON)); IF(!INPUT(INV)) X= 0X0F; OUTPUT_D(X); { DO X = ~ X; OUTPUT_D(X); { WHILE (!INPUT(INV)); CHONGDOI_INV(); } }WHILE(INPUT(OFF)); } } } } NGUYEN THANH NGHIA 17 Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.4 Count from 00 to 99 with Start and Stop Schematic:
NGUYEN THANH NGHIA 18
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.4 Count from 00 to 99 with Start and Stop Operation: When the power is supplied, the LED displays 00. When the button Start is pressed, the circuit counts from 00 to 99. If the button Stop is pressed, the counting value is hold and stop counting. When the button Start is pressed again, then the counter is counted again. Seven segment led is common Anode, and this led is directly connected with microcontroller.
NGUYEN THANH NGHIA 19
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.3 Count from 00 to 99 with Start and Stop Flowchart:
NGUYEN THANH NGHIA 20
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 2. Application: with signle push button 2.3 Count from 00 to 99 with Start and Stop WHILE(TRUE) Code: { #INCLUDE <TV_16F887.C> FOR (DEM=0;DEM<100;DEM++) #DEFINE START PIN_E0 { #DEFINE STOP PIN_E1 OUTPUT_C(MA7DOAN[DEM %10]); SIGNED INT8 DEM; OUTPUT_D(MA7DOAN[DEM /10]); INT1 CHOPHEP = 0; DELAY_MS(100); VOID MAIN() DO { { SET_TRIS_E(0xFF); IF (!INPUT(START)) SET_TRIS_C(0x00); {CHOPHEP=1;} ELSE IF (!INPUT(STOP) ) SET_TRIS_D(0x00); {CHOPHEP=0;} CHOPHEP=0; } WHILE(CHOPHEP==0); } } } NGUYEN THANH NGHIA 21 Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.1 Why matrix keypad? Typically one port pin is required to read a button. When there are a lot of button that has to be read, it is not feasible to allocate one pin for each of them. This is when a matrix keypad arrangement is used to reduce the pin count.
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.2 Operation The blue lines are the columns and the red lines the rows. The columns and the rows are NOT in contact! Suppose that we want to make a key matrix. To do this, we will have to connect a button to each knot. The buttons will have a push-to-make contact. When the operator pushes this button, it will connect the column and the row that it corresponds to.
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.2 Operation With a matrix including 16 key, we only need 8 line signal: 4 for raws and 4 for columns – called matrix 4×4. H0H1H2H3 are input and C0C1C2C3 are output. When the keys in matrix is not press then H0H1H2H3 = ‘1111’. NGUYEN THANH NGHIA 26 Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.2 Operation Flowchart of keypad 4x4. Column code of 4 lower bit including the states: “1110B”, “1101B”, “1011B”, “0111B” combine with 4 higher bit of raw is always “1111B”, so the data combined with byte in hexa is “FEH”, “FDH”, “FBH”, “F7H”. NGUYEN THANH NGHIA 27 Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.2 Operation The flowchart of scan matrix keypad 4x4 with debounce.
NGUYEN THANH NGHIA 28
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.4 Scan matrix keypad, display on seven segment led Operation: Connect Microcontroller with 1 led seven segment and 4 × 4 key matrix. Create a program to scan a matrix keypad, display the name of the key pressed on the 7-segment led.
{ SIGNED INT8 MAPHIM, HANG, COT; MAPHIM=HANG=0XFF; FOR(COT=0;COT<4;COT++) { OUTPUT_B(MAQUETCOT[COT]); IF (!INPUT(PIN_B4)) {HANG=0; BREAK;} ELSE IF (!INPUT(PIN_B5)) {HANG=1; BREAK;} ELSE IF (!INPUT(PIN_B6)) {HANG=2; BREAK;} ELSE IF (!INPUT(PIN_B7)) {HANG=3; BREAK;} } IF (HANG!=0XFF) MAPHIM = COT*4 + HANG; RETURN(MAPHIM); } UNSIGNED INT KEY_4X4() { UNSIGNED INT8 MPT1,MPT2; MPT1=QUET_MT_PHIM(); IF (MPT1!=0XFF) { DELAY_MS(10); MPT1=QUET_MT_PHIM(); DO{MPT2=QUET_MT_PHIM();} WHILE (MPT2==MPT1); } RETURN(MPT1); }
NGUYEN THANH NGHIA 33
Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.5 Scan matrix keypad, display on 2 seven segment led Microcontroller connect with 2 7-segment leds and 4×4 matrix keypad. Create a program to scan the matrix keypad, display keycodes pressed and shift. For beginning, the dot will be displayed at the 0th LED. When pressing any key, for example 3, the dot will shift to the 1st LED, the number 3 will display at the 0th LED. if the key 5 is pressed, the number 3 of the 0th LED will shift to the 1st LED, the number 5 will display at the 0th LED. NGUYEN THANH NGHIA 34 Chapter 7: PUSH BUTTON - MATRIX KEYPAD 4x4 3. Matrix keypad 3.5 Scan matrix keypad, display on 2 seven segment led Schematic: