Jump to content

April 2017 »

23 242526272829

Recent Comments

Recent Comments

- - - - -

The beginnings of PIC (Hello World)

4: Adsense

  • 015
  • 014
  • 013

Hello GDNet

First keep in mind this is a rather long post. I also have images in a entry album for you.

So my PIC Micro Controller starter kit arrive a few days ago and I started to tinker around with it. I really like this piece of hardware.
The circuit build on the development board is very clean. It contains a 6 pin power connector, a 14 pin expansion header, a potentometer (dial), push button, and 4 LED's. There is also 7 resistors and 2 capacitors on the board. By the looks of it there is 1 resistor for each LED so you don't overload them, 2 for the push button, 1 for the expansion header, 1 capacitor for the potentometer and 1 capacitor for the MCU socket. This is just by looking at the board not quite sure if this is acurate would have to review the schematic which I am not quite good at yet.

The programmer (PICkit 3) has a button designed fast wipe the micro controller with a specified hex file. It also has 3 LED's to indicate what is happening.

First before I get into HelloWorld I would like to the pain in the ass features I found with the MPLABX IDE.
  • First I spent hours trying to figure out why the hell the ide could not find the chip on my development board to program it. Turns out by default the IDE assumes you are using a variable range power supply to power the board so I needed to change the options in the project to power the development board through the PICkit 3 programmer.
  • The dreaded device ID not found error. Next the IDE could not find the device ID of my MCU wtf!!!!. 2 hours later I stumbled apon an answer. THE MPLABX IDE MUST BE RUN IN ADMINISTRATOR MODE!!!!! WTF!!!!!!! The users manual stated nothing of the sort. So to get it working I needed to start the IDE in admin mode and after it is started I need to plug the programmer into the usb port. If it is not done in that order you will get errors when trying to connect to the programmer and the chip.

Here is a little quick overview of the specific chip I used for this into project I find typing this stuff out helps me remember anyway.
  • There are 3 types of memory on the PIC16 enhanced mid range. Program memory (Flash), Data memory, and EEPROM memory.
  • Program memory stores the program, data memory handles all the components, EEPROM is persistant memory.
  • Data memory is separated into 32 banks on the PIC16 enhanced mid range.
  • Banks: You deal with these the most. It contains your registers and other cool stuff.
  • Every bank contains the core registers, the special function registers are spread out amongst all the banks, every bank has general purpose ram for variables, and every bank has a section for shared ram which is accessible from all banks.
The HelloWorld project uses 4 instructions, and 4 directives. Instructions instruct the MCU and directives instruct the assembler.
  • banksel: Tells the assembler to select a specific memory bank. This is better to use then the raw instruction because it allows you to select by register name instead of by memory bank number.
  • errorlevel: Used to suppress errors and warnings the assembler spits out.
  • org: Used to set where in program memory the following instructions will reside
  • Labels: used to modularize code it is not a directive per se but a useful thing to use.
  • end: tells the assembler to stop assembling.
  • bsf: bit sets a register (turns it on) sets value to a 1.
  • bcf: bit clear a register (turns it off) sets value to a 0.
  • clrf: initializes a registers bits to 0 so if you have 0001110 it will be come 0000000
  • goto: move to a labeled spot in memory not as efficient as alternative methods
  • LATC: Is a data LATCH. This one is a LATCH for PORTC allows read-modify-write. We use this to write to the appropriate I/O pin for the LED. You allways write with LATCHES it is better to read from PORT
  • PORTC: Reads the pin values for PORTC always write to LATCHES never to PORTS
  • TRISC: Determins if the pin is a input(1) or an output(0)
Explanation of Project:

So generally speaking assembler is very verbose especially on the PIC16 enhanced because you need to ensure you are in the proper bank before trying to manipulate the appropriate register. So in order to light the LED we need to ensure the I/O pin for the LED we want to light is set to an output. We should then initialize the data LATCH which is an array so that all bits are 0. Then we need to turn on (high)(1) the appropriate I/O port that our LED sits on in this case it is RC0 which is wired to LED 1 on DS1.

The code to do this follows forgive the formatting assembler is very strict in that labels can only be in column 1 and include directives can only be in column 1. Everything else must be indented. Also there are some configuration settings for the MCU in the beginning of the file. I am not sure what each one does yet has I did not get a chance to read the specific details yet in the data sheet. These may mess up formatting a bit because it seems they need to be on the same line unwrapped etc... which makes it extend out very far. I will need to look into how to wrap these for readability.
Lastly the code is heavily commented to go with the above explanation.
; --Lesson 1 Hello World
; LED's on the demo board are connected to I/O pins RC0 - RC3.
; We must configure the I/O pin to be an output.
; When the pin is driven high (RC0 = 1) the LED will turn on.
; These two logic levels are derived from the PIC MCU power pins.
; The PIC MCU's power pin is VDD which is connected to 5V and the
; source VSS is ground 0V a 1 is equivalent to 5V and 0 is equivalent to 0V.

; -----------------LATC------------------
; Bit#: -7---6---5---4---3---2---1---0---
; LED:  ---------------|DS4|DS3|DS2|DS1|-
; ---------------------------------------
#include <p16F1829.inc>   ; for PIC specific registers.  This links registers to their respective addresses and banks.

    ; configuration flags for the PIC MCU

    errorlevel -302    ; supress the 'not in bank0' warning

    ORG 0              ; sets the program origin for all subsequent code

    banksel TRISC      ; select bank1 which contains TRISC
    bcf     TRISC,0    ; make IO Pin RC0 an output
    banksel LATC       ; select bank2 which contains LATC
    clrf    LATC       ; init the data LATCH by turning off all bits
    bsf     LATC,0     ; turn on LED RC0 (DS1)
    goto    $          ; sit here forever!

Note: GameDev.net moderates comments.