Menu

BIT approach

Gigi
2024-01-30
2024-02-03
  • Gigi

    Gigi - 2024-01-30

    I'm playing around with some coding, I had problems with:
    dim code(5) as byte
    IF code(1).0=1 then ....... 'does this method work?
    dim code(64) as bit 'This also doesn't work and is it correct?

    Thanks

     
  • Anobium

    Anobium - 2024-01-30

    You have to be very specific about the question.

    The code below will work, however, the compiler will optimise this to make a simple variable.bat assignment. Try is as look at the ASM

    dim code(5) as byte
    IF code(1).0=1 then testresult = 1
    

    The code below will work, however, the compiler will optimise this to make a simple variable.bat assignment. Try is as look at the ASM

    dim code(5) as byte
    IF code(1).0=1 then testresult = 1
    IF code(2).0=1 then testresult = 1
    IF code(2).0=1 then testresult = 1
    

    The following produces very different ASM

    dim code(5) as Byte
    dim testresult as Bit
    dim arrayaddress as Byte
    IF code(arrayaddress).0 = 1 then testresult = 1 
    

    Why? The arrayaddress is a variable, not a constant as in the first example.

    So, what are you trying to achieve? Setting a bit in a array is generates a lot of ASM. Copying the element, caching the value, setting the bit value, setting the array element.

    There are ways, faster ways. Setwith(), Rotate etc.


    Arrays of BITS is not supported.


     
  • Gigi

    Gigi - 2024-01-30

    No, I summarized too much and a different problem. As soon as I can I'll post an example to help you understand better.

     
    • Anobium

      Anobium - 2024-01-30

      :-)

      I thought so... setting bits is easy. We do it all the time in the GLCD libraries where speed is really important ( and, we do not use direct assignment of array element.. it is too slow ).

       
  • Gigi

    Gigi - 2024-01-30

    This is the 40bit (5byte) serial transmission I should get:

    define Serial GPIO.0 ' TX serial

    dir serial out

    dim Temp as byte
    dim Temp1,Tsel as Byte
    dim Ser_byte(5) as byte
    dim tempw as word

    ser_byte= 170,0,255
    do
    tx_data
    wait 2 s
    loop

    sub tx_data

     tempw=0    'checksum
    for temp=1 to 3
        tempw +=Ser_byte(temp)
    next
    Ser_byte(4)=tempw:Ser_byte(5)= tempw_H
    
    
     for temp=1 to 5
        for temp1=0 to 7
            if Ser_byte(temp).temp1=1 then 
                serial= ! serial:wait 2 mS  
              else 
                serial= ! serial:wait 1 mS
            end if 
        next
    next
    serial= ! serial:wait 5 mS  'BIT di STOP TX
    serial=1        ' spegne TX
    

    end sub

     
  • Gigi

    Gigi - 2024-01-30

    The error seems to be:
    if Ser_byte(temp).temp1=1 then

    it works well like this:
    for temp=1 to 5
    tsel= Ser_byte(temp)
    for temp1=0 to 7
    if tsel.temp1=1 then
    serial= ! serial:wait 2 mS
    else
    serial= ! serial:wait 1 mS
    end if
    next
    next

     
  • Gigi

    Gigi - 2024-01-30

    but this seems better to me, I haven't tried it yet but it should work.

    for temp=1 to 5
        repeat 8
            if Ser_byte(temp).0=1 then 
                serial= ! serial:wait 2 mS  
              else 
                serial= ! serial:wait 1 mS
            end if
            Rotate Ser_byte(temp) right 
        end repeat
    next
    
     
    • Anobium

      Anobium - 2024-01-30

      I prefer the rotate code.

      Remove to SET C OFF to ensure you do not carry C.

      The following will be faster.

      for temp=1 to 5
          outbits = Ser_byte(temp)
          repeat 8
              if outbits.0 = 1 then 
                  serial= ! serial:wait 2 mS  
              else 
                  serial= ! serial:wait 1 mS
              end if 
              set C off
              rotate outbits right
          end repeat
      next
      
       
  • Gigi

    Gigi - 2024-01-31

    OK, everything works perfectly

     
    • Anobium

      Anobium - 2024-01-31

      Excellent. Please share you final solution. Be good to see.

       
  • Gigi

    Gigi - 2024-02-03

    Scusa il ritardo, I did as you advised


    for temp=1 to 5
    repeat 8
    if Ser_byte(temp).0=1 then
    serial= ! serial:wait 2 mS
    else
    serial= ! serial:wait 1 mS
    end if
    set C off
    Rotate Ser_byte(temp) right
    end repeat

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.