• 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.

PIC-Duino Template File

Jon Wilder

Active Member
I'm creating a template for Arduino-like behavior in XC8. Working on the interrupt implementation as we speak, but this is what I have thus far -

PIC-Duino C source file -

C:
/* pic-duino.c */

#include <xc.h>
#include "pic-template.h"

void main() {
    /* The main wrapper, where all the magic happens */
    setup();
    while(1) {
        loop();
    }
}

/* End of file */
PIC-Duino C header file -

C:
/* pic-duino.h */

/**
* A wrapper for setup code that only runs once. Will be user defined in main.c.
*/
void setup(void);

/**
* A wrapper for user defined main loop that runs forever. Will be user defined in main.c.
*/
void loop(void);

/* End of file */
Really simple to use. In the Source Files virtual directory in your MPLAB X project, create your main source file, main.c. Additionally, load the source file pic-duino.c into your Source Files virtual directory. Optionally, load the header file pic-duino.h into the Header Files virtual directory. Lastly, in main.c, include the header file pic-duino.h, then define two void functions void of any parameters, named 'setup' and 'loop' just like an Arduino sketch -

C:
#include <xc.h>
#include "pic-duino.h"

void setup() {
// put your setup code here, to run once:
}

void loop() {
// put your main code here, to run repeatedly:
}

/* End of file */
 

nsaspook

Well-Known Member
Most Helpful Member
No disrespect but why introduce the Arduino sketch quasi OOP behaviors in a C 'get sh!t done' PIC programming environment?
 

Jon Wilder

Active Member
No disrespect but why introduce the Arduino sketch quasi OOP behaviors in a C 'get sh!t done' PIC programming environment?
Just my means of speeding up code development. Almost like having a startup framework for most projects. Most of my coding style is in fact "straight to the point".
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Jon... If its to bring Arduino lovers in, you would need to do a shed load of wrappers.... You can download JSON files to program pic's in the Arduino IDE as is...

Going the other way will be a bit messy...
 

gophert

Well-Known Member
Most Helpful Member
Isn't the setup + loop pretty much the standard way to program a pic? Or any microcontroller? If it isn't, I should have filed for a patent back in the 1990s. I think every one of my c code main.c files have (some definitions and initial state code) then a "while(1){ repeat forever}" section.

Similar concept for .asm when I had more brain power and being a purist was important to me and c-compilers had more bugs than today.
 

nsaspook

Well-Known Member
Most Helpful Member
Just my means of speeding up code development. Almost like having a startup framework for most projects. Most of my coding style is in fact "straight to the point".
Sure. If you're not using the Arduino abstract machine then MPLABX will provide a default xc8 setup + loop framework with any MCC compatible chip. It won't write application code for you but it will provide what's needed to get the chip to a programmable hardware state starting point with ease.
 

Latest threads

EE World Online Articles

Loading
Top