Shop OBEX P1 Docs P2 Docs Learn Events
For Bean or anyone that uses PROPBASIC... — Parallax Forums

For Bean or anyone that uses PROPBASIC...

dennodenno Posts: 223
edited 2020-08-29 21:17 in Propeller 1
Having trouble with I2C in PROPBASIC with the HMC6352 compass from Sparkfun. In PBASIC I do this, and it works.
HMC6352: (Not all GOSUB's are displayed.
'first read
  GOSUB I2C_Start                            ' Start communications on the I2C buss
  I2C_DATA = HMC6352_WRITE
  GOSUB I2C_Write                            ' Address the HMC6352 in write mode
  I2C_DATA = GET_HEADING_command
  GOSUB I2C_Write                            ' Send the get rawHeading command
  PAUSE 10
  GOSUB I2C_Stop                             ' End communications on the I2C buss
  PAUSE 10
  GOSUB I2C_Start                            ' Start communications on the I2C buss
  I2C_DATA = HMC6352_READ
  GOSUB I2C_Write                            ' Address the HMC6352 in read mode
  PAUSE 10                                   ' Give the HMC6352 time to calculate the rawHeading
  GOSUB I2C_Read
  rawHeading.HIGHBYTE = I2C_DATA                ' Read in the high byte
  GOSUB I2C_ACK
  GOSUB I2C_Read
  rawHeading.LOWBYTE = I2C_DATA                 ' Read in the low byte
  GOSUB I2C_NACK
  GOSUB I2C_Stop                             ' End communications on the I2C buss
  PAUSE 10
  rawHeading = rawHeading / 10               'whole degrees
'      DEBUG DEC4 ? rawheading
  RETURN

The problem I'm having in PROPBASIC is joining the HIGHBYTE and LOWBYTE, as seen in the code below: Adding "heading1" and "heading2" does not work.
SUB compass_get_heading
  I2CSPEED 1
  I2CSTART HMC6352_data,HMC6352_clk
  I2CWRITE HMC6352_data, HMC6352_clk, HMC6352_WRITE
  PAUSE 10
  I2CWRITE HMC6352_data, HMC6352_clk, GET_HEADING
  PAUSE 10
  I2CSTOP HMC6352_data, HMC6352_clk
  PAUSE 10
  I2CSTART HMC6352_data,HMC6352_clk
  I2CWRITE HMC6352_data, HMC6352_clk, HMC6352_READ
  PAUSE 10
  I2CREAD HMC6352_data, HMC6352_clk, heading_HIGH_byte, 0
  PAUSE 10                                   ' Give the HMC6352 time to calculate the rawHeading
  I2CREAD HMC6352_data, HMC6352_clk, heading_LOW_byte, 1
  PAUSE 10
  I2CSTOP HMC6352_data, HMC6352_clk
  PAUSE 10
  heading1 = heading_LOW_byte 
  heading2 = heading_HIGH_byte 
ENDSUB

Any words of wisdom. I am using the FLIP module..thanks to all

Comments

  • BeanBean Posts: 8,129
    You would need to multiply the high byte by 256, then add it to the low byte.

    heading2 = heading2 * 256
    heading1 = heading1 + heading2

    heading1 should now contain the correct value.

    Bean
  • dennodenno Posts: 223
    edited 2020-08-31 13:41
    Bean, thank you for your response. Wow, something so simple. I have spent hours trying to sort that little problem out. Thank you for your help. I do enjoy working with PROPBASIC. I guess because, I have been writing PBASIC for decades, yes decades, and I just find PROPBASIC to sort of follow in line with PBASIC.

    What does the I2CSPEED do exactly. Do I really need the "PAUSE 10's" in the SUB routine?
    TASK HMC6352compass LMM 'RUNNING IN COG 2
    
    HMC6352_data   PIN 0
    HMC6352_clk    PIN 1
    heading             VAR   LONG
    heading_HIGH_byte   VAR   LONG
    heading_LOW_byte    VAR   LONG
    heading1            VAR   LONG
    heading2            VAR   LONG
    heading3            VAR   LONG
    valueSTR_3          HUB   string (3)
    compass_get_heading   SUB
    
    '---------[Main Routine]---------------
    '================================HMC6352 COMPASS ROUTINE=====================================
     DO
       GOSUB compass_get_heading
        SEROUT 30, baud2, 13
        SEROUT 30, baud2, 10
        SEROUT 30, baud2, "heading "
        SEROUT 30, baud2, valueSTR_3
        PAUSE 50
     LOOP'
    
    SUB compass_get_heading
      I2CSPEED .5
      I2CSTART HMC6352_data,HMC6352_clk
      I2CWRITE HMC6352_data, HMC6352_clk, HMC6352_WRITE
      PAUSE 10
      I2CWRITE HMC6352_data, HMC6352_clk, GET_HEADING
      PAUSE 10
      I2CSTOP HMC6352_data, HMC6352_clk
      PAUSE 10
      I2CSTART HMC6352_data,HMC6352_clk
      I2CWRITE HMC6352_data, HMC6352_clk, HMC6352_READ
      PAUSE 10
      I2CREAD HMC6352_data, HMC6352_clk, heading_HIGH_byte, 0
      PAUSE 10                                   ' Give the HMC6352 time to calculate the rawHeading
      I2CREAD HMC6352_data, HMC6352_clk, heading_LOW_byte, 1
      PAUSE 10
      I2CSTOP HMC6352_data, HMC6352_clk
      PAUSE 10
      heading1 = heading_LOW_byte 
      heading2 = heading_HIGH_byte 
      heading2 = heading2 * 256
      heading3 = heading1 + heading2
      heading3 = heading3 / 10  ''''to get whole degrees..
      valueSTR_3 = STR heading3,4,5
      WRLONG heading3_location, heading3
    ENDSUB
    
    ENDTASK ''''''HMC6352
    
  • BTW...the HMC6352 is a great compass module, but they (Sparkfun) doesn't offer it anymore. I can hold 1 degree accuracy, if it is on a level plane. I use it to hold an azmuth angle for my directv dish on my boat while it swings at anchor.

    Anyone got a HMC6352 for sale....?
Sign In or Register to comment.