HighSchoolRobots.com

The cheapest way to get into robotics
Home
Site Map
Why JAL?
Getting started with JAL
Tiny Bootloader
Serial Cable
VHS Cassette bot
Servo controller board
Downloads
Contact Us
Your first JAL program
Jal 2 Using variables
Jal 3 FOR loops and ports
Jal 4 IF THEN and 7 segme
Jal 5 Inputs and WHILE lo
Jaluino servo shield
Cheap Serial LCD

Jal 1  Your first JAL program: Isn't this exciting! You are well on your way to blinking a small light on and off continuously. OK so maybe that doesn't sound so cool, but in reality a lot of what you will do in robotics will consist of turning pins on and off. You can do a lot of cool stuff with a blinking light. So here it is. Just bring up jaledit and copy the program below into a new window, or download the code here. There is a worksheet to go along with this activity in the downloads section.

 

On the hardware side you will need the max232 cable and for starters you could use a simple breadboard circuit. If you look at the picture below you'll see a simple circuit to get you up and running. The top right corner is for the 5V regulator. I use a 7805 so the top pin is the 6-12V input then gnd is middle and +5V output is bottom. On the top left corner of the board is the connection for the max232 cable to plug into. Mine is wired top to bottom gnd (from middle of 7805), 5V (from Bottom of 7805),  TX, RX.  You can see a couple of capacitors between + and - on either side, this helps give reliable serial communication. The little button switch is wired from gnd to pin 4 (MCLR), and the resistor on the left is tying MCLR high until the button is pressed to reset the pic. There are two yellow wires supplying power to the pic and a resistor/LED on pin b4 for the all important blinkiness. And don't forget to tie your positive rails and negative rails together (the red wires at the bottom).

 

The Code:

 

-- comments are preceded by a double dash and a space.
-- They are not part of the program. You may omit them if you want.
-- This program was compiled with the jallib libraries and Jal V2 compiler

include 16f88  -- chip definitions
pragma target clock 8_000_000     -- oscillator frequency
pragma bootloader long_start     -- tells the compiler to compile for a bootloader

include delay  -- standard delay procedures

pin_b4_direction = output  -- tells the compiler to set pin b4 as an output pin


forever loop -- your program fits inside the forever loop and loops forever.

   pin_B4 = on -- turn the pin on (+5 Volts)

   delay_100ms( 2 ) -- wait for 200 ms or about 1/5 of a second.

   pin_B4 = off -- turn the pin off (0 volts)

   delay_100ms( 2 ) -- wait again

end loop -- loops back to the beginning of the forever loop to start over

 

Compiling and loading:  Now hit F9 to compile and see if you got it right. If there are errors, you get to figure out why. I am assuming you have already tried compiling one of the sample programs that comes with the pack you are using. If you were able to compile the sample program but not this one, the most likely reason is library / include file issues. My code assumes you are using the Jalpack from Sunish Isaac and tinybootloader. See below for differences between the Jalpack include files and Bert's starter pack. The stuff in the forever loop will remain the same but the header will  change depending on which pack you are using.

 

Once you have a compiled .hex file, load it up in tinybld and program the chip. Still no blinky light? You did connect an LED to pin B4 didn't you? You should probably use a 200-500 ohm resistor between the LED and ground to limit current and avoid blowing out your LED, or damaging your pic. Still nothing? Reverse the LED. It is directional. It needs to be connected with the short wire (also the side with the shaved flat area on the plastic) towards the circuit's ground. Still nothing? Make sure you have +5V connected to pin 14 (Vdd) of the PIC and ground connected to pin 5 (Vss). Also make sure you have a resistor 1k ohm to 10k ohm or so connected between +5V and pin 4 (pin a5). This keeps the pic from reseting.

 

Extra Credit: Try blinking your initials in morse code. Try different blink speeds. What is your limit? Can you detect 30 Hz flashes, 40 Hz?  What if you blink the LED at the frequency of your guitar string, point the LED at the guitar string and pluck? What if you connect a speaker instead of an LED?


If you are ready to move on go to Jal 2 using variables.

Headers: What are the differences between the Jalpack and Bert's starter pack?  See below.

Jalpack header

 

include 16f88  -- chip definitions

include delay  -- standard delay procedures     

pragma bootloader long_start     -- tells the compiler to compile for a bootloader 

 

pragma target clock 8_000_000     -- oscillator frequency

 

pin_b4_direction = output     -- set pin B4 as an output

 

So why did I go with the Jalpack rather than Bert's starter pack, when Bert's is simpler?

I am a teacher and I like the guts to be out in the open. It makes teaching basic concepts easier if the important chip and compiler options are easy to see. If you are a more sophisticated user you might like the simplicity of Bert's all inclusive include file, or you could customize an include file for your own particular setup.

 

If you are programming the pic on a high voltage programmer and not using tinybld then add these lines under the pragma target clock line. This sets up the fuses that are unavailable to the bootloader program.

 

pragma target OSC INTOSC_NOCLKOUT -- internal oscillator with no clock output

pragma target WDT disabled -- no watchdog

pragma target LVP disabled -- no Low Voltage Programming

pragma target MCLR external -- reset externally

 

If you want to know what the different settings are for the above chip options look at the bottom of the 16f88.jal include file. If you want to know what they mean, see the datasheet.

Bert's starter pack
 
include 16f88_bert  -- bert's include file for the f88
 
pin_b4_direction = output -- set pin B4 as an output
 
Bert's starter pack is already set up for a bootloader, so the pragma bootloader long_start line is not needed. Also the 16f88_bert include file already has a pragma target clock line in it. It is defaulted to 20Mhz so you will want to edit that line in the file if you plan to run off the internal 8 Mhz clock.