View previous topic :: View next topic |
Author |
Message |
Vincent112
Joined: 30 Jul 2011 Posts: 4
|
Undefined Identifier Errors |
Posted: Sat Jul 30, 2011 6:58 am |
|
|
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
|
|
Posted: Sat Jul 30, 2011 1:31 pm |
|
|
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
|
|
Posted: Sat Jul 30, 2011 4:10 pm |
|
|
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
|
|
Posted: Sat Jul 30, 2011 4:19 pm |
|
|
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
|
|
Posted: Sat Jul 30, 2011 5:27 pm |
|
|
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 |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Aug 04, 2011 4:33 am |
|
|
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 |
|
|
|