undeclared identifier in expression

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
dariods
Posts: 25
Joined: 26 Apr 2023 07:12

undeclared identifier in expression

#1 Post by dariods » 24 May 2023 09:51

I am trying to call functions using the switch case method but i am getting a compiling error "Undeclared identifier (Function_name) in expression". How do i solve the error?

Code: Select all

          void main_menu() {
    unsigned int ex = 1;
    unsigned int sw = 1;
    CustomChar(1,1);
    Lcd_out(1,3,"Battery Capacity");
    Lcd_out(2,3,"Charge Alarm");
    Lcd_out(3,3,"Tire Size");
    Lcd_out(4,3,"Exit");
    while(ex==1);
    {
     if(PORTB.F3 == 0)
     if(sw !=4)
      { 
        sw++;
        CustomChar(sw,1);
      }
     else if (PORTB.F2 == 0)
     if (sw !=1) 
      {
       sw--;
       CustomChar(sw,1);
      }
        if(PORTB.F4 == 0)
        {
         Delay_ms(2);
         while(PORTB.F4 == 0);
         {
            switch (sw){
               case 1:
                   //{
                    //ex=0;
                    battery_capacity();
                    break;
                   //}
               case 2:
                   //{
                    ex=0;
                    charge_alarm();
                    //break;
                   //}
               case 3:
                   //{
                    ex=0;
                    tire_size();
                    //break;
                   //}
               case 4:
                   //{
                    ex=0;
                    break;
                   //}
              }
          }
      }
    }
   }


hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: undeclared identifier in expression

#2 Post by hexreader » 24 May 2023 10:41

There are a lot of errors in your code - too many to list

Here is my attempt at fixes, but mostly guesswork from the limited snippet of code given

Code: Select all

// simple program for forum - a complete mess, but do not blame me - I did not write it
// PIC16F877A with 8 MHz external 2-pin crystal
// EasyPIC V7 board

// I am trying to call functions using the switch case method but i am getting
// a compiling error "Undeclared identifier (Function_name) in expression". How do i solve the error?

// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;

// dummy functions missing from code
void CustomChar(unsigned char mysw, unsigned char myval){
    // code missing from here
}
void battery_capacity(){
    // code missing from here
}
void charge_alarm(){
    // code missing from here
}
void tire_size(){
    // code missing from here
}

void main_menu(){
  unsigned int ex = 1;
  unsigned int sw = 1;
    
    CustomChar(1,1);
    Lcd_out(1,3,"Battery Capacity");
    Lcd_out(2,3,"Charge Alarm");
    Lcd_out(3,3,"Tire Size");
    Lcd_out(4,3,"Exit");
    while(ex == 1){
        if(PORTB.F3 == 0){
            if(sw != 4)
            {
                sw++;
                CustomChar(sw,1);
            }
        }
        else if (PORTB.F2 == 0){
            if (sw != 1)
            {
                sw--;
                CustomChar(sw,1);
            }
        }
        if(PORTB.F4 == 0){
            Delay_ms(2);
            while(PORTB.F4 == 0){

                switch (sw){
                    case 1:
                        //ex = 0;
                        battery_capacity();
                        break;
                    case 2:
                        ex = 0;
                        charge_alarm();
                        break;
                    case 3:
                        ex = 0;
                        tire_size();
                        break;
                    case 4:
                        ex = 0;
                        break;
                }
            }
        }
    }
}

// dummy nain function
void main(void){
    // set-up code missing from here
    Lcd_Init();
    
    while(1){                                                                   // main loop
        main_menu();
    }
}
If you want a complete answer, post your full code
I promise that nobody will want to steal this code :wink:
Start every day with a smile...... (get it over with) :)

dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Re: undeclared identifier in expression

#3 Post by dariods » 24 May 2023 18:26

hexreader wrote:
24 May 2023 10:41
There are a lot of errors in your code - too many to list

Here is my attempt at fixes, but mostly guesswork from the limited snippet of code given

Code: Select all

// simple program for forum - a complete mess, but do not blame me - I did not write it
// PIC16F877A with 8 MHz external 2-pin crystal
// EasyPIC V7 board

// I am trying to call functions using the switch case method but i am getting
// a compiling error "Undeclared identifier (Function_name) in expression". How do i solve the error?

// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;

// dummy functions missing from code
void CustomChar(unsigned char mysw, unsigned char myval){
    // code missing from here
}
void battery_capacity(){
    // code missing from here
}
void charge_alarm(){
    // code missing from here
}
void tire_size(){
    // code missing from here
}

void main_menu(){
  unsigned int ex = 1;
  unsigned int sw = 1;
    
    CustomChar(1,1);
    Lcd_out(1,3,"Battery Capacity");
    Lcd_out(2,3,"Charge Alarm");
    Lcd_out(3,3,"Tire Size");
    Lcd_out(4,3,"Exit");
    while(ex == 1){
        if(PORTB.F3 == 0){
            if(sw != 4)
            {
                sw++;
                CustomChar(sw,1);
            }
        }
        else if (PORTB.F2 == 0){
            if (sw != 1)
            {
                sw--;
                CustomChar(sw,1);
            }
        }
        if(PORTB.F4 == 0){
            Delay_ms(2);
            while(PORTB.F4 == 0){

                switch (sw){
                    case 1:
                        //ex = 0;
                        battery_capacity();
                        break;
                    case 2:
                        ex = 0;
                        charge_alarm();
                        break;
                    case 3:
                        ex = 0;
                        tire_size();
                        break;
                    case 4:
                        ex = 0;
                        break;
                }
            }
        }
    }
}

// dummy nain function
void main(void){
    // set-up code missing from here
    Lcd_Init();
    
    while(1){                                                                   // main loop
        main_menu();
    }
}
If you want a complete answer, post your full code
I promise that nobody will want to steal this code :wink:
I just followed the way you placed the functions battery_capacity, charge_alarm, tire_size. The functions were after the main_menu. Just placed them before main_menu and problem solved. Thanks once again for helping me out.

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: undeclared identifier in expression

#4 Post by hexreader » 24 May 2023 18:35

There is better way:

Use function prototypes at the top of your code.

That way any order of functions is good

Like this:

Code: Select all

// simple program for forum - a complete mess, but do not blame me - I did not write it
// PIC16F877A with 8 MHz external 2-pin crystal
// EasyPIC V7 board

// I am trying to call functions using the switch case method but i am getting
// a compiling error "Undeclared identifier (Function_name) in expression". How do i solve the error?

// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;

// function prototypes
void CustomChar(unsigned char mysw, unsigned char myval);
void battery_capacity();
void charge_alarm();
void tire_size();

void main_menu(){
  unsigned int ex = 1;
  unsigned int sw = 1;
    
    CustomChar(1,1);
    Lcd_out(1,3,"Battery Capacity");
    Lcd_out(2,3,"Charge Alarm");
    Lcd_out(3,3,"Tire Size");
    Lcd_out(4,3,"Exit");
    while(ex == 1){
        if(PORTB.F3 == 0){
            if(sw != 4)
            {
                sw++;
                CustomChar(sw,1);
            }
        }
        else if (PORTB.F2 == 0){
            if (sw != 1)
            {
                sw--;
                CustomChar(sw,1);
            }
        }
        if(PORTB.F4 == 0){
            Delay_ms(2);
            while(PORTB.F4 == 0){

                switch (sw){
                    case 1:
                        //ex = 0;
                        battery_capacity();
                        break;
                    case 2:
                        ex = 0;
                        charge_alarm();
                        break;
                    case 3:
                        ex = 0;
                        tire_size();
                        break;
                    case 4:
                        ex = 0;
                        break;
                }
            }
        }
    }
}

// dummy nain function
void main(void){
    // set-up code missing from here
    Lcd_Init();
    
    while(1){                                                                   // main loop
        main_menu();
    }
}

// dummy functions missing from code
void CustomChar(unsigned char mysw, unsigned char myval){
    // code missing from here
}
void battery_capacity(){
    // code missing from here
}
void charge_alarm(){
    // code missing from here
}
void tire_size(){
    // code missing from here
}
Start every day with a smile...... (get it over with) :)

dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Re: undeclared identifier in expression

#5 Post by dariods » 25 May 2023 18:17

hexreader wrote:
24 May 2023 10:41
If you want a complete answer, post your full code
I promise that nobody will want to steal this code :wink:
I have no issues in posting the full code as I have copied the code from the internet for a voltmeter using PIC16F877A and have modified it to make a battery coulometer. Infact someone could like proof read the code and rectify the mistakes and make it more efficient. But I have two reasons for not sharing the full code now. One my code is not yet complete and second expecting someone to proof read the code for me is like asking too much. If you are willing to do so I will surely post it. When completed and working successfully I will be posting it for others to use it if they want to.
Thanks.

dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Re: undeclared identifier in expression

#6 Post by dariods » 28 Jun 2023 13:13

hexreader wrote:
24 May 2023 10:41
If you want a complete answer, post your full code
I promise that nobody will want to steal this code :wink:
Here is my full code viewtopic.php?f=88&t=80057

Post Reply

Return to “mikroC PRO for PIC General”