04.11.2014 Views

Nuts & Volts

Nuts & Volts

Nuts & Volts

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

GETTING STARTED WITH PICs<br />

through the major sections of code.<br />

The top section states: *** Define<br />

Program Variables *****. This is where<br />

the variable gets defined. A single variable<br />

is established and given the simple<br />

nickname of “x.” The program will use<br />

that later as a general storage space for<br />

the FOR-NEXT loop command counter.<br />

Program variables need to have<br />

their size defined. In this example, “x”<br />

does not need to be larger than 255,<br />

so a byte size will do.<br />

‘ *** Program Variables ***<br />

x var byte<br />

After the variable definition, the<br />

main program loop is entered. The<br />

description line ‘*** Main program<br />

loop *** begins this section and it’s<br />

quickly followed by the main: label.<br />

‘ *** Main program loop ***<br />

main:<br />

This label defines a location within<br />

the program where the main section of<br />

code begins. Under this label is where<br />

the LEDs are sequenced. The HIGH and<br />

LOW commands are used to turn the<br />

LED on (HIGH) and off (LOW). Because<br />

this program has to repeat the same<br />

function for each LED, the program<br />

could have a bunch of HIGH and LOW<br />

commands. That would work, but it<br />

would also take up more than 31 lines.<br />

To simplify the program, the FOR-<br />

NEXT command will be used. The<br />

FOR-NEXT command creates a small<br />

loop where everything between the<br />

line that starts with FOR and the line<br />

that starts with NEXT is rerun a specified<br />

number of times. For example,<br />

look at the code section below.<br />

for x = 8 to 15<br />

high x<br />

pause 10<br />

low x<br />

pause 10<br />

next<br />

The section of code starts with<br />

the FOR command followed by a<br />

simple little math type statement, x =<br />

8 to 15. What this means is that every<br />

time this list of commands is executed,<br />

increment the variable x by one<br />

starting with 8 and ending at 15. This<br />

is known as a FOR-NEXT loop.<br />

The value of the variable x is<br />

actually tested at the NEXT command<br />

line. If x equals 15, the program leaves<br />

the FOR-NEXT loop and moves on to<br />

the commands following the NEXT<br />

command line. If x does not equal 15,<br />

the program control jumps back to<br />

the FOR line and x is increased by 1.<br />

This allows you to write a chunk of<br />

repeating commands without having<br />

to write them over and over again.<br />

In this loop of code, notice how<br />

the HIGH and LOW commands share<br />

the variable x. Each time the program<br />

runs through the FOR-NEXT loop, a<br />

different LED should be lit. By using<br />

the variable x after the HIGH and<br />

LOW commands, a different LED is<br />

turned on and then off.<br />

The value of x must match up to<br />

the pins connected to the LEDs. In<br />

this case, because the LEDs are connected<br />

to Port C, we can use 8 through<br />

15 as the FOR-NEXT counter values<br />

while also using them as the pin variable<br />

in the HIGH or LOW commands.<br />

This project uses a very short<br />

delay time between the HIGH and<br />

LISTING 1<br />

LOW commands. The intent is to make<br />

the light scroll forward quickly. Notice<br />

that I only said forward. That’s because<br />

this FOR-NEXT loop only lights LEDs 8<br />

to 15. It does not reverse the direction<br />

and light 15 through 8. That requires a<br />

separate FOR-NEXT loop. The next<br />

section of code is very similar and is<br />

another FOR-NEXT loop. In this<br />

section, though, the FOR-NEXT loop<br />

counts down instead of counting up.<br />

for x = 15 to 8 step -1<br />

high x<br />

pause 10<br />

low x<br />

pause 10<br />

next<br />

That change of counting direction<br />

is done with the step –1 added to the<br />

FOR command line. The step –1 directs<br />

the FOR-NEXT command to add a -1<br />

(negative 1) to the value of x each time<br />

through the loop. The FOR command<br />

line starts at 15 and goes to 8. It’s the<br />

opposite of the first FOR-NEXT loop.<br />

The purpose of counting the opposite<br />

direction is to make the LED light<br />

‘******************************************************************<br />

‘* Name : chap6code.BAS *<br />

‘* Author : Chuck Hellebuyck *<br />

‘* Notice : Copyright (c) 2005 Electronic Products *<br />

‘* : All Rights Reserved *<br />

‘* Date : 3/31/05 *<br />

‘* Version : 1.0 *<br />

‘* Notes : Scroll 8 LEDs *<br />

‘* : *<br />

‘******************************************************************<br />

‘ *** Program Variables ***<br />

x var byte ‘ FOR-NEXT loop variable defined<br />

‘ *** Main program loop ***<br />

Main:<br />

for x = 8 to 15<br />

high x<br />

pause 10<br />

low x<br />

pause 10<br />

next<br />

‘ Main loop label<br />

‘ Loop 8 times thru code<br />

‘ Turn on next LED<br />

‘ Delay 10 milliseconds<br />

‘ Turn off LED<br />

‘ Delay 10 milliseconds<br />

‘ Is x = 7 yet?<br />

for x = 15 to 8 step -1 ‘ Loop 8 times in negative direction<br />

high x<br />

‘ Turn next LED on<br />

pause 10<br />

‘ Pause 10 milliseconds<br />

low x<br />

‘ Turn off LED<br />

pause 10<br />

‘ Pause 10 milliseconds<br />

next<br />

‘ Is x = 0 yet?<br />

goto main<br />

end<br />

‘ Jump to main and do it again<br />

‘ Stop if the program gets here<br />

April 2006 77

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!