Why does this program switch pins?


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Question Why does this program switch pins?

    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>
    Last edited by Archangel; - 23rd August 2006 at 19:40. Reason: to correct title

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Smile

    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


    ---------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Aug 2006
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Clear As Mud

    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!

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default I Think . .

    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

  6. #6
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Wink

    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.
    Code:
    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.
    Last edited by rhino; - 23rd August 2006 at 23:31.
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Ahhh

    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

  8. #8
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    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.
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

Similar Threads

  1. Change On Interrupt, PIC16F884
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 14th November 2008, 17:25
  2. Replies: 11
    Last Post: - 6th November 2008, 10:27
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  4. PIC16F684 Program question
    By Nicholas in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th December 2006, 14:30
  5. Output PIC module
    By freelancebee in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th September 2005, 20:10

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts