Fork me on GitHub

Project Notes

#390 GettingBlinky/UsingC

Getting up and running building XC8 C projects on MacOSX and a PIC12F675. Let’s get Blinky again!

Build

Notes

The LEAP#331 GettingBlinky project was a simple test of the PIC12F675 with PIC assembler. See that project for details of the PIC12F675 Development Board I’m using.

As the PIC12F675 is one of the lowerend 8-bit chips in the PIC family, I wasn’t sure at first how well it might be supported by C compilers. Well, I shouldn’t have been concerned. In addition to support from independent compiler makers, Microchip offer their own XC Compilers

This project is a quick test of the MPLAB XC8 compiler, which supports all 8-bit PICs, is cross platform and has a free version available!

I started with a useful tutorial by Luke Briner explaining the differences between assembler and C code for a PIC:

clip

Toolchain

I’m using MPLAB X IDE v3.51 and MPLAB XC8 running on MacOSX, and a PicKit 3 programmer. NB: subsequently updated and recompiled with MPLAB X IDE v5.30.

The New Project Wizard

A quick summary. Many of these settings can be changed later in project properties.

First create a new stand-alone project

create_project_01

Select the device:

create_project_02

Select a debugger (if any):

create_project_03

Select a programmer (or do this later):

create_project_04

Select the XC8 compiler:

create_project_05

Select name and folder for the project:

create_project_06

Chip Configuration

The configuration word (address: 2007h) - documented in section 9.1 of the datasheet - is used to configure chip features. The IDE includes a configuration bits editor that can help derive suitable values.

In XC8, these are conveniently defined with pragma. I’m using a basic configuration, with a 4MHz internal clock and all other features disabled:

  • INTOSC oscillator: CLKOUT function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN
  • Watchdog Timer Enable bit (WDT disabled)
  • Power-Up Timer Enable bit (PWRT disabled)
  • GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
  • Brown-out Detect Enable bit (BOD disabled)
  • Code Protection bit (Program Memory code protection is disabled)
  • Data Code Protection bit (Data memory code protection is disabled)

With MPLAB XC8, it is not necessary to specify the device in code. Instead, the IDE records this in the NetBeans nbproject/configurations.xml file. That makes the code more portable to other devices, but not to other toolchains.

Code

See Clinky.X/main.c.

One of the main benefits of coding in C instead of assembler is that bank switching is handled automatically.

This is a trivial demonstration of GPIO port manipulations, toggling active-low LEDs on GP0 and GP1. There are a number of ways to toggle multiple bits in a port (and at least one way to do it wrong!).

One can write directly to the GPIO port, use the GPIObits struct, or even use the MPLAB Code Configurator (MCC) tool to generate boiler-plate pin functions.

I’m using GPIObits in combination with a shadow register. This makes bit manipulation very explicit and understandable, while ensuring no undesirable flipping of neighbouring bits.

What I discovered you can’t do safely is this:

GPIObits.GP0 = 1;
GPIObits.GP1 = 0;

.. because bit changes may not be fast enough to be read reliably for the next bit-flip, meaning that changes get “lost”.

Programming

I initially tested the code using a PIC12F675 Development Board as detailed in LEAP#331 GettingBlinky. Works like a charm!

UsingC_devboard_build

Programming a Stand-alone Chip

Next I wired up a stand-alone PIC12F675 on a breadboard

Breadboard

Schematic

Programming and running when powered by the programmer:

Build

And running independently with a 5V power supply.

UsingC_running_standalone

Credits and References

About LEAP#390 PICLED
Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.