PDA

View Full Version : 4-bit switch



Earl Foster
03-27-2009, 10:58 PM
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[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 (http://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

Beau Schwabe (Parallax)
03-27-2009, 11:05 PM
Earl Foster,

How about something like this?




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


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Earl Foster
03-27-2009, 11:52 PM
Serious improvement over my code. I did not realize you could assign inputs in that fashion. Thanks

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET (http://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)
03-27-2009, 11:55 PM
Or (assuming interval > 30):




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




-Phil

Post Edited (Phil Pilgrim (PhiPi)) : 3/27/2009 7:16:46 PM GMT

Brian Fairchild
03-28-2009, 12:55 AM
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[21..24] == 0
maxtime := 30
if ina[21] == 1
maxtime := interval
if ina[22] == 1
maxtime := maxtime + interval << 1
if ina[23] == 1
maxtime := maxtime + interval << 2
if ina[24] == 1
maxtime := maxtime + interval << 3

Phil Pilgrim (PhiPi)
03-28-2009, 01:19 AM
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 Schwabe (Parallax)
03-28-2009, 01:50 AM
... 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 (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Earl Foster
03-28-2009, 02:44 AM
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 (http://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