Hi,
im working on a two wheeled balancing robot project.
Im using an LIS3LV02DL 3-Axis accelerometer to gather acceleration readings of the bot
Im using a PIC18F452 and programming in C
ive used a logic analysers protocol analyser feature and the I2C protocol sent is correct as far as i can tell but am receiving no data from the Acc.
im sending the salve address of the accelerometer followed by the who am i slave address which should return 00111010.
i assume ive made a mistake in the code, could this be double checked, the relevant segments are shown below
Any help will be appreciated, Thanks
im working on a two wheeled balancing robot project.
Im using an LIS3LV02DL 3-Axis accelerometer to gather acceleration readings of the bot
Im using a PIC18F452 and programming in C
ive used a logic analysers protocol analyser feature and the I2C protocol sent is correct as far as i can tell but am receiving no data from the Acc.
im sending the salve address of the accelerometer followed by the who am i slave address which should return 00111010.
i assume ive made a mistake in the code, could this be double checked, the relevant segments are shown below
Rich (BB code):
//Global declarations
const unsigned char slave_address_write = 0b00111010;
const unsigned char slave_address_read = 0b00111011;
const unsigned char sub_address_whoami = 0b00001111;
void I2C_START ()
{
// SCL period = 100KHZ so 100 times slower then clock
// so high time = low time = Delay10TCYx (5);
PORTCbits.RC7 = 1; //SDA
Delay100TCYx(1);
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
PORTCbits.RC7 = 0; //SDA
Delay100TCYx(1);
}
//-------------------------------------------------------------------------
void I2C_STOP ()
{
PORTCbits.RC6 = 0; //SCL
PORTCbits.RC7 = 0; //SDA
Delay100TCYx(1);
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
PORTCbits.RC7 = 1; //SDA
Delay100TCYx(1);
}
//-------------------------------------------------------------------------
void I2C_SLAVE_ADD (unsigned char Slave_Address)
{
//Slave Address = 0011101
unsigned char bit_sent;
for ( bit_select = 7; bit_select >= 0 ; bit_select--)
{
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
bit_sent = (Slave_Address >> bit_select) & 0x01;
PORTCbits.RC7 = bit_sent; //SDA
Delay100TCYx(1);
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
}
// ACK
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
PORTCbits.RC7 = 0; //SDA
Delay100TCYx(1);
TRISC= 0x80; // port C7 input
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
TRISC= 0x00; // port C output
}
//------------------------------------------------------------------------
void I2C_SUB_ADD (unsigned char Sub_Address)
{
//MSB sent first, MSB = buffer = 0
//LSB 7 bits = adderss
unsigned char bit_sent;
for ( bit_select = 7; bit_select >= 0 ; bit_select--)
{
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
bit_sent = (Sub_Address >> bit_select) & 0x01;
PORTCbits.RC7 = bit_sent; //SDA
Delay100TCYx(1);
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
}
// ACK
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
PORTCbits.RC7 = 0; //SDA
Delay100TCYx(1);
TRISC= 0x80; // port C7 input
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
TRISC= 0x00; // port C output
}
//-------------------------------------------------------------------------
void I2C_READ ()
{
// READ
TRISC= 0x80; // port C7 input
for ( bit_select = 7; bit_select >= 0 ; bit_select--)
{
Delay100TCYx(1);
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
// Read data now
ReadData = (PORTCbits.RC7 << bit_select) | ReadData;
// Latch Data rising edge clock
} // end of for loop
// ACK
Delay100TCYx(1);
PORTCbits.RC6 = 0; //SCL
Delay100TCYx(1);
PORTCbits.RC6 = 1; //SCL
Delay100TCYx(1);
PORTCbits.RC6 = 0; //SCL
TRISC= 0x00; // port C output
}
//------------------------------------------------------------------------
In main:
// call procedures
I2C_START ();
I2C_SLAVE_ADD (slave_address_write); // and Write = 0
I2C_SUB_ADD (sub_address_outxL); // buffer msb = 0
I2C_START ();
I2C_SLAVE_ADD (slave_address_read); // and Read = 1
I2C_READ (); //expected return 00111010
I2C_STOP ();
Last edited by a moderator: