Activity 2. Keypad 4x4 and LCD 16x2

The purpose of this activity is for microcontroller to read an ASCII character from a keypad 4x4, and display it in a LCD 16x2.

    • If ‘C’ key is pressed, then the screen clears.

(45 minutes)


Activity 2

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



Step 2. The program in C language is written.

Write in CCS 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 <flex_lcd.h> // The h file of the lcd driver

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

                      // The #define LCD_DB4 PIN_B4 etc statements in flex_lcd.c

                      // should be checked and possibly modified.

                      // These statements determine the pins of the microcontroller

                      // that are connected to LCD 16x2.

                           

#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

   lcd_init();  //initialization routine for the LCD 16x2  

   while(TRUE){

      k=kbd_getc();    //keypad reading

     

      //If a key is pressed (k! = 0) and if the key pressed is not 'C',

      //the character will appear on the LCD

      if (k!=0 && k!='C'){

         printf(lcd_putc,"%c",k);

      }

     

      //If 'C' key is pressedm then the screen clears

      else if(k=='C'){

         printf(lcd_putc,"\f");

      } 

   }

}

   

// initialization routine

void init(void){

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

}



Step 3. Use the CCS C Compiler to translate the programm from C language to the microcontroller machine code. Load to the microcontroller the hex file (machine code) that was created from the CCS Compiler.



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



Step 5. Suggested modifications and discussion:

    • what do we need to change in the circuit and in the code so that the keypad 4x4 is connected to PORTC?