Write an Embedded C Program to Display Keypad Value on LED

30 Apr 2023 Balmiki Mandal 0 Embedded C

Embedded C Programming is an essential tool for embedded system development. It allows developers to create programs that control devices, such as microcontrollers, with precision and accuracy. In this tutorial, we will show you how to write an Embedded C Program to Display Keypad Value on LED.

What You Will Learn

  • How to configure a microcontroller's ports to read keypad inputs
  • How to write an Embedded C program to display the keypad value on LED
  • How to compile and debug the program on a microcontroller

Hardware Requirements

  • A microcontroller board
  • Connecting wires
  • LEDs
  • Keypad

Step 1: Connect the Hardware

The first step is to connect the hardware components. Connect the LEDs to the output pins of the microcontroller. Connect the keypad to the input pins of the microcontroller. Make sure the connections are correct.

The following diagram shows how the hardware should be connected:

Keypad and LED Connection

Step 2: Configure the Microcontroller Registers

Next, we need to configure the microcontroller registers so that it can read the keypad inputs. Each pin of the microcontroller needs to be configured as either an input or an output depending on its intended function. To configure the registers, we need to use the appropriate commands in the Embedded C Program.

Step 3: Write the Embedded C Program

Once the microcontroller has been configured, we can start writing the Embedded C Program to Display Keypad Value on LED. The program should include functions to read the keypad inputs and to display the corresponding value on the LED. The following example code shows how the program should be written:

#include <reg51.h> 
// Include the registers file 

void main() 
{ 
  while(1) 
  { 
    // Read the keypad input 
    int keypad_val = KEYPAD_READ(); 
    
    // Set the LED output based on the keypad value 
    if(keypad_val == 1) 
      LED = 1; 
    else if (keypad_val == 2) 
      LED = 2; 
    else if (keypad_val == 3) 
      LED = 3; 
  
    // Repeat for all keypad values 
  } 
}

Step 4: Compile and Debug the Program

Once the program has been written, we need to compile it and debug it on the microcontroller. This can be done using the appropriate tools for the microcontroller board. Use the debugging tools to check that the program is running correctly and that the values from the keypad are being displayed correctly on the LED.

Conclusion

In this tutorial, we have shown you how to write an Embedded C Program to Display Keypad Value on LED. We have also explained how to configure the microcontroller registers and how to compile and debug the program on the microcontroller board. With these skills, you can now develop more advanced embedded systems with microcontrollers.

 

Example 02 embedded C program to display the value of a keypad on an LED

#include <reg51.h> // Include header file for 8051 microcontroller

// Define pins for LED and keypad rows/columns
sbit LED = P1^0;
sbit ROW1 = P2^0;
sbit ROW2 = P2^1;
sbit ROW3 = P2^2;
sbit ROW4 = P2^3;
sbit COL1 = P2^4;
sbit COL2 = P2^5;
sbit COL3 = P2^6;

// Function to scan keypad and return key value
char scan_keypad()
{
    char key_val = 0; // Initialize key value to 0
    
    // Scan first row
    ROW1 = 0;
    if (COL1 == 0) key_val = '1';
    if (COL2 == 0) key_val = '2';
    if (COL3 == 0) key_val = '3';
    ROW1 = 1;
    
    // Scan second row
    ROW2 = 0;
    if (COL1 == 0) key_val = '4';
    if (COL2 == 0) key_val = '5';
    if (COL3 == 0) key_val = '6';
    ROW2 = 1;
    
    // Scan third row
    ROW3 = 0;
    if (COL1 == 0) key_val = '7';
    if (COL2 == 0) key_val = '8';
    if (COL3 == 0) key_val = '9';
    ROW3 = 1;
    
    // Scan fourth row
    ROW4 = 0;
    if (COL1 == 0) key_val = '*';
    if (COL2 == 0) key_val = '0';
    if (COL3 == 0) key_val = '#';
    ROW4 = 1;
    
    return key_val; // Return key value
}

// Main function
void main()
{
    char key = 0; // Initialize key value to 0
    
    while (1) // Infinite loop
    {
        key = scan_keypad(); // Scan keypad and get key value
        
        if (key != 0) // If a key is pressed
        {
            LED = 1; // Turn on LED
            P0 = key; // Display key value on LED
            while (scan_keypad() != 0); // Wait until key is released
            LED = 0; // Turn off LED
        }
    }
}

In this program, we define the pins for the LED and keypad rows/columns using the sbit keyword. We also define a function scan_keypad() to scan the keypad and return the value of the pressed key.

In the main() function, we continuously scan the keypad using a while loop. If a key is pressed, we turn on the LED and display the key value on the LED by setting the value of P0 (Port 0) to the key value. We also wait until the key is released before turning off the LED.

Note that this program is for illustration purposes only and may need to be modified for specific hardware configurations or requirements.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.