Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Simple PIC12F509 code in XC8 C compiler has multiple build errors

Status
Not open for further replies.
T

treez

Guest
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 <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 ();
}
 

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.
 

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.
 

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?
 

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.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
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 <xc.h>
#include <stdint.h>


#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;
}
 

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.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
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.
 
  • Like
Reactions: BAHKO and treez

    T

    Points: 2
    Helpful Answer Positive Rating

    BAHKO

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top