PROPBASIC and the I2C commands...

dennodenno Posts: 109
edited October 23 in Propeller 1 Vote Up0Vote Down
Bean, I am stumped...the following is the code that is running in COG 7. It is written for the HMC6352 compass module from Sparkfun.
TASK HMC6352compass  'RUNNING IN COG 2

HMC6352_data   PIN 0
HMC6352_clk    PIN 1

HMC6352_read     CON  $43
HMC6352_WRITE  CON  $42
GET_HEADING       CON  $41

heading_HIGH_byte   VAR   LONG
heading_LOW_byte    VAR   LONG
I2C_data            VAR   LONG
I2C_lsb             VAR   LONG
valueSTR            HUB    string (4)
ackbitvar           VAR   LONG 
ackbitvalue         VAR  LONG

compass_get_heading   SUB
I2C_ACK               SUB
I2C_NACK              SUB
'---------[Main Routine]---------------
 DO
   GOSUB compass_get_heading
    valueSTR = STR heading_HIGH_byte,4, 5
    SEROUT 30, baud2, 1
    SEROUT 30, baud2, 10
    SEROUT 30, baud2, valueSTR
    PAUSE 100
 LOOP

SUB compass_get_heading
  I2CSTART HMC6352_data,HMC6352_clk
  I2C_DATA = HMC6352_WRITE
  I2CWRITE HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvar
  I2C_DATA = GET_HEADING
  I2CWRITE HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvar
  I2CSTOP HMC6352_data, HMC6352_clk
  PAUSE 10
  I2CSTART HMC6352_data,HMC6352_clk
  I2C_DATA = HMC6352_READ
  I2CWRITE HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvar
  PAUSE 10                                   ' Give the HMC6352 time to calculate the rawHeading
  I2CREAD HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvalue
  heading_HIGH_byte = I2C_DATA                ' Read in the high byte
  GOSUB I2C_ACK
  I2CREAD HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvalue
  heading_LOW_byte = I2C_DATA                 ' Read in the low byte
  GOSUB I2C_NACK
  I2CSTOP HMC6352_data, HMC6352_clk
ENDSUB
'
SUB I2C_ACK
  LOW   HMC6352_data                                  ' Pull HMC6352_data low
  INPUT HMC6352_clk                                  ' Let HMC6352_clk float high
  LOW   HMC6352_clk                                  ' Pull HMC6352_clk low
  INPUT HMC6352_data                                  ' Let HMC6352_data float high
ENDSUB
'
SUB I2C_NACK
  INPUT HMC6352_data                                  ' Let HMC6352_data float high
  INPUT HMC6352_clk                                  ' Let HMC6352_clk float high
  LOW   HMC6352_clk                                  ' Pull HMC6352_clk low
ENDSUB

ENDTASK HMC6352   

'======================================================

and I get the following ERROR message, that I do not understand...

PropBasic Version 00.01.48 Aug 9, 2018

Finished Compile. 435 Lines Read, 1989 Lines Generated, 0 Warnings, 0 Errors.

Brads Spin Tool Compiler v0.15.3 - Copyright 2008,2009 All rights reserved
Compiled for i386 Win32 at 08:17:48 on 2009/07/20
Loading Object LCDdisplay_SE
Loading Object LCD_1.spin
Loading Object longDistSensor.spin
Loading Object compass.spin
Loading
Object volt_measure.spin
Loading Object LED_blink.spin
Loading Object blinkAnotherLED.spin
Loading Object sonicDistance.spin

LCDdisplay_SE(177,41) Error : Unresolved Symbol - HMC6352_compass_COG
mov __temp1,HMC6352_compa

Thanks..DennO

Comments

  • 5 Comments sorted by Date Added Votes
  • There is no way I can diagnose an error without all the code.

    The only thing that jumps out at me is that you should not put the task name after ENDTASK. But I don't think that would cause an error, but it might confuse the compiler.

    You don't need to generate the ACK and NACK signal as a separate step. They are generated or received by the normal I2CREAD and I2CWRITE commands.

    Bean
  • dennodenno Posts: 109
    edited October 20 Vote Up0Vote Down
    Bean, once again you have been a big help. I will play around with it a little more. After a while, I did find a couple of errors, involving what I named the TASK in the beginning, and the name I used at the TASK itself. My fault. I removed the ACK and the NACK.

    Now, the program loads, and compiles. But I am not getting an accurate reading from the HMC6352, meaning the "heading_HIGH_byte" does not change from 255 and the "heading_LOW_byte" only changes a few degrees one way or the other. So, my question is...is there some sort of COMMAND that needs to go between the two "I2CREAD(s)". I have included the code below for your inspection. And, does it make a difference which byte is read first, the HIGH or LOW byte? I added the "I2CSPEED" to see if it makes a difference, but it doesn't seem too. Adding the HIGH and LOW byte should give the heading...?
    TASK HMC6352compass  '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
    I2C_data            VAR   LONG
    I2C_lsb             VAR   LONG
    valueSTR            HUB    string (4)
    valueSTR1           HUB    string (4)
    ackbitvar           VAR   LONG 
    ackbitvalue         VAR  LONG
    compass_get_heading   SUB
    
    '---------[Main Routine]---------------
     DO
       GOSUB compass_get_heading
        valueSTR = STR heading_HIGH_byte,4, 5
        valueSTR1 = STR heading_LOW_byte,4, 5
        SEROUT 30, baud2, 13
        SEROUT 30, baud2, valueSTR
        SEROUT 30, baud2, 10
        SEROUT 30, baud2, valueSTR1
        PAUSE 1000
     LOOP
    
    SUB compass_get_heading
      I2CSPEED .5
      I2CSTART HMC6352_data,HMC6352_clk
      I2C_DATA = HMC6352_WRITE
      I2CWRITE HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvar
      I2C_DATA = GET_HEADING
      I2CWRITE HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvar
      I2CSTOP HMC6352_data, HMC6352_clk
      PAUSE 10
      I2CSTART HMC6352_data,HMC6352_clk
      I2C_DATA = HMC6352_READ
      I2CWRITE HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvar
      PAUSE 10                                   ' Give the HMC6352 time to calculate the rawHeading
      I2CREAD HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvalue
      heading_LOW_byte = I2C_DATA                 ' Read in the low byte
      I2CREAD HMC6352_data, HMC6352_clk, I2C_DATA, ackbitvalue
      heading_HIGH_byte = I2C_DATA                ' Read in the high byte
      I2CSTOP HMC6352_data, HMC6352_clk
      heading = heading_HIGH_byte + heading_LOW_byte
    ENDSUB
    
    ENDTASK ''''''HMC6352   
    
    

    Thanks again Bean, for your time, as I suspect that you are a very busy person, much appreciated...\
    DennO
  • Bean, I was wondering if you had time to look at the above code. I am not getting good readings, meaning, the "heading_HIGH_byte" only gives me a reading of 255, and the "heading_LOW_byte only gives a reading of 11 to 13, as I turn the compass, and no way does the two add to the correct heading....

    Any suggestions....thanks...DennO
  • What is ackbitvalue when you read heading_LOW_byte ?
    I would assume it has to be zero, a one will probably abort the transfer.

    I don't see where you set it to any value...

    Also, Why are you always using I2C_DATA instead of just putting the value or variable directly in the I2CREAD or I2CWRITE line ?

    And there is no reason to use ackbitvar if you are not going to look at what it's value is...Just leave it off.
    SUB compass_get_heading
      I2CSTART HMC6352_data,HMC6352_clk
      I2CWRITE HMC6352_data, HMC6352_clk, HMC6352_WRITE
      I2CWRITE HMC6352_data, HMC6352_clk, GET_HEADING
      I2CSTOP HMC6352_data, HMC6352_clk
      PAUSE 10
    
      I2CSTART HMC6352_data,HMC6352_clk
      I2CWRITE HMC6352_data, HMC6352_clk, HMC6352_READ
      PAUSE 10                                   ' Give the HMC6352 time to calculate the rawHeading
      I2CREAD HMC6352_data, HMC6352_clk, heading_LOW_byte, 0
      I2CREAD HMC6352_data, HMC6352_clk, heading_HIGH_byte, 1
      I2CSTOP HMC6352_data, HMC6352_clk
    
      heading = heading_HIGH_byte * 256
      heading = heading + heading_LOW_byte
    ENDSUB
    

    Bean
  • Bean...I have so much to learn...I guess I am just set in my old programming ways, with PBASIC. I can see where your code is easier and take up less space...Thanks again...for your help and knowledge....

    DennO
Sign In or Register to comment.