«

»

Print this Post

Introduction to PIC Programming from an AVR Guru

I’m not ashamed to say it: I’m a bit of an ATMEL guy. AVR microcontrollers are virtually exclusively what I utilize when creating hobby-level projects. Wile I’d like to claim to be an expert in the field since I live and breathe ATMEL datasheets and have used many intricate features of these microchips, the reality is that I have little experience with other platforms, and have likely been leaning on AVR out of habit and personal convention rather than a tangible reason. Although I was initially drawn to the AVR line of microcontrollers because of its open-source nature (The primary compiler is the free AVR-GCC) and longstanding ability to be programmed from non-Windows operating systems (like Linux), Microchip’s PIC has caught my eye over the years because it’s often a few cents cheaper, has considerably large professional documentation, and offers advanced integrated peripherals (such as native USB functionality in a DIP package) more so than the current line of ATTiny and ATMega microcontrollers. From a hobby standpoint, I know that ATMEL is popular (think Arduino), but from a professional standpoint I usually hear about commercial products utilizing PIC microcontrollers. One potential drawback to PIC (and the primary reason I stayed away from it) is that full-featured C compilers are often not free, and as a student in the medical field learning electrical engineering as a hobby, I’m simply not willing to pay for software at this stage in my life.

I decided to take the plunge and start gaining some experience with the PIC platform. I ordered some PIC chips (a couple bucks a piece), a PIC programmer (a Chinese knock-off clone of the Pic Kit 2 which is <$20 shipped on eBay), and shelved it for over a year before I got around to figuring it out today. My ultimate goal is to utilize its native USB functionality (something at ATMEL doesn’t currently offer in DIP packages). I’ve previously used bit-banging libraries like V-USB to hack together a USB interface on AVR microcontrollers, but it felt unnecessarily complex. PIC is commonly used and a bit of an industry standard, so I’m doing myself a disservice by not exploring it. My goal is USB functionality, but I have to start somewhere: blinking a LED.

Here’s my blinking LED. It’s a bit anticlimactic, but it represents a successful program design from circuit to writing the code to programming the microchip.

Based on my limited experience, it seems you need 4 things to program a PIC microcontroller with C:

The first thing I did was familiarize myself with the pin diagram of my PIC from its datasheet. I’m playing with an 18F2450 and the datasheet is quite complete. If you look at the pin diagram, you can find pins labeled MCLR (reset), VDD (+5V), VSS (GND), PGC (clock), and PGD (data). These pins should be connected to their respective counterparts on the programmer. To test connectivity, install and run the PICkit2 installer software and it will let you read/verify the firmware on the chip, letting you know connectivity is solid. Once you’re there, you’re ready to start coding!

I wish I were friends with someone who programmed PIC, such that in 5 minutes I could be shown what took a couple hours to figure out. There are quite a few tutorials out there – borderline too many, and they all seem to be a bit different. To quickly get acquainted with the PIC programming environment, I followed the “Hello World” Program in C tutorial on PIC18F.com. Unfortunately, it didn’t work as posted, likely because their example code was based on a PIC 18F4550 and mine is an 18F2450, but I still don’t understand why such a small difference caused such a big problem. The problem was in their use of LATDbits and TRISDbits (which I tried to replace with LATBbits and TRISBbits). I got around it by manually addressing TRISB and LATB. Anyway, this is what I came up with:

#include <p18f2450.h> // load pin names
#include <delays.h>   // load delay library

#pragma config WDT = OFF // disable watchdog timer
#pragma config FOSC = INTOSCIO_EC // use internal clock

void main() // this is the main program
{
	TRISB=0B00000000; // set all pins on port B as output
	while(1) // execute the following code block forever
	{
		LATB = 0b11111111; // turn all port B pins ON
		Delay10KTCYx(1);   // pause 1 second
		LATB = 0b00000000; // turn all port B pins OFF
		Delay10KTCYx(1);   // pause 1 second
	}
}


A couple notes about the code: the WDT=OFF disables the watchdog timer, which if left unchecked would continuously reboot the microcontroller. The FOSC=INTOSCIO_EC section tells the microcontroller to use its internal oscillator, allowing it to execute code without necessitating an external crystal or other clock source. As to what TRIS and LAT do, I’ll refer you to basic I/O operations with PIC.

Here is what the MPLAB IDE looked like after I successfully loaded the code onto the microcontroller. At this time, the LED began blinking about once per second. I guess that about wraps it up! This afternoon I pulled a PIC out of my junk box and, having never programmed a PIC before, successfully loaded the software, got my programmer up and running, and have a little functioning circuit. I know it isn’t that big of a deal, but it’s a step in the right direction, and I’m glad I’ve taken it.

About the author

Scott W Harden

Scott Harden has had a lifelong passion for computer programming and electrical engineering, and recently has become interested in its relationship with biomolecular sciences. He has run a personal website since he was 15, which has changed names from HardenTechnologies.com, to KnightHacker.com, to ScottIsHot.com, to its current SWHarden.com. Scott has been in college for 10 years, with 3 more years to go. He has an AA in Biology (Valencia College), BS in Cell Biology (Union University), MS in Molecular Biology and Microbiology (University of Central Florida), and is currently in a combined DMD (doctor of dental medicine) / PhD (neuroscience) program through the collaboration of the College of Dentistry and College of Medicine (Interdisciplinary Program in Biomedical Science, IDP) at the University of Florida in Gainesville, Florida. In his spare time Scott builds small electrical devices (with an emphasis on radio frequency) and enjoys writing cross-platform open-source software.

Permanent link to this article: http://www.SWHarden.com/blog/2012-06-24-introduction-to-pic-programming-from-an-avr-guru/

5 comments

  1. Mike

    If you’re heading in the direction of doing PIC-to-PC USB communication, I’ll point out that there are at least a few complaints about Microchip’s USB library. But I started with this project: Building a PIC18F USB device.

    Also, getting a project written for one PIC to work for another requires a little work. You just have to change all of the necessary references.

    1. Scott

      Thanks for the heads up! Over the last couple days I’ve been researching the native USB functionality of pic 18F* and you’re absolutely right! Driver software still needs to be installed, and linux support is questionable. [sigh] I wish there were an easier way to develop cross-platform USB devices that require a minimum of software configuration. FTDI serial adapter ICs seem to be the best option, but they’re so expensive! … and not breadboardable [sigh again]

      1. Mike

        Doesn’t FTDI still require a driver on Linux?

        You can get a USB-capable PIC to act as pretty much any USB device, including a serial (COM) device of course. In fact, I have one on my workbench right now (APRS packet radio decoder). So, to me, a PIC USB serial device and the FTDI adapter are equivalent, except that the PIC route saves a special cable and a MAX232. You should even be able to get the PIC USB device to act like the FTDI adapter, if you know the VID and PID. I would be surprised if someone on the interweb hasn’t already figured this out.

        Before I built my PIC USB-based packet decoder I fooled around with sending the data through a serial adapter. It’s not FTDI, it’s one of the other devices…got it on eBay for something like $2 from China. Like this: eBay USB to UART. But, like you, I wanted a more minimal approach and got the PIC USB thing going. Also, that serial converter caused some weird problems for my radio.

        (By the way, the right sidebar thing on this page is overlaying my reply textbox.)

  2. Cid

    You might wan to check a program called “PIC simulator IDE”, that lets you program all the pic models and it’s really simple to use, I use that because the Mplab IDE it’s just too complicated. But it’s really nice to see your guide here. Maybe i can learn how to use it after all.

    Keep up all the great work! I discovered your page today and man, i’m amazed. I always wanted to make a really simple ballon and there is your idea to use just a microcontroller. Fantastic!

  3. Graham

    You might want to check out the MPLAB X IDE sometime – it’s much better than the older MPLAB IDE, although there is a bit of a learning curve if you’re used to MPLAB. The only downside I can see is that it doesn’t support the PICkit 2 programmer very well, so you don’t get the single step debug feature.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>