Saturday, June 25, 2022
HomeElectronicsMatrix Keypad Embedded-C Driver For A Microcontroller

Matrix Keypad Embedded-C Driver For A Microcontroller


saniEmbedded system mission that typically includes a microcontroller unit (MCU) person interface (UI) is a typical and inevitable requirement. The straightforward type of arriving this selection of UI is with the assistance of a monochrome LCD, which serves as a primary output machine and a matrix keypad interface for accepting inputs from the person. In reality, majority of the UI designs are with 2×16 LCD ASCII compliant show, sometimes JHD162A together with both 4×4 or 4×3 matrix keypads.

Right here, we begin with the event of a tool driver for a matrix keypad, sometimes written in embedded-C for microcontroller. We’ll take into account an 8-bit AT89S52 MCU (works effectively for any C51 MCU), which is definitely out there and whose structure is acquainted to many people. We’ll implement a C driver for scanning a key within the polled mode first and see learn how to take a look at it with out implementing or coupling the output driver (LCD).

Fig. 1: Experimental setup for interfacing keypad and testing its driver

We will then examine the design features of changing this driver by binding/locking it into an asynchronous exterior interrupt pin (/INT0 pin) and current the implementation and testing features concerned in attaining driver in embedded C. It’s suggested to think about additional features with Keil µVision 5 IDE with C51 cross-compiler (that help legacy C51 microcontroller households) put in in Home windows 10 host PC together with simulation software program resembling Proteus 7.7 or above for validation.

The mission has 4 components the place the supply program (Project1 by way of Project4) of every half is written in embedded C programming language. The hex code generated by way of Keil µVision 5 IDE is used for simulation and verification utilizing Proteus software program.

Fig. 2: Typical keypad layout diagram
Fig. 2: Typical keypad format diagram

Earlier than we dig into the technicality, we have to perceive the necessity for this conversion. Since majority of the embedded initiatives take care of this customary polled-loop driver for keypad, the necessity to convert arises in following circumstances:

(a) When the MCU is predicted to deal with the site visitors over serial (sometimes UART) port for attaining networking. Allow us to take into account an instance of dealing with the info utilizing GSM’s ASCII terminal instructions (AT) within the type of SMS. Whereas the MCU is busy in dealing with this SMS site visitors utilizing handshaking choices, if person presses any enter, forcing the microcontroller’s consideration, the normal strategy of polled-loop driver fails to recognise the person inputs. This requirement causes a major problem when the person is trying to configure the info and site visitors is flooding over networking (on this case serial) interface.

Fig. 3: Source listing for polled-loop key scanning implemented in embedded-C
Fig. 3: Supply itemizing for polled-loop key scanning carried out in embedded-C

(b) If the MCU is busy in performing the sensor information processing, both utilizing the off-chip ADC interface choice or on-chip ADC register polling, and the person is triggering an enter utilizing the enter machine. On this case additionally, the person consideration will likely be unanswered because the MCU is already busy in sensory process.

Polled mode 4×3 embedded C driver and its validation for C51 MCU

Allow us to take into account the 4×3 matrix keypad with 4 rows and three columns being related to Port 1 of AT89S52 MCU, as proven in Fig. 1. That is mainly the connections in Proteus simulation software program. The 4 LEDs are used just for testing and are related to P2.0 by way of P2.3. A typical keypad format diagram is proven in Fig. 2. The polled-loop key-scanning algorithm could be very easy and simple, as proven in steps 1 by way of 7 given under:

Step-1: Configure the columns as inputs (logic-1) and rows as outputs (logic-0)
Step-2: Begin polling the KeyScan by searching for any change in column (1 to 0)
Step-3: Is there any change in columns (i.e., C1=0 or C2=0 or C3=0)
If sure,
then, go to Step – 4.
else
Go to Step-2.
Step-4: Make R1=0 and hold R2=R3=R4=1 and,
If C1=0 then,
pressed key’s – ‘1’ and return.
else if C2=0 then,
pressed key’s – ‘2’ and return.
else if C3=0 then,
pressed key’s – ‘3’ and return.
else
go to step – 5:
Step-5: Make R2=0 and hold R1=R3=R4=1 and,
If C1=0 then,
pressed key’s – ‘4’ and return.
else if C2=0 then,
pressed key’s – ‘5’ and return.
else if C3=0 then,
pressed key’s – ‘6’ and return.
else
go to step – 6:
Step-6: Make R3=0 and hold R1=R2=R4=1 and,
If C1=0 then,
pressed key’s – ‘7’ and return.
else if C2=0 then,
pressed key’s – ‘8’ and return.
else if C3=0 then,
pressed key’s – ‘9’ and return.
else
go to step – 7
Step-7: Make R4=0 and hold R1=R2=R3=1 and,
If C1=0 then,
pressed key’s – ‘*’ and return.
else if C2=0 then,
pressed key’s – ‘0’ and return.
else if C3=0 then,
pressed key’s – ‘#’ and return.
else
go to step – 2

The implementation of this primary half (Project1) in embedded C perform is proven in Fig. 3. Upon any key press, the ASCII equal of the pressed key’s recognized and is returned. Therefore, within the testing of the circuit given in Fig. 1, the decrease nibble alone is adequate because the LEDs point out the pressed digit.

For instance, if the person presses key-‘1’, the ASCII of ‘1’, which is 0x31, i.e., 0110 0001, is proven on the LEDs. In the identical means, all of the keys of the keypad could also be examined by observing their equal binary outputs on LEDs.

Fig. 4: Circuit diagram for interrupt driven keypad
Fig. 4: Circuit diagram for interrupt pushed keypad

Allow us to now take a look at the strategy of changing the above polled-loop driver by binding the interrupt and show the method of context switching from the applying to interrupt service routine (ISR), and vice-versa.

Circuit diagram for interrupt-driven keypad

There are two exterior interrupt pins /INT0 and /INT1 of AT89S52, which can be found as alternate-pin capabilities of the P3.2 and P3.3 GPIOs, respectively. Amongst these, allow us to take into account utilization of /INT0 pin, the P3.2 pin. A 3-input AND gate 4073 IC is used such that its output should be related to /INT0 (P3.2), whereas the inputs are related to the three columns C1, C2, and C3, as proven in Fig. 4.

Implementation of interrupt-driven driver

For attaining the important thing enter, because the columns had been locked to interrupt pin, any key pressed will end result right into a change of column sign and thereby generate an interrupt at /INT0 pin. We should allow the interrupt /INT0 pin by configuring the IE register. Furthermore, we should configure it as an edge-triggered sort of interrupt utilizing TCON register in principal software.

Fig. 5: Device initialisation routine in the main code for interrupt driven keypad driver
Fig. 5: Machine initialisation routine in the primary code for interrupt pushed keypad driver

The fundamental configuration within the code for Project3 is proven in Fig. 5. The MCU’s C-ISR perform ‘Keypad_ISR’ is carried out as indicated in Fig. 6.

Fig. 6: Keypad C ISR for AT89S52 MCU
Fig. 6: Keypad C ISR for AT89S52 MCU

Some essential observations of the carried out ISR are:

  1. The interrupt should be configured as edge triggered. That is achieved in AT89S52 by configuring TCON register. In any other case, even for a single press the interrupt will carry on operating as if a number of occasions are occurring.
  2. An interrupt service routine perform should not return any worth. Therefore, we use the idea of world shared useful resource, which in our case would be the international variable. Within the implementation, the variable ‘Key’ will likely be performing as a shared useful resource. Discover that this shared useful resource is accessible not solely in the primary but additionally within the ISR.
  3. Within the implementation given right here an LED is deliberately related to the unused GPIO P1.7 pin of AT89S52, which is able to toggle upon the important thing press as a result of line-3 of Fig. 6.
  4. At any time when a key’s pressed by the person, the corresponding column standing within the keypad will likely be modified to 1. In consequence, the interrupt will likely be triggered, and the applying will likely be paused because it enters into /INT0 ISR. This course of is known as {hardware} context switching. Discover that in this time, the system stack will likely be used. (Financial institution-1 in RAM)
  5. Contained in the ISR, the precise checking or figuring out the particular row’s key, which is chargeable for the important thing press, is detected and the confirmed key’s stored within the shared useful resource variable ‘Key’ and the ISR ends by forcing it to return. The suspended software will now be prepared by context retrieval utilizing stack’s POP operations.
  6. The pseudo-code proven in Fig. 7 is proposed for detecting a legitimate key pressed in principal code for additional software growth.

    Fig. 7: Pseudo-code source listing for detecting a keypress in main application
    Fig. 7: Pseudo-code supply itemizing for detecting a keypress in principal software

  7. One other vital inference is that there will likely be no key debouncing downside, in contrast to in polled-loop keypad. In consequence, the intentional delay for each consecutive key press will not be needed.

By contemplating this strategy of interrupt-based keypad driver, one can implement and obtain the real-time response for UI with out porting the RTOS into the MCU. It’s because the ISR will run no matter what the applying is doing, forcing the CPU’s consideration. Subsequently, in consequence if the design is finished with this advisable UI, other than attaining the real-time response, the reminiscence footprint (each Flash and RAM) can be significantly decreased.

Fig. 8: User interface design with interrupt based matrix keypad and LCD (8-bit)
Fig. 8: Person interface design with interrupt based mostly matrix keypad and LCD (8-bit)

After downloading the supply packages from EFY web site, utilizing circuit proven in Fig.8 together with Project2, the polled-loop software is examined with any 4-digit enter as person ID and ‘1982’ as password. The ISR-locked keypad driver is examined utilizing Fig.4 and Project3. Lastly, the identical software with LCD is examined utilizing Fig. 9 and Project4.

Fig. 9: User interface design with interrupt based matrix keypad using LCD and LED
Fig. 9: Person interface design with interrupt based mostly matrix keypad utilizing LCD and LED

Obtain Supply Code


N. Abid Ali Khan is Assistant Professor within the Division of ECE at Vasavi School of Engineering (Autonomous), Hyderabad. He has been engaged on power-efficient embedded networking architectures and is enthusiastic about microcontrollers, ARM, RTOS, embedded programs, and programming



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments