+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Advanced Member level 5
    Points: 34,340, Level: 45
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    7,137
    Helped
    517 / 517
    Points
    34,340
    Level
    45

    Simple PIC12F509 code in XC8 C compiler has multiple build errors

    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:
    /* 
     * 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 
    #include 
    
    #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 ();
    }

  2. #2
    Full Member level 5
    Points: 2,633, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    266
    Helped
    11 / 11
    Points
    2,633
    Level
    11

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Iam not sure of the errors you are getting. If you show it will be better. I am guessing that uint8_t type declaration may not be available. Also generally goto statement is avoided. In if statements == is used for comparison. Delayms function definition not available.



    •   AltAdvertisement

        
       

  3. #3
    Super Moderator
    Points: 264,250, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,176
    Helped
    14042 / 14042
    Points
    264,250
    Level
    100

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Using SFR names like TRISB not defined for PIC12F509, also very basic syntax errors, e.g. two in one line

    Code:
    here:
        if {zero_x = 1} {goto here;}
    Don't want to start a discussion about "goto considered harmful", a more common C syntax for the same function would be
    Code:
    while (zero_x);
    I guess, your first C program since long?

    Read the datasheet, keep a C text book (e.g. Kernighan/Ritchie The C programming language) at hand. To know the valid SFR symbol names for PIC12F509, you can review PIC12F509.h in XC8 include folder.

    Process the syntax errors reported by XC8 top-down, fix one or more, retry.

    - - - Updated - - -

    To start with actual technical points, a zero crossing detector might need quite a bit of software filtering to ignore transients.



  4. #4
    Advanced Member level 5
    Points: 34,340, Level: 45
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    7,137
    Helped
    517 / 517
    Points
    34,340
    Level
    45

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    ok thanks, this is for a piece of test kit which just has to turn our product on at the mains peak.

    Delayms function definition not available.
    Thanks, so there is no delay function available for pic12f509?
    We have to use inline ASM and do it like that?



    •   AltAdvertisement

        
       

  5. #5
    Super Moderator
    Points: 264,250, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,176
    Helped
    14042 / 14042
    Points
    264,250
    Level
    100

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    so there is no delay function available for pic12f509?
    It is, just read the compiler manual about the provided built-in delay library functions. Instead of guessing, process the error messages, as suggested. Come back if there are open points.


    1 members found this post helpful.

  6. #6
    Advanced Member level 5
    Points: 34,340, Level: 45
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    7,137
    Helped
    517 / 517
    Points
    34,340
    Level
    45

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Thankyou all....now solved



    •   AltAdvertisement

        
       

  7. #7
    Advanced Member level 5
    Points: 34,340, Level: 45
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    7,137
    Helped
    517 / 517
    Points
    34,340
    Level
    45

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Hello,
    Actually its building but not working........

    ive changed it to just take a port high/low/high..........but its not working.do you know why?

    Code:
    /* 
     * File:   zapper.c
     * Author: 
     *
     * Created on 03 June 2017, 23:55
     */
    
    
    
    //This uses PIC12F509
    //MPLAB X IDE
    //XC8 C compiler (free)
    
    #define  _XTAL_FREQ 4000000
    
    #include 
    #include 
    
    
    #pragma config OSC = IntRC      // Oscillator Selection bits (internal 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     GP2
    
    //Define input
    #define zero_x     GP4
    
    //Define actions
    
    
    
    //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) {
        TRISGPIO = 0b00011000;
    //    TRIS = 0x18;
        return;
    }
    
    //Declare variables
    //
    
    void main(void) {
        setup_ports();
    
    
       while(1){
        GP2 = 1;
        __delay_ms(30);   //delay to get to the mains peak
        GP2 = 0;
        __delay_ms(30);
       }
     
    
        return;
    }



  8. #8
    Super Moderator
    Points: 83,085, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,663
    Helped
    4536 / 4536
    Points
    83,085
    Level
    70

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Check bit 5 of the OPTION register. If it is zero the GP2 pin becomes dedicated as a clock input to the timer module and it's TRIS bit is ignored.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  9. #9
    Super Moderator
    Points: 264,250, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,176
    Helped
    14042 / 14042
    Points
    264,250
    Level
    100

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Read datasheet chapter OPTION register
    If the T0CS bit is set to ‘1’, GP2 is forced to be an input even if TRIS GP2 = ‘0’.
    Need to reset T0CS
    Code:
    OPTION = 0xDF;
    or better define useful OPTION register values for your application.


    2 members found this post helpful.

  10. #10
    Advanced Member level 5
    Points: 34,340, Level: 45
    Achievements:
    7 years registered

    Join Date
    Sep 2008
    Location
    cambridge
    Posts
    7,137
    Helped
    517 / 517
    Points
    34,340
    Level
    45

    Re: Simple PIC12F509 code in XC8 C compiler has multiple build errors

    Thankyou all....finally working now.



--[[ ]]--