Wednesday, May 30, 2012

PIC18 Getting Started

Now that I have my development boards in hand, I'm getting set up to work with Microchip PIC18 microcontrollers.  My first step (detailed here) is to just get an LED to blink and confirm that I can compile and run code on the microcontroller.  Going forward, I'll do a post each on testing of the other functional parts of my project as I get them working:  the UART interface to an RFID module, working with an SD card and working with the VS1003 MP3 chip.  I don't get much time to play with this stuff, so it'll probably be a few months before I'm done.

I'm going to be using the new MPLAB X IDE, which you can download for free at the MPLAB X IDE download page.  There's video on that page that describes everything you need to do to download and install the IDE.  There are several options to download different compilers.  I selected the "MPLAB IDE X v1.10", "MPLAB X IDE Release Notes/User' Guide", and "MPLAB C18 Lite Compiler for PIC18 MCUs".  The other compiler option for the PIC18 series is the HITECH C compiler, and you can also download a Lite version of this from the same page.  From the research I've done, it looks like the C18 compiler is a better option for compatibility with the application libraries provided by Microchip.  It also looks like both of these compilers will be combined and migrated to the new XC8 compiler from Microchip very soon (or maybe will already be by the time I actually post this), but I'm going to stick with the C18 compiler for now.

For a programmer/debugger, I'm using the PICkit 2, which runs about $35.  Microchip offers several packages that come with the PICkit 2 plus a development board (or the newer PICkit 3 with a development board), but since I have my own board, I bought only the programmer.  There are several other options for programmers from Microchip, and many more from third party sources.  Microchip offers a Development Tool Selector where you can put in the microcontroller you want to use and it will show you which of their development tools are compatible with it.  There's also a device support list for the PICkit 2 specifically, which shows the microcontroller I'm using, PIC18LF26J11, is supported.


After installing the IDE and PICkit 2 driver, I populated enough of my circuit board to start testing with the PIC.  I added the LDO regulators for 5V, 3.3V, and 2.5V, a power LED, the reset circuit, crystal, LEDs, and DIP socket.  My first mistake was soldering on a 48 MHz crystal when I saw 48 MHz as the max clock speed.  I started up the LED blink program, and the timing wasn't even close.  After some digging, I figure out that 48 MHz was the max clock speed, but to get that you either need an external clock or a 12 MHz crystal with the 4x PLL activated.  I'm not finding it in the datasheet right now, but I seem to recall 25 MHz was about the max crystal frequency the PIC could drive.  I'm using 16 MHz for now.


When you download the Microchip C18 compiler, be sure to check out the included documentation.  It's located in the install directory in a "doc" folder (for me it is at C:\Program Files (x86)\Microchip\mplabc18\v3.40\doc).  The "Getting Started" document and "User Guide" are obviously helpful, but the "hlpPIC18ConfigSet.chm" and "PIC18F Peripheral Library Help Document.chm" are also extremely useful.  The first one lets you select your microcontroller and then list all of the configuration settings and possible values.  The configuration settings do similar things as fuses in AVRs, but it seems to be a little smoother with PICs to use them.  The issue I had at first is that the configuration settings names between chips vary, so if you're trying to get sample code for a different PIC to work with yours and are seeing compiler issues it'd be a good idea to reference this document.  For example, the sample LED blink program I found in the user guide used "WDT" to set the watchdog timer to off, but for the  PIC18LF26J11 , I needed to use "WDTEN" instead.  The library help document shows all of the functions available in the C18 library, and again they're broken down by what's available for your specific microcontroller.

The following code is what I wrote as a sample program to make sure I had everything working correctly and could program the PIC.  I wired an LED each to pins 2, 3, & 4 on the PIC (PORTA0-2), and set them blinking at a 1, 2, & 4 Hz rate.  This is where I figured out about the crystal frequency being off, because the blink rate was much slower than in should have been.  I'm using two of the libraries that come with the C18 compiler, and had to add them to the project to get it to work (though I just referenced them from their default location).

#include <p18lf26j11.h>
#include <delays.h>

#pragma config OSC = HS //High speed crystal
#pragma config WDTEN = OFF //Disable watchdog timer
#pragma config XINST = OFF //Disable Extended CPU mode

#define LED1hzPin LATAbits.LATA0
#define LED1hzTris TRISAbits.TRISA0
#define LED2hzPin LATAbits.LATA1
#define LED2hzTris TRISAbits.TRISA1
#define LED4hzPin LATAbits.LATA2
#define LED4hzTris TRISAbits.TRISA2

char i;

void main()
{
        //Set LED Pins data direction to OUTPUT
        LED1hzTris = 0;
        LED2hzTris = 0;
        LED4hzTris = 0;
        //Set LED Pins to OFF
        LED1hzPin = 0;
        LED2hzPin = 0;
        LED4hzPin = 0;

        i = 0;
        while(1)
        {
            Delay10KTCYx(50);//Delay 1/8 sec
            LED4hzPin = ~LED4hzPin;//Toggle LED Pin
            if (i%2==0)
                LED2hzPin = ~LED2hzPin;//Toggle LED Pin
            if (i%4==0)
                LED1hzPin = ~LED1hzPin;//Toggle LED Pin
            i++;
        }
}

This step was pretty simple, but it's one that can take some time tracking down silly errors when you're starting with something new, and now I'm ready to move on to my actual project.  Next up will be getting the RFID module working.

No comments:

Post a Comment