CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Undefined Identifier Errors

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Vincent112



Joined: 30 Jul 2011
Posts: 4

View user's profile Send private message

Undefined Identifier Errors
PostPosted: Sat Jul 30, 2011 6:58 am     Reply with quote

Hi guys, I'm quite new to CCS C coding. I'm wanted to test the keypad function but bump into some errors while compiling it.
Code:

#include <16f877a.h>
#include <kbd1.c>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#byte port_c=0x07

void set_message_address_1()
{
   set_tris_c(0b00000000);
   port_c=0;
}

void set_message_address_2()
{
   set_tris_c(0b00000000);
   port_c=0;
   port_c=00000011;
}

void set_message_address_3()
{
   set_tris_c(0b00000000);
   port_c=0;
   port_c=00101110;
}

void set_message_address_4()
{
   set_tris_c(0b00000000);
   port_c=0;
   port_c=00110101;
}

void set_message_address_5()
{
   set_tris_c(0b00000000);
   port_c=0;
   port_c=00111010;
}

void main()
{

int8 k;

   while(TRUE)
   {
      k = kbd_getc();

      if(k != 0)
      {
         if (k == '1')
         {
            void set_message_address_1();
         }
         if (k == '2')
         {
            void set_message_address_2();
         }
         if (k == '3')
         {
            void set_message_address_3();
         }
         if (k == 'A')
         {
            void set_message_address_4();
         }
         if (k == '4')
         {
            void set_message_address_5();
         }
      }
   }
}

I've try to compile it but failed. These are the errors:
Error 12: Undefined identifier
Error 12: Undefined identifier k
Error 12: Undefined identifier k
Error 12: Undefined identifier k

Can someone tell me what is the problem? I've already defined k in the main but I've no idea why this is happening. Thank you in advance for all the advice given.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 30, 2011 1:31 pm     Reply with quote

Quote:
if(k != 0)
{
if (k == '1')
{
void set_message_address_1();
}
if (k == '2')
{
void set_message_address_2();
}
if (k == '3')
{
void set_message_address_3();
}
if (k == 'A')
{
void set_message_address_4();
}
if (k == '4')
{
void set_message_address_5();
}
}
}

Get rid of all these 'void' keywords. In C, they only belong in a function
prototype or a function declaration. You don't use them when you call
the function.
ERICOO



Joined: 13 Jun 2011
Posts: 14
Location: NIGERIA

View user's profile Send private message

PostPosted: Sat Jul 30, 2011 4:10 pm     Reply with quote

Check the include file (the key pad driver) if K is not declared in there then declare K as a character variable in your main code. e.g
Char k;
just before your functions declarations.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 30, 2011 4:19 pm     Reply with quote

Look at his main code. He has it declared:
Quote:
void main()
{

int8 k;

while(TRUE)
{
k = kbd_getc();
Vincent112



Joined: 30 Jul 2011
Posts: 4

View user's profile Send private message

PostPosted: Sat Jul 30, 2011 5:27 pm     Reply with quote

PCM programmer wrote:
Quote:
if(k != 0)
{
if (k == '1')
{
void set_message_address_1();
}
if (k == '2')
{
void set_message_address_2();
}
if (k == '3')
{
void set_message_address_3();
}
if (k == 'A')
{
void set_message_address_4();
}
if (k == '4')
{
void set_message_address_5();
}
}
}

Get rid of all these 'void' keywords. In C, they only belong in a function
prototype or a function declaration. You don't use them when you call
the function.


thanks for the help Very Happy
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Aug 04, 2011 4:33 am     Reply with quote

PCM programmer wrote:
Quote:
if(k != 0)
{
if (k == '1')
{
void set_message_address_1();
}
if (k == '2')
{
void set_message_address_2();
}
if (k == '3')
{
void set_message_address_3();
}
if (k == 'A')
{
void set_message_address_4();
}
if (k == '4')
{
void set_message_address_5();
}
}
}


Just some style points: firstly this sort of "if this, do that" code is a classic candidate for a switch:
Code:

   switch (k)
   {
      case '1':
         set_message_address_1();
         break;
      case '2':
         set_message_address_2();
         break;
      case '3':
         set_message_address_3();
         break;
      case 'A':
         set_message_address_4();
         break;
      case '4':
         set_message_address_5();
         break;
      default:
         // Character was not valid.
         // Do nothing, or some other error response as required.
         // break is not strictly necessary here, but later
         // C derivatives, eg C#, need it so I always use it.
         break;
   }

You can more or less add as many cases as you like and the default case catches anything you've not specifically dealt with, such as wrong characters.

Also I'd declare k as a char as that more accurately describes what it is. In CCS C char is the same as int8. I'd also give it a more descriptive name, such as:

char Command_Character;

or something. It all makes the code easier to read as it makes its sense clearer.

Personally I wouldn't use subroutines for code as simple as this, instead just including the few lines in each case. The reason is that stack on PICs is limited and each call uses up that precious stack.

RF Developer
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group