Hello,
I am using your gyro accelerometer package to build a Ball balancing bot for my Final project at School, at the moment however i cannot get the MinIMU-9 v2 board to work properly with I2C. I am using a dsPIC33FJ64GP802 as a master and when I initiate communication with the gyro or Accelerometer it acknowledges however when i request to read it does not send me data. i do not know what the issue is. i have attached my source code
int Write(int,int,int); //Write function for I2C
int Read(int,int); //Read Function for I2C
void I2C_start();
void I2C_stop();
void I2C_restart();
void I2C_receive();
void I2C_wait();
void I2C_transmit(int);
void I2C_ack(int);
int Write(int add, int reg, int data)
{
I2C_start();//Start sequence on Bus
I2C_transmit(add); //Loading the transmit register with slave address
if(I2C1STATbits.ACKSTAT==0x0) //if acknowledge from slave
{
I2C_transmit(reg);//loading Transmit reg with register to be written too
if (I2C1STATbits.ACKSTAT==0x0)//if acknowledge from slave
{
I2C_transmit(data); //loading the trasmit register with data
if (I2C1STATbits.ACKSTAT==0x0)//if acknowledge from slave
{
I2C_stop();//stop sequence
return 0; //return to call function and continue
}
else
{
I2C_stop();//stop sequence
return 1; //return to call function and restart communication
}
}
else
{
I2C_stop(); //stop sequence
return 1; //return to call function and restart communication
}
}
else
{
I2C_stop(); //stop sequence
return 1; //return to call function and restart communication
}
}
int Read(int add, int reg)
{
I2C_start(); //Start sequence on Bus
I2C_transmit(add); //Loading the transmit register with slave address
if (I2C1STATbits.ACKSTAT==0x0) //if acknowledge from slave
{
I2C_transmit(reg); //loading Trasmit register with register to be read from
if (I2C1STATbits.ACKSTAT==0x0) //if acknowledge from slave
{
I2C_restart();
I2C_transmit(add|0x01);//Loading the tran reg with slave add & read
if (I2C1STATbits.ACKSTAT==0x0)//if acknowledge from slave
{
Nop(); Nop();
I2C_receive(); //Receive enable sequence
if(add==GADD)
GR[0]=I2C1RCV;//Storing information that was received
else
AR[0]=I2C1RCV;
I2C_ack(0);
Nop(); Nop();
I2C_receive();
if(add==GADD)
GR[1]=I2C1RCV;//Storing information that was received
else
AR[1]=I2C1RCV;
I2C_ack(0);
Nop(); Nop();
I2C_receive();
if(add==GADD)
GR[2]=I2C1RCV;//Storing information that was received
else
AR[2]=I2C1RCV;
I2C_ack(0);
Nop(); Nop();
I2C_receive();
if(add==GADD)
GR[3]=I2C1RCV; //Storing information that was received
else
AR[3]=I2C1RCV;
I2C_ack(1);
I2C_stop(); //stop sequence
return 0; //return to function and continue
}
else
{
I2C_stop(); //stop sequence
return 1; //return to call function and restart communication
}
}
else
{
I2C_stop(); //stop sequence
return 1; //return to call function and restart communication
}
}
else
{
I2C_stop(); //stop sequence
return 1; //return to call function and restart communication
}
}
void I2C_start()
{
I2C1CONbits.SEN=0x1;
while(I2C1CONbits.SEN==0x1);
}
void I2C_stop()
{
I2C1CONbits.PEN=0x1;
while(I2C1CONbits.PEN==0x1);
}
void I2C_restart()
{
I2C1CONbits.RSEN=0X1;
while(I2C1CONbits.RSEN==0x1);
}
void I2C_ack(int ack)
{
I2C1CONbits.ACKDT=ack;
I2C1CONbits.ACKEN=0x1;
while(I2C1CONbits.ACKEN==0x1);
}
void I2C_transmit(int y)
{
I2C1TRN=y;
while(I2C1STATbits.TRSTAT==0x1);
}
void I2C_receive()
{
I2C1CONbits.RCEN=0x1;
while(I2C1STATbits.RBF==0x0);
I2C_wait();
}
void I2C_wait()
{
while((I2C1CON&0x001F)||(I2C1STAT&0x4003));
}
Thank you