sr2w
Junior Member level 1
I am receiving same value as the output even when I change the orientation of the sensor.
What could be the reason ?
1) I send slave address (with write bit).
2) I send sub address.
3) I send slave address (with read bit).
What could be the reason ?
1) I send slave address (with write bit).
2) I send sub address.
3) I send slave address (with read bit).
Code:
void main(void)
{
TRISC = 0x18; // Set RC3(SCL) and RC4(SDA) as inputs.
ANSELC = 0; // In debug mode I can see RC3 and RC4 high after stepping into
// this line of code, so I know SDA and SCL are properly pulled
// high.
TRISB = 0; // Saving received I2C data in PORTB.
ANSELB = 0; // After this step the PORTB is filled with the last
// SSPBUF data (0X3C), I find this weird
RC3PPS = 0X14; //
RC4PPS = 0X15; // Setting RC3 and RC4 as SCL and SDA pins
SSP1CON1 = 0X28; // SSPEN - 1, SSPM - 1000
SSP1ADD = 0X03; // clock frequency 31.25 kHz
SSP1STAT = 0X80; // SMP - 1
// First write with slave address
SSP1CON2bits.SEN = 1; // Start I2C
while (SSP1CON2bits.SEN == 1); // Wait for "START" to finish
PIR3bits.SSP1IF = 0;
SSP1BUF = 0X3C; // LSM9DS0 Accelerometer address(with write bit)
// Page 33 in LSM9DS0 data sheet
while((PIR3bits.SSP1IF == 0) && (SSP1STATbits.BF == 1) && (SSP1STATbits.R_nW == 1));
PIR3bits.SSP1IF = 0; // Wait for transmission to complete
// Slave sub Address
SSP1BUF = 0X2C; // sub-address of Z-axis acceleration data
// register, first 8 bits
while((PIR3bits.SSP1IF == 0) && (SSP1STATbits.BF == 1) && (SSP1STATbits.R_nW == 1));
PIR3bits.SSP1IF = 0; // Wait for transmission to complete
// Repeated Start
SSP1CON2bits.RSEN = 1; // Repeated Start so that I can start reading the
// above Z axis data
while (SSP1CON2bits.RSEN == 1);/* Wait if Transmit in progress */
PIR3bits.SSP1IF = 0;
SSP1BUF = 0X3D; // LSM9DS0 Accelerometer address(with read bit)
// Page 33 in LSM9DS0 data sheet
while((PIR3bits.SSP1IF == 0) && (SSP1STATbits.BF == 1) && (SSP1STATbits.R_nW == 1));
PIR3bits.SSP1IF = 0; // Wait for transmission to complete
SSP1CON2bits.RCEN = 1; // Enable receive in I2C
//while ( SSP1CON2bits.RCEN == 1);
while(SSP1STATbits.BF == 0);
PIR3bits.SSP1IF = 0;
PORTB = SSP1BUF;
SSP1CON2bits.ACKDT = 1;
SSP1CON2bits.ACKEN = 1;
while( SSP1CON2bits.ACKEN == 1);
PIR3bits.SSP1IF = 0;
SSP1CON2bits.PEN = 1;
while(1);
}