Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
Resource icon

Mplabx -I2C-Temperature sensor TMP102 2015-07-09

i got ack from the slave bt its not receivng right value of temperature.its receiving continously FF
here is the code.....i need help...

int main()

{
I2C1CONbits.GCEN = 1;
TRISA=0;
// SDA=0;
// SCL=0;
char y;

// unsigned int data2,data1,data3;
unsigned char data1,data2,data3;
int data4,data5,data6;
//while(1){
InitI2C();
//while(1){
I2C_Start();
// AckI2C1();// send start sequence
y = I2C_Write_Byte(0x48);
y = I2C_Write_Byte(0x00);
//y = I2C_Write_Byte(0x06);

// I2C1CONbits.RCEN =1;
// while(I2C1CONbits.RCEN==0);
I2C_ReStart();

y = I2C_Write_Byte(0x49);
//astergetsI2C1(0x04, 0x10, 0x03);
//data1= MastergetsI2C1(0x02,0x10,0x02);
while(1){
data1 = I2C_Read_Byte(0);
data2 = I2C_Read_Byte(1);

data3=data2>>4;
data4=(data1<<4)&0xff0;
data5=data3|data4;
data6=data5*0.0625;
PORTA=data6;
LATA=data6;
__delay_ms(7000);
}
I2C_Stop();

return(EXIT_SUCCESS);

}

void InitI2C(void)
{
SDA_dir = 0; // Data pin direction
SCL_dir = 0; // clock pin direction
SCL= 1;
SDA=1;
int temp;
TRISA = 0x00;
OSCCON = 0x7701;
CLKDIV = 0x0000;
OSCTUN = 0x0000;
I2C1ADD = 0x0027;
// slave device address
I2C1BRG = 0x0004;

I2C1CONbits.I2CEN = 0; // Disable I2C Mode
I2C1CONbits.DISSLW =0; // Disable slew rate control
IFS1bits.MI2C1IF = 0; // Clear Interrupt
I2C1CONbits.I2CEN = 1; // Enable I2C Mode

temp = I2C1RCV; // read buffer to clear buffer full
I2C_Stop(); // set bus to idle

}

void I2C_Start(void)
{

int x = 0;
I2C1CONbits.ACKDT = 0; //Reset any previous Ack
__delay_us(10);
I2C1CONbits.SEN = 1; //Initiate Start condition
Nop();

//the hardware will automatically clear Start Bit
//wait for automatic clear before proceding
while (I2C1CONbits.SEN)
{
__delay_us(1);
x++;
if (x > 20)
break;
}
__delay_us(2);

}

void I2C_Stop(void)
{

int x = 0;

//initiate stop bit
I2C1CONbits.PEN = 1;

//wait for hardware clear of stop bit
while (I2C1CONbits.PEN)
{
__delay_us(1);
x ++;
if (x > 20)
break;
}
//I2C1CONbits.RCEN = 0;
IFS1bits.MI2C1IF = 0; // Clear Interrupt
I2C1STATbits.IWCOL = 0;
I2C1STATbits.BCL = 0;
__delay_us(10);

}

void I2C_ReStart(void)
{

int x = 0;

I2C1CONbits.RSEN = 1; //Initiate restart condition
Nop();

//the hardware will automatically clear restart bit
//wait for automatic clear before proceding
while (I2C1CONbits.RSEN)
{
__delay_us(1);
x++;
if (x > 20)
break;
}

__delay_us(2);

}

//void I2C_Send_ACK(void)
//{
// I2C1CONbits.ACKDT = 0;
// I2C1CONbits.ACKEN = 1;
// while(I2C1CONbits.ACKEN);
//}
//
//void I2C_Send_NACK(void)
//{
// I2C1CONbits.ACKDT = 1;
// I2C1CONbits.ACKEN = 1;
// while(I2C1CONbits.ACKEN);
//}

unsigned char I2C_Write_Byte(unsigned char data)
{

int i;
while(I2C1STATbits.BCL ==1);
while (I2C1STATbits.TBF==1){}
// Clear Interrupt

I2C1TRN = data; // load the outgoing data byte
while(I2C1STATbits.IWCOL==1);
while( IFS1bits.MI2C1IF == 0)
IFS1bits.MI2C1IF = 0;
// wait for transmission
for (i=0; i<500; i++)
{
if (!I2C1STATbits.TRSTAT) break;
__delay_us(1);
}
if (i == 500)
{
return(1);
}

// Check for NO_ACK from slave, abort if not found
if (I2C1STATbits.ACKSTAT == 1)
{
//I2C_Stop();
return(1);
}
__delay_us(2);

return(0);

}

//function reads data, returns the read data, with ack

unsigned char I2C_Read_Byte( char ack)
{
//int j;
I2C1CONbits.RCEN = 1;
while(I2C1CONbits.RCEN==0);
// I2C1STATbits.I2COV = 0;
// j=I2C1RCV<<8;
// AckI2C1();
int i,data = 0;
// I2C1CONbits.RCEN = 1;
data= I2C1RCV;


while (I2C1STATbits.RBF != 1);

data= I2C1RCV;
__delay_us(300);
data = I2C1RCV ;


I2C1CONbits.ACKDT = 1;
I2C1CONbits.ACKEN = 1;




// set I2C module to receive
//I2C1CONbits.RCEN = 1;
data = I2C1RCV ;
//if no response, break

__delay_us(2);
//get data from I2CRCV register
data = I2C1RCV;
if(ack==0){
I2C1CONbits.ACKDT = 0;
}
else{
I2C1CONbits.ACKDT = 1;
}
// set ACK to high
I2C1CONbits.ACKEN = 1;

//wait before exiting
__delay_us(10);

return data;
}
  • Like
Reactions: kash
Author
NIKITA PATEL
Views
7,450
First release
Last update
Rating
0.00 star(s) 0 ratings

Latest threads

Back
Top