-1

I'm trying to use a 4x4_keypad.h, LCD_16x2_4bit.h and osc_config.h to get my 4x4_keypad.c, Calculator.c and LCD_16x2_4bit.c cleaner and tidied up. But I get many errors that me and my colleagues can't understand or solve.

Would some please be so kind and give me a hint what I could have done wrong?

I'm using MPLAB X IDE 6.00 and MPLAB XC8 for my PIC18F4550.

//4x4_keypad.h
##ifndef KEYPAD_H
#define KEYPAD_H
#include <xc.h>

#define write_port LATD
#define read_port PORTD

unsigned char col_loc,rowloc,temp_col;

unsigned char keypad[4][4]= {'7','8','9','/',
                             '4','5','6','*',
                             '1','2','3','-',
                             ' ','0','=','+'};


/********************************Keypad Function*******************************/
unsigned char keyfind();


#endif  /* 4X4_KEYPAD_H */ 
//LCD_16x2_4bit.h
#ifndef LCD_16X2_4BIT_H
#define LCD_16X2_4BIT_H


#include <xc.h>
/*********************Definition of Ports********************************/

#define RS LATB0                    /*PIN 0 of PORTA is assigned for register select Pin of LCD*/
#define EN LATB1                    /*PIN 1 of PORTA is assigned for enable Pin of LCD */
#define ldata LATB                  /*PORTB(PB4-PB7) is assigned for LCD Data Output*/ 

/*********************Proto-Type Declaration*****************************/

void MSdelay(unsigned int );        /*Generate delay in ms*/
void lcd_init();                    /*Initialize LCD*/
void lcd_command(unsigned char );   /*Send command to LCD*/
void lcd_data(unsigned char x);     /*Send data to LCD*/
void lcd_print(char *);             /*Display data string on LCD*/
void lcd_clr();                     /*Clear LCD Screen*/



#endif  /* LCD_16X2_4BIT_H */
//osc_config.h
// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>
#define itoa

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1L
//#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
//#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
//#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSC_EC // Oscillator Selection bits (Internal oscillator, CLKO function on RA6, EC used by USB (INTCKO))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    
// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

//4x4_keypad.c
#include "osc_config.h"
#include "4x4_keypad.h"
#include "LCD_16x2_4bit.h"

unsigned char keyfind()
{      
        write_port = 0xf0;                   /*Make lower nibble as low(Gnd) and Higher nibble as High(Vcc)*/
        
        do
        {
            write_port = 0xF0;               /*Make lower nibble as low(Gnd) and Higher nibble as High(Vcc)*/
            col_loc = write_port & 0xf0;     /*mask port with f0 and copy it to col_loc variable*/   
        
        }while(col_loc!=0xf0);               /*Check initially at the start there is any key pressed*/   
        
        do
            {
               
                PORTD = 0xF0;
                col_loc = write_port & 0xf0;
            }while(col_loc==0xf0);           /*Wait for key press*/
            MSdelay(20);
            col_loc = write_port & 0xf0;
   
    
    while(1)
    {
        write_port  = 0xfe;                 /*make Row0(D0) Gnd and keep other row(D1-D3) high*/
        col_loc = read_port & 0xf0;         /*Read Status of PORT for finding Row*/
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=0;                       /*If condition satisfied get Row no. of key pressed*/
            while(temp_col!=0xf0)           /*Monitor the status of Port and Wait for key to release*/
            {
                temp_col = read_port & 0xf0;/*Read Status of PORT for checking key release or not*/    
            }
            break;
        }
        write_port = 0xfd;                  /*make Row1(D1) Gnd and keep other row(D0-D2-D3) high*/
        col_loc = read_port & 0xf0;         /*Read Status of PORT for finding Row*/
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=1;                       /*If condition satisfied get Row no. of key pressed*/
            while(temp_col!=0xf0)           /*Monitor the status of Port and Wait for key to release*/
            {
                temp_col = read_port & 0xf0;/*Read Status of PORT for checking key release or not*/
            }
            break;
        }
        write_port = 0xfb;                  /*make Row0(D2) Gnd and keep other row(D0-D1-D3) high*/
        col_loc = read_port & 0xf0;         /*Read Status of PORT for finding Row*/
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=2;                       /*If condition satisfied get Row no. of key pressed*/
            while(temp_col!=0xf0)           /*Wait for key to release*/
            {
                temp_col = read_port & 0xf0;/*Read Status of PORT for checking key release or not*/
            }
            break;
        }
        write_port = 0xf7;                  /*make Row0(D3) Gnd and keep other row(D0-D2) high*/
        col_loc = read_port & 0xf0;         /*Read Status of PORT for finding Row*/
        temp_col=col_loc;
        if(col_loc!=0xf0)
        {
            rowloc=3;                       /*If condition satisfied get Row no. of key pressed*/
            while(temp_col!=0xf0)           /*Wait for key to release*/
            {
                temp_col = read_port & 0xf0;/*Read Status of PORT for checking key release or not*/
            }
            break;
        }
        
    }

    
    
    while(1)
    {
        if(col_loc==0xe0)
        {
             lcd_data(keypad[rowloc][0]);   /*Display Keypad location on LCD*/
             return keypad[rowloc][0];      /*Return key pressed value to calling function*/
            
        }
        else 
            if(col_loc==0xd0)
        {
            lcd_data(keypad[rowloc][1]);    /*Display Keypad location on LCD*/
            return keypad[rowloc][1];       /*Return key pressed value to calling function*/
            
        }
        else
            if(col_loc==0xb0)
        {
            lcd_data(keypad[rowloc][2]);    /*Display Keypad location on LCD*/
            return keypad[rowloc][2];       /*Return key pressed value to calling function*/
            
        }
        else
            
        {
                
            lcd_data(keypad[rowloc][3]);    /*Display Keypad location on LCD*/
            return keypad[rowloc][3];       /*Return key pressed value to calling function*/
           
        }    
    }
    
   MSdelay(300);     
 
}
//Calculator.c
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "osc_config.h"                     /*Include Header file for oscillator configuration*/
#include <string.h>
#include "4x4_keypad.h"                     /*Include Header file for keypad operation*/
#include "LCD_16x2_4bit.h"                  /*Include Header file for LCD operation*/


void display_float( double );


void main()

{
    unsigned int i=0;                       /*Define variable for indexing of array*/
    unsigned long no1,no2;                  /*Define variable for storing no.*/
    unsigned long result=0;                 /*To store Result of operation*/
    double div_result=0;                    /*Store division float result*/
    unsigned char key,operator,flag=0;      /*key for key pressed,Operator for '+','-' etc*/
    unsigned char op1[10],op2[10],a;        
    unsigned long output[10],output1[10];   /*variable for Displaying result on LCD*/
    OSCCON = 0x72;                          /*Use Internal Oscillator with Frequency 8MHZ*/
    TRISB = 0;                              /*PORTB as Output Port*/
    TRISD = 0xf0;                           /*PORTD.0-PORTD.3 as a Output Port and PORTD.4-PORTD.7 as a Input Port*/
    write_port = 0xf0;
    
    lcd_init();                             /*Initialize LCD*/
    
    MSdelay(20);
    lcd_print("CASIO");                     /*Display string on LCD*/
    MSdelay(1000);
    lcd_clr();                              /*clear display screen*/
    
    
    MSdelay(20);
    while(1)
    {
        
         
      key= keyfind();                       /*Call keyfind() function for detecting pressed key*/
      MSdelay(30);
      if(key!=' ')
      {  
            
                if(key!='+' && key!='-' && key!='/' && key!='*' && key!='=')
                 {
                    if(flag==0)
                    {
                        op1[i] = key;       /*Assign 1st no string to op1 stored that in array*/
                        i++;
                    }
                    else
                    {
                        op2[i] = key;       /*Assign 2nd no string to op2 stored that in array*/
                        i++;
                    }    
                 }
                      
             if(key=='+' || key=='-' || key=='*' || key=='/' )
             {
             
                 operator=key;              /*Store operator*/
                 i=0;                       /*For another value enter of 2nd no.*/
                 flag=1;                        
  
             }
                
                
       if(key=='=')
       {   
           no1=atol(op1);                   /*C function(<stdlib.h>) used to convert string into long integer*/
           no2=atol(op2);                   /*C function(<stdlib.h>) used to convert string into long integer*/
           lcd_command(0xc0);               /*Start displaying result on 2nd Row and 1st location*/
           
           
           
        switch(operator)
        {
            /*Case for addition operation*/
            case '+':                       
                       
                        
                        result = no1+no2;
                        ltoa(output,result,10); /*C function(<stdlib.h>) 
                                                 convert long integer value to string for display*/
                        lcd_print(output);
                        break;
                        
            /*Case for Subtraction operation*/            
            case '-':
                        
                        result = no1-no2;
                        ltoa(output,result,10);
                        lcd_print(output);
                        break;
                        
            /*Case for Multiplication operation*/
            case '*':
                        
                        result = no1*no2;
                        ltoa(output,result,10);
                        lcd_print(output);
                        break;
                        
            /*Case for Division operation*/
            case '/':
                        
                        if(no2==0)                  /*Check for math error*/
                        {  
                            
                            lcd_clr();
                            lcd_print("MATH ERROR");
                        }
                        else
                        {   
                           
                            div_result = (float)no1/(float)no2;     /*result should be in float */
                            
                             /*Function used display float value*/
                            display_float(div_result);
                            
                        }   
                        break;
            
            default :
                
                break;
        
        
        }
        
        
       } 
    
    }
      else 
      {
          lcd_clr();                    /*Clear display screen*/    
          MSdelay(10);
         
          memset(op1,0,10);             /*memset Clear array of no.1 by copying 0 to op1's 10 location*/
          memset(op2,0,10);             /*Clear array of no.2*/    
          memset(output,0,10);          /*Clear array of output array which display final result*/
          break;
         
      }
    
       
    MSdelay(50);
    
  }
    
    
}


void display_float(double div_result)
{
    float temp;
    unsigned long output[15],output1[15];
        temp=div_result - ((int)div_result);    /* get value after decimal point i.e. float value*/
        temp=100*temp;                          /*convert it into non-decimal value*/
        itoa(output,(int)div_result,10);        /*convert value before deciaml point into ASCII value*/
        lcd_print(output);                      
        lcd_print(".");                         /*Display Decimal point*/
        itoa(output1,(int)temp,10);             /*convert value after deciaml point into ASCII value*/
        lcd_print(output1);

} 
//LCD_16x2_4bit.c
#include "LCD_16x2_4bit.h"



//****************************Functions********************************


void lcd_init()
{
    MSdelay(50);      
    lcd_command(0x33);              /*send for initialization of LCD*/
    lcd_command(0x32);              /*send for initialization*/
    lcd_command(0x28);              /*use 2 line and initialize 5*7 matrix in (4-bit mode)*/
    lcd_command(0x01);              /*clear display screen*/
    lcd_command(0x06);              /*increment cursor (shift cursor to right)*/
    lcd_command(0x0c);              /*display on cursor off*/
    lcd_command(0x84);              /*1st row 1st column i.e. 1st location of of 1st row of LCD*/
    MSdelay(50);
}


void lcd_command(unsigned char cmd )
{
    ldata= 0xF0 & cmd;              /*Send higher nibble of command first to PORT*/ 
    RS = 0;                         /*Command Register is selected i.e.RS=0*/ 
    EN = 1;                         /*High-to-low pulse on Enable pin to latch data*/ 
    MSdelay(1);
    EN = 0;
    MSdelay(5);
    ldata= (cmd<<4);                /*Send lower nibble of command to PORT */
    RS = 0;                         /*Command Register is selected i.e.RS=0*/ 
    EN = 1;
    MSdelay(1);
    EN = 0;
    MSdelay(5);

}

void lcd_data(unsigned char dat)
{
    ldata= 0xF0 & dat;              /*Send higher nibble of data first to PORT*/
    RS = 1;                         /*Data Register is selected*/
    EN=1;                           /*High-to-low pulse on Enable pin to latch data*/
    MSdelay(1);
    EN=0;
    MSdelay(5);
    ldata= (dat<<4);                /*Send lower nibble of data to PORT*/
    RS = 1;                         /*Data Register is selected*/
    EN=1;                           /*High-to-low pulse on Enable pin to latch data*/
    MSdelay(1);
    EN=0;
    MSdelay(5);

}

void lcd_print(char *msg)            /*use this function to display string on LCD which prints each element of string*/
{
    unsigned char i=0;
    while(msg[i]!='\0')            
    {
        
      lcd_data((unsigned char)msg[i]);
      i++;
    
    }
        
}

void lcd_clr()
{
    lcd_command(0x01);              /*clear display screen*/
    lcd_command(0x80);              /*1st row 1st column i.e. 1st location of of 1st row of LCD*/
}


void MSdelay(unsigned int val)
{
 unsigned int i,j;
 for(i=0;i<=val;i++)
     for(j=0;j<81;j++);             /*This count Provide delay of 1 ms for 8MHz Frequency */
 }
Mike
  • 3,738
  • 5
  • 18
  • 36
  • 1
    Welcome to stackoverflow. You're supposed to share the errors or any other details in order to get better help. This will help the other developers to analyze the issue and provide you a solution more quickly. – Kozmotronik May 11 '22 at 06:24
  • Here : `##ifndef KEYPAD_H`is a typo – Mike May 11 '22 at 08:39
  • 1
    Defining global variables in header files, as your `4x4_keypad.h` does, is usually wrong. See https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files – Nate Eldredge May 13 '22 at 06:03
  • And maybe not related, but `unsigned char keyfind();` is not how you declare a function with no arguments in modern C. It ought to be `unsigned char keyfind(void);`. https://stackoverflow.com/questions/41803937/func-vs-funcvoid-in-c99 – Nate Eldredge May 13 '22 at 06:06
  • Could you post the errors you are getting? – cup May 23 '22 at 13:05

0 Answers0