View Full Version : Compass module and I2C bus
vladimir059@hot
- 8th December 2006, 10:36
Hello everybody. I bought compass module CMPS03 produced by Devantech. It’s employs I2C bus. I wrote a program but it doesn’t work properly. I expect readings from 0 to 359 but I have from 65535 to 49279 and readings are jumping. I’m novice in I2C bus technology, and I can’t figure out what is the problem. If someone has experience with this module, please let me know where I made a mistake or at least point me on a good tutorial of I2C bus.
Thank you in advance
Vladimir
This is my code
'---------------- Definitions---------------------------------------------
define OSC 20
'------------- Define LCD registers and bits------------------------------
Define LCD_DREG PORTB 'Set LCD data port
Define LCD_DBIT 0 'Set starting data bit
Define LCD_RSREG PORTB 'Set LCD register select port
Define LCD_RSBIT 4 'Set LCD register select bit
Define LCD_EREG PORTB 'Set LCD enable port
Define LCD_EBIT 5 'Set LCD enable bit
define DEBUG_REG PORTC
DEFINE DEBUG_BIT 3
DEFINE DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
DPIN var PORTc.2 ' I2C data pin
CPIN var PORTc.1 ' I2C clock pin
bearing var word
Main
i2cread dpin,cpin,%11000000,1,[bearing]
lcdout $fe, 1,"Bearing ", # bearing
DEBUG " Compass Bearing ",DEC bearing,13,10
pause 200
goto main
mister_e
- 8th December 2006, 14:39
Have a look at this one for Basic Stamp
http://www.robot-electronics.co.uk/htm/cmps2bs2.shtml
http://www.robot-electronics.co.uk/htm/cmpsatom.shtml
I don't think your software is the main problem.. double check your hardware first
And probably you need to calibrate it first...
http://www.robot-electronics.co.uk/htm/cmps_cal.shtml
EDIT: AHHHH F*** :( You used a Word sized Variable... it must be a BYTE sized variable for this specific register
vladimir059@hot
- 11th December 2006, 04:46
Hello Mister E.
I tried everything, but it doesn’t work.
I contacted to Devantech Co and Gerald said:
“We've had this before.
It's the PIC Basic Pro compiler.
It does not correctly implement the I2C bus protocol (SCL bus hold).”
Maybe exist other way to implement I2C protocol, or some sort of trick which can help me to overcome these limits
Vladimir
mister_e
- 11th December 2006, 07:06
mpffffff well if it works with a Stamp.. and i use I2CREAD/WRITE in 90% of my stuff without problem, i would probably look at something else first before to tell it's a compiler related stuff.
Tell us your PIC model
Post your config fuses settings
post your schematic
Once again, i doubt it's a PBP problem...
mister_e
- 11th December 2006, 07:17
what happened to your results when you change to a Byte Sized variable? you should read something between 0-255
NOW if you want to use a word sized variable, YOU MUST READ register #2, not 1, so try...
ControlByte VAR BYTE
Register VAR BYTE
ControlByte = $C0
Register = 2
'
'
'
'
i2cread dpin,cpin,ControlByte,Register,[bearing.highbyte, bearing.lowbyte]
and you should read something in a range of 0-3599
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1249&stc=1&d=1165821831">
About now?
vladimir059@hot
- 12th December 2006, 02:15
Big Thank You Mister E.
You and MELABs staff helped me to find mistake. I insert in my code two lines and now program works great. These are two lines:
DEFINE I2C_HOLD 1
DEFINE I2C_SLOW 1
Maybe, someone will have the same problems in future, so I decided to post all my code. It works flawless.
Thanks again
Vladimir
'PIC16f872
'---------------- Definitions---------------------------------------------
define OSC 20
'------------- Define LCD registers and bits------------------------------
DEFINE I2C_HOLD 1
define I2C_SLOW 1
Define LCD_DREG PORTB 'Set LCD data port
Define LCD_DBIT 0 'Set starting data bit
Define LCD_RSREG PORTB 'Set LCD register select port
Define LCD_RSBIT 4 'Set LCD register select bit
Define LCD_EREG PORTB 'Set LCD enable port
Define LCD_EBIT 5 'Set LCD enable bit
define DEBUG_REG PORTC 'Set DEBUD port
DEFINE DEBUG_BIT 1 'Set DEBUG pin
DEFINE DEBUG_BAUD 2400 'Set DEBUG baud rate
DEFINE DEBUG_MODE 0 'Set DEBUG mode
DPIN var PORTc.4 ' I2C data pin
CPIN var PORTc.3 ' I2C clock pin
bearing var word ' word size bearing value 0 - 359
bearing1 var byte ' byte size bearing value 0 - 255
Main:
i2cread dpin,cpin,%11000000,2,[bearing.Highbyte,bearing.lowbyte]
lcdout $fe, 1,"BEARING ",DEC BEARING/10
i2cread dpin,cpin,%11000000,1,[bearing1]
lcdout $fe, $c0,"Bearing ", dec bearing1
DEBUG " Compass Bearing ", # bearing,13,10
pause 20
goto main
mister_e
- 12th December 2006, 03:30
Nice!
Now forward the info to Devantech ;)
vladimir059@hot
- 12th December 2006, 05:12
DONE. I mailed Info to Devantech.
Best Wishes.
Vlad
shaiqbashir
- 26th December 2006, 10:35
Hi guys!
Well ! i have some confusions in understanding the following program :
1) what is the purpose and function of this line :
DEBUG " Compass Bearing ", # bearing,13,10
2) Why is the address be written as %11000000?
3) WHat is the function of this line:
i2cread dpin,cpin,%11000000,2,[bearing.Highbyte,bearing.lowbyte]
4) and what this DEC sign means, i have checked the help section, it is no command. Then why it is written there :
lcdout $fe, 1,"BEARING ",DEC BEARING/10
lcdout $fe, $c0,"Bearing ", dec bearing1
Please help me as soon as possible!
and can anyone tell me whats the cost of this CMPS-03?
Can we operate it through I2C on PIC16F84A?
vladimir059@hot
- 26th December 2006, 16:21
Hello shaiqbashir!
I will try to give you answers.
1) what is the purpose and function of this line :
DEBUG " Compass Bearing ", # bearing,13,10
This is optional line, for development purposes only. It is sends “bearing” value serially to a terminal. In my program this command sends serial string to PORT_C, pin No1, baud rate 2400.
2) Why is the address be written as %11000000?
This is address designated by Devantech (Devantech is company producing the compass module)
3) WHat is the function of this line:
i2cread dpin,cpin,%11000000,2,[bearing.Highbyte,bearing.lowbyte]
Function of this line is reading "bearing" value through I2C bus, as WORD size variable.
4) and what this DEC sign means, i have checked the help section, it is no command. Then why it is written there :
lcdout $fe, 1,"BEARING ",DEC BEARING/10
lcdout $fe, $c0,"Bearing ", dec bearing1
DEC means decimal. This is not command, this is modifier, it is similar to # sign and gives ASCII representation of value sending to LCD.
and can anyone tell me whats the cost of this CMPS-03?
http://www.robotshop.ca/home/products/robot-parts/sensors/magnetic-sensors-compass/devantech-magnetic-compass-module-cmps03.html
Can we operate it through I2C on PIC16F84A?
Why not? In my application I use PIC 16F872 because 16F84A has limited amount of ports (pins).
Best regards Vlad
shaiqbashir
- 26th December 2006, 16:29
Many Many Thanks Mr. Vladimir
please tell me the following two points as well!
1) Which type of LCD screen have u used. Can i use an LCD screen with three terminals (+5V, GND, control) using Serout command? What lcd u have used?
2) On each PIC, it is usually written that which is the SDA pin and which is the SLA one. While when i was reading the diagram of PIC16F84A , it was not mentioned. According to you , i can use this I2C on PIC16F84A, is it possible that we can use any two pins for I2C operation? or are there always two fixed pins dedicated for it?
I shall be looking for ur precious reply!
Take carez!
Good Bye!
vladimir059@hot
- 26th December 2006, 17:47
Hello shaiqbashir!
I used Hitachi style parallel LCD. As I understood you have serial LCD, it can be connected to PICs without problem , but you need to define , port ,pin, baud rate, and other protocol parameters. You can use SEROUT, SEROUT2 and DEBUG commands. DEBUG command is a bit more complicated as it’s involve DEFINE statements. Please check up PIC Basic PRO manual.
As your second question, frankly speaking I’m not I2C bus expert; I just began to learn it two-three weeks ago. I cannot give you comprehensive answers. It is all I know, some microcontrollers has designated I2C pins, and I used such PIC. I really don’t know how will work PICs without designated pins.
Good luck in your experiments
Vlad
Archangel
- 26th December 2006, 20:29
Many Many Thanks Mr. Vladimir
please tell me the following two points as well!
<font color=red>
1) Which type of LCD screen have u used. Can i use an LCD screen with three terminals (+5V, GND, control) using Serout command? What lcd u have used?
2) On each PIC, it is usually written that which is the SDA pin and which is the SLA one. While when i was reading the diagram of PIC16F84A , it was not mentioned. According to you , i can use this I2C on PIC16F84A, is it possible that we can use any two pins for I2C operation? or are there always two fixed pins dedicated for it?</font color>
Good Bye!
Hi shaiqbashir,
PBP Manual shows a schematic on page 85 using a 16F84 as an example.
Your serial LCD should hook up to the pin he defined as DEBUG. you must make sure the baud rates agree.
JS
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.