|
|
View previous topic :: View next topic |
Author |
Message |
Blasterdito Guest
|
Undefined identifier |
Posted: Wed Jul 09, 2008 3:36 pm |
|
|
Hi everybody.
First of all, I have zero programing experince in any language.
So this may be a stupid question.
After reading and researching for a feew weeks I have decided to learn
c and starded with pic16f84a,
I found some code on the internet and tried to compile it but I get compiler errors-
This is the code
" #include <16F84A.h>
#use delay(clock=10000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT
#use rs232(baud=1200,parity=N,xmit=PIN_A2,rcv=PIN_A3,bi ts=9)
void clockwait(void);
void main()
{
unsigned char byt; // Holds each byte received
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
while(1) // Loop forever...
{
byt=0; // Starting a new data frame
clockwait(); // Ignore start bit
for(t=0;t<8;t++) // Grab eight bits of data... LINE 21
{
clockwait();
byt|=input(PIN_A0)<<t;
}
clockwait(); // Ignore parity bit
clockwait(); // Ignore stop bit
putc(byt); // Send byte to the transmitter
} // ... rinse and repeat
}
clockwait()
{
// Waits for the next clock cycle...
while(!input(PIN_A1)); // Wait for clock to go HI
while(input(PIN_A1)); // Wait for clock to go LO
}"
Thes are the errors I'm getting
"*** Error 12 "main.c" Line 21(11,12): Undefined identifier t
*** Error 12 "main.c" Line 21(16,17): Undefined identifier t
*** Error 12 "main.c" Line 21(21,22): Undefined identifier t
*** Error 12 "main.c" Line 24(33,34): Undefined identifier t
4 Errors, 1 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Wed Jul 09 22:50:02 2008"
After reading about this for a feew days I think that i need to declare
a variable "t" , but i can't get the exact hang of it .
any help wuld be highly appreciated.
P.S I'm using ccs pcm and Mplab IDE ver 8.10
cheers |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Jul 09, 2008 4:43 pm |
|
|
Well, I guess I should say "Welcome to the world of programming". Our little world here doesn't just involve programming software but also involves dealing with hardware, very specific hardware.
If you're just starting to learn programming, if you haven't already, I would strongly suggest you purchase at least one or two books related to the study of 'C'. First learn how everything fits together. Do your first program called 'Hello World' and progress from there.
I would try to get a little hang on how C wants things organized first before trying to venture out and controlling these little bugs called micro-controllers. They are fun little boogers but can cause a lot of headaches even for the very experienced. Now don't let me discourage you but you have a bit of studying to do.
As far as your program goes, you do need to declare a variable 't'. Everything that is compared, examined, evaluated, added, subtracted and so on needs to be declared somehow. Your for() loop is comparing 't' to a number. It should be declared as an 'int', meaning an integer. C can declare an integer as 'signed' or 'unsigned' meaning 'signed' can be a positive or negative number. 'Unsigned' can only be a positive number, hence the 'signed' portion is not present.
Studying and then doing will help you learn faster. By 'doing' you will be able to 'see' your mistakes and learn quicker by them. Also, a variable of 'char' will _usually_ store a character such as the letter 'A' or a string of characters (several letters bunched together to, say, make a word). You have integers of different sizes; int1 (one bit), int8 (eight bits) int16, int32 and floats (not the rootbeer kind though).
There are many people here that are willing to help out as long as you are willing to put forth the effort first to learn, which looks like you are.
Welcome and good luck.
Ronald |
|
|
Guest
|
|
Posted: Wed Jul 09, 2008 5:13 pm |
|
|
rnielsen wrote: |
There are many people here that are willing to help out as long as you are willing to put forth the effort first to learn, which looks like you are.
Welcome and good luck.
Ronald |
Indeed I am.
I have spent the last feew weeks just figure out what equipment to purchase, programmer, compiler , pic etc etc
I decided to go with pcm and pic16f mostly for the build in functions and the fact that that chip seems to fit my needs.
I'm reading "An intruduction to programming The Microchip Pic in CCS C"
by Nigel Gardner (Wich I highly recomend to everybody in my position)
As for my problem :
I tried to use this sollution (before I actully posted here)
"clockwait(); // Ignore start bit
unsigned char t ;
for(t=0; t<8; t++) // Grab eight bits of data..."
But I still get this error
"*** Error 51 "main.c" Line 20(7,15): A numeric expression must appear here
*** Error 12 "main.c" Line 21(11,12): Undefined identifier t" |
|
|
Indy
Joined: 16 May 2008 Posts: 24
|
|
Posted: Thu Jul 10, 2008 2:01 am |
|
|
Your modification would be right when programmed in C++ but in traditional C there is a limitation that all variables have to be declared at the start of the function, i.e. the first line after the '{'. So move your modification up a few lines and it should be ok.
As a remark: When posting code use the 'code' buttons functionality. This will help to preserve the formatting of your text and makes for easier reading. |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 10, 2008 2:13 am |
|
|
Variables need to be defined at the start of the program 'section'. Declare 't' the line after the declaration for 'byt' (or before it).
Basically, there in C, are two types of variables. 'local', and 'global'. Global variables can be seen/used by everyone - these have to be declared before use, and 'outside' the code sections - so up with the #use RS232. 'local' variables are only visible in the routine that declares them, and need to be declared 'inside' the routine, at it's start.
There is a third 'type', where variables are declared inside a 'sub-section' of the code (so if - for instance-, you have an 'if' statement, or a 'while' loop, again a variable can be declared at the beginning of this), but this only applies if you are using a late compiler, and the 'ANSI' programming directive.
In each case though, the declaration _must_ be at the start of the section.
I'd suspect that the book you have, is concentrating on the 'differences' in CCS C. The need to declare at the start of the section, is fundamental to C, and though the examples in the book do this, it probably just isn't mentioned. I'd get a basic 'generic C' manual, and hopefully this will give these basics. Though it is not really 'easy to follow', the most fundamental 'reference', is K&R ('The C programming Language', by Brian Kernighan, and Dennis Ritchie). They _wrote_ C, and the first chapters do give a basic 'getting started' tutorial, but this also gives the basic syntax for everything. Add the changes from the CCS book, and you should be well under way.
Best Wishes |
|
|
Guest
|
|
Posted: Thu Jul 10, 2008 3:54 am |
|
|
Thx very much for the answer,
I have tryed to put "unsigned char t;" everywhere now, inlcuding
above and belowe the declaration of byt, but now I'm getting these errors
(I think I'm going to hang my self soon)
Executing: "C:\Program Files\PICC\Ccsc.exe" +FM "main.c" +DF +LN +T +A +M +Z +Y=9 +EA
>>> Warning 203 "main.c" Line 18(1,1): Condition always TRUE
>>> Warning 208 "main.c" Line 41(1,10): Function not void and does not return a value clockwait
>>> Warning 202 "main.c" Line 4(5,8): Variable never used: rs232_errors
*** Error 112 "main.c" Line 22(1,1): Function used but not defined: ... clockwait SCR=139
1 Errors, 3 Warnings.
Halting build on first failure as requested. |
|
|
Indy
Joined: 16 May 2008 Posts: 24
|
|
Posted: Thu Jul 10, 2008 5:11 am |
|
|
Welcome into the world of programming.
Computers always exactly do what you tell them to do, which is not necessarily what you intend to do...
The compiler stops at the first error it encounters. Now you have fixed one error the compiler continues compiling and has found another error.
You declared the function clockwait as: Code: | void clockwait(void); |
but it is defined as:Note that the two lines don't match. By omitting the function return type an 'int' is assumed. Solve by adding 'void' as in your function declaration. |
|
|
Guest
|
|
Posted: Thu Jul 10, 2008 6:12 am |
|
|
Problemm solved
I delcared the variable t like this :
unsigned char t; ( unsigned int t; should do the work as well)
And if not c++ don't forget get to make the declaration at the start of the program
Like this
" ----code snip----
void clockwait(void);
void main()
{
unsigned char byt; // Holds each byte received
unsigned char t; // declaration of variable t
----code snip---
"
And also plz notice Indys previous post about declaring and defining funktions.
Now the firmware is compiling, I only get this warning
"Warning 202 "main.c" Line 4(5,8): Variable never used: rs232_errors"
But I'm assuming that this is because the receiving end is not conected
thank you very much for all your answers
|
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Jul 10, 2008 8:26 am |
|
|
Warnings are just that, Warnings. The compiler is simply letting you know that it sees a _potential_ problem. It's not a show stopper. Things like "Condition always TRUE". This could be the main while(1) loop in main(). "Variable never used", this is just that, you've declared a variable that you never use ie. waisted memory space.
An 'Error' will stop things dead. Quite often the actual error won't be with the line the compiler has indicated but somewhere above that line. You could have missed a } or ) and the compiler is trying to include all of the lines below that point for something like a for() or if() statement.
Ronald |
|
|
Guest
|
|
Posted: Thu Jul 10, 2008 9:45 am |
|
|
All replies appreciated.
Right now I need all the clarification I need. (since i started to learn programing just a few weeks ago).
cheers |
|
|
|
|
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
|