2014-02-11 01:11 PM
Hi all,I'm trying to read data from a L3G4200D gyro through an SPI module of aPIC32MX460F.The following is the code I'm using:
void
Spi1STL3G4200DConfig( void ) {unsigned
int configA, configB;int
rData;configA = SPI_MODE8_ON |
SPI_CKE_ON | CLK_POL_ACTIVE_HIGH | MASTER_ENABLE_ON | SEC_PRESCAL_3_1 | PRI_PRESCAL_4_1; // 80/12 = 6.66Mhz configB = SPI_ENABLE; rData = SPI1BUF; IFS0CLR = 0x03800000; SPI1STATCLR = 0x40; OpenSPI1(configA, configB); //configuro i pin SPI1 mPORTDSetPinsDigitalOut(BIT_0 | BIT_9 | BIT_10); mPORTCSetPinsDigitalIn(BIT_4); //C4 input digitaleif
(STL3G4200D_SpiReadRegister(WHO_AM_I) != 0xD3) // ALWAYS OK !return
-1; DelayMs(200);STL3G4200D_SpiWriteRegister(CTRL_REG1, 0b00001111);
STL3G4200D_SpiWriteRegister(CTRL_REG5, 0b00000010); //low pass filtering DelayMs(200); STL3G4200D_SpiReadRegister(0x29); // ALWAYS FAILS WITH CODE HANGING AT SECOND ''while (!SPI1STATbits.SPIRBF)'' } int STL3G4200D_SpiReadRegister( char address){ int toRead; address |= 0x80; //stiamo leggendo....0xA9 dopo l'OR CS1GYRO = 0; // _RD9 SPI1BUF = address; while (!SPI1STATbits.SPIRBF); toRead = SPI1BUF; // ALWAYS 0xFF AFTER GYRO INIT SPI1BUF = 0x00; while (!SPI1STATbits.SPIRBF); // CODE HANGS HERE AFTER GYRO INIT toRead = SPI1BUF; CS1GYRO = 1; return toRead; } void STL3G4200D_SpiWriteRegister(unsigned int address, unsigned int data) { address &= 0x7F; //vogliamo scrivere CS1GYRO = 0; SPI1BUF = address; //SPI.transfer(address); while (!SPI1STATbits.SPIRBF); SPI1BUF = data; //SPI.transfer(data); while (!SPI1STATbits.SPIRBF); CS1GYRO = 1; }The code always hangs after gyro initialization and after the successful reading ofdevice ID.I use the same code
(with different prescaler values according to therequested SPI clock rates) to read XYZ channels from accelerometerswithout any problem. Lastly I'm able to read data from the gyro using I2C communicationprotocol but I needperformance so I'd like to use SPI module.Thank's for your help.Raffaele