But my code is not working - it just ouputs 4626. I am using a dsPIC30F4011 with a 20Mhz crystal.
It uses 16-bit SPI and the address that I need to send to get the rotation rate is 0x0400. It will then send me back the rotation rate in a 14-bit number. I am using code that Sobrietytest posted which I slightly modifided for my part. I know that the gyro works because I have tested it on its evaluation board.
Here is the connection diagram:
Currently my code outputs the int 4626 constantly over the serial port and the number doesnt change. The number should be changing with the rotation rate.
Here is my code:
Code: Select all
#include <Spi_Const.h>
const char CS_PIN = 0; // CS pin is RF0
int HiByte;
int LoByte;
int XGValue;
short buffer;
char txt[6];
int i = 0;
void InitMain() {
ADPCFG = 0xFFFF; // Set AN pins as digital
// Initialise SPI module
Spi_Init_Advanced(_SPI_MASTER, _SPI_16_BIT, _SPI_PRESCALE_SEC_5,
_SPI_PRESCALE_PRI_4, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_END,
_SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE);
TRISF.CS_PIN = 0; // Set CS pin as output
PORTF.CS_PIN = 1; // Set CS high
Uart2_Init(9600); // Initialise UART module
}
void readADIS() {
PORTF.CS_PIN = 0; // Select DAC module
Spi_Write(0x04); // Send data via SPI
Spi_Write(0x00); // Send data via SPI
PORTF.CS_PIN = 1; // Deselect DAC module
Delay_us(15); // Stall period between data
PORTF.CS_PIN = 0; // Select DAC module
HiByte = Spi_Read(buffer); // Read the MSByte
LoByte = Spi_Read(buffer); // Read the LSByte
PORTF.CS_PIN = 1; // Deselect DAC module
HiByte = (HiByte <<8); // Bitshift the MSByte 8 places left
XGValue = HiByte + LoByte; //then add them together
XGValue = (XGValue & 0b0011111111111111); //Strip off the ND and EA flags
}
void main() {
InitMain();
while (1) { // Main loop
readADIS();
WordToStr(XGValue, txt); // Convert XGValue to array of numbers
for (i=0; i<6; i++) // Display XGValue on serial
Uart2_Write_Char(txt[i]);
Uart2_Write_Char('\n'); // new line
Delay_ms(300); // Update rate
}
}
Thanks