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

FOR Loops:

 

A FOR loop is a loop that runs a specific number of times. For example, if I wanted to count up 10 times from 0 to 9, I could make a FOR loop for that. Example:


For 10 loop

x = x + 1

end loop



Ports: Rather than control each pin individually, you can also control 8 pins at a time. There are two ports on the pic16f88. Port A and Port B. To use a port you need to tell the compiler whether it is an input or an output (declare the port) just like you did with the pin_b4 earlier. Here is a sample program that will count up to 9 in binary, using 4 pins from port_b. Remember to copy over the include files and the speed declaration, or download the full code here.


portb_direction = all_output    -- notice that all_output is used rather than output to specify port direction.


var byte x = 0


forever loop

for 10 loop

portb = x -- this displays whatever the value of x is as a binary number on portb

x = x + 1

delay_100ms(4)

end loop -- at this point the program returns to the beginning of the For loop until 10 loops are done.

x = 0 -- reset your variable to 0, otherwise the next time it loops it will start at 9 and count up from there.

end loop



You will need to connect LED lights to pins b0, b1, b2, and b3 the same way you connected LED’s in the previous exercises. Since B2 is also used for programming the PIC you may need to disconnect the LED from B2 while programming.


 

Try modify your program to count up to 13 (1101), then count back down to 3 (0011) then start over. You’ll need two FOR loops one after the other.



Make a Binary stopwatch. The seconds should be on port b and the tens of seconds on port a. When it gets to 59 seconds it should start over. This requires two variables and two nested loops. The seconds loop will be inside the tens of seconds loop. You'll need to wire LED's to pins a0-a3


Counting in binary does boost your geek cred, but if you want to count in decimal you'll need to do a bit more work. Check out Jal 4 IF THEN and 7 segment displays