Microcontroller Embedded C Programming Lecture 166| Detecting key press event

  • Post author:
  • Post category:Blog

 

Detecting key press event

 

In this article, let’s understand implementing code to detect different key press events. 

First of all, you should remember that when no key is pressed, C1, C2, C3, and C4 read HIGH.

The key detecting logic is very simple →  Scan for keys row by row one at a time.

 

For that, now you have to get the help of output pins.

Figure 1. When key ‘2’ pressed
Figure 1. When key ‘2’ pressed

 

In Figure 1 you can see that R2, R3, and R4 are made HIGH and only R1 is made LOW. So, at this stage software can detect the key pressed in the R1 row only. 

For example, when key ‘2’ is pressed, C2 will be pulled down to LOW and reads LOW. That’s why C2 will read 0. And C1 is still 1, C3 is 1, and C4 is 1. That’s how you detect which key is pressed. So, here C2 returns 0, hence the software should conclude that key ‘2’ is pressed. 

So, when R1 is LOW, if C2 reads LOW then conclude that key ‘2’ is pressed. That’s the logic.

 

Then proceed to the next row, which is R2. 

Detecting key press event
Figure 2. When key ‘6’ pressed

Make R1 HIGH, R3 HIGH, and R4 HIGH, and make R2 LOW. So, at this stage software can detect the key pressed in the R2 row only. 

For example, when key ‘6’ is pressed, C3 will be pulled to LOW and reads LOW.

So, when you press key ‘6’, what happens here? C3 reads 0 and the rest will read 1. And hence you should conclude that key ‘6’ is pressed, because R2 is LOW. 

After that, you have to proceed to R3. 

 

Make  R3 LOW and all the other rows HIGH. After that, you can scan for the keys in row R3. Like that you have to cover all the rows and you have to do this for every iteration of an infinite loop. 

So, you have to scan row by row in an infinite loop in the program.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.