1. 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.
    Dismiss Notice

PIC12F509 C Code will not compile...build errors

Discussion in 'Microcontrollers' started by Flyback, Jun 4, 2017.

  1. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    Hello,
    Do you know why this code for PIC12F509 will not compile?
    It is written in MPLAB X IDE and using XC8 C compiler.
    All the code does is pick out a low to high transition on PORT GP4 and then wait 3 ms, then take PORT GP2 high.
    I am festooned with build errors at the moment.

    Code (c):

    /*
     * File:   zapper.c
     * Author:
     *
     * Created on 03 June 2017, 23:55
     */


    //This code turns the product ON at the mains peak voltage
    //It does this by using the zero crossing detector input.

    //This uses PIC12F509
    //MPLAB X IDE
    //XC8 C compiler (free)

    #define  _XTAL_FREQ 4000000

    #include <xc.h>
    #include <stdint.h>

    #pragma config OSC = ExtRC      // Oscillator Selection bits (external RC oscillator)
    #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
    #pragma config CP = OFF         // Code Protection bit (Code protection off)
    #pragma config MCLRE = ON       // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)

    //I have not set the MCLR pin as an input because it will be noise susceptible.
    //Therefore i set MCLR up as reset, but i will never use it as reset..but will
    //simply tie the pin top Vdd on the PCB.

    //Define output
    #define FETS     LATBbits.RB2;

    //Define input
    #define zero_x     PORTBbits.RB4;

    //Define actions
    #define ON    LATBbits.RB2 = 1  /*Turn ON FETs*/


    //Declare functions which set up the microcontroller
    void    disable_interrupts(void);   //How do this?
    void    disable_pullups(void);      //How do this?
    void    setup_ports(void);


    void    setup_ports(void) {
        TRISB = 0b00011000;
        return;
    }

    //Declare variables
    uint8_t    count;

    void main(void) {
        setup_ports();
        //10 second delay
        for (count=1;count<=100;count++)   {
        __delayms(100);
        }

    here:
        if {zero_x = 1} {goto here;}
    here1:
        if {zero_x = 0} {goto here1;}

        //When it gets to this point, the zero crossing input has just gone high

        __delayms(3);   //delay to get to the mains peak
        ON;              //Turn ON FETs...at the mains peak

        while(1){;}

        return ();
    }
     
     
    Last edited by a moderator: Jun 4, 2017
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Replace this with this

    while(zero_x == 1 );
    while(zero_x == 0);

    using goto's in C will haunt you..
    is wrong... You don't need a return as the function main() is declared void

    Untested but try this
    Code (c):

    /*
     * File:   zapper.c
     * Author:
     *
     * Created on 03 June 2017, 23:55
     */

    //This code turns the product ON at the mains peak voltage
    //It does this by using the zero crossing detector input.
    //This uses PIC12F509
    //MPLAB X IDE
    //XC8 C compiler (free)
    #define  _XTAL_FREQ 4000000
    #include <xc.h>
    #include <stdint.h>
    #pragma config OSC = ExtRC      // Oscillator Selection bits (external RC oscillator)
    #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
    #pragma config CP = OFF         // Code Protection bit (Code protection off)
    #pragma config MCLRE = ON       // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)
    //I have not set the MCLR pin as an input because it will be noise susceptible.
    //Therefore i set MCLR up as reset, but i will never use it as reset..but will
    //simply tie the pin top Vdd on the PCB.
    //Define output
    #define FETS     LATBbits.RB2
    //Define input
    #define zero_x     PORTBbits.RB4
    //Define actions
    #define ON    LATBbits.RB2 = 1  /*Turn ON FETs*/

    //Declare functions which set up the microcontroller
    //void    disable_interrupts(void);   //How do this?  No need to do it at all
    //void    disable_pullups(void);      //How do this?
    //void    setup_ports(void);
    void    setup_ports(void) {  // this will work without definition as it comes first!!
        TRISB = 0b00011000;
        return;
    }
    //Declare variables
    uint8_t    count;
    void main(void) {
        setup_ports();
        //10 second delay
        for (count=1;count<=100;count++)   {
        __delay_ms(100);
        }
     while(zero_x == 1);
     while(zero_x == 0);
     
        //When it gets to this point, the zero crossing input has just gone high
        __delay_ms(3);   //delay to get to the mains peak
        ON;              //Turn ON FETs...at the mains peak
        while(1){}
    }
     
     
    Last edited: Jun 4, 2017
    • Like Like x 1
  3. jjw

    jjw Member

    Joined:
    Apr 16, 2012
    Messages:
    270
    Likes:
    15
    Location:
    Helsinki, Finland
    The semicolon at the end of #define sentence is an error.
    If zero_x = 1 should be if zero_x == 1
    If zero_x = 0 -> if zero_ x == 0
    Return statements are not needed.

    How does the compiler know which PIC is used?
     
    • Like Like x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    Yep missed that... I knew I should have tested it...
    It picks it up from MPLAB ide..
     
    • Like Like x 1
  6. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,072
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    Just a few points: The 12F509 is a simple chip. It does not have any LATx register, TRISx is simply TRIS, and the port is not PORTx, but rather GP0..GP5 . The 12F505, a 14-pin chip, is different.

    Flyback is apparently Treez on other forums.

    John
     
    • Like Like x 1
  7. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    Thankyou so much,
    Massive reduction in errors now, but still wont compile...error remaining says "struct/union required" and points to the 'while' lines.
    Any ideas greatly appreciated.

    /*
    * File: zapper.c
    * Author:
    *
    * Created on 03 June 2017, 23:55
    */

    //This code turns the product ON at the mains peak voltage
    //It does this by using the zero crossing detector input.

    //This uses PIC12F509
    //MPLAB X IDE
    //XC8 C compiler (free)

    #define _XTAL_FREQ 4000000

    #include <xc.h>
    #include <stdint.h>


    #pragma config OSC = ExtRC // Oscillator Selection bits (external RC oscillator)
    #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
    #pragma config CP = OFF // Code Protection bit (Code protection off)
    #pragma config MCLRE = ON // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)

    //I have not set the MCLR pin as an input because it will be noise susceptible.
    //Therefore i set MCLR up as reset, but i will never use it as reset..but will
    //simply tie the pin top Vdd on the PCB.

    //Define output
    #define FETS GPIO.GP2

    //Define input
    #define zero_x GPIO.GP4

    //Define actions
    #define ON GPIO.GP2 = 1; /*Turn ON FETs*/


    //Declare functions which set up the microcontroller
    //void disable_interrupts(void); //How do this?
    //void disable_pullups(void); //How do this?
    void setup_ports(void);


    void setup_ports(void) {
    // TRIS = 0b00011000;
    TRIS = 0x18;
    return;
    }

    //Declare variables
    uint8_t count;

    void main(void) {
    setup_ports();
    //10 second delay
    for (count=1;count<=100;count++) {
    __delay_ms(100);
    }


    while(zero_x == 1 ) {;}
    while(zero_x == 0) {;}

    //When it gets to this point, the zero crossing input has just gone high

    __delay_ms(3); //delay to get to the mains peak
    ON; //Turn ON FETs...at the mains peak

    while(1){;}

    return;
    }
     
  8. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    ...Thankyou all
    Now solved.
     
  9. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,072
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    Hi Flyback,

    You know, for such a simple program, you may want to consider the 10F32x chips. The 10F320 is $0.42 each in lots of 100 and the 12F509 is $0.52 each in lots of 100 from Digikey. The 10F32x offers the LATx register, interrupts, and 16 MHz. I didn't do a full comparison of alternatives, but since it seems you are starting almost from scratch, a newer chip might be much better, smaller, and cheaper.

    John
     
    • Like Like x 1

Share This Page