jerryd
Member level 2
Micro forum,
MS Windows, MPLAB X v6.05, XC8 v2.41, PIC18f4550
I'm trying to establish I2C communications between the pic and an MPU6050 Accelerometer and Gyroscope Sensor.
I have read both data sheets and everything I could read and copy on the internet that applies to I2C and these devices.
All I'm trying to do is send the address of the PMU6050(0xD0) and see if there is an Acknowledgement(ACK).
I run the code in my main.c and display the results(ACKSTST) on my 7 segment display. The value is always 0 even if I try to make it fail by taking the PMU out of the fixture.
I have attached a photo of my test fixture and the code.
Any suggestions?
Code:
jerryd
MS Windows, MPLAB X v6.05, XC8 v2.41, PIC18f4550
I'm trying to establish I2C communications between the pic and an MPU6050 Accelerometer and Gyroscope Sensor.
I have read both data sheets and everything I could read and copy on the internet that applies to I2C and these devices.
All I'm trying to do is send the address of the PMU6050(0xD0) and see if there is an Acknowledgement(ACK).
I run the code in my main.c and display the results(ACKSTST) on my 7 segment display. The value is always 0 even if I try to make it fail by taking the PMU out of the fixture.
I have attached a photo of my test fixture and the code.
Any suggestions?
Code:
Code:
/* I2Cmain.c */
#include "config.h"
void I2C_Init()
{
TRISB0=1; // Set up I2C lines by setting as input
TRISB1=1;
SSPSTAT=0x80; // Slew rate disabled, other bits are cleared
SSPCON1=0x28; // Enable SSP port for I2C Master mode, clock = FOSC / (4 * (SSPADD+1))
SSPCON2=0;
SSPADD = 20000000 / (4 * 100000 + 1); // Clock @ 100 kHz
SSPIE=1; // Enable SSPIF interrupt
SSPIF=0;
} // end void I2C_Init()
void I2C_Ready()
{
while(BCLIF); // Wait if bit collision interrupt flag is set
// Wait for Buffer full and read write flag
while(SSPSTATbits.BF || (SSPSTATbits.R_nW));
SSPIF=0; // Clear SSPIF interrupt flag
} // end void I2C_Ready()
char I2C_Write(unsigned char data)
{
SSPBUF=data; // Write data to SSPBUF
I2C_Ready();
if(ACKSTAT) // Check for acknowledge bit
return 1;
else
return 2;
} // end char I2C_Write(unsigned char data)
char I2C_Start(char slave_write_address)
{
SSPCON2bits.SEN=1; // Send start pulse
while(SSPCON2bits.SEN); // Wait for completion of start pulse
SSPIF=0;
if(!SSPSTATbits.S) // Check whether START detected last
{return 0;} // Return 0 to indicate start failed
return (I2C_Write(0xD0)); // Write slave device address with write
} // end char I2C_Start(char slave_write_address)
char I2C_Stop()
{
I2C_Ready();
PEN=1; // Stop communication
while(PEN){;} // Wait for end of stop pulse
SSPIF = 0;
if(!SSPSTATbits.P) // Check whether STOP is detected last
{
return 0; // If not return 0 to indicate start failed
}
return 0x01;
} // end char I2C_Stop()
// main function
void main(void)
{
int x;
HWsetup();
I2C_Init();
I2C_Start(0xD0);
if(ACKSTAT)
{ x = 0; }else{ x = 1; }
I2C_Stop();
here:
displayValue(x);
goto here;
} // end void main(void)
void HWsetup(void)
{
TRISA = 0x00;
PORTA = 0x00;
TRISB = 0b00000011;
PORTB = 0x00;
INTCON2bits.RBPU = 0;
TRISC = 0x00;
PORTC = 0x00;
TRISD = 0x00;
PORTD = 0x00;
} // end HWsetup()
jerryd
Attachments
Last edited by a moderator: