Programming PIC Microcontrollers - 10 Steps - Instructables
Programming PIC Microcontrollers - 10 Steps - Instructables
PIC microcontrollers are a very useful and versatile tool for use in many electronic projects. They are very inexpensive and easy to
find. They are also very powerful and many are capable of speeds up to 64 MIPS using the internal oscillator block, about 16 times
faster than most comparable AVR microcontrollers. PICs are also easy to program, however getting the project set up can some times
be tricky. These instructions will walk through the process of setting up the software, creating a new project, and programming some
very simple functions to test the configuration and ensure everything is working. They are designed to be very open ended; after the
project is created and the basics are finished the reader is encouraged to explore all the features and extras not covered in these
instructions. Also you will find that these instructions will start out walking through step by step, but as the instructions near the end the
reader is encouraged to explore other ways of accomplishing the tasks and make the project their own.
What you will need To build a project with a PIC microcontroller only requires a few items.
PIC microcontroller
These instructions are for programming a PIC18F series MCU, although others are similar.
Obtained from Microchips website.
Microchip allows students with valid .edu email addresses sample PIC's for free!
The PIC I am using to create these instructions is a PIC18F22K80
PICkit 3 In-Circuit Debugger
Available from Microchip.
Costs $45 for general public, and #34 with student discount if you have an .edu email address.
There are also other programmers which will work just as well; however, this is the best one for starting out.
Breadboard and breadboard wires
LEDs, buttons, potentiometers, or anything else you would like to connect to the PIC
https://www.instructables.com/Programming-PIC-Microcontrollers/ 1/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
Before doing any programming the first step is to build the hardware. Although the PIC18F portfolio is very large, many of the chips
have several commonalities. For more detailed information see the "Guidelines for Getting Started with PIC18Fxxxx Microcontrollers"
section in your devices datasheet. For detailed pin-outs of the PIC microcontroller see the "Pin Diagram" section in your devices
datasheet.
For my example I have an LED connected between RA0 and ground, the wiper of a potentiometer connected to AN1, and a DPST
switch connected to RA2. You may find it easier to program the PIC if you have sketched down a schematic of your circuit.
https://www.instructables.com/Programming-PIC-Microcontrollers/ 2/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
These instructions will use XC8 compiler and MPLAB X IDE by Microchip. This step will explain how to get these tools and ensure they
have been installed correctly.
1. To get the latest version of the software visit Microchips website at http://www.microchip.com/pagehandler/en-us/family/mplabx/
2. Select the software for your OS and follow the standard installation instructions.
Note: If you are using Windows 8 you may need to run the installers in compatibility mode for Windows 7.
In this step we will create a new project based on a template from Microchip.
The project will now show up in Project Explore on the left hand side of the screen.
Before we can get started programming we need to set the build parameters.
Create Configuration
1. In the Project Properties dialog select "Conf: [Default]" in the Categories list
1. In the Device box type the name of the device you are using. In my case PIC18F26K80
2. In the Hardware Tools list select PICkit3
3. In the Compiler Toolchain select XC8 (v...) Where ... is the version you have installed.
4. Select Apply
2. Under Conf: [Default] select PICkit 3
1. For Option categories select Power
2. Check "Power target circuit from PICkit3
3. Select Apply.
3. Under Conf: [Default] select XC8 compiler
1. For Option categories select Optimizations
2. Set "Optimization Set" to "none"
3. Select Apply
4. Click OK to close the dialog box
Test the Configuration To test the configuration click the clean and build button (the one with the hammer and broom). Text will start
scrolling in the output window at the bottom of the page. If everything is successful the this text will say BUILD SUCCESSFUL (total
time: ...). If you get an error, go back through this step making sure that you did not miss anything, and that everything was applied.
https://www.instructables.com/Programming-PIC-Microcontrollers/ 3/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
The next step is setting the configuration bits. The configuration bits tell the MCU its initial conditions for when it turns on. They are
used to set the clock source and speed, watchdog time configuration, and other similar features. Configuration bits are device
dependent, so check the data sheet for the chip you are using for more information.
If everything has worked move on to the next step. If there are errors or warnings fix them before moving on.
https://www.instructables.com/Programming-PIC-Microcontrollers/ 4/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
The next step is to start programming; however, before we get to the application code we must program the system code. The system
code are the low level functions such as configuring the oscillator and basic delay functions.
Determining Settings
Before we can program the settings, we must choose what speed we would like to run at. For this example I will use 16MHz as most
PIC's can run at this speed. For my configuration I will use the 4MHz postscaller from the HF-INTOSC, and the 4x PLL giving an output
frequency of 4MHz*4x=16MHz
Applying Settings
1. Return to MPLAB
2. In the project explorer under Source Files open system.c
3. At the bottom of this file is the function ConfigureOscillator. Remove the comments in that function.
4. To set the bits of a register type in all caps the register name, followed by the lowercase word bits and then a period and the bit
name.
5. To set the bits follow that with an equal sign. To use binary type 0bXXXX where XXXX is the binary number. Lastly end the line
with a semi-colon.
6. Set all the bits as determined above for the OSCCON register. Example: OSCCONbits.IRCF = 0b101;
7. Do the same for all other needed oscillator registers. See below for an example of a finished ConfigureOscillator function.
8. When finished build and check for warnings/errors
PIC18F microcontrollers need 4 clock cycles to execute one line of assembly code. Therefore with a clock of 16MHz, lines will be
executed at 4 million lines per second = 4000 lines per milli-second. Since a for loop will take one instruction each time for the
comparison, and two for the operation one for the body of the loop, it will work perfectly. We just need the for loop to loop 1000 time per
milli-second.
1. In system.c create a new function at the bottom of the file of type void wait_ms(uint16_t time)
2. Below is the completed function
/**
* Wait for a given number of milli-seconds using busy waiting scheme.
* @param time - time in ms to wait.
*/
void wait_ms(uint16_t time)
{
static long timel = 0;
timel = time * 1000l;
for( ; timel; timel--);// no initial condition, while time is >0, decrement time each loop
}
https://www.instructables.com/Programming-PIC-Microcontrollers/ 5/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
The best way to test that everything is set up correctly is to blink an LED light. If the light blinks at the expected rate then everything
has been configured correctly. In this example the LED is connected to PORT A, Pin 0 (RA0 on the datasheet). If you have your LED
connected to a different pin, use the appropriate registers and bits.
The function void main(void) is the main entry point of the program. When the MCU first powers on it will enter into this function. The
first line calls the ConfigureOscillator function you filled in to set the clock source and speed. The next line calls InitApp, a function that
we will fill in shortly, and finally it enters an infinite loop. Since there is no operating system for the function to return to, there is no
return call at the end.
The finished function should look like this:
https://www.instructables.com/Programming-PIC-Microcontrollers/ 6/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
4. Next create another function immediately after called uint16_t adc_convert(uint8_t channel)
/** * Preform an analog to digital conversion. * @param channel The ADC input channel to use. * @return The value of the conversion. */
uint16_t adc_convert(uint8_t channel){
ADCON0bits.CHS = channel; // select the given channel
ADCON0bits.GO =0b1; // start the conversion
while(ADCON0bits.DONE); // wait for the conversion to finish
return(ADRESH<<8)|ADRESL; // return the result}
8. Build and download the code. As you turn the POT the speed the LED blinks should change
https://www.instructables.com/Programming-PIC-Microcontrollers/ 7/8
11/20/24, 5:44 PM Programming PIC Microcontrollers : 10 Steps - Instructables
1. To set a pin as an input, write a 1 to that pins TRIS register bit - TRISAbits.TRISA2 = 1;
2. If a pin share analog features, it may be necessary to set it to digital by clearing the appropriate bit in ANCONx register
3. When writing a value to a pin use the LAT register; however, when reading a value from a pin use the PORT register - value =
PORTAbits.RA2;
4. Change main to the following:
voidmain(void){
uint16_t adc_value; // variable to hold ADC conversion result in
That is it! You now have the basic knowledge of how to set up a new project, read and write to digital pins, and how to read from
analog pins. These three features will allow you to do 90% of the projects using PICs on the Internet. Also, as you continue your
exploration into PIC microcontrollers you will find that most other features require very similar steps to configure peripherals, and read
and right to registers.
https://www.instructables.com/Programming-PIC-Microcontrollers/ 8/8