hi all
im using my pic32mx360f512l with explorer 16 to read my inertial sensor unit (ADIS16448) via SPI. im using pic32 compiler to write the code.
i wrote a code and im receiving data, but i think im reading wrong data. i'm not sure where is the problem.
if anyone can help me with thihs i'll be thankful.
below is the code
///////////////////////////////////////////////////////////////////////////////////////
#ifdef __PIC32MX__
#include <plib.h>
#include <p32xxxx.h>
#include <stdlib.h>
#endif
//********* Configuration Bit settings ************************
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
#ifdef __PIC32MX__
#pragma config POSCMOD=XT, FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_20, FPLLODIV=DIV_1 // to have a system clock 80 MHz
#pragma config FPBDIV=DIV_1, FWDTEN=OFF, CP=OFF, BWP=OFF
#endif
// RG9 as SS for SPI slave MCU
#define SPI_SS_TRIS TRISGbits.TRISG9
#define SPI_SS_PORT PORTGbits.RG9
void SPI2Init(void)
{
//config SPI1
SPI2CONbits.ON = 0; // disable SPI port
SPI2CONbits.SIDL = 0; // Continue module operation in Idle mode
SPI2BUF = 0; // clear SPI buffer
SPI2CONbits.DISSDO = 0; // SDOx pin is controlled by the module
SPI2CONbits.MODE16 = 1; // set in 16-bit mode, clear in 8-bit mode
SPI2CONbits.SMP = 0; // Input data sampled at middle of data output time
SPI2CONbits.CKP = 1; // spi mode 3 ( based on the IMU )
SPI2CONbits.CKE = 1; // spi mode 3 ( based on the IMU )
SPI2CONbits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode
#define baud_rat 0x031; // fpb/(2*(49+1))
SPI2BRG = baud_rat; // select clock speed so SCK is 800KHz
SPI_SS_PORT = 1; //
SPI_SS_TRIS = 0; // set SS as output
SPI2CONbits.ON = 1; // enable SPI port, clear status
}
unsigned short writeSPI2( unsigned short data )
{
SPI2BUF = data; // write to buffer for TX
while(!SPI2STATbits.SPIRBF); // wait for transfer to complete
return SPI2BUF; // read the received value
}//writeSPI2
int main(void) {
int j,k,i;
SPI2Init();
for (i=0; i<0xffff; i++); // a simple delay
static short registers_data[12]; // to store sensors data
//NOTE: the data should be stored in registers_data in the same order
// as in pTxBuff, that is registers_data[0]holds pTxBuff[0] corresponding register data
for(j = 0; j<12; j++);
{
registers_data[j] = 0x0000;
}
static short pTxBuff[12]; // register address
//NOTE: below i wrote registers in that way so the MSB is the address and LSB is don't care
pTxBuff[0] = 0x3C00; //status register
pTxBuff[1] = 0x0A00; // X acc
pTxBuff[2] = 0x0C00; // Y acc
pTxBuff[3] = 0x0E00; // Z acc
pTxBuff[4] = 0x0400; // X gyro,
pTxBuff[5] = 0x0600; // Y gyro
pTxBuff[6] = 0x0800; // Z gyro
pTxBuff[7] = 0x1000; // X mag
pTxBuff[8] = 0x1200; // Y mag
pTxBuff[9] = 0x1400; // Z mag
pTxBuff[10] = 0x1600; //baro
pTxBuff[11] = 0x1800; //temp
// main loop
for (k=0; k<100; k++)
{
SPI_SS_PORT = 0;
for (i=0; i<0xffff; i++); // a simple delay
registers_data[0] = writeSPI2(pTxBuff[0]); //1st byte to slave//dummy byte in buffer
for(j=1; j<12; j++)
{
registers_data[j-1] = writeSPI2(pTxBuff[j]); //j byte to slave//j byte in buffer
}
registers_data[11] = writeSPI2(pTxBuff[0]);
SPI_SS_PORT = 1;
} // main loop
while(1);
}
////////////////////////////////////////////////////////////////////////////////////////
im using my pic32mx360f512l with explorer 16 to read my inertial sensor unit (ADIS16448) via SPI. im using pic32 compiler to write the code.
i wrote a code and im receiving data, but i think im reading wrong data. i'm not sure where is the problem.
if anyone can help me with thihs i'll be thankful.
below is the code
///////////////////////////////////////////////////////////////////////////////////////
#ifdef __PIC32MX__
#include <plib.h>
#include <p32xxxx.h>
#include <stdlib.h>
#endif
//********* Configuration Bit settings ************************
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
#ifdef __PIC32MX__
#pragma config POSCMOD=XT, FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_20, FPLLODIV=DIV_1 // to have a system clock 80 MHz
#pragma config FPBDIV=DIV_1, FWDTEN=OFF, CP=OFF, BWP=OFF
#endif
// RG9 as SS for SPI slave MCU
#define SPI_SS_TRIS TRISGbits.TRISG9
#define SPI_SS_PORT PORTGbits.RG9
void SPI2Init(void)
{
//config SPI1
SPI2CONbits.ON = 0; // disable SPI port
SPI2CONbits.SIDL = 0; // Continue module operation in Idle mode
SPI2BUF = 0; // clear SPI buffer
SPI2CONbits.DISSDO = 0; // SDOx pin is controlled by the module
SPI2CONbits.MODE16 = 1; // set in 16-bit mode, clear in 8-bit mode
SPI2CONbits.SMP = 0; // Input data sampled at middle of data output time
SPI2CONbits.CKP = 1; // spi mode 3 ( based on the IMU )
SPI2CONbits.CKE = 1; // spi mode 3 ( based on the IMU )
SPI2CONbits.MSTEN = 1; // 1 = Master mode; 0 = Slave mode
#define baud_rat 0x031; // fpb/(2*(49+1))
SPI2BRG = baud_rat; // select clock speed so SCK is 800KHz
SPI_SS_PORT = 1; //
SPI_SS_TRIS = 0; // set SS as output
SPI2CONbits.ON = 1; // enable SPI port, clear status
}
unsigned short writeSPI2( unsigned short data )
{
SPI2BUF = data; // write to buffer for TX
while(!SPI2STATbits.SPIRBF); // wait for transfer to complete
return SPI2BUF; // read the received value
}//writeSPI2
int main(void) {
int j,k,i;
SPI2Init();
for (i=0; i<0xffff; i++); // a simple delay
static short registers_data[12]; // to store sensors data
//NOTE: the data should be stored in registers_data in the same order
// as in pTxBuff, that is registers_data[0]holds pTxBuff[0] corresponding register data
for(j = 0; j<12; j++);
{
registers_data[j] = 0x0000;
}
static short pTxBuff[12]; // register address
//NOTE: below i wrote registers in that way so the MSB is the address and LSB is don't care
pTxBuff[0] = 0x3C00; //status register
pTxBuff[1] = 0x0A00; // X acc
pTxBuff[2] = 0x0C00; // Y acc
pTxBuff[3] = 0x0E00; // Z acc
pTxBuff[4] = 0x0400; // X gyro,
pTxBuff[5] = 0x0600; // Y gyro
pTxBuff[6] = 0x0800; // Z gyro
pTxBuff[7] = 0x1000; // X mag
pTxBuff[8] = 0x1200; // Y mag
pTxBuff[9] = 0x1400; // Z mag
pTxBuff[10] = 0x1600; //baro
pTxBuff[11] = 0x1800; //temp
// main loop
for (k=0; k<100; k++)
{
SPI_SS_PORT = 0;
for (i=0; i<0xffff; i++); // a simple delay
registers_data[0] = writeSPI2(pTxBuff[0]); //1st byte to slave//dummy byte in buffer
for(j=1; j<12; j++)
{
registers_data[j-1] = writeSPI2(pTxBuff[j]); //j byte to slave//j byte in buffer
}
registers_data[11] = writeSPI2(pTxBuff[0]);
SPI_SS_PORT = 1;
} // main loop
while(1);
}
////////////////////////////////////////////////////////////////////////////////////////
Attachments
-
118.7 KB Views: 36