What is PIC Programming? (with examples) How to do it? | robocombo

Big2

New member
What is PIC Programming; PIC microcontrollers (very useful and versatile components for pic programming examples used in many electronic projects)
structured and functional.
It is cheap and easy to find. They are also very powerful - from a processing point of view - and can reach speeds of up to 64 MBPS using most internal
oscillator blocks.
It's about 16 times faster than most comparable AVR microcontrollers. PICs are also easy to program, but getting the project set up
sometimes it can be difficult. These instructions are for installing the software, creating a new project and testing the configuration and making sure
everything works.
Very simple functions will go through the programming process. It is designed to be very open-ended; once the project is created and the basics are complete
then the reader is encouraged to explore all the features and extras not covered in these instructions. You can click on the links to browse the products.
PIC Development Boards PIC Microcontrollers Integrated
What is PIC Programming?
Programming PIC Microcontrollers
Requirements to create a project with PIC programming logic;
list of 6 items
PIC microcontroller
list of 4 items nesting level 1
These instructions are for programming PIC18F series MCUs.
Compatible Microcontrollers
Mikrochip allows free sample PICs for students with .edu email addresses!
The PIC model we used to create these instructions; PIC18F22K80
list end nesting level 1
PICkit 3 In-Circuit Debugger
list of 2 items nesting level 1
Microchip Program v3.0
There are other programs available that might work; however, this is best for starters.
list end nesting level 1
Breadboard and Jumper Cables
LED
Button
Potentiometer
list end
What is PIC Programming?
Step 1: Hardware Creation
The first step before doing any programming is to build the hardware. Although the PIC18F portfolio is huge, most of the chips have a few common features.
has. For more detailed information, you can browse the datasheet files. For detailed pinouts of the PIC microcontroller, refer to "Pin
See the "Diagram" section. Note: VDD = Positive Voltage / VSS = Ground.
list of 11 items
Connect the MCLR pin to VDD through a 1kΩ resistor.
Connect a 0.1μF capacitor between each adjacent VDD-VSS pair or AVDD-AVSS pair.
Connect a 10μF capacitor between VCAP and Vss.
MCL will connect the PICkit 3 pins to pin 1.
Connect VDD to pin 2 of PICkit 3.
Connect VSS to pin 3 of PICkit 3.
Connect the PGD pin to pin 4 of the PICkit 3.
Connect the PGC pin to pin 5 of the PICkit 3.
Leave pin 6 of PICkit 3 disconnected.
Connect any analog input to pins with ANx functionality where x is a number.
Connect digital inputs or outputs to pins with Rxy function; where x is a letter identifying the port and y is a number identifying the bit.
list end
In the example, there is an LED between RA0 and VSS, a potentiometer connected to AN1, and a DPST switch connected to RA2. If you have drawn a schematic
of your circuit, the PIC
You may find it easier to program the microcontroller.
Step 2: Software
We will be using XC8 and MPLAB X IDE by Microchip . In this step, we will see how to install these tools correctly.
list of 2 items
To get the latest version of the software http://www.microchip.com/pagehandler/en-us/family/mplabx/
Visit the Microchips website .
Select the appropriate software for your operating system and follow the standard installation instructions.
list end
Note: If you are using Windows 8, you may need to run the installers in compatibility mode for Windows 7.
list of 6 items
Launch MPLAB X after software is installed
In the menu bar choose Tools->Options
In the Options dialog, select the Embedded tab and make sure XC8 is listed in the Toolchain list.
If it is listed, select OK and continue to the next step.
If not listed, make sure the instillation is complete and click the Scan for Build Tools button.
If it's still not listed, visit the Microchips forum for help with your specific issue.
Look.
list end
Step 3: Create New Project
In this step, we will create a new project.
list of 9 items
Select File-> New Project ... in the menu bar.
In the new file dialog expand Samples and select Microchip
Select PIC18 Template C in the project box
Select Next
Give the project the name you want
Select the location where the project will be saved in the Project Location box
Leave Project Folder as default options
Check the "Set as Main Project" box
Select Finish.
list end
The project will now appear in Project Explore on the left side of the screen.
Step 4: Creating the Parameters
Before we start programming, we need to set the build parameters. Create Configuration
list of 6 items
Right-click the project name in the projects toolbar.
In the Project Properties dialog, select Manage Configurations....
In the Configurations dialog, select New. (3rd image)
Enter the Default value in the New Configuration Name dialog box and click OK. (4th image)
Make sure Default is selected in the Configurations dialog and click Set Active
Click OK in the Configurations dialog box.
list end
Setting Configuration Properties
list of 4 items
In the Project Properties dialog, select "Conf: [Default]" in the Categories list (6th Image)
list of 4 items nesting level 1
In the Device box, type the name of the device you are using. In my case PIC18F26K80
Select PICkit3 in the Hardware Tools list (Image 7)
In the Compiler Toolchain select XC8 (v...) ... is the version you have installed.
Select Apply
list end nesting level 1
Under conf: Select [Default] PICkit 3
list of 3 items nesting level 1
Select Power for the option categories (Image 8)
"Power Target Circuit from PICkit 3" (Image #8)
Select Apply.
list end nesting level 1
Under conf: [Default] select XC8 compiler
list of 3 items nesting level 1
Select Optimizations for the option categories (Image 9)
Set " Optimization Set to "none"
Select Apply
list end nesting level 1
Click OK to close the dialog
list end
Click the clean and build button (hammer and broom) to test the configuration. The text will start scrolling in the output window at the bottom of the
page.
If all is successful, this text will say BUILD SUCCESSFULLY (total time: ...) . If you get an error, make sure you haven't missed anything and everything
is applied.
Make sure to repeat this step.
Step 5: Setting the Configuration Bits
The next step is to set the configuration bits. Configuration bits tell the MCU the initial conditions at the time it is powered on.
Used to set the clock source and speed, and other similar features. Configuration bits are device dependent, so for more information
refer to the datasheet of the chip you are using.
list of 9 items
In the project explorer (PIC Memory Views) expand Source Files and open (Configuration bits). (Image 1)
Remove all text below the #endif line
Notice that a new tab opens at the bottom of the screen
Adjust the bits as needed for your project. Since these are chip dependent, see the datasheet for more information on what each does. Some
common settings are:
list of 6 items nesting level 1
Extended Instruction Set - Must be set to OFF when using Template
Oscillator - Used to select the processor. Leave it as Internal RC oscillator unless you are using an external crystal. Other oscillator configurations
See the data sheet for Note: CLKOUT allows easier debugging and should be turned on if available.
PLL Enable - Allows future use of PLL. Note: This does not turn on the PLL, it just activates it. It is recommended to enable it.
Timer - Used to ensure that the processor is not locked. But it makes debugging much more difficult. circuit during startup programming
It is recommended to disable it and enable it only after the project is almost complete.
Code / Table Write / Read protection - Used to disable writing or reading to specific memory ranges. Disable all of these.
If you're unsure of a setting, it's usually safe to leave it as the default.
list end nesting level 1
After all the configuration bits are set, click the "Generate Source Code to Output" button at the bottom of the panel. (2nd.
Image)
The panel will now switch to the Output tab. Select and copy all the text in this tab (3rd Image)
Paste below configuration_bits.c file and save. (4th image)
Click the broom and hammer icon to clean and rebuild the project. (5th image)
Make sure the build was successful. Also make sure there are no errors in the output
list end
If everything worked, go to the next step. If there are errors or warnings, correct them before continuing.
Step 6: Configuring the Oscillator
The next step is to start programming; however, we need to program the system code before we move on to the application code. System code, configuration
of the oscillator
and low-level functions such as basic delay functions.
Specifying Settings
In order for us to program the settings, we must choose what speed we will work at. For this example, I'll use 16MHz as most PICs can run at this speed.
my configuration
For this, we will use 4x PLL from HF-INTOSC giving 4MHz postscaller and 4MHz * 4x = 16MHz output frequency.
list of 6 items
Locate the section labeled Oscillator Configurations in the datasheet.
The first thing listed in this section are the Oscillator Types. If you are using internal oscillator use INTIO1 related settings
On the next page you will find a schematic drawing of an oscillator similar to the one shown. Check the signal in this drawing to ensure the correct speed
is selected.
It is useful to monitor. (1st and 2nd Images)
The next step is to program these settings into the MCU. This is done by adjusting the registers. The first register to set is OSCCON. (3rd image)
list of 5 items nesting level 1
IDLEN - used to control the action of the sleep command. It can be left as default.
IRCF - Oscillator selection. Since we are using HF-INTOSC/4 (4MHz), we will need to set this to a binary value of 101
OSTS - Read-only bit
HFIOFS - Read-only bit
SCS - clock selection bits. Since we are using internal oscillator we will set it to 1x where x can be 0 or 1
list end nesting level 1
The next record is OSCCON2; however this record is mostly read only and doesn't matter at this point
The last oscillator configuration register is OSCTUNE. We will not adjust the frequency of this project, but we should use this register to turn on the
PLL using the PLLEN bit.
list end
Applying Settings
list of 1 items
list of 8 items nesting level 1
Let's go back to the MPLAB program
Open system /C under Source Files in Project Explorer.
Below this file is the ConfigureOscillator function. Remove comments in this function.
To set the bits of a register type to uppercase, type the register name, followed by the lowercase bits, followed by the dot and the bit name.
Follow this with an equal sign to set the bits. To use binary type 0bXXXX; where XXXX is the binary number. Finally, semicolon the line
finish.
Set all the bits mentioned above for the OSCCON register. Example: OSCCONbits.IRCF = 0b101;
Do the same for all other oscillator registers. See below for an example of a completed ConfigureOscillator function.
Compile when complete and check for warnings/errors
list end nesting level 1
list end
/** * Configure the clock source and speed */void ConfigureOscillator(void){
OSCCONbits.IRCF =0b101;
OSCCONbits.SCS =0b00;
OSCTUNEbits.PLLEN =0b1;}
Step 7: Wait for the Milli Seconds Function
One of the most useful functions is wait_ms. However, this is not a standard library function and must be programmed. For this application, given
There will be a loop that will hold the processor until the time has elapsed. The PIC18F microcontroller needs 4 clock cycles to execute one line of assembly
code.
So at a 16MHz clock, lines will be executed at 4 million lines per second = 4000 lines per millisecond. The for loop is used for each comparison.
Since it will take one instruction - one operation - at a time, it will work perfectly. It's enough for the for loop to loop 1000 times per millisecond.
list of 2 items
Create a new function under the file of type void wait_ms (uint16_t time) in System.c
You can see the completed code below.
list end
/**
* 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
}
list of 4 items
Open system.h in the Header Files folder in the project browser
Add the line void wait_ms (uint16_t) at the end; to prototype the function.
Change line 8 from 8000000L to 16000000L
Check for errors/warnings
list end
Step 8: Flashing the LED
The best way to test that everything is set up correctly is to flash an LED light. If the light is flashing at the expected rate, everything is configured
correctly.
In this example the LED is connected to PORT A, Pin 0 (RA0 in the datasheet). If you connected your LED to a different pin, use the appropriate registers
and bits.
list of 1 items
Open the main.c file in the project viewer under source files.
list end
The void main (void) function is the main entry point of the program. The MCU will enter this function when first powered on. The first line is the one
you fill in to set the clock source and speed.
Invokes the ConfigureOscillator function. The next line calls InitApp, a function that we will fill in shortly, and finally an infinite
enters the loop. Since there is no OS for the function to return, there is no return call at the end either. The finished function should look like this:
list of 2 items
While add the following code immediately above the loop.
list of 1 items nesting level 1
Set LED pin as output - TRISAbits.TRISA0 = 0; // Setting the TRIS bit to 0 will be the output, setting it to 1 will be the input.
list end nesting level 1
While add the following code into the loop
list of 4 items nesting level 1
Turn the LED OFF - LATAbits.LATA0 = 0; // LAT bits control the output of a pin. 0 = LOW, 1 = HIGH
wait 1/2 second - wait_ms(500);
Set the LED to ON - LATAbits. LATA0 = 1;
wait 1/2 second - wait_ms(500);
list end nesting level 1
list end
void main(void)
{
/* Configure the oscillator for the device */
ConfigureOscillator();
/* Initialize I/O and Peripherals for application */
InitApp();

TRISAbits.TRISA0 = 0; // set pin as output
while(1)
{
LATAbits.LATA0 = 0; // set pin LOW
wait_ms(500); // wait 0.5 seconds
LATAbits.LATA0 = 1; // set pin HIGH
wait_ms(500); // wait 0.5 seconds
}
}
list of 6 items
Build the program and check for errors or warnings
Make sure the PICkit is properly connected to the PIC and the computer
Click the create and program device button (the button to the right of the clean and build button)
Select PICkit 3 if prompted and click OK
When a double check appears on the alert, check if you have the correct PIC enabled and click OK.
If a warning appears about the Target Device ID, click OK to ignore it
list end
Step 9: Reading Analog Value
Until now the program had set an LED to be flashing. We will use a potentiometer to generate an analog signal that will change the speed of the LED.
The ADC takes an analog voltage and outputs a digital value.
list of 3 items
Open user.c in the project browser under Source Files.
Create a new function above the InitApp function void init_adc(void)
Enter the code below to initialize the ADC module
list end
/** * Initialize the Analog to Digital Converter. */void init_adc(void){
TRISAbits.TRISA1 =0b1;// set pin as input
ANCON0bits.ANSEL1 =0b1;// set pin as analog
ADCON1bits.VCFG =0b00;// set v+ reference to Vdd
ADCON1bits.VNCFG =0b0;// set v- reference to GND
ADCON1bits.CHSN =0b000;// set negative input to GND
ADCON2bits.ADFM =0b1;// right justify the output
ADCON2bits.ACQT =0b110;// 16 TAD
ADCON2bits.ADCS =0b101;// use Fosc/16 for clock source
ADCON0bits.ADON =0b1;// turn on the ADC}
list of 1 items
Just create another function after uint16_t adc_convert(uint8_t channel)
list end
/** * 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 conversionwhile(ADCON0bits.DONE);// wait for the conversion to finishreturn(ADRESH<<8)|ADRESL;// return the result}
list of 3 items
Add the InitApp function to the line init_adc()
Add prototype to user.h file uint16_t adc_convert(uint8_t);
Change the parent item to match the following:
list end
voidmain(void){
uint16_t adc_value;// variable to hold ADC conversion result in/* Configure the oscillator for the device */
ConfigureOscillator();/* Initialize I/O and Peripherals for application */
InitApp();
TRISAbits.TRISA0 =0;// set pin as outputwhile(1){
LATAbits.LATA0 =0;// set pin LOW
adc_value = adc_convert(1);// preform A/D conversion on channel 1
wait_ms(adc_value>>2);// wait 0.5 seconds
LATAbits.LATA0 =1;// set pin HIGH
adc_value = adc_convert(1);// preform A/D conversion on channel 1
wait_ms(adc_value>>2);// wait 0.5 seconds}}
list of 1 items
Compile and download the code. The LED flashes as the POT changes its speed.
list end
Step 10: Read the Digital Value
With the switch off, we will have the program complete what it does, and with the switch on, the LED will be solid until the program is closed again.
list of 4 items
To set a pin as input, write 1 to the TRIS register bit for that pin - TRISAbits.TRISA2 = 1;
If a pin shares analog characteristics, it may be necessary to set it digitally by clearing the appropriate bit in the ANCONx register.
Use the LAT register when writing a value to a pin; however, it is necessary to use the PORT register when reading a value from a pin - value = PORTAbits.RA2;
Change main as follows:
list end
voidmain(void){
uint16_t adc_value;// variable to hold ADC conversion result in/* Configure the oscillator for the device */
ConfigureOscillator();/* Initialize I/O and Peripherals for application */
InitApp();
TRISAbits.TRISA0 =0;// set pin as output
TRISAbits.TRISA2 =1;// set pin as input
ANCON0bits.ANSEL2=0;// set pin as digitalwhile(1){if(PORTAbits.RA2)// if the pin is high{
LATAbits.LATA0 =1;// set the pin as high}else// if the pin is low{// blink the LED
LATAbits.LATA0 =0;// set pin LOW
adc_value = adc_convert(1);// preform A/D conversion on channel 1
wait_ms(adc_value>>2);// wait some time
LATAbits.LATA0 =1;// set pin HIGH
adc_value = adc_convert(1);// preform A/D conversion on channel 1
wait_ms(adc_value>>2);// wait some time}}}
Now you have the basic knowledge of how to set up a new project, how to read from digital pins and analog pins. These three features are
It will allow you to do 90% of projects using PICs. Also, as you continue your explorations of PIC microcontrollers, other features
You will find that most require very similar steps to configure peripherals and read and read registers.
 
Top