Hello,
I completed the SPI example on the Learn website given here:
http://learn.parallax.com/tutorials/language/propeller-c/propeller-c-simple-protocols/spi-example
I would now like to read data from the Parallax L3G4200D gyroscope module using the 3-wire SPI mode. Here is my code so far:
/*
Gyroscope Test XYZ Axes.c
*/
#include "simpletools.h" // Include simple tools
const int DATA = 12;
const int CLK = 13;
const int CS = 14;
const int CTRL_REG1 = 0x20;
const int CTRL_REG2 = 0x21;
const int CTRL_REG3 = 0x21;
const int CTRL_REG4 = 0x22;
const int STATUS_REG = 0x27;
unsigned char statusReg;
signed char x; // X-axis value
signed char y; // Y-axis value
signed char z; // Z-axis value
int main() // Main function
{
high(CS); // CS line high (inactive)
low(CLK); // CLK line low
low(CS); // CS line low (start SPI)
shift_out(DATA, CLK, MSBFIRST, 16, 0b0010000000011111); // Write CTRL_REG1
shift_out(DATA, CLK, MSBFIRST, 16, 0b0010001000001000); // Write CTRL_REG3
shift_out(DATA, CLK, MSBFIRST, 16, 0b0010001110000001); // Write CTRL_REG4
high(CS); // CS line high (stop SPI)
while(1) // Main loop
{
low(CS); // CS low selects chip
shift_out(DATA, CLK, MSBFIRST, 8, 10100111); // Send read register address, x-axis
statusReg = shift_in(DATA, CLK, MSBPOST, 16); // Get value from register
if(statusReg == 0)
{
shift_out(DATA, CLK, MSBFIRST, 8, 10100111); // Send read register address, x-axis
statusReg = shift_in(DATA, CLK, MSBPOST, 16); // Get value from register
}
else
{
shift_out(DATA, CLK, MSBFIRST, 8, 10100111); // Send read register address, x-axis
statusReg = shift_in(DATA, CLK, MSBPOST, 16); // Get value from register
//x = shift_in(DATA, CLK, MSBPRE, 8); // Get value from register
/*
cmd = YOUT8 & readMask;
shift_out(DATA, CLK, MSBFIRST, 7, cmd); // Send read register address, y-axis
shift_out(DATA, CLK, MSBFIRST, 1, 0b0); // Send don't care value
y = shift_in(DATA, CLK, MSBPRE, 8); // Get value from register
cmd = ZOUT8 & readMask;
shift_out(DATA, CLK, MSBFIRST, 7, cmd); // Send read register address, z-axis
shift_out(DATA, CLK, MSBFIRST, 1, 0b0); // Send don't care value
z = shift_in(DATA, CLK, MSBPRE, 8); // Get value from register
high(CS);
*/ // De-select chip
// print("%cx=%d y=%d z=%d%c", HOME, x, y, z, CLREOL); // Display measurement
pause(500); // Wait 0.5s before repeat
}
}
I am trying to develop this code without using the bit masks first so I can make sure I understand everything. My first question deals with reading data from the device. In the datasheet on page 26, we see:
"The SPI read command is performed with 16 clock pulses:
Bit 0: READ bit. The value is 1.
Bit 1: MS bit. When 0, do not increment address; when 1, increment address in multiple
reading.
Bit 2-7: address AD(5:0). This is the address field of the indexed register.
Bit 8-15: data DO(7:0) (read mode). This is the data that is read from the device (MSb first).
The multiple read command is also available in 3-wire mode."
Why is there a bit 8-15? Isn't that the data we are trying to get? I thought it would be something like this (taken from my code above):
shift_out(DATA, CLK, MSBFIRST, 8, 10100111); // Send read register address, x-axis
statusReg = shift_in(DATA, CLK, MSBPOST, 16); // Get value from register
Where we are sending a read bit (1) followed by 7 bits containing the address of STATUS_REG. Then the shift_in function would display the values in that register. However, the datasheet seems to say that there should be 16 bits. Can someone please explain what I'm missing and if I am generally heading in the right direction?
Thank you,
David