Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Making silly syntax error!

Status
Not open for further replies.

large_ghostman

Well-Known Member
Most Helpful Member
Ok I am using the C template in mplabx, and I know nthe mistake is a silly one but I cant find it! say you have two files the H files and the C files.
On the user template h file I put the following


Code:
int READ_ADC(void);

and in the user.c file I put this

Code:
int READ_ADC(void)
                    {

    return (((unsigned int)ADRESH)<<8)|(ADRESL);
   
}

its almost a direct copy of the ADC lib with C18, so why am I getting a syntax error on the h file when I try to compile??? I know I have to be missing something really stupid :D

At the moment I am just starting to learn again, I am slowly altering the ADC lib. Once I have got the hang of the multi file system template, my Idea is to completely redo my LCD lib. I have BIG plans for a really good LCD lib! Nothing like the bog standard ones on the net!! But first I am slowly building up the basics.
 
DOHHHhhhhhhhhhhhhh I knew it would be stupid:oops: Thanks North Guy, I completely forgot to define it!!!!! It was the syntax error that threw me. I focussed on the actual syntax and couldnt see a problem, totally forgot it can also mean it hadnt been defined :rolleyes:.
 
Next question, how do you Define it? I understand with things like a setting of a register, but I cant find in the ADC header file how they did the define for readadc.
 
You don't need to define it, but if it is defined it may mess things up.

Say

C:
#define READ_ADC(x) (x = ADRESH<<8|ADRESL)

now you can use it:

C:
READ_ADC(z);

this translates to:

C:
z = ADRESH<<8|ADRESL;

Now you define a function:

C:
int READ_ADC(void);

Because of the macro, it doesn't define the function but translates to:

C:
int void = ADRESH<<8|ADRESL;

This gives you a syntax error.

To avoid such confusion, MACROS() are usually defined in uppercase, but functions() in lowercase.
 
I am still not sure what to do with this. The ADC lib isnt much help, all it has is
Code:
/* Stores the result of an A/D conversion. */
union ADCResult
{
    int lr;            //holds the 10-bit ADC Conversion value as integer
     char br[2];        //holds the 10-bit ADC Conversion value as two byte values
};

Code:
#if    defined (ADC_V15) || defined (ADC_V15_1)   

unsigned int ReadADC(unsigned char );
                 
#else

int ReadADC(void);

#endif

So I am still not sure how to get rid of the syntax error. Its always the small stuff that trips me up :D
 
yes still throws an error????? hmmmm Ian mentioned he had to uninstall 3.47 because it was a bit screwy
 
well I got around it, but still dont get why it didnt work, all I did was move the
int READ_ADC(void);

into the C file at the top, but surely as I had called the .h file first, it should have been ok to leave it in the .h file? The rest of the functions so far are all

void whatever (void)
{
;
}

and have caused no problems?? So I guess I am missing something, time to get the books out see what I am doing wrong
 
It has been a LONG time since I've worked with microcontrollers and this may not be even close to correct, but have you tried calling the function just by saying

Code:
int var_name = function_name();

instead of

Code:
int function_name(void);

?

I have gotten used to coding in Java and C++, so I'm very rusty when it comes to uC programming.
 
Post the complete files (.h file and .c file). Sometimes syntax error is caused by errors in earlier lines, not the line where the error is detected. If you are missing the ; somewhere.
Post the complete error message.
Also, try to give the function more "unique" name, so that there is no risk that it collides with existing names.
 
Code:
#ifndef JUNEBUG_DEV_BOARD_H
#define   JUNEBUG_DEV_BOARD_H
#ifdef   __cplusplus
extern "C" {
#endif
#ifdef   __cplusplus
}
#endif
#endif   /* JUNEBUG_DEV_BOARD_H */
/* Junebug dev board set ups, this file is called by junebug_dev_board.c and
sets up the junebug dev board for using the on board 18f1320*/
/*LEDS on june bug are charlie plexed, so one pin is held low while the other
isheld high to turn on*/
void LED1_ON(void); //ANNODE is RA0 pin 1. CATHODE is RA6 PIN 15
void LED1_OFF( void); // opposite of led1 on
// ADC peripheral prototypes
/* first VR1_ON will set port direction and anaologue on digital off
will also select chanel AN1*/
void VR1_ON (void);  //
void ADC_ON(void); //Enable the ADC peripheral
void ADC_OFF(void); // Disable the ADC peripheral
void SELECT_VR1(void);// Junebug Pot 1 channel select VR1 sits on RA1 pin2
  // on the 18f1320 chip
void SET_ADC_INTERNAL_REF(void); // Function to set ADC mod to use VDD & Vss as REF
void SET_ADC_EXTERNAL_REF(void); // Function to set ADV to use external voltage REF

Thats the .h

this is the .c

This compiles ok now, since I moved it into the .c file

Code:
#include "junebug_dev_board.h"
#include <p18f1320.h>



int READ_ADC(void);



//turns Led1 on, on the june bug as they are charliplexed

void LED1_ON(void)
                 {

    TRISAbits.RA0 =0;  //led1 ANODE RA0 set to output
    TRISAbits.RA6 =0;  //led1 CATHODE RA6 set to output
    LATAbits.LATA6 =0; //LED1 cathode set low
    LATAbits.LATA0 =1; //LED1 ANNode set high

    }
                       //Function to turn LED1 off
void LED1_OFF(void)
                  {

    LATAbits.LATA6 =0;
    LATAbits.LATA0 =0;

    }
/* Some function's for setting up and using the junebug ADC unti with the
* Pic18f1320 chip on the tutor board */


void ADC_ON(void)
                {
    ADCON0bits.ADON =1;  //Enable the ADC peripheral
    }

void ADC_OFF(void)
                 {
    Nop();
    ADCON0bits.ADON =0;  // Disable the ADC peripheral
}

void SELECT_VR1(void)   // Select channel AN1 for junebug VR1 RA1 pin2 of 18f1320
                    {   // so we need to select chanel 1 this is ADCON0
ADCON0bits.CHS0 =1;      //bits 4-2 set chanel select. AN1 is 001
ADCON0bits.CHS1 =0;
ADCON0bits.CHS2 =0;

}

void SET_ADC_INTERNAL_REF(void)
                                {

    ADCON0bits.VCFG0 =0;   // Sets ADC to VDD & VSS as voltage REF
    ADCON0bits.VCFG1 =0;
   
}

void SET_ADC_EXTERNAL_REF(void)
                                {

    ADCON0bits.VCFG0 =1;            //  sets V- REF to RA2 AN2 pin 6
    ADCON0bits.VCFG1 =1;            // sets V+ REF to RA3 AN3 pin 7

}

void VR1_ON(void)
                {
    ADCON1bits.PCFG1=0;      //Sets AN1 to anologue
    TRISAbits.RA1=1;         //Sets AN1 (RA1) To input
   // ADCON2=0b10110101;  breaks down as follows
    ADCON2bits.ADCS0=1;     //ADCS0-ADCS2 Sets ADC converson clock to FOSC/16
    ADCON2bits.ADCS1=0;
    ADCON2bits.ADCS2=1;

    ADCON2bits.ACQT0=0;     //ACQT0-ACQT2 set AQU time. 110 sets to 16 TAD
    ADCON2bits.ACQT1=1;
    ADCON2bits.ACQT2=1;

    ADCON2bits.ADFM=1;      //Result right justified (bit6 does nothing)
                            // Setting bit 7 (ADFM)to 0= left just
}

int READ_ADC(void)
                    {

    return (((unsigned int)ADRESH)<<8)|(ADRESL);
   
}

It's just from the template's and copying a sample file, I am trying to get used to mplab X and multiple files. Before I have always put everything in the main file, but it gets too big and is awkward to find things in. I thought I woud start really simple by kind of following a template.
the syntax error only occurs when I put

int READ_ADC(void); into the .h file, the errors just says syntax error with a blue line under, when you click the blue line it takes you straight to that line, when I move it to .c and compile the error is gone.
Bit of a head scratcher :D, still I guess its mistakes we learn most from :D.

I am trying to resist diving straight into the LCD driver :D, if I can get the basics right then it should be ok.

Also I tried it with Kiel for 8051 and it compiled fine with it in the .h, so not sure if its the new C18 or not
 
Strange.. This is a long shot, but try what happens if you switch:

#include "junebug_dev_board.h"
#include <p18f1320.h>

to:

#include <p18f1320.h>
#include "junebug_dev_board.h"

And then put the function prototype in the "junebug_dev_board.h" file.
 
:D now that works!!!! I didnt think it mattered what order you had the #include files were in. I will try it also with C18 3.46 might just be the 3.47 version.
I would never have thought to swap the order over, its the small stuff that gets you every time :D
 
:D now that works!!!! I didnt think it mattered what order you had the #include files were in. I will try it also with C18 3.46 might just be the 3.47 version.
I would never have thought to swap the order over, its the small stuff that gets you every time :D

There is something in the <p18f1320.h> file that interferes with the function. I think what NorthGuy said is correct. There may be a macro with the same name. Maybe you should rename the function "READ_ADC".
It would be good practice to use lower case letters for function names and upper case for macro names.
 
for some reason I had it the other way around, I thought functions were upper case! anyway I will switch over to functions lower case. Thanks again for the help.
I cant find anything in the <p18f1320.h> that should mess with it! and I made sure I didnt use the name used in the ADC.h file. Anyway it works so thats a start :D.
 
I cant find anything in the <p18f1320.h> that should mess with it! and I made sure I didnt use the name used in the ADC.h file. Anyway it works so thats a start :D.

Then the problem may be that you are missing the final #endif in your .h file.

If you have include guard:

#ifndef JUNEBUG_DEV_BOARD_H
#define JUNEBUG_DEV_BOARD_H

then you must have

#endif

at the end of the file. If you do not have that, and the next file you try to include <p18f1320.h> also has an include guard, you will get into trouble.

EDIT:
it looks like you have the line:
#endif /* JUNEBUG_DEV_BOARD_H */

In the wrong place, it must be the last line of the file.
 
I will double check, the strange thing is, it works both ways round in 3.46 it seems to just be 3.47 giving me trouble.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top