Arguably, Microchip’s PIC microcontrollers do not get enough posts here. One of the drawbacks for some of us is that Linux support for PICs is not very well known. The information is out there, but no one has laid out the process of going from writing C code to programming a chip. Written for Linux users that are familiar with microcontrollers, basic circuits, the C programming language, and can read a datasheet, this how-to should get you up and programming a PIC quickly with Linux.
The Compiler:
The Small Device C Compiler, sdcc is what will be used to create the .hex file needed to program a PIC. Support for PICs is still growing, and still in beta, so be aware that things outside the code and chips of this article may need some debugging. However, like every other open source project out there, more contributing users will help the project. Best of all, it is free, with ports to Windows and MacOS X, this is a compiler that handles many architectures and devices without the program limit of free versions of for-pay compilers that are limited to Windows. Sdcc is available through various distributions’ package managers including Ubuntu and Fedora.
To install sdcc on Ubuntu:
sudo apt-get install sdcc
To install sdcc on Fedora:
sudo yum install sdcc
The Chips:
Three different PIC chips were used in the writing of this tutorial: the 40 pin PIC16F887, the 14 pin PIC16F688, and the 8 pin PIC12F675. You can follow along with any of these chips as well as other chips.
The Programmer:
We will be using two programmers, Olimex’s PICStart+ compatible PIC-MCP-USB programmer, and Microchip’s PICkit 2. Both programmers have been tested to work with the three chips used here.
The PICStart+ programmers use the picp program. Most any PICStart+ compatible programmer will work with picp. Easily installed in Ubuntu with:
<pre>sudo apt-get install picp
For Fedora and other distributions may have to download and install it from source. So, in an empty directory of your choosing:
wget http://home.pacbell.net/theposts/picmicro/picp-0.6.8.tar.gz
tar -xzf picp-0.6.8.tar.gz
cd picp-0.6.8
make
sudo make install
The source is on [Jeff Post]‘s Development Tools for PIC programmers page along with other programming options.
If you will be using the PIC16F887 and picp, you will need to modify your /etc/picp/picdevrc file by adding the following lines:
[16F887]
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
PICSTART
[16F887:def]
20 00 3f ff 3f ff 00 7f
00 7f 3f ff 3f ff 00 ff
00 ff 00 00 00 00 00 00
0D 10 20 00 04 20 07 02
00 00 01 00 00 00 00 00
00 01 22 0f
[16F887:defx]
3f ff 07 00 00 00 00 00
00 00 00 00 00 00 00 00
3f ff 07 00 00 00 00 00
00 00 00 00 00 00 00 00
The above lines are modified parameters for PIC16F886 found in a post by [Al Williams]. For chips not already in /etc/picp/picdevrc, additional parameters will need to be added to /etc/picp/picdevrc.
PICkit 2 programmers will work with another program called pk2cmd hosted by Microchip here. You will need to install pk2cmd from source. so in a directory of your choosing:
wget http://ww1.microchip.com/downloads/en/DeviceDoc/pk2cmdv1.20LinuxMacSource.tar.gz
tar -xzf pk2cmdv1.20LinuxMacSource.tar.gz
cd pk2cmdv1.20LinuxMacSource
make linux
sudo make install
Note that Microchip touts the PICkit 3 as a replacement for the PICkit 2. It is not a replacement for the PICkit 2 as there are no Linux drivers for the PICkit 3, so do not buy the PICkit 3 thinking it will work in Linux.
There is also another program that claims to work with a range of DIY PIC programmers: PICPgm. We have not tried this program or any of the DIY programmers at this point. We know there are other PIC programmers out there, both cheap and expensive, that have not been mentioned. Perhaps a PIC programmer roundup is in need of writing.
The Code:
The code for this how-to is a kind of hello world program using LEDs. The code for this is hosted on Github, you can follow along with the blink.c file for the PIC16F887, PIC16F688, or PIC12F675. Also included are working .hex files. Here is the PIC16F887 code as a reference as we walk through each major operation:
//Simple program to get started programming
//PIC microcontrollers in Linux.
//Written by Devlin Thyne.
//Released to the public domain.
#include "pic/pic16f887.h"
//Use these configuration words:
//0x2ff4 0x3fff
//Set the configuration words:
unsigned int at _CONFIG1 configWord1 = 0x2FF4;
unsigned int at _CONFIG2 configWord2 = 0x3fff;
//To compile:
//sdcc -mpic14 -p16f887 blink.c
//To program the chip using picp:
//Assuming /dev/ttyUSB0 is the serial port.
//Erase the chip:
//picp /dev/ttyUSB0 16f887 -ef
//Write the program:
//picp /dev/ttyUSB0 16f887 -wp blink.hex
//Write the configuration words (optional):
//picp /dev/ttyUSB0 16f887 -wc 0x2ff4 0x3fff
//Doing it all at once: erasing, programming, and reading back config words:
//picp /dev/ttyUSB0 16f887 -ef -wp blink.hex -rc
//To program the chip using pk2cmd:
//pk2cmd -M -PPIC16f887 -Fblink.hex
//Setup variables
unsigned char ucharCount = 0;
unsigned int uintDelayCount = 0;
void main(void)
{
//Set PORTC to all outputs
TRISC = 0x00;
ucharCount = 0;
uintDelayCount = 0;
//Loop forever
while ( 1 )
{
//Delay Loop
while ( uintDelayCount < 10000 )
{
//Increment the loop counter
uintDelayCount++;
}
//Reset delay loop counter
uintDelayCount = 0;
//Increment the count
ucharCount++;
//Display the count on the PORTC pins
PORTC = ucharCount;
}
}
The first line is the #include for the header file of the particular chip you will be using. It tells the compiler which registers are available and where they are located in memory. In most systems, the header files will be in /usr/share/sdcc/include.
Then we setup the configuration word or words fuses. They are only able to be written when the chip is programmed, but we can define them here so we don’t have to manually program them later. The PIC16F887 has the address for the configuration words defined in its header file as _CONFIG1 and _CONFIG2. The PIC16F688 and PIC12F675 do not have the configuration word address defined in their header (we said sdcc was in beta, didn’t we?), so we just use the address of the configuration word: 0×2007. The configuration words are specific to the chip model and application and are described in the chapter “Special Features of the CPU” in each of the respective chips’ datasheets. In the blink.c samples, the configuration word is just a 16bit hexadecimal word, but the word can be made more human readable by ANDing together the configuration options. Check out the chips’ header files for the names of the options.
Next, we setup some global variables, one for the value that will be output on the LEDs and the other for a delay counter.
In the void main(), we set the PORTC tristate register, TRISC to all outputs. The PIC12F675 has only one port, GPIO, and its tristate register is TRISIO. After setting the tristate register, we enter an infinite loop with while(1). Inside that loop is a delay loop so that we can see the LEDs changing. Following the delay loop, the display counter is incremented and then written to PORTC (or GPIO) for display on the LEDs.
Compiling the Code:
Now that we have reviewed the code, it is time to turn it into something a PIC can use. sdcc will take the blink.c file and make a bunch of files. One of these files will be blink.hex which will be what the PIC device programmer will be writing to the PIC. Here’s how:
For the PIC16F887:
sdcc -mpic14 -p16f887 blink.c
For the PIC16F688:
sdcc -mpic14 -p16f688 blink.c
For the PIC12F675:
sdcc -mpic14 -p12f675 blink.c
The -mpic14 option tells sdcc that it will be compiling for the 14-bit instructions of the PIC16 and PIC12 families. The second option is the specific chip that code will be compiled for. The last thing on the line is the file containing the C code that will be compiled.
Programming the Chip:
To program a chip you will take your device programmer and connect the chip you want to load with your program. Unless you are using a socket programmer like the PIC-MCP-USB, you will need to consult the datasheets of the programmer and the chip to be programmed for the proper connection. Once properly connected, you will need to run the program to run the programmer:
For a PICStart+ programmer on /dev/ttyUSB0 programming a PIC16F887 :
picp /dev/ttyUSB0 16f887 -ef -wp blink.hex -rc
For a PICkit 2 programmer programming a PIC16F887:
pk2cmd -M -PPIC16f887 -Fblink.hex
If you are programming another chip, or the PICStart+ programmer is on a port besides /dev/ttyUSB0, you will need to make corresponding changes to the commands.
Note: The code provided for the PIC16F887 disables low-voltage programming. Some of the programmers available but not directly mentioned only perform low-voltage programming. If you have one of these programmers, you will need to change the code so that the low-voltage programming bit in the configuration words allows for low-voltage programming. The low-voltage programming pin on the microcontroller will also need to be pulled low during normal operation.
Wire the Circuit:
The circuit for this project with the code provided is really simple to breadboard. Below are the schematics for the three chips:
Start out by connecting the Vdd pins to a positive voltage source between 4.5 volts and 6 volts and the Vss pin to ground. The 40 pin PIC16F887 and the 14 pin PIC16F688 will both need a pullup resistor on their master clear pin. To any one or all of the PORTC pins (or GPIO pins for the PIC12F675), connect LEDs with current-limiting resistors to ground. Note that pin 4 of the PIC12F675 is only an input and will not light an LED. The current out of any pin of the three chips used is limited to 20mA, so the current-limiting resistors are optional for most cheap jellybean LEDs. What you should see when you power up the circuit are blinking LEDs. The LEDs should be lighting to a binary count.
Your turn!
Now that we have given you a start with programming PICs using Linux, we hope to see more projects using these chips and the tools we have mentioned above. Though this article was written for Linux users, Windows and MacOS X users should be able to use sdcc for their PIC programming needs.
Image information: The Tux logo is by Larry Ewing, Simon Budig, and Anja Gerwinski, via Wikimedia Commons. The Microchip logo is a registered trademark of Microchip Technology Incorporated.
Filed under: linux hacks, Microcontrollers "
Nenhum comentário:
Postar um comentário