Hello everyone,
I have a problem with I2c communication in DSPIC33F. I am using DSPIC 33F as my master and pololu MinIMU 9 v2 as my slave.
pololu.com/catalog/product/1268 this is the information regarding my slave. It consists of three devices and my slave exactly is L3GD20 3-axis gyroscope
The problem is when I try to observe the readings of slave, I could see only the slave address repeatedly( I m viewing it on CRO).Except that I could not see anything else on CRO.
I m posting my program here.
#include "p33Fxxxx.h"
#include <p33FJ64MC802.h>
#include <i2c.h>
#include <String.h>
// calculate baud rate of I2C
#define Fosc (8000000)
#define Fcy (Fosc/2) // no PLL
#define Fsck 100000
#define I2C_BRG 0x188 //((Fcy/2/Fsck)-1)
#define I2C_ON 0x8000
void readFloat(float *ptr);
unsigned char SlaveAddress_read;
unsigned char SlaveAddress_write;
int main (void)
{
float value = 0;
float temp;
I2C1CONbits.A10M = 0;
char status;
unsigned char i2cbyte;
//Enable channel
OpenI2C1( I2C_ON, I2C_BRG );
TRSTAT=1;
SlaveAddress_read = 0b11010111;
SlaveAddress_write = 0b11010110;
while (1)
{
readFloat(&value);
}
return 0;
}
void readFloat(float *ptr)
{
unsigned char rx_data[6];
I2C1CONbits.SEN = 1;
while(I2C1CONbits.SEN);
MasterWriteI2C1(SlaveAddress_write);
while (I2C1STATbits.ACKSTAT);//Return Ack Status
MasterWriteI2C1(0b10101000);
while (I2C1STATbits.ACKSTAT);//Return Ack Status
I2C1CONbits.RSEN = 1;
MasterWriteI2C1(SlaveAddress_read);
while (I2C1STATbits.ACKSTAT);//Return Ack Status
rx_data[0] = MasterReadI2C1();
I2C1CONbits.ACKDT=0; // Set for ACk
I2C1CONbits.ACKEN=1; // Send Ack
while(I2C1CONbits.ACKEN); // wait for ACK to complete
rx_data[1] = MasterReadI2C1();
I2C1CONbits.ACKDT=0; // Set for ACk
I2C1CONbits.ACKEN=1; // Send Ack
while(I2C1CONbits.ACKEN); // wait for ACK to complete
rx_data[2] = MasterReadI2C1();
I2C1CONbits.ACKDT=0; // Set for ACk
I2C1CONbits.ACKEN=1; // Send Ack
while(I2C1CONbits.ACKEN); // wait for ACK to complete
rx_data[3] = MasterReadI2C1();
I2C1CONbits.ACKDT=0; // Set for ACk
I2C1CONbits.ACKEN=1; // Send Ack
while(I2C1CONbits.ACKEN); // wait for ACK to complete
rx_data[4] = MasterReadI2C1();
I2C1CONbits.ACKDT=0; // Set for ACk
I2C1CONbits.ACKEN=1; // Send Ack
while(I2C1CONbits.ACKEN); // wait for ACK to complete
rx_data[5] = MasterReadI2C1();
I2C1CONbits.ACKDT=1; // Set for NotACk
I2C1CONbits.ACKEN=1; // Send Nack
while(I2C1CONbits.ACKEN); // wait for ACK to complete
I2C1CONbits.ACKDT=0;
StopI2C1(); //Send the Stop condition
IdleI2C1(); //Wait to complete
memcpy(ptr, &rx_data, 6);
}
Actually I found the problem that slave is not sending the acknowledgement to proceed on to next step. Hence the bus is getting busy only by the slave address microcontroller is sending. Is this something related to Hardware?
I connected both the devices (dspic33f and gyro) to a common power supply of 3.3v(through PC USB port). I took pull up of 4.7 k . I gave power supply to Vin of gyro rather than to Vdd. If something is wrong please let me know.
I have been working on it since a month but could not fix the problem. If there is any problem with the code or problem regarding the slave(gyro) please help me,it’s extremely urgent.
Thankyou.