Position: Index >

Program PIC16F887 using Linux (PIC12F675)

2017-07-22 06:33  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

This article describes the Program PIC16F887 using Linux (PIC12F675). The content is very simple, very helpful. Components in this article can help you understand better understanding of this article. For example, in this article, you can go to find and buy these components:PIC12F675.
If you have a high-quality knowledge of the circuit, please contact us. We appreciate your sharing what you know schematic knowledge to us. 

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:

view sourceprint?
1sudo apt-get install sdcc

To install sdcc on Fedora:

view sourceprint?
1sudo 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

view sourceprint?
1<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:

view sourceprint?
1wget http://home.pacbell.net/theposts/picmicro/picp-0.6.8.tar.gz
2tar-xzf picp-0.6.8.tar.gz

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:

view sourceprint?
020 0 0 0 0 0 0 0
030 0 0 0 0 0 0 0
0720 00 3f ff 3f ff 00 7f
0800 7f 3f ff 3f ff 00 ff
0900 ff 00 00 00 00 00 00
100D 10 20 00 04 20 07 02
1100 00 01 00 00 00 00 00
1200 01 22 0f
153f ff 07 00 00 00 00 00
1600 00 00 00 00 00 00 00
173f ff 07 00 00 00 00 00
1800 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:

view sourceprint?
1wget http://ww1.microchip.com/downloads/en/DeviceDoc/pk2cmdv1.20LinuxMacSource.tar.gz
2tar-xzf pk2cmdv1.20LinuxMacSource.tar.gz

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:

view sourceprint?
01//Simple program to get started programming
02//PIC microcontrollers in Linux.
03//Written by Devlin Thyne.
04//Released to the public domain.
06#include "pic/pic16f887.h"
08//Use these configuration words:
09//0x2ff4 0x3fff
11//Set the configuration words:
12unsignedintat _CONFIG1 configWord1 = 0x2FF4;
13unsignedintat _CONFIG2 configWord2 = 0x3fff;
15//To compile:
16//sdcc -mpic14 -p16f887 blink.c
18//To program the chip using picp:
19//Assuming /dev/ttyUSB0 is the serial port.
21//Erase the chip:
22//picp /dev/ttyUSB0 16f887 -ef
24//Write the program:
25//picp /dev/ttyUSB0 16f887 -wp blink.hex
27//Write the configuration words (optional):
28//picp /dev/ttyUSB0 16f887 -wc 0x2ff4 0x3fff
30//Doing it all at once: erasing, programming, and reading back config words:
31//picp /dev/ttyUSB0 16f887 -ef -wp blink.hex -rc
33//To program the chip using pk2cmd:
34//pk2cmd -M -PPIC16f887 -Fblink.hex
36//Setup variables
37unsignedcharucharCount = 0;
38unsignedintuintDelayCount = 0;
42????//Set PORTC to all outputs
43????TRISC = 0x00;
45????ucharCount = 0;
46????uintDelayCount = 0;
48????//Loop forever
49????while( 1 )
51????????//Delay Loop
52????????while( uintDelayCount < 10000 )
54????????????//Increment the loop counter
55????????????uintDelayCount ;
58????????//Reset delay loop counter
59????????uintDelayCount = 0;
61????????//Increment the count
62????????ucharCount ;
64????????//Display the count on the PORTC pins
65????????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:

view sourceprint?
1sdcc -mpic14 -p16f887 blink.c

For the PIC16F688:

view sourceprint?
1sdcc -mpic14 -p16f688 blink.c

For the PIC12F675:

view sourceprint?
1sdcc -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 :

view sourceprint?
1picp /dev/ttyUSB0 16f887 -ef -wp blink.hex -rc

For a PICkit 2 programmer programming a PIC16F887:

view sourceprint?
1pk2cmd -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:

blink schematic Program PIC16F887 using Linux

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.