Shop OBEX P1 Docs P2 Docs Learn Events
Help a Newbie? Will this work? — Parallax Forums

Help a Newbie? Will this work?

Andy_FAndy_F Posts: 5
edited 2009-03-28 01:28 in Propeller 1
I'm not new to microcontrollers, but new to the Propeller.

Here goes:

I have some variables (longs) defined in my program. Each one is a pattern for lights being on or off. I am interested in the first 24 bits of each pattern and want to check each bit in turn and turn on the output if it is a one - off if zero.

Which of these methods will work? Sorry, but the editor seems to remove the leading spaces. I've put periods in to represent the leading spaces. Both of these compile fine, which leads me to believe both will work.

repeat index from 0 to 23
...IF frontleft[noparse][[/noparse]index]
......OUTA[noparse][[/noparse]0]~~
...ELSE
......OUTA[noparse][[/noparse]0]~

There's a few others, but that's the jist for one of them - 24 comparisons. Or, do I have to do this:

repeat index from 0 to 23
...IF frontleft[noparse][[/noparse]index] == %1
......OUTA[noparse][[/noparse]0]~~
...ELSE
......OUTA[noparse][[/noparse]0]~


Does the first one work, or the second? I'm waiting for my Propeller Demo Board to get here and wanted to get started on the program.

Thanks for any help.

Andy

Comments

  • JomsJoms Posts: 279
    edited 2009-03-27 23:33
    I am unsure of your answer but if you want to show code examples without attaching hit the 'post reply' button. In there you can click on code which will put [noparse][[/noparse] code ] , [noparse][[/noparse] / code ] brackets around your code, that way it will show all spaces just like in the program...

    Sorry I couldn't be more help on the repeat loop, as I am just learning the prop also...
  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-27 23:44
    I'm not sure what you're asking. Do you have LEDs connected to I/O pins 0-23 and you want to set them on or off depending on the low order 24 bits of a value? If so, you can just say "OUTA[noparse][[/noparse]23..0] := frontleft & $00FFFFFF". The "& $00FFFFFF" is optional. It isn't necessary, but it makes things clearer. If you want to turn one LED on and off in a pattern contained in the variable "frontleft", you'd do
    repeat index from 0 to 23
       outa[noparse][[/noparse] 0 ] := (frontleft & |< index) <> 0
       waitcnt(clkfreq/20 + cnt)
    


    Take a look at the various operators I've used and see if you can figure out why this works. The WAITCNT statement just adds a 50ms delay between bits displayed so the whole loop takes a little over a second.
  • Andy_FAndy_F Posts: 5
    edited 2009-03-28 01:15
    Mike Green said...
    I'm not sure what you're asking. Do you have LEDs connected to I/O pins 0-23 and you want to set them on or off depending on the low order 24 bits of a value? If so, you can just say "OUTA[noparse][[/noparse]23..0] := frontleft & $00FFFFFF". The "& $00FFFFFF" is optional. It isn't necessary, but it makes things clearer. If you want to turn one LED on and off in a pattern contained in the variable "frontleft", you'd do
    repeat index from 0 to 23
       outa[noparse][[/noparse] 0 ] := (frontleft & |< index) <> 0
       waitcnt(clkfreq/20 + cnt)
    


    Take a look at the various operators I've used and see if you can figure out why this works. The WAITCNT statement just adds a 50ms delay between bits displayed so the whole loop takes a little over a second.

    Sorry, should have been clearer. Actually, I am controlling 10 outputs, each one is controlled by a pattern like 'frontleft'. I have a delay and switch checking, etc, I just didn't include them. Basically, each bit of each pattern represents a tick. I control the outputs based on the patterns and the delay at the bottom of the repeat loop controls how long each tick lasts. I also check if the master switch has been turned off and quit the loop if it has (after I wait for a 50ms debounce time and check it again).

    As to what you wrote:

    I think I get the AND and the bitwise decode - as it steps index through from 0 to 23, pin0 is turned on or off based on what the bit in frontleft is. Basically what I was fumbling around trying to do. What I don't understand is the requirement for the "is not equal to 0"

    Could I trouble you to explain?

    Thanks very much,

    Andy
  • Andy_FAndy_F Posts: 5
    edited 2009-03-28 01:25
    Mike Green said...

    outa[noparse][[/noparse] 0 ] := (frontleft & |< index) <> 0


    I GET IT!!!

    If the bit at (index) position of frontleft is NOT a zero - set outa[noparse][[/noparse]0] to that bit - in other words, of its a 1, set outa[noparse][[/noparse]0] to a 1 as well.

    If the bit is a zero, outa[noparse][[/noparse]0] will be set to zero as well - because of the AND. I think. I did a lot better with assembler.

    Did I learn well, master?


    Thanks,

    Grasshopper

    <loved 'Kung Fu' as a kid!>

    Post Edited (Andy_F) : 3/28/2009 1:47:20 AM GMT
  • JomsJoms Posts: 279
    edited 2009-03-28 01:28
    I am no where near a master, but I think you got it.

    I just learned the same lesson from Mr. Green the other day in a past post of mine...

    http://forums.parallax.com/forums/default.aspx?f=25&m=337395
Sign In or Register to comment.