For programming PIC microcontrollers we require a piece of hardware that can communicate between the computer and the microcontroller known as a programmer. There are several programmers available out there but we will be using PICkit3 for this example due to its versatility. OneΒ importantΒ noteΒ before we start, is that if you plan on buying a PICkit3 programmer/debugger, make sure you buy it from Microchip or Microchip authorized dealers only, otherwise thereβs no guarantee that the programmer will work, in fact we had to buy a second PICkit3 because the first one we bought was a Chinese clone and didnβt work.
In order to learn how to burn programs into a PIC microcontroller, you need to understand the following topics:
-ICSP
-Hardware connections
-MPLAB X IDE and IPE
-Configuration Bits
So letβs get into these topics one by one.
In general, there are two methods for programming PIC devices.
Self-Programming
This technique uses a special program known as a Bootloader to write the code into theΒ controllerβs program memory via different interfaces such as UART, SPI etc. This techniqueΒ is used when there are frequent firmware updates in the system. The disadvantage of thisΒ method is that an extra program space is used for the Bootloader.
In-Circuit System Programming(ICSP)
ICSP is a technique where a device is programmed even after the device is placed in a circuit board. This means that the pins used to program the microcontroller can also be used as a GPIO pin after programming is completed.
The pins used for programming are:
- MCLR/Vpp
- Vdd
- Gnd
- ICSPDAT
- ICSPCLK
How does the microcontroller know that it is in programming mode?
The device enters the programming mode by applying a set of signals to the programming pins.
For example, for PIC16F88X the programming mode can be activated by holding ICSPDAT(PGD)Β and ICSPCLK(PGC)Β low while raising MCLR pin from VIL to VIHH (13V), then applying VDD and data. These actions are performed by PICkit3 in a synchronized manner.
Once in the programming mode, the IC knows whatever data comes next to programming pins are not I/O(input/output) signals, rather they are ICSP commands or data to be written into the FLASH memory.
But where does this data come from?
Well, this is where the MPLABX IDE comes into the picture. MPLAB X is a free softwareΒ development tool used to write/edit, compile, debug and download your code onto aΒ microcontroller.
Hardware connections
- Connect MCLR from PICkit3 to MCLR (Pin1) of IC and pull-up using a 4.7k resistor.
- Connect Vdd and Gnd from PICkit3 to Vdd (Pin32) and Gnd (Pin31) of IC.
- Connect ICSPDAT and ICSPCLK from PICkit3 to IC.
- Connect an LED through a 470ohm resistor on pin RA6 (Pin10).
- Apply 5V DC on Vdd and Gnd.
- Connect the PICkit3 to the computer using the USB cable.
- Pin 32, 11 and 31, 12 are internally shorted in the IC.
- ICSPDAT and ICSPCLK are sometimes also known as PGD and PGC.
Steps to create a new project
Open the IDE and create a new project by following the steps given below:
- Choose,Β projectΒ β Microchip embeddedβ>Standalone project
- Select deviceΒ Β Β β Deviceβ>PIC16F887
- Select toolΒ Β Β Β Β Β Β Β β Hardware toolsβ>PICkit3
- Select compiler -Compiler Toolchainsβ>XC8 (Click βDownload latestβ for the first-time install)
- Select project name βType in a suitable name and click finish.
After creating a new project we need to add a source file.
Right click on source fileβ>newβ>main.c and give it a name.
Now before writing our first code, let us understand the significance of βCONFIGURATION BITSβ. These bits as the name suggests, are used to configure the microcontroller. These bits are used by MPLAB IPE and PICkit3 to take some important decisions such as, whether weβll be using the Internal or an external oscillator, setting up the clock frequency, programming the pic microcontroller either in high voltage programming mode or low voltage programming mode etc.
To configure these bits:
Go to Windowβ>PIC Memory Viewsβ>Configuration Bits
At the bottom select these options
FOSCΒ Β Β Β Β Β EXTRC_CLKOUTΒ Β Β Β Β Β HS (For crystal oscillator)
WDTEΒ Β Β Β Β Β Β Β OFF
PWRTEΒ Β Β Β Β Β Β OFF
MCLREΒ Β Β Β Β Β Β ON
CPΒ Β Β Β Β Β Β Β Β Β Β OFF
CPDΒ Β Β Β Β Β Β Β Β Β OFF
BORENΒ Β Β Β Β Β Β ON
IESOΒ Β Β Β Β Β Β Β Β Β OFF
FCMENΒ Β Β Β Β Β Β Β OFF
LVPΒ Β Β Β Β Β Β Β Β Β Β OFF
BOR4VΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β BOR40
WRTΒ Β Β Β Β Β Β Β Β Β OFF
Click Generate Source code. Copy the code generated at the bottom of the screen and paste into the main source code.
A simple LED blink program is given below.
/* * File:Β Β Blinky.c * Author: Murtaza.S */ // CONFIG1 #pragma config FOSC = HSΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Oscillator Selection bits (HS oscillator) #pragma config WDTE = OFFΒ Β Β Β Β Β Β Β Β Β Β Β Β Β // Watchdog Timer Enable bit #pragma config PWRTE = OFFΒ Β Β Β Β Β Β Β Β Β Β Β // Power-up Timer Enable bit #pragma config MCLRE = ONΒ Β Β Β Β Β Β Β Β Β Β Β Β // RE3/MCLR pin function select bit #pragma config CP = OFFΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Code Protection bit #pragma config CPD = OFFΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Data Code Protection bit #pragma config BOREN = ONΒ Β Β Β Β Β Β Β Β Β Β Β // Brown Out Reset Selection bits #pragma config IESO = OFFΒ Β Β Β Β Β Β Β Β Β Β Β Β Β // Internal External Switchover bit #pragma config FCMEN = OFFΒ Β Β Β Β Β Β Β Β // Fail-Safe Clock Monitor Enabled bit #pragma config LVP = OFFΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Low Voltage Programming Enable bit // CONFIG2 #pragma config BOR4V = BOR40VΒ Β // Brown-out Reset Selection bit #pragma config WRT = OFFΒ Β Β Β Β Β Β // Flash Program Memory Self Write Enable bits #include <xc.h> void delay(unsigned int t); void main(void){ TRISA = 0x00; PORTA = 0x00; while(1){ PORTA = 0xff; delay(500); PORTA = 0X00; delay(500); } } void delay(unsigned int t)Β Β Β // This is a simple loop to generate delay. { unsigned int i,j; for(i=0;i<t;i++) { for(j=0;j<120;j++); } } Click on build main project (F11).ode>
Now open the MPLAB IPE
Select the device as PIC16F887, click apply, import the hex file by clicking on browse. The Hex file can be found in MPLABX PROJECTSβ>project folder nameβ>distβ>defaultβ>production.
Finally, click βprogramβ to burn the code into your device.
Source: How to Use PICKit3 to upload code to pic microcontroller