Shop OBEX P1 Docs P2 Docs Learn Events
Very basic prop code help — Parallax Forums

Very basic prop code help

whiteoxewhiteoxe Posts: 794
edited 2009-09-13 18:33 in General Discussion
VAR
·· long Stack[noparse][[/noparse]9]
PUB Main
··· cognew(Toggle(16,3000000,10),@Stack)
··· Toggle(17 ,2000000,20)
PUB Toggle(Pin,Delay,Count)
··· dira[noparse][[/noparse]pin]~~ 'set to output
··· REPEAT Count
······· !outa[noparse][[/noparse]Pin]
······· waitcnt(Delay + cnt)

I need a little help with the above code.

The program is supposed to blink led's off and on. When I look at it I dont see how this works.
The dira sets pins 16 and 17 to ouput.

outa sets pins to high or low. I dont see how the RETURN function is going to set the [noparse][[/noparse]pin]
to turn off and on the LEDS. The return loop is going to continually turn or·keep the LED's on ?·······

Also when I use VAR instead of CON , for some of my spin trials it will not compile using VAR. ??· jumpin.gif

Post Edited (thewhiteoxe) : 9/13/2009 7:11:07 AM GMT

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-09-13 07:20
    Congratulations for massaging the text code to align correctly. Next time though you can use [noparse][[/noparse] code ] tags (without the spaces) to preserve formatting. [noparse]:)[/noparse]

    1. The pin is toggled using the ! (not) operator. Basically, it converts a 1 into a 0 and a 0 into a 1, which when done to outa will make it high or low.
    2. There is no "return" function. What did you mean by this?
    3. The loop repeats count number of times, however many times that is (10 and 20 for this code)
    4. The toggle function ends when the loop finishes, and will go into a limbo state since there's nothing else to do.
    5. VAR where?
  • whiteoxewhiteoxe Posts: 794
    edited 2009-09-13 07:39
    Whoops ! I meant REPEAT. i Have another Question about the code but I;ll ha ve to post it a bit later.
  • whiteoxewhiteoxe Posts: 794
    edited 2009-09-13 15:32
    VAR
    ·· long Stack[noparse][[/noparse]9]
    PUB Main
    ··· cognew(Toggle(11001100,3000000,10),@Stack)
    ··· Toggle(11110000 ,2000000,20)
    PUB Toggle(Pin,Delay,Count)
    ··· dira[noparse][[/noparse]pin]~~ 'set to output
    ··· REPEAT Count
    ······· !outa[noparse][[/noparse]Pin]
    ······· waitcnt(Delay + cnt)


    When cog 0 Pin of 11110000 goes through the REPEAT loop Twenty times·!outa changes the Pin value to
    00001111. But does'nt the pin value stay at 00001111 for each pass ?

    Also the manual says the delay , in the case of cog 0 ,of 2,000,000, is the number of clock cycles for the delay. But if that was so there would be no need for + cnt . So does the clock count up to 10,000,000
    and then starrt from 0 again ?


    Ps. I just used random binary numbers in the above. I dont understand Binary if you have a tip where I can learn this I'll follow it up. skull.gif

    Post Edited (thewhiteoxe) : 9/13/2009 3:38:11 PM GMT
  • SRLMSRLM Posts: 5,045
    edited 2009-09-13 15:44
    Remember that when you use the index operator ( brackets, [noparse]/noparse ) that it returns a single value at the index. In the case of one of the registers, it's a single bit. So !outa[noparse][[/noparse]pin] will change just a single bit.

    Read the waitcnt description in the manual. It says that waitcnt waits for count to be equal to the argument, so if you just put in 2_000_000 then it will wait for the clock to equal that. If instead you add it to cnt, then it will effectively wait for 2_000_000 cycles, then continue.

    For learning binary, just google binary tutorial. There's lots of stuff out there; find something that works for you.
  • whiteoxewhiteoxe Posts: 794
    edited 2009-09-13 15:49
    Thanks for the clarity, I should have realised to google the Binary, and for the tip about only changing a single Bit.

    Was I correct about the value staying the same for each loop in REPEAT ?
  • SRLMSRLM Posts: 5,045
    edited 2009-09-13 18:31
    The bit given by pin of outa will have two different states in the loop (0 or 1). This is due to the ! (not) operator changing outa between the two states. Delay is constant, and I'm not sure about count. My guess is that count is constant.
  • W9GFOW9GFO Posts: 4,010
    edited 2009-09-13 18:33
    thewhiteoxe said...
    Thanks for the clarity, I should have realised to google the Binary, and for the tip about only changing a single Bit.

    Was I correct about the value staying the same for each loop in REPEAT ?

    Picture a row of 32 light switches on a wall. Each switch has a number (1 to 32) so that each switch corresponds to a pin on the Propeller chip. We will call this row of switches "outa".

    If we want to turn every switch on we would say "outa ~~", to turn them all off, "outa ~"

    To turn on a group of switches we could say "outa[noparse][[/noparse]4..6]~~" That would turn on switches 4, 5 and 6.

    To turn on just switch 17 we would say "outa[noparse][[/noparse]17]~~".

    To change switch 17 we say "!outa[noparse][[/noparse]17]".

    "pin" does not change within the repeat loop. "pin" is like the number on the switch, not the position the switch is in.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Servo Boss, a 12 channel servo tester kit from Gadget Gangster.
Sign In or Register to comment.