Tuesday 20 June 2017

Training Tuesday #8 Pic Programming

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.
  1. Connect the MCLR pin through a 1kΩ resistor to VDD.
  2. Connect a 0.1μF capacitor between every pair of adjoining VDD-VSS pairs or AVDD-AVSS pairs.
  3. Connect a 10μF capacitor between VCAP and Vss.
  4. Connect MCLR pin to pin 1 of the PICkit 3.
  5. Connect VDD to pin 2 of the PICkit 3.
  6. Connect VSS to pin 3 of the PICkit 3.
  7. Connect PGD pin to pin 4 of the PICkit 3.
  8. Connect PGC pin to pin 5 of the PICkit 3.
  9. Leave pin 6 of the PICkit 3 unconnected.
  10. Connect any analog inputs to pins with ANx functionality where x is a number.
  11. 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.
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.

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.
  1. On the menu bar select File->New Project...
  2. In the new file dialog box expand Samples and select Microchip Embedded
  3. In the project box select PIC18 C Template
  4. Select Next
  5. Give the project any name you like
  6. Choose a location to save the project to in the Project Location box
  7. Leave the Project Folder as default options
  8. Check "Set as Main Project" box
  9. Select Finish
The project will now show up in Project Explore on the left hand side of the screen.

Step four

Before we can get started programming we need to set the build parameters.
Create Configuration
  1. Right click on the project name in the projects tool bar.
  2. In the Project Properties dialog select Manage Configurations...
  3. In the Configurations dialog select New
  4. In the New Configuration Name dialog enter Default and click OK
  5. In the Configurations dialog make sure Default is selected and click Set Active
  6. Click OK in the Configurations dialog
Set Configuration Properties
  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
    2. Power Check "Power target circuit from PICkit3
    3. Select Apply.
    4. Under Conf: [Default] select XC8 compiler
      1. For Option categories select Optimizations
      2. Set "Optimization Set" to "none"
      3. Select Apply
    5. 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.
  1. In the project explorer expand Source Files and open configuration_bits.c
  2. Remove all the text below the #endif line
  3. Notice a new tab has opened at the bottom of the screen
  4. 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:
    1. Extended Instruction Set -- Should be set to OFF when using template
    2. 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.
    3. 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.
    4. 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.
    5. Code/Table Write/Read protects -- Used to disable writing or reading to certain ranges of memory. Leave all of these disabled.
    6. If unsure about a setting, it is usually safe to leave it default.
    7. After all configuration bits have been set, click the "Generate Source Code to Output" button at the bottom of the panel.
    8. The panel will now switch to the Output tab. Select all the text in this tab and copy it to the clip board
    9. Paste it at the bottom of the configuration_bits.c file and pres save.
    10. Clean and build the project again by clicking the broom and hammer icon.
    11. 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
  1. In the datasheet find the section labeled Oscillator Configurations
  2. The first thing listed in this section is Oscillator Types. If you are using the internal oscillator then use the settings relating to INTIO1
  3. 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.

  • IRCF -- Oscillator selection. Since I am using HF-INTOSC/4 (4MHz) I will need to set this to a binary value of 101
  • OSTS -- Read only bit
  • HFIOFS -- Read only bit
  • SCS -- clock select bits. Since I am using the internal oscillator, I will set to 1x where x can be 0 or 1
  • The next register is OSCCON2; however, this register is mostly read only and not important at this point
  • The last oscillator configuration register is OSCTUNE. We will not tune the frequency for this project, however we must use this register to turn on the PLL using the PLLEN bit.
  • 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. When finished build and check for warnings/errors
    5. Do the same for all other needed oscillator registers. See below for an example of a finished ConfigureOscillator function.
    6. Set all the bits as determined above for the OSCCON register. Example: OSCCONbits.IRCF = 0b101;
    7. 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.
    8. 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.

    Useful links 

    http://www.microchip.com/mplab/mplab-code-configurator



    Thanks for taking the time to read this if you liked what you've read then please check back tomorrow after 17.00bst for another update thanks again. Dobby 

    No comments:

    Post a Comment

    dobby repairs

    Hi everyone I know that I haven't posted on here for quite some time  I probably won't be posting on here again for a while but I wa...

    Total Pageviews