Newbie question, Declaring variable in c

Discussion in 'Programmer's Corner' started by Blasterdito, Jul 9, 2008.

  1. Blasterdito

    Thread Starter New Member

    Jul 9, 2008
    5
    0
    Hi everybody. :D

    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,bits=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 know that i need to declare
    a variable "t" , but i can't get the exact hang of it :confused:.

    any help wuld be highly appreciated.

    P.S I'm using ccs pcm and Mplab IDE ver 8.10


    cheers
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,178
    1,799
    As t is used as a counter with a range of [0..8] you might try

    Code ( (Unknown Language)):
    1.  
    2. unsigned char t ;
    3.  
    if you decalre it inside the function then it is a local variable, and if you declare it outside the function then it is global.
     
  3. Blasterdito

    Thread Starter New Member

    Jul 9, 2008
    5
    0
    Thank you so much for the answer, now i know that I have bin close all the time, but I still don't know where exactly put code

    This is how i tried it

    "
    ----code snip---

    while(1) // Loop forever...
    {

    byt=0; // Starting a new data frame

    clockwait(); // Ignore start bit


    unsigned char t ; YOUR CODE HERE


    for(t=0; t<8; t++) // Grab eight bits of data...
    {
    clockwait();
    byt|=input(PIN_A0)<<t;

    ----code snip----
    "


    These are the errors I got

    "Executing: "C:\Program Files\PICC\Ccsc.exe" +FM "main.c" +DF +LN +T +A +M +Z +Y=9 +EA
    >>> Warning 203 "main.c" Line 15(1,1): Condition always TRUE
    *** 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
    *** 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
    5 Errors, 1 Warnings.
    Halting build on first failure as requested.
    BUILD FAILED: Thu Jul 10 00:34:40 2008"
     
  4. roddefig

    Active Member

    Apr 29, 2008
    149
    0
    Is "YOUR CODE HERE" in the actual source is that just an annotation you made after posting here?

    I'd also lose the space between t and the semicolon. I don't think that's the problem (at least not with gcc) but it's just bad form.

    I'd recommend reading a book on C or some tutorials on the internet ("c tutorial" is a good google query).
     
  5. Blasterdito

    Thread Starter New Member

    Jul 9, 2008
    5
    0

    "YOUR CODE HERE"
    means that I have used the suggested declaration from Papabravo "unsigned char t ;"

    My question is where tu put it ? (look at my previous post where I put it )

    After I put it in the code 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"

    .
     
  6. roddefig

    Active Member

    Apr 29, 2008
    149
    0
    Ahh, I think I know what the problem is. Move the declaration to the line under "unsigned char byt;" and see if that fixes the problem.
     
  7. Blasterdito

    Thread Starter New Member

    Jul 9, 2008
    5
    0

    Yes that is the problem , if not c++ the variables must be declared 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 in my original code I declared my function like this

    "void clockwait(void);"
    and defined it like this
    "clockwait()" But it should be like the declaration "void clockwait(void)"

    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 :)
     
Loading...