John Abshier
11-03-2006, 12:37 PM
CON 'CMPSO3Demo
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
SCL = 0 'Define Constants
SDA = 1
VAR
OBJ
C : "CMPS03"
Tv : "Tv_Text"
PUB Main | version, bBearing, wBearing
Init
Tv.str(string("Bus check "))
Tv.dec(C.BusCheck)
Tv.out(13)
Tv.str(string("Device Present "))
Tv.dec(C.Present)
Tv.out(13)
if C.Present
Tv.str(string("Present"))
Tv.out(13)
version := C.GetVersion
Tv.str(string("Version "))
Tv.hex(version,8)
Tv.out(13)
bBearing := C.GetBBearing
Tv.str(string("Byte Bearing "))
Tv.hex(bBearing,8)
Tv.out(13)
wBearing := C.GetWBearing
Tv.str(string("Word Bearing "))
Tv.hex(WBearing,8)
PUB Init
Tv.start(12) ' should check fail
Tv.out($00) ' clear
Tv.out($01) ' home
C.Init(SDA, SCL)
CON 'CMPS03 object
CMP_ADDRESS = $C0
OBJ
I2C : "I2C LowLevel"
PUB Init(SDA, SCL)
repeat 9
I2C.i2cStop
I2C.Initialize(SDA, SCL) ' Initialize i2c MASTER in present COG
PUB GetVersion
result := ReadReg($00)
return
PUB GetBBearing
result := ReadReg($1)
return
PUB GetWBearing : wBearing
wBearing := ReadReg($2)
wBearing <<= 8
wBearing |= REadReg($3)
return
PRI ReadReg(reg)
I2C.i2cStart
I2C.i2cWrite(CMP_ADDRESS | 0, 8)
I2C.i2cWrite(reg, 8)
I2C.i2cStart
I2C.i2cWrite(CMP_ADDRESS | 1, 8)
result := I2C.i2cRead(0, 8)
return
PUB BusCheck
result := I2C.testBus
return
PUB Present
result := I2C.devicePresent(CMP_ADDRESS)
return
The output of the program that reads a CMPS03 compass is
Version 9· This is the correct value.
bBearing $9E8· This should be a byte. E8 would be reasonable.
wBearing $9E80CD6 This should be a word. $0CD6 is a consistent result with $E8 for bBearing
I don't understand what is going on.· I thought that the result local variable was set to 0 on a call.
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
SCL = 0 'Define Constants
SDA = 1
VAR
OBJ
C : "CMPS03"
Tv : "Tv_Text"
PUB Main | version, bBearing, wBearing
Init
Tv.str(string("Bus check "))
Tv.dec(C.BusCheck)
Tv.out(13)
Tv.str(string("Device Present "))
Tv.dec(C.Present)
Tv.out(13)
if C.Present
Tv.str(string("Present"))
Tv.out(13)
version := C.GetVersion
Tv.str(string("Version "))
Tv.hex(version,8)
Tv.out(13)
bBearing := C.GetBBearing
Tv.str(string("Byte Bearing "))
Tv.hex(bBearing,8)
Tv.out(13)
wBearing := C.GetWBearing
Tv.str(string("Word Bearing "))
Tv.hex(WBearing,8)
PUB Init
Tv.start(12) ' should check fail
Tv.out($00) ' clear
Tv.out($01) ' home
C.Init(SDA, SCL)
CON 'CMPS03 object
CMP_ADDRESS = $C0
OBJ
I2C : "I2C LowLevel"
PUB Init(SDA, SCL)
repeat 9
I2C.i2cStop
I2C.Initialize(SDA, SCL) ' Initialize i2c MASTER in present COG
PUB GetVersion
result := ReadReg($00)
return
PUB GetBBearing
result := ReadReg($1)
return
PUB GetWBearing : wBearing
wBearing := ReadReg($2)
wBearing <<= 8
wBearing |= REadReg($3)
return
PRI ReadReg(reg)
I2C.i2cStart
I2C.i2cWrite(CMP_ADDRESS | 0, 8)
I2C.i2cWrite(reg, 8)
I2C.i2cStart
I2C.i2cWrite(CMP_ADDRESS | 1, 8)
result := I2C.i2cRead(0, 8)
return
PUB BusCheck
result := I2C.testBus
return
PUB Present
result := I2C.devicePresent(CMP_ADDRESS)
return
The output of the program that reads a CMPS03 compass is
Version 9· This is the correct value.
bBearing $9E8· This should be a byte. E8 would be reasonable.
wBearing $9E80CD6 This should be a word. $0CD6 is a consistent result with $E8 for bBearing
I don't understand what is going on.· I thought that the result local variable was set to 0 on a call.