PIC Micro's For Beginners - Write
your first program
There is one other thing to cover before we
start controlling the pins on the
PIC micro, and that is the
oscillator configurations. You will notice that in the above
picture, pins 15 and 16 are labelled
OSC1 and
OSC2. These are the
connections for an external oscillator. The oscillator speed is what
determines the speed of the
PIC micro, and there are different types
of oscillators, not to mention speeds. Without getting into to much
detail,
crystal oscillators are the most accurate type you can use, and
I would use any other for any timing sensitive applications. They
are not expensive and can be found
here. For any other application (for
example, this tutorial), I use the internal oscillator (a feature
that is common amongst the 18F PIC's). More information on
oscillators can be found in my
oscillator selection guide.
Now we have covered all of the vitals and the basic
layout of the PIC, what better for your first program other then the
flashing LED! (This program was written with the compiler called
Swordfish - free download)
Device = 18F1320 // Specify the PIC type
Clock = 8 // Specify the Clock speed (oscillator)
Config OSC = INTIO2 // Specify the config data for the internal oscillator
Include "utils.bas" // Include the UTILS.BAS library for the SetAllDigital command
Dim LED As PORTA.0 // Assign an alias for "LED"
// Start Of Program...
OSCCON = %01111111 // Sets up the internal oscillator for 8Mhz
Utils.SetAlldigital // Make all Pins digital I/O's
Low(LED) // Make the LED pin an output and set it low
While True // Create an infinite loop
LED = 1 // Turn on the LED
DelayMS(500) // Delay for half a second
LED = 0 // Turn off the LED
DelayMS(500) // Delay for half a second
Wend // Go back and check the While condition
Watch this video for a breakdown of what the
program does and watch it being simulated!
Wherever you see "//", this indicates that the
remainder of the line is a remark. This is handy for keeping track
of what a program is doing by leaving your own comments in. I've
gone to the liberty of remarking every line of code, watch
the video for a more in-depth explanation of everything, I
don't want to over clutter you with text by explaining the program
line by line here.
Ok, so you have the code, and you know what a PIC is -
you now need to get a
PIC programmer and program your code onto your PIC!
Next Part of the guide -
Program the PIC, click to
continue