PDA

View Full Version : Why does this program switch pins?



Archangel
- 23rd August 2006, 19:31
Hi, All,
I copied this demo program from "Programming PIC Microcontrollers with Pic Basic" ,by Chuck Hellebuyck. Its function is to scroll across LEDs hooked up to each pin on port B like the lights on the front of the Knight Rider car, so as to demonstrate how the FOR NEXT statements count.

My question is why does it do its counting by switching pins, instead of counting only on 1 pin? To my UNTRAINED Eye it does not mention the other pins, yet it works just as he promises.

<quote> LED var Byte 'LED variable setup as byte

PortB = %00000000 'Initiate all port B pins to low
Trisb = %00000000 'Setup port b as all outputs
main: 'Label for beginning of main loop

' *********** Light LEDs in right direction

for led = 0 to 7 'Loop through all LEDs
portB.0[LED] = 1 'Set each pin of portb high (5 volts) which turns the LED on
pause 1000 'Pause 1000 milliseconds (1 second) with LED on
portb.0[LED] = 0 'Set each pin of portb low (0 volts) which turns the LED off
next 'Continue until all 7 have been lit once

' *********** Light LEDs in left direction

for led = 7 to 0 step -1 'Loop through all LEDs backwards
portb.0[led] = 1 'Set pin 0 of portb high (5 volts) which turns the LED on
pause 1000 'Pause 1000 milliseconds (1 second) with LED on
portb.0[led] = 0 'Set pin 0 of portb low (0 volts) which turns the LED off
next 'Continue until all 7 have been lit once

goto main 'Jump to the main label and do it all again and again

END 'This line is not needed but its safe to put it here just in case
' the program gets lost.

</quote>

sayzer
- 23rd August 2006, 20:04
This might be more explanatory.

LED = 0 -> PORTB.0[0] -> PORTB.0 -> pin 6
LED = 1 -> PORTB.0[1] -> PORTB.1 -> pin 7
LED = 2 -> PORTB.0[2] -> PORTB.2 -> pin 8
LED = 3 -> PORTB.0[3] -> PORTB.3 -> pin 9
LED = 4 -> PORTB.0[4] -> PORTB.4 -> pin 10
LED = 5 -> PORTB.0[5] -> PORTB.5 -> pin 11
LED = 6 -> PORTB.0[6] -> PORTB.6 -> pin 12
LED = 7 -> PORTB.0[7] -> PORTB.7 -> pin 13


---------

dccch
- 23rd August 2006, 20:25
Hi, you need to do some decimal to binary conversion
D= Bin
0= 0000 all PORT pins set to 0
1= 0001 only PORT.0 is set
2= 0010 only PORT.1 is set
3= 0011 PORT.0 and PORT.1 are set
4= 0100
5= 0101
6= 0110
7= 0111
8= 1000
and so on

Archangel
- 23rd August 2006, 20:47
OK Guys Let's just pretend I am a dummy, I think we can all do that.

I get the binary value of each pin, 00000001 = pin 0 = 1 and 00000010 pin 1 = 1 and so on. I get that the code "FOR led = 0 to 7 "makes it count 0 to seven, What statement in this code makes it not count only on pin 0 where it starts and makes it go pin to pin. I am using a FOR NEXT statement in one of my own programs to flash some LEDs a given amount of counts and it stays on the pin I told it to, so my question is " Exactly which statement makes this code select pins instead of flashing only 1 pin"? This code is so short and sweet as to be elegant, but it has outsmarted me, for the time being anyway.

Anyway, Thank you all for the help, and if you are, jumping up and down hollering "You Idiot" ...Ok I'm good with that, DEIDIOFY ME!

Archangel
- 23rd August 2006, 23:03
HMMMMM Dangerous . .

I think it it the [LED] call to the variable, I took it out of the second section of the code and addad a pause and now it swweps in only 1 direction and flashes 8 times port 0 then cycles all over again.
JS

rhino
- 23rd August 2006, 23:28
Joe -
The author is using a different method for telling the micro which pin to turn on or off. He's using an array. With this method you can minimize the amount of code needed, and hence the cleaner, tidier code. Let's walk through it with some more comments.


LED var Byte 'LED variable setup as byte

PortB = %00000000 'Initiate all port B pins to low
Trisb = %00000000 'Setup port b as all outputs
main: 'Label for beginning of main loop

' *********** Light LEDs in right direction

for led = 0 to 7 'Loop through all LEDs
This variable not only counts up, but also indicates which pin
should turn on or off.
portB.0[LED] = 1 'Set each pin of portb high (5 volts) which turns the LED on
The first time through the loop, LED = 0. So the statement
PORTB.0[LED] = 1 is the same as writing portB.0 = 1.
The next time through the loop, LED will = 1, which is the same
as writing portB.1 = 1 and so on.
Think of it as turn on PORTB.0 + the value in the LED variable.
pause 1000 'Pause 1000 milliseconds (1 second) with LED on


Now look at Sayzer's post and it should make much more sense.

Archangel
- 24th August 2006, 02:35
So thats how an array works !
Melanie gripes about people not taking classes, but here in the states you try to go to class and they start you out learning how to solder, regardless of your experience, so 5 years later you do not remember why you started to class, and still never got to programming PICs, and thats OK if your 20, not if your 50, and yes Melanie, I am enrolled in school, I do buy the books, I talk a little too much Smack, and apoligize for my seemingly condecending tone, it's not intended . .
I am much too impressed with her Brains and Wit, anyway I'll hit the books some more, so my posts are more meaningful. Soooo THANK YOU ALL who posted and helped me understand, sorry if I bored you
JS

rhino
- 24th August 2006, 04:24
You know something.... I tried to find in the PBP manual where it explains this functionality, but couldn't find it. I'm sure it's buried in there somewhere, but I didn't see it. I was planning on adding what the manual said to my post. The only reason I knew of this method was from reading up on this forum. Believe me, I know it can get frustrating sometimes trying to make something work or understanding what's under the hood, but with this forum and the manual.... you're sitting on a gold mine of knowledge. Can't think of too many things that other people haven't asked or answered here before. What's cool about the forum are the guru's that expose either the undocumented features of the compiler or add functionality to it by publishing include files. It really is a great resource to have at your finger tips.