Wednesday, August 29, 2012

Hello World for PIC

When testing out a new programming language the simplest program is to write out "Hello World". Making an LED light when a switch is closed must be similar for a micro controller. My first foray into PIC coding in C was to respond to three switches with three LEDs and it worked well. What I'm impressed by is the simplicity of the code and how easy it was to put that code into the PIC ready for use.

The code is here:

/*************************************************
 * Description: switch on LEDs with switches
 *
 * File: switches.c
 * Author: Chris Hill
 * Created: August 2012
 *
 ************************************************/

#include
#include

__CONFIG(FOSC_XT & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_ON);

/***** GLOBAL VARIABLES *****/
union {
    uint8_t port;
    struct {
        unsigned RB0 : 1;
        unsigned RB1 : 1;
        unsigned RB2 : 1;
        unsigned RB3 : 1;
        unsigned RB4 : 1;
        unsigned RB5 : 1;
        unsigned RB6 : 1;
        unsigned RB7 : 1;
        };
    } sPORTB;


#define SW1 PORTBbits.RB7
#define SW2 PORTBbits.RB6
#define SW3 PORTBbits.RB5

#define sLED1 sPORTB.RB2
#define sLED2 sPORTB.RB1
#define sLED3 sPORTB.RB0

void main() {

    // initialisation
    TRISB = 0b11111000;

    for (;;) {
        sLED1 = SW1 ? 1 : 0;
        sLED2 = SW2 ? 1 : 0;
        sLED3 = SW3 ? 1 : 0;

        PORTB=sPORTB.port;
    }
}



It looks quite long at first sight, but it is simple enough, most of the complexity coming from a union to create a shadow for PORTB. This allows the shadow PORTB to be addressed as an eight bit integer or as individual bits. I like to use the shadow for ports because it helps with the read-modify-write problem of I/O ports. There no need to debounce the switches, the LEDs would just flash so fast that no one would notice.

The compiler makes accessing registers in various banks much easier, such as TRISB, makes everything a bit more readable and so less prone to errors - though perhaps professional assembler programmers might not agree. The resulting code is 47 words long out of 4096 available in the PIC16F873, so room to grow.

The hardware layout to test this was set up on bread board, rather randomly set up as a temporary test. I used an external crystal - I usually do - so I needed to connect two 33pF capacitors between GND and the crystal and pins 8 & 9, hook up +5v to pin 20, two GND pins 8 & 19 and connect the MCLR (pin 1) to +5v. You need this for every circuit so it it easy enough to leave it on a bread board ready for the next circuit. This is not quite as plug and go as an Arduino, but not too hard either. If I build something I want to make more permanent then something on Veroboard is easy to make too.

Overall I think the PicKit 3 and the XC8 C compiler from Microchip has encouraged me to dabble again micro controller electronics. So what next?

Monday, August 27, 2012

PicKit 3

I've had a long lay off from electronics, but the bug has bitten again. I wanted to drive some small motors at variable speeds. The way I would have tried in the past would have been to try using pulse width modulation from a Microchip PIC pin to create the pulses to drive a motor. The motor draws too much current to drive directly from a PIC pin, but a transistor or a half bridge would be fine, either using the PIC pin to control the motor.

I dug out the PIC chips I could find and looked on-line for datasheets etc. I found some PIC16F873 chips - t'Interweb informs me that these are out-of-date, but I thought I'd have a go anyway.

In the past I would have used Microchip's MPLAB to create the code for the PIC chip. The code would have been PIC assembler for their mid-range chips. I would then have copied the compiled code (as a hex file) to a program supplied by Velleman that used their K8084 PIC programmer to write it out to the chip. I never used the assembler enough to get properly familiar with it, so it was always an error-prone struggle that put me off a bit. All of this software expected to run on Windows which I am very happy to have moved away from onto Linux, so there is a another problem. Lastly the Velleman board connects with a serial cable (not USB) and it seems that a USB to Serial converter does not work with it.

I have since discovered Arduino. It uses USB, can be powered by the USB, has a C compiler that run in an IDE on Linux and works very well. Uploading the code to the Arduino is simple via the USB connection and the serial connection allows communication between the PC and the board. Arduino is easy to use and seems very popular, it does seem to be a bit of a one-size-fit-all. The chip usually comes pre-installed on a board with the external connections presented around the edges. Arduino is more expensive than a PIC and there are hundreds of PIC devices to choose from. I think the idea of using a PIC and Arduino in suitable places seems interesting too.

While I was looking at all of this I noticed that Microchip have ditched their in-house development platform and moved to Netbeans. This is an IDE I have used before. It works well so I thought I'd try it out. A download and install later and I could now assemble PIC code on my Linux PC, so the first Windows hurdle was overcome. Then I discovered that the Velleman PIC programmer didn't work - I think some of my soldering of the kit has let go. I looked for what the modern way of programming PICs (even my old ones) would be and discovered PicKit3. It is a programmer and debugger from Microchip which integrates nicely with the new Netbeans IDE, which they now call MPLabX. I found also that there are clones of PicKit 3 available so I ordered one from a Chinese company. It came with a Zero Insertion Force board for a range of chip sizes to plug into the PicKit 3 so plain PIC chips can be programmed.

So all that remained was to find a C compiler for PIC chips. As I looked through the literature on-line I discovered that Microchip have just released a new compiler called XC-8 for the mid-range PIC chips, like mine. The basic option is free. I have just downloaded it and installed it on my laptop into MPLabX, so now I need to try it.