Shop OBEX P1 Docs P2 Docs Learn Events
4-bit switch — Parallax Forums

4-bit switch

Earl FosterEarl Foster Posts: 185
edited 2009-03-27 19:44 in Propeller 1
I am using a 4-bit switch·to adjust timing.· The below procedure is called with each loop of the program and it works without any problems but I wonder if there was a better way of programming it.· A more efficient way.· It is pretty straightforward with only 4 bits but if I need a 5-bit or 6-bit switch the code starts to get pretty long and would be easy to mess up the binary count.

Trying to get better at coding so I was just wondering.
Pub Get_Timing
   case ina[noparse][[/noparse]21..24]
      %0000: maxtime:= 30
      %0001: maxtime := interval * 1
      %0010: maxtime := interval * 2
      %0011: maxtime := interval * 3
      %0100: maxtime := interval * 4
      %0101: maxtime := interval * 5
      %0110: maxtime := interval * 6
      %0111: maxtime := interval * 7
      %1000: maxtime := interval * 8
      %1001: maxtime := interval * 9
      %1010: maxtime := interval * 10
      %1011: maxtime := interval * 11
      %1100: maxtime := interval * 12
      %1101: maxtime := interval * 13
      %1110: maxtime := interval * 14
      %1111: maxtime := interval * 15



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET

"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-03-27 16:05
    Earl Foster,

    How about something like this?

    Pub Get_Timing|temp
        temp := ina[noparse][[/noparse]21..24]
        if temp <> 0
           maxtime := interval * temp
        else
           maxtime := 30
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Earl FosterEarl Foster Posts: 185
    edited 2009-03-27 16:52
    Serious improvement over my code. I did not realize you could assign inputs in that fashion. Thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-27 16:55
    Or (assuming interval > 30):

    maxtime := (ina[noparse][[/noparse]21..24] * interval) #> 30
    
    
    


    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 3/27/2009 7:16:46 PM GMT
  • Brian FairchildBrian Fairchild Posts: 549
    edited 2009-03-27 17:55
    Depending on your definition of 'efficiency' you might want to look at losing the '*' in all the above. Since the prop doesn't have hardware multiplication you'll suffer a performance hit using it. If your routine only runs once at power up then it won't be an issue; if it runs often you may want to speed it up.

    I'll happily admit to still learning SPIN but something like this may well be faster...

        maxtime := 0
    
        if ina[noparse][[/noparse]21..24] == 0
          maxtime := 30 
        if ina[noparse][[/noparse]21] == 1
            maxtime := interval
        if ina[noparse][[/noparse]22] == 1
            maxtime := maxtime + interval << 1
        if ina[noparse][[/noparse]23] == 1
            maxtime := maxtime + interval << 2
        if ina[noparse][[/noparse]24] == 1
            maxtime := maxtime + interval << 3
    
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-27 18:19
    Even in Spin, which is interpreted, multiplications are done in assembly code and add minimal overhead compared with trying to circumvent them in Spin.

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-03-27 18:50
    ... And the winner is.... Phil with a Spin·execution speed of 25.4 micro seconds.

    See attached timing code.

    Original code - 38.2us
    Beau's Code· - 35.6us
    Phil's Code··· -·25.4us
    Brian's Code· -·78.0us


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Earl FosterEarl Foster Posts: 185
    edited 2009-03-27 19:44
    I would have to say Phil won too for speed but it did changed the way the procedure works.· So Beau wins!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    WWW.HAPB.NET

    "Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman

    Post Edited (Earl Foster) : 3/27/2009 8:00:29 PM GMT
Sign In or Register to comment.