Legacy Forum: Preserving Nearly 20 Years of Community History - A Time Capsule of Discussions, Memories, and Shared Experiences.

I2C Compass

Hitec robotics including ROBONOVA humanoid, HSR-8498HB servos, MR C-3024 Controllers and RoboBasic
56 postsPage 4 of 41, 2, 3, 4
56 postsPage 4 of 41, 2, 3, 4

Post by Gort » Sun Aug 23, 2009 4:09 am

Post by Gort
Sun Aug 23, 2009 4:09 am

I have had no luck so far getting my compass to work using NovaOne's code. I am using the PWM ports 0 and 1. The compass value being displayed on my LCD is always 255? Not sure what I am doing wrong.
I have had no luck so far getting my compass to work using NovaOne's code. I am using the PWM ports 0 and 1. The compass value being displayed on my LCD is always 255? Not sure what I am doing wrong.
Gort
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 555
Joined: Wed May 31, 2006 1:00 am
Location: KC, MO, USA

Post by RoboJeronimo » Sun Aug 23, 2009 2:56 pm

Post by RoboJeronimo
Sun Aug 23, 2009 2:56 pm

At the beggining I had the same problem. That's wrong port conection. Are your constants...

CONST SDA = 0 'Use port 0 for Data, constant SDA
CONST SCL = 1 'Use port 1 for Clock, constant SCL

or

CONST SDA = 1 'Use port 0 for Data, constant SDA
CONST SCL = 0 'Use port 1 for Clock, constant SCL
At the beggining I had the same problem. That's wrong port conection. Are your constants...

CONST SDA = 0 'Use port 0 for Data, constant SDA
CONST SCL = 1 'Use port 1 for Clock, constant SCL

or

CONST SDA = 1 'Use port 0 for Data, constant SDA
CONST SCL = 0 'Use port 1 for Clock, constant SCL
RoboJeronimo
Robot Builder
Robot Builder
Posts: 19
Joined: Fri Jul 10, 2009 11:54 am

Post by Gort » Sun Aug 23, 2009 10:29 pm

Post by Gort
Sun Aug 23, 2009 10:29 pm

My constants are

CONST SDA = 46 'Use port 46 for Data, constant SDA
CONST SCL = 47 'Use port 47 for Clock, constant SCL

I should have said that I am using the PWM connections 0 and 1 which are ports 46 and 47.
My constants are

CONST SDA = 46 'Use port 46 for Data, constant SDA
CONST SCL = 47 'Use port 47 for Clock, constant SCL

I should have said that I am using the PWM connections 0 and 1 which are ports 46 and 47.
Gort
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 555
Joined: Wed May 31, 2006 1:00 am
Location: KC, MO, USA

Post by NovaOne » Tue Aug 25, 2009 1:07 pm

Post by NovaOne
Tue Aug 25, 2009 1:07 pm

I'd like to help Gort...but its all in this thread...my cards are on the table....

as Dr. Lanning said..."I'm sorry. My responses are limited. You must ask the right questions."
I'd like to help Gort...but its all in this thread...my cards are on the table....

as Dr. Lanning said..."I'm sorry. My responses are limited. You must ask the right questions."
NovaOne
Savvy Roboteer
Savvy Roboteer
Posts: 405
Joined: Thu Jul 05, 2007 7:30 am

Post by Gort » Sat Aug 29, 2009 2:41 am

Post by Gort
Sat Aug 29, 2009 2:41 am

My issue is the compass address. NovaOne is using the CMPS03 which has an address of 0x60. I am using the HMC6352 which has a compass address of 0x42. I also have two more questions about NovaOne's code. He sets the I2cAddr in Main: too &Hc0 which I think is a pointer variable, like in C. Can I use a hex number instead for my compass address which is 0x42 or do I need to use a pointer variable? The other question is I am not sure what I2cReg = 1 is doing in Main:.
My issue is the compass address. NovaOne is using the CMPS03 which has an address of 0x60. I am using the HMC6352 which has a compass address of 0x42. I also have two more questions about NovaOne's code. He sets the I2cAddr in Main: too &Hc0 which I think is a pointer variable, like in C. Can I use a hex number instead for my compass address which is 0x42 or do I need to use a pointer variable? The other question is I am not sure what I2cReg = 1 is doing in Main:.
Gort
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 555
Joined: Wed May 31, 2006 1:00 am
Location: KC, MO, USA

Post by i-Bot » Sat Aug 29, 2009 1:55 pm

Post by i-Bot
Sat Aug 29, 2009 1:55 pm

&H is just the prefix to say the number is in HEX to the Robobasic compiler so &HC0 is 0x0c and &H42 would be 0x42 in C speak.

I2cReg = 1 specifies the register to be read from the compass in the I2cByteRead subroutine. The CMPS03 appears as a bank of memory over the I2C bus.

Your HMC6352 is different using a seperate command and response. This means the I2CByteRead routine will not work for you.

You need a couple of new subroutines:
DIM I2cData as INTEGER

WriteA:
GOSUB I2cStart
I2cBuf = I2cAddr
GOSUB I2cOutByte
I2cBuf = "A"
GOSUB I2cOutByte
RETURN

ReadBearing:
GOSUB I2cStart
I2cBuf = I2cAddr OR 1 ' or in the read bit to address
GOSUB I2cOutByte
GOSUB I2cInByte
I2cDataWord = I2cBuf * 256
GOSUB I2cInByte
I2cDataWord = I2cDataWord + I2CInByte
GOSUB I2cStop
RETURN

Then from Main call these with the required delay between.

I didn't check these and my head is in C mode today, so you need to work on them.
&H is just the prefix to say the number is in HEX to the Robobasic compiler so &HC0 is 0x0c and &H42 would be 0x42 in C speak.

I2cReg = 1 specifies the register to be read from the compass in the I2cByteRead subroutine. The CMPS03 appears as a bank of memory over the I2C bus.

Your HMC6352 is different using a seperate command and response. This means the I2CByteRead routine will not work for you.

You need a couple of new subroutines:
DIM I2cData as INTEGER

WriteA:
GOSUB I2cStart
I2cBuf = I2cAddr
GOSUB I2cOutByte
I2cBuf = "A"
GOSUB I2cOutByte
RETURN

ReadBearing:
GOSUB I2cStart
I2cBuf = I2cAddr OR 1 ' or in the read bit to address
GOSUB I2cOutByte
GOSUB I2cInByte
I2cDataWord = I2cBuf * 256
GOSUB I2cInByte
I2cDataWord = I2cDataWord + I2CInByte
GOSUB I2cStop
RETURN

Then from Main call these with the required delay between.

I didn't check these and my head is in C mode today, so you need to work on them.
i-Bot
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1142
Joined: Wed May 17, 2006 1:00 am

Post by Gort » Sun Aug 30, 2009 2:31 am

Post by Gort
Sun Aug 30, 2009 2:31 am

Thanks I-Bot, here is my code so far. Now the issue is that I am alwas getting a value of 10 for the compass.

Main:
I2cAddr = &H42 'Default address of compass
I2cReg = 1 'Register containing Compass bearing as a single Byte
GOSUB WriteA
DELAY 100
GOSUB ReadBearing
PRINT &HFE,&H01
PRINT "heading = "
PRINT FORMAT (I2cDataWord, DEC)
DELAY 4000
GOTO Main

WriteA:
GOSUB I2cStart
I2cBuf = I2cAddr
GOSUB I2cOutByte
I2cBuf = "A"
GOSUB I2cOutByte
PRINT &HFE,&H01
PRINT "Done with WriteA"
DELAY 1000
RETURN

ReadBearing:
GOSUB I2cStart
I2cBuf = I2cAddr OR 1 ' or in the read bit to address
GOSUB I2cOutByte
GOSUB I2cInByte
I2cDataWord = I2cBuf * 256
GOSUB I2cInByte
I2cDataWord = I2cDataWord + I2cData
GOSUB I2cStop
PRINT &HFE,&H01
PRINT "Done with ReadBearing"
PRINT FORMAT (I2cData, DEC)
DELAY 1000
RETURN
Thanks I-Bot, here is my code so far. Now the issue is that I am alwas getting a value of 10 for the compass.

Main:
I2cAddr = &H42 'Default address of compass
I2cReg = 1 'Register containing Compass bearing as a single Byte
GOSUB WriteA
DELAY 100
GOSUB ReadBearing
PRINT &HFE,&H01
PRINT "heading = "
PRINT FORMAT (I2cDataWord, DEC)
DELAY 4000
GOTO Main

WriteA:
GOSUB I2cStart
I2cBuf = I2cAddr
GOSUB I2cOutByte
I2cBuf = "A"
GOSUB I2cOutByte
PRINT &HFE,&H01
PRINT "Done with WriteA"
DELAY 1000
RETURN

ReadBearing:
GOSUB I2cStart
I2cBuf = I2cAddr OR 1 ' or in the read bit to address
GOSUB I2cOutByte
GOSUB I2cInByte
I2cDataWord = I2cBuf * 256
GOSUB I2cInByte
I2cDataWord = I2cDataWord + I2cData
GOSUB I2cStop
PRINT &HFE,&H01
PRINT "Done with ReadBearing"
PRINT FORMAT (I2cData, DEC)
DELAY 1000
RETURN
Gort
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 555
Joined: Wed May 31, 2006 1:00 am
Location: KC, MO, USA

Post by i-Bot » Sun Aug 30, 2009 10:01 am

Post by i-Bot
Sun Aug 30, 2009 10:01 am

Try this:
Main:
I2cAddr = &H42 'Default address of compass
'I2cReg = 1 '!!Not needed for HMC
GOSUB WriteA
DELAY 100
GOSUB ReadBearing
PRINT &HFE,&H01
PRINT "heading = "
PRINT FORMAT (I2cDataWord, DEC)
DELAY 4000
GOTO Main

WriteA:
GOSUB I2cStart
I2cBuf = I2cAddr
GOSUB I2cOutByte
I2cBuf = "A"
GOSUB I2cOutByte
GOSUB I2cStop ' !! I missed the stop
PRINT &HFE,&H01
PRINT "Done with WriteA"
DELAY 1000
RETURN

ReadBearing:
GOSUB I2cStart
I2cBuf = I2cAddr OR 1 ' or in the read bit to address
GOSUB I2cOutByte
I2cAckBit = 0 'NAck !!Proper handling of response
GOSUB I2cInByte
I2cDataWord = I2cBuf * 256
I2cAckBit = 1 'Ack last byte
GOSUB I2cInByte
I2cDataWord = I2cDataWord + I2cData
GOSUB I2cStop
PRINT &HFE,&H01
PRINT "Done with ReadBearing"
PRINT FORMAT (I2cData, DEC)
DELAY 1000
RETURN
Try this:
Main:
I2cAddr = &H42 'Default address of compass
'I2cReg = 1 '!!Not needed for HMC
GOSUB WriteA
DELAY 100
GOSUB ReadBearing
PRINT &HFE,&H01
PRINT "heading = "
PRINT FORMAT (I2cDataWord, DEC)
DELAY 4000
GOTO Main

WriteA:
GOSUB I2cStart
I2cBuf = I2cAddr
GOSUB I2cOutByte
I2cBuf = "A"
GOSUB I2cOutByte
GOSUB I2cStop ' !! I missed the stop
PRINT &HFE,&H01
PRINT "Done with WriteA"
DELAY 1000
RETURN

ReadBearing:
GOSUB I2cStart
I2cBuf = I2cAddr OR 1 ' or in the read bit to address
GOSUB I2cOutByte
I2cAckBit = 0 'NAck !!Proper handling of response
GOSUB I2cInByte
I2cDataWord = I2cBuf * 256
I2cAckBit = 1 'Ack last byte
GOSUB I2cInByte
I2cDataWord = I2cDataWord + I2cData
GOSUB I2cStop
PRINT &HFE,&H01
PRINT "Done with ReadBearing"
PRINT FORMAT (I2cData, DEC)
DELAY 1000
RETURN
i-Bot
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1142
Joined: Wed May 17, 2006 1:00 am

Post by Gort » Sun Sep 06, 2009 8:52 pm

Post by Gort
Sun Sep 06, 2009 8:52 pm

The code in the below PDF is not perfect but it works with the SparkFun I2C HMC6352 compass. Thanks I-Bot and NovaOne for your help.

http://robosavvy.com/Builders/Gort/Spar ... on%201.pdf
The code in the below PDF is not perfect but it works with the SparkFun I2C HMC6352 compass. Thanks I-Bot and NovaOne for your help.

http://robosavvy.com/Builders/Gort/Spar ... on%201.pdf
Gort
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 555
Joined: Wed May 31, 2006 1:00 am
Location: KC, MO, USA

Post by Gort » Mon Sep 07, 2009 12:49 am

Post by Gort
Mon Sep 07, 2009 12:49 am

phpBB [media]
phpBB [media]
Gort
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 555
Joined: Wed May 31, 2006 1:00 am
Location: KC, MO, USA

Post by limor » Tue Oct 20, 2009 12:25 pm

Post by limor
Tue Oct 20, 2009 12:25 pm

I updated the Robonova Knowledgebase and added this Sparkfun module solution link. btw: The module is available at RoboSavvy.com/store
I updated the Robonova Knowledgebase and added this Sparkfun module solution link. btw: The module is available at RoboSavvy.com/store
limor
Savvy Roboteer
Savvy Roboteer
User avatar
Posts: 1845
Joined: Mon Oct 11, 2004 1:00 am
Location: London, UK

Previous
56 postsPage 4 of 41, 2, 3, 4
56 postsPage 4 of 41, 2, 3, 4