Activity 1. Keypad 4x4 and LEDs

The purpose of this activity is for microcontroller to read an ASCII character from a keypad 4x4, and display its code in 8 LEDs.

(30 minutes)


Activity 1

Table 1. ASCII code for digits

Digit

ASCII code (hex)

ASCII code (binary)

0

0x30

0011 0000

1

0x31

0011 0001

2

0x32

0011 0010

3

0x33

0011 0011

4

0x34

0011 0100

5

0x35

0011 0101

6

0x36

0011 0110

7

0x37

0011 0111

8

0x38

0011 1000

9

0x39

0011 1001

 

Table 2. ASCII code for characters

Character

ASCII code (hex)

ASCII code (binary)

/

0x2F

0010 1111

X

0x58

0101 1000

-

0x2D

0010 1101

+

0x2B

0010 1011

=

0x3D

0011 1101

C

0x43

0100 0011



Step 1. The circuit is drawn in the Proteus Design Suite.



Step 2. The program in C language is written.

Write in CCS C Compiler the program in C language

 #include <main.h> // the file main.h with the

                  // initial settings is included.

                  // This file must be placed in the same

                  // folder with the project.

                  // Also the 18F4550.h file must exist

                  // in the same folder with the project

               

#include <keypad.h>   // The h file of the keypad driver

                      // should be in the same folder where we will save our program.

                      // The #define row0 PIN_B4 etc statements in keypad.h

                      // should be checked and possibly modified.

                      // These statements determine the pins of the microcontroller

                      // that are connected to the keypad 4x4.

                           

#byte PORTB =0xF81  

                     // We attribute to the memory position 0xF81 the name PORTB.

                     // This means that we define a 8 bit variable whose value

                     // will be stored to the memory position F81h.

                     // The memory position F81h is the PORTD data register.

 

#byte PORTD=0xF83    // F83h is the position or PORTD data register

                     // at the data memory of the microcontroller

                     // SFR Special Function Register

 

//initialization routine

void init(void);

 

// ********* main program ************************

void main(){

   char k;      //variable for storing the ASCII code of the key pressed

   init();      //initialization routine

   kbd_init();  //initialization routine for the keypad 4x4

 

   while(TRUE){

      k=kbd_getc(); //Keypad reading.

                    //If a key is pressed, the variable k gets the ASCII code of the key.

                    //If no key is pressed, the function kbd_getc() returns the value 0.

      If(k!=0){     //If a key is pressed

         PORTD=k;   //The ASCII code of the key pressed is transferred to PORTD

      }        

   }

}

 

//initialization routine

void init (void){

   set_tris_d(0x00);   //PORTD is defined as output

   PORTD=0x00;         //PORTD takes the initial value of 0x00

} 


Step 3. Compile the program in C in order to create the program in the microcontroller machine code (hex file).



Step 4. Load to the microcontroller the hex file (program in machine code) that was created from the CCS C Compiler.



Step 5. Run the simulation and check the correct operation of the circuit.



Step 6. Suggested modifications and discussion:

    • modify the code so that PORTD does not display the ASCII code but the numeric value of the key 

      Tip
      . Notice the values of the first 4 bits of the ASCII codes of digits 0, 1, 2,… 9.