Shop OBEX P1 Docs P2 Docs Learn Events
access to byte array passed to subroutine — Parallax Forums

access to byte array passed to subroutine

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2006-12-10 01:48 in General Discussion
I try to read bytes from an array passed to a subroutine
for further processing.

SUB mtI2CwritePage
· mtVARaddress_LSB = __PARAM1
· mtVARaddress_MSB = __PARAM2
· mtVARrdata = __PARAM3
· mtI2C_START
· mtI2C_SEND mtVARi2cid
· mtI2C_SEND mtVARaddress_LSB
· mtI2C_SEND mtVARaddress_MSB
· for mtVARwdata=0 to __PARAM4
··· __PARAM2 = mtVARrdata[noparse][[/noparse]mtVARwdata]·· 'this gives error
··· mtI2C_SEND __PARAM2
· next
· mtI2C_STOP
· return

the sub is called with mtI2CwritePage address,array,count· 'address is word, array is byte, count is byte
array is the address of a byte array.
How to make the subroutine know that array (__PARAM3) is the address of a byte array?

regards peter

Comments

  • BeanBean Posts: 8,129
    edited 2006-12-09 12:12
    Peter,
    Arrays are passed by address (byte). So you use the virtual array "__RAM" to access the data in the array.

    SUB mtI2CwritePage
      mtVARaddress = __WPARAM12
      mtVARrdata = __PARAM3
      limit = __PARAM4  
    
      mtI2C_START
      mtI2C_SEND mtVARi2cid
      mtI2C_SEND mtVARaddress_LSB
      mtI2C_SEND mtVARaddress_MSB
      FOR cnt = 1 to limit
    
        mtI2C_SEND __RAM(mtVARrdata)
        INC mtVARrdata
      next
      mtI2C_STOP
      return
    
    

    P.S. You should store your __PARAMx values as soon as possible. Many commands use the as temporary storage (overwriting their value). This can cause problems that are very hard to find.

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "People who are willing to trade their freedom for·security deserve neither and will lose both." Benjamin Franklin


    Post Edited (Bean (Hitt Consulting)) : 12/9/2006 12:19:07 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-12-09 12:14
    Yes, I just found out about __RAM.
    The following compiles,

    SUB mtI2CwritePage
    · mtVARaddress_LSB = __PARAM1
    · mtVARaddress_MSB = __PARAM2
    · mtVARrdata = __PARAM3
    · mtI2C_START
    · mtI2C_SEND mtVARi2cid
    · mtI2C_SEND mtVARaddress_LSB
    · mtI2C_SEND mtVARaddress_MSB
    · for mtVARwdata=0 to __PARAM4
    ··· __PARAM3 = mtVARrdata+mtVARwdata
    ··· mtI2C_SEND __RAM(__PARAM3)
    · next
    · mtI2C_STOP
    · return

    Is this the easiest way when using sx48?

    regards peter
  • BeanBean Posts: 8,129
    edited 2006-12-09 12:19
    Peter,
    Your too fast [noparse];)[/noparse] See my edited post above.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "People who are willing to trade their freedom for·security deserve neither and will lose both." Benjamin Franklin
    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-12-09 12:39
    Thanks Bean.
    Your example showed the for/next must start with 1.
    I had 0 and noticed in the list view it then loops one too many.
    I try to use as few temporary variables as possible.
    It is quite safe to use the higher __PARAMx directly
    as long as called functions have only 1 parameter.
    Obviously I check that in the list view and if any __PARAMx is used
    that my code is using, then I introduce a new temporary variable.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-12-10 01:48
    I defined bytes as aliases within an array

    mtVAR··var·byte(6)
    mtVARwdata·var·mtVAR(0)
    mtVARrdata·var·mtVAR(1)
    mtVARi2cid·var·mtVAR(2)
    mtVARcount·var·mtVAR(3)
    mtVARaddressL·var·mtVAR(4)
    mtVARaddressH·var·mtVAR(5)

    then the subroutine

    SUB mtI2CwritePage
    · mtVARaddressL = __PARAM1
    · mtVARaddressH = __PARAM2
    · mtVARrdata = __PARAM3
    · mtVARcount = __PARAM4
    · mtI2C_START
    · mtI2C_SEND mtVARi2cid
    · mtI2C_SEND mtVARaddressL
    · mtI2C_SEND mtVARaddressH
    · for mtVARwdata=1 to mtVARcount· 'error
    ··· __PARAM3 = mtVARrdata+mtVARwdata
    ··· mtI2C_SEND __RAM(__PARAM3)
    · next
    · mtI2C_STOP
    · return

    gives compile error
    byte parameter expected "mtVARwdata"
    but mtVARwdata is defined as a byte
    How to resolve this?

    regards peter
Sign In or Register to comment.