PDA

View Full Version : Raise to the power of



jimseng
- 25th September 2009, 13:46
Hi.
How does one do "to the power of" in picbasic
I am trying to do a bitwise AND of some numbers in a loop a bit like this:
for i = 1 to 16
x = (y & ((2 ^ i) -1))

next i

But ^ means something else.

I am trying to do this:
x = Y & 1
x = Y & 3
x = Y & 7
x = Y & 15...etc

It's all to do with setting serial latched driver outputs on one by one sequentially from a single number

Jerson
- 25th September 2009, 13:53
It will be easier to do this



for i = 1 to 16
x = y & 1 ' take the lowest bit
' put it out here
y = y >> 1 ' shift right by 1 place
next


Better still, have you considered using the SHIFTOUT command? It's made for applications like these.

jimseng
- 25th September 2009, 14:11
I am indeed using shiftout. I don't think I filly understand it though...I am a little inexperienced with these things.

I basically receive two numbers serially from my PC and using shiftout send them as a word to the serial latches. All is good. With my test LEDs if I send the number 45 to the latches the leds all light as expected like this: 101101
However I want to switch each LED on sequentially with a pause rather than in 1 go so the sequence would be like this:
000001
000101
001101
101101

After learning about bitwise AND I thought I could do it in a loop but so far I have failed.
I appreciate your help!

mackrackit
- 25th September 2009, 14:42
Can we see your code?

jimseng
- 25th September 2009, 14:57
yes you may :



Main:

serin portc.7,6,lowbyte 'sit and wait for an input
serin portc.7,6,highbyte 'sit and wait for an input
outdata.lowbyte = lowbyte
outdata.highbyte = highbyte
if outdata = 0 then ' pause time according to turning on or off
Tptime = 0
else
Tptime = ptime 'from eprom
endif

FOR LoopCount = 1 to 16
bitmask = (outdata & ((DCD loopcount) - 1)) 'find the dec number corresponding to the first pin to switch on
If (outdata & (DCD loopcount) - 1) <> (outdata & (DCD (loopcount - 1)) - 1) Then ' If we sent it just now don't bother again
GOSUB SyncSend ; send the data
pause TPtime 'pause for a bit
endif
NEXT LoopCount
GOTO Main

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''
;----[Send synchronous data]------------------------------------------------
SyncSend:
SHIFTOUT SDout, SClk, 1,[bitmask\16]
HIGH Strobe ; strobe the shift registers latches
@ NOP ; hold Strobe for 1uS
LOW Strobe ; Strobe idles LOW
RETURN


I think I'm nearly there with this method (DCD)
I would love to know if there is a more efficient way of doing it.
Jerson, could you expand on what your method does?

Jerson
- 25th September 2009, 17:02
I can see what you are trying to do. Basically, you have an integer that you want to put on 2 x 8 bit shift registers. The integer will change based on the pattern you want displayed. This is straightforward like you're doing it now. But, if you wish to light each LED one by one depending on the input word, this will do it



outputword var word ' this goes to the shift registers
bitpos var word ' roll the bit you wish to mask out



Main:

serin portc.7,6,lowbyte 'sit and wait for an input
serin portc.7,6,highbyte 'sit and wait for an input
outdata.lowbyte = lowbyte
outdata.highbyte = highbyte
if outdata = 0 then ' pause time according to turning on or off
Tptime = 0
else
Tptime = ptime 'from eprom
endif

bitpos = 1
FOR LoopCount = 1 to 16 ' for each bit of the input
OutputWord = bitmask & bitpos
gosub SyncSend


bitpos = bitpos << 1 ' move the bit 1 place to the left
' so that we can read the next bit
pause TPtime ' wait for the pause time you want
next
GOTO Main


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''
;----[Send synchronous data]------------------------------------------------
SyncSend:
SHIFTOUT SDout, SClk, 1,[OutputWord\16]
HIGH Strobe ; strobe the shift registers latches
@ NOP ; hold Strobe for 1uS
LOW Strobe ; Strobe idles LOW
RETURN


this line will let you light 1 led at a time (dot mode)

bitpos = bitpos << 1 ' move the bit 1 place to the left so that we can read the next bit


But this will let you retain the ones that are already lit (bar mode)

bitpos = bitpos << 1 +1 ' move the bit 1 place to the left so that we can read the next bit and retain the one that was on previously

jimseng
- 25th September 2009, 17:28
Hey thanks.
I managed to do it with dcd a bit like my previous post with a few tweaks to get it right but it is not nearly as efficient as yours. I think I almost get what is going on now.