Raise to the power of


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2009
    Posts
    33

    Default Raise to the power of

    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

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    It will be easier to do this

    Code:
    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.

  3. #3
    Join Date
    Jul 2009
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    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!
    Last edited by jimseng; - 25th September 2009 at 14:13.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Can we see your code?
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Jul 2009
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    yes you may :

    Code:
    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?

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    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

    Code:
    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

  7. #7
    Join Date
    Jul 2009
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    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.

Similar Threads

  1. Battery powered applications
    By NavMicroSystems in forum Off Topic
    Replies: 7
    Last Post: - 22nd June 2009, 07:12
  2. Pic getting part power from Analog Port
    By ShaneMichael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 22nd April 2009, 10:34
  3. PIC power backup with "super capacitor"
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 24th January 2007, 23:51
  4. 12 Servo's together does not seem to work, Power problem
    By macx75 in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th December 2006, 19:30
  5. problems on power up of PIC
    By dmairspotter in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th June 2006, 14:11

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts