GyroScope Problem
kenwtn
Posts: 250
I just plugged in Parallax's Gyroscope module and ran test program. When displaying X,Y,Z I get -1 for all 3 values? The Connection is very straight forward so really not sure what is going on with it unless the Module itself might be bad. Note: just took it out of pack today for the 1st time.
On the PDF download from Parallax it shows Connecting Gnd, Vdd, SCL, SDA But further reading shows CS 1: For IC2 0: For IC2 Disabled I currently do not have this pin connected.
Also, I using a BS2Pe
Below is my CODE:
' {$STAMP BS2pe}
' {$PBASIC 2.5}
BaudMode CON 84 '2400 True
LCD PIN 15 'LCD Display
SDA PIN 0 ' SDA of gyro connected to P0
SCL PIN 1 ' SCL of gyro connected to P1
WRITE_Data CON $3C ' Requests Write operation
READ_Data CON $3D ' Requests Read operation
MODE CON $02 ' Mode setting register
X_MSB CON $03 ' X MSB data output register
X VAR Word
Y VAR Word
Z VAR Word
rawl VAR Word
rawh VAR Word
' Variables for I2C communications
I2C_DATA VAR Byte
I2C_LSB VAR Bit
I2C_REG VAR Byte
I2C_VAL VAR Byte
HIGH LCD ' Set Display Line to Low
PAUSE 100 ' Power up delay
SEROUT LCD, BaudMode, ["?S0"] ' No Boot Screen
PAUSE 200 ' pause to allow LCD EEPROM to program
SEROUT LCD, BaudMode, ["?G420"] ' Sets geometry of LCD as 4X20
PAUSE 200 ' pause to allow LCD EEPROM to program
'SEROUT LCD, 84, ["?BFF"] '100% backlight
SEROUT LCD, Baudmode, ["?B7F"] '15% backlight
'SEROUT LCD, Baudmode, ["?B7E"] '50% baclight
PAUSE 300 ' pause to allow LCD EEPROM to program \
SEROUT LCD, BaudMode, ["?c0"] 'Kill Cursor
GOSUB ClearScreen
PAUSE 100 ' Power up delay
I2C_REG = MODE ' Set operating mode to continuous
I2C_VAL = $0
GOSUB I2C_Write_Reg
PAUSE 1000
DO
GOSUB GetRawReading
SEROUT LCD, BaudMode, ["?y0?x04"]
SEROUT LCD, BaudMode, ["RAW X = ",11, SDEC X]
SEROUT LCD, BaudMode, ["?y1?x04"]
SEROUT LCD, BaudMode, ["RAW Y = ",11, SDEC Y]
SEROUT LCD, BaudMode, ["?y2?x04"]
SEROUT LCD, BaudMode, ["RAW Z = ",11, SDEC Z]
PAUSE 250
LOOP
ClearScreen:
SEROUT LCD, Baudmode, ["?f"] ' clear the LCD
SEROUT LCD, BaudMode, ["?y0?x00"] ' cursor at line 0, col 0
PAUSE 500
RETURN
GetRawReading:
PAUSE 400 ' Wait for new data
' Send request to X MSB register
GOSUB I2C_Start
I2C_DATA = WRITE_Data
GOSUB I2C_Write
I2C_DATA = X_MSB
GOSUB I2C_Write
GOSUB I2C_Stop
'Get data from register (6 bytes total, 2 bytes per axis)
GOSUB I2C_Start
I2C_DATA = READ_Data
GOSUB I2C_Write
' Get X
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_ACK
X = (rawH << 8) | rawL
' Get Z
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_ACK
Z = (rawH << 8) | rawL
' Get Y
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_NACK
Y = (rawH << 8) | rawL
GOSUB I2C_Stop
RETURN
'
I2C functions
' Set I2C_REG & I2C_VAL before calling this
I2C_Write_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
I2C_DATA = I2C_VAL
GOSUB I2C_Write
GOSUB I2C_Stop
RETURN
' Set I2C_REG before calling this, I2C_DATA will have result
I2C_Read_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
GOSUB I2C_Stop
GOSUB I2C_Start
I2C_DATA = READ_DATA
GOSUB I2C_Write
GOSUB I2C_Read
GOSUB I2C_NACK
GOSUB I2C_Stop
RETURN
I2C_Start:
LOW SDA
LOW SCL
RETURN
I2C_Stop:
LOW SDA
INPUT SCL
INPUT SDA
RETURN
I2C_ACK:
LOW SDA
INPUT SCL
LOW SCL
INPUT SDA
RETURN
I2C_NACK:
INPUT SDA
INPUT SCL
LOW SCL
RETURN
I2C_Read:
SHIFTIN SDA, SCL, MSBPRE, [I2C_DATA]
RETURN
I2C_Write:
I2C_LSB = I2C_DATA.BIT0
I2C_DATA = I2C_DATA / 2
SHIFTOUT SDA, SCL, MSBFIRST, [I2C_DATA\7]
IF I2C_LSB THEN INPUT SDA ELSE LOW SDA
INPUT SCL
LOW SCL
INPUT SDA
INPUT SCL
LOW SCL
RETURN
On the PDF download from Parallax it shows Connecting Gnd, Vdd, SCL, SDA But further reading shows CS 1: For IC2 0: For IC2 Disabled I currently do not have this pin connected.
Also, I using a BS2Pe
Below is my CODE:
' {$STAMP BS2pe}
' {$PBASIC 2.5}
BaudMode CON 84 '2400 True
LCD PIN 15 'LCD Display
SDA PIN 0 ' SDA of gyro connected to P0
SCL PIN 1 ' SCL of gyro connected to P1
WRITE_Data CON $3C ' Requests Write operation
READ_Data CON $3D ' Requests Read operation
MODE CON $02 ' Mode setting register
X_MSB CON $03 ' X MSB data output register
X VAR Word
Y VAR Word
Z VAR Word
rawl VAR Word
rawh VAR Word
' Variables for I2C communications
I2C_DATA VAR Byte
I2C_LSB VAR Bit
I2C_REG VAR Byte
I2C_VAL VAR Byte
HIGH LCD ' Set Display Line to Low
PAUSE 100 ' Power up delay
SEROUT LCD, BaudMode, ["?S0"] ' No Boot Screen
PAUSE 200 ' pause to allow LCD EEPROM to program
SEROUT LCD, BaudMode, ["?G420"] ' Sets geometry of LCD as 4X20
PAUSE 200 ' pause to allow LCD EEPROM to program
'SEROUT LCD, 84, ["?BFF"] '100% backlight
SEROUT LCD, Baudmode, ["?B7F"] '15% backlight
'SEROUT LCD, Baudmode, ["?B7E"] '50% baclight
PAUSE 300 ' pause to allow LCD EEPROM to program \
SEROUT LCD, BaudMode, ["?c0"] 'Kill Cursor
GOSUB ClearScreen
PAUSE 100 ' Power up delay
I2C_REG = MODE ' Set operating mode to continuous
I2C_VAL = $0
GOSUB I2C_Write_Reg
PAUSE 1000
DO
GOSUB GetRawReading
SEROUT LCD, BaudMode, ["?y0?x04"]
SEROUT LCD, BaudMode, ["RAW X = ",11, SDEC X]
SEROUT LCD, BaudMode, ["?y1?x04"]
SEROUT LCD, BaudMode, ["RAW Y = ",11, SDEC Y]
SEROUT LCD, BaudMode, ["?y2?x04"]
SEROUT LCD, BaudMode, ["RAW Z = ",11, SDEC Z]
PAUSE 250
LOOP
ClearScreen:
SEROUT LCD, Baudmode, ["?f"] ' clear the LCD
SEROUT LCD, BaudMode, ["?y0?x00"] ' cursor at line 0, col 0
PAUSE 500
RETURN
GetRawReading:
PAUSE 400 ' Wait for new data
' Send request to X MSB register
GOSUB I2C_Start
I2C_DATA = WRITE_Data
GOSUB I2C_Write
I2C_DATA = X_MSB
GOSUB I2C_Write
GOSUB I2C_Stop
'Get data from register (6 bytes total, 2 bytes per axis)
GOSUB I2C_Start
I2C_DATA = READ_Data
GOSUB I2C_Write
' Get X
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_ACK
X = (rawH << 8) | rawL
' Get Z
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_ACK
Z = (rawH << 8) | rawL
' Get Y
GOSUB I2C_Read
rawH = I2C_Data
GOSUB I2C_ACK
GOSUB I2C_Read
rawL = I2C_Data
GOSUB I2C_NACK
Y = (rawH << 8) | rawL
GOSUB I2C_Stop
RETURN
'
I2C functions
' Set I2C_REG & I2C_VAL before calling this
I2C_Write_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
I2C_DATA = I2C_VAL
GOSUB I2C_Write
GOSUB I2C_Stop
RETURN
' Set I2C_REG before calling this, I2C_DATA will have result
I2C_Read_Reg:
GOSUB I2C_Start
I2C_DATA = WRITE_DATA
GOSUB I2C_Write
I2C_DATA = I2C_REG
GOSUB I2C_Write
GOSUB I2C_Stop
GOSUB I2C_Start
I2C_DATA = READ_DATA
GOSUB I2C_Write
GOSUB I2C_Read
GOSUB I2C_NACK
GOSUB I2C_Stop
RETURN
I2C_Start:
LOW SDA
LOW SCL
RETURN
I2C_Stop:
LOW SDA
INPUT SCL
INPUT SDA
RETURN
I2C_ACK:
LOW SDA
INPUT SCL
LOW SCL
INPUT SDA
RETURN
I2C_NACK:
INPUT SDA
INPUT SCL
LOW SCL
RETURN
I2C_Read:
SHIFTIN SDA, SCL, MSBPRE, [I2C_DATA]
RETURN
I2C_Write:
I2C_LSB = I2C_DATA.BIT0
I2C_DATA = I2C_DATA / 2
SHIFTOUT SDA, SCL, MSBFIRST, [I2C_DATA\7]
IF I2C_LSB THEN INPUT SDA ELSE LOW SDA
INPUT SCL
LOW SCL
INPUT SDA
INPUT SCL
LOW SCL
RETURN
Comments
You're right about the connection looking pretty straight forward. Just the four pins as shown on page 2 of the guide are required.
I think receiving all "-1" values indicate a communication problem. Sometimes the connection in a breadboard can be bad. I'd suggest pulling the wires and sensor out of the breadboard and plug it back in in a different location. Use a different set of hookup wires too.
I've had a couple of circuits not work until they were moved and rewired. I don't know if connection problem was the wire's fault or a bad clip inside the breadboard's fault.