Good afternoon folks welcome to another Training Tuesday this week we will cover pic programming
Tools or items needed
- Pic microcontroller
- Pickit 3
- breadboard
Step one
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.
Note: VDD = Positive Voltage and VSS = Ground.
Note: VDD = Positive Voltage and VSS = Ground.
- Connect the MCLR pin through a 1kΩ resistor to VDD.
- Connect a 0.1μF capacitor between every pair of adjoining VDD-VSS pairs or AVDD-AVSS pairs.
- Connect a 10μF capacitor between VCAP and Vss.
- Connect MCLR pin to pin 1 of the PICkit 3.
- Connect VDD to pin 2 of the PICkit 3.
- Connect VSS to pin 3 of the PICkit 3.
- Connect PGD pin to pin 4 of the PICkit 3.
- Connect PGC pin to pin 5 of the PICkit 3.
- Leave pin 6 of the PICkit 3 unconnected.
- Connect any analog inputs to pins with ANx functionality where x is a number.
- Connect any digital inputs or outputs to pins with Rxy functionality where x is a letter identifying the port, and y is a number identifying the bit.
Step two
now is the time to get the software you can do that here
Step three
In this step we will create a new project based on a template from Microchip.
- On the menu bar select File->New Project...
- In the new file dialog box expand Samples and select Microchip Embedded
- In the project box select PIC18 C Template
- Select Next
- Give the project any name you like
- Choose a location to save the project to in the Project Location box
- Leave the Project Folder as default options
- Check "Set as Main Project" box
- Select Finish
Step four
Before we can get started programming we need to set the build parameters.
Create Configuration
Create Configuration
- Right click on the project name in the projects tool bar.
- In the Project Properties dialog select Manage Configurations...
- In the Configurations dialog select New
- In the New Configuration Name dialog enter Default and click OK
- In the Configurations dialog make sure Default is selected and click Set Active
- Click OK in the Configurations dialog
- In the Project Properties dialog select "Conf: [Default]" in the Categories list
- In the Device box type the name of the device you are using. In my case PIC18F26K80
- In the Hardware Tools list select PICkit3
- In the Compiler Toolchain select XC8 (v...) Where ... is the version you have installed.
- Select Apply
- Under Conf: [Default] select PICkit 3
- For Option categories select
- Power Check "Power target circuit from PICkit3
- Select Apply.
- Under Conf: [Default] select XC8 compiler
- For Option categories select Optimizations
- Set "Optimization Set" to "none"
- Select Apply
- Click OK to close the dialog box
Step five
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.
- In the project explorer expand Source Files and open configuration_bits.c
- Remove all the text below the #endif line
- Notice a new tab has opened at the bottom of the screen
- Set the bits as needed for your project. Since these are chip dependent, check the data sheet for more information about what each does. Some common settings follow:
- Extended Instruction Set -- Should be set to OFF when using template
- Oscillator -- Used to select the processor. Unless you are using an external crystal, leave set as Internal RC oscillator. See data sheet for other oscillator configurations. Note: CLKOUT will allow for easier debugging, and should be turned on if available.
- PLL Enable -- Will allow for future use of the PLL. Note: this will not turn on the PLL, it will only enable it. It is recommended to enable it.
- Watchdog Timer -- The watch dog timer is used to ensure the processor will not lock up. It however makes it much harder to debug. It is recommended to disable it while initially programming, and only enable it after the project is nearly done.
- Code/Table Write/Read protects -- Used to disable writing or reading to certain ranges of memory. Leave all of these disabled.
- If unsure about a setting, it is usually safe to leave it default.
- After all configuration bits have been set, click the "Generate Source Code to Output" button at the bottom of the panel.
- The panel will now switch to the Output tab. Select all the text in this tab and copy it to the clip board
- Paste it at the bottom of the configuration_bits.c file and pres save.
- Clean and build the project again by clicking the broom and hammer icon.
- Ensure the build was successful. Also check to make sure there was no errors in the output
Step six
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
- In the datasheet find the section labeled Oscillator Configurations
- The first thing listed in this section is Oscillator Types. If you are using the internal oscillator then use the settings relating to INTIO1
- On the next page or two you will find a schematic drawing of the oscillator similar to the one shown. It is helpful to trace the signal on this drawing to ensure the correct speed is being selected.
The next step is to program these settings to the MCU. This is done by setting registers. The first register to set is OSCCON. IDLEN -- used to control the action of the sleep command. Can be left as default.
- Return to MPLAB
- In the project explorer under Source Files open system.c
- At the bottom of this file is the function ConfigureOscillator. Remove the comments in that function
- When finished build and check for warnings/errors
- Do the same for all other needed oscillator registers. See below for an example of a finished ConfigureOscillator function.
- Set all the bits as determined above for the OSCCON register. Example: OSCCONbits.IRCF = 0b101;
- 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.
- 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.
What have we learned
we have learned the basics of pic programming and now have the software to do so the rest depends on what you would like to do there are many uses for a pic chip the best way to learn it is just to have a play.
No comments:
Post a Comment