cannot read values from sensor with I2C

Status
Not open for further replies.

vinay shabad

Junior Member level 3
Joined
Dec 13, 2010
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,769
hiii i am working on PIC24F ,i am trying to read a temperature value fom sensor with I2C with the following program can any one please suggest me where i am wrong and how to do



#include <P24F16KA102.h>
#include "I2CM1.h"
void initI2CportSHT21(){

TRISBbits.TRISB5 = 1; // Digital Output (make it input only when reading data. see spi)
// PORTBbits.RB5 = 1;
TRISBbits.TRISB6 = 1; // Digital Output
// PORTBbits.RB6 = 1;

// I2C1CON = 0x28; // enable I2C Master mode
//I2C1CON2 = 0x00; // clear control bits
I2C1STAT = 0x80; // disable slew rate control; disable SMBus

//I2C1ADD = 19; // set baud rate to 100 kHz (Fosc = 48 MHz)

IFS1bits.MI2C1IF = 0;
IEC1bits.MI2C1IE = 0;

}

void config_i2c1_write(){
unsigned char value;
I2C1CON=0b1000001000110000;
// I2C1BRG=39;
IdleI2C1();

StartI2C1();
/* Wait till Start sequence is completed */
while(I2C1CONbits.SEN);
/* Clear interrupt flag */
IFS1bits.MI2C1IF = 0;


/*send address for write*/
MasterWriteI2C1(0x80);//
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle

//IFS1bits.MI2C1IF = 0; // Clear interrupt flag
while(I2C1STATbits.ACKSTAT);


/*send pointer*/
MasterWriteI2C1(0x00);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
//IFS1bits.MI2C1IF = 0; // Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

/*send address for write humidity*/

MasterWriteI2C1(0xE3);//
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
//IFS1bits.MI2C1IF = 0; // Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

MasterWriteI2C1(0x00);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
// IFS1bits.MI2C1IF = 0; // Clear interrupt flag
while(I2C1STATbits.ACKSTAT);


StopI2C1();



IdleI2C1();

RestartI2C1();
/*send adress for read*/
MasterWriteI2C1(0x81);//
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag


value = MasterReadI2C1();
while(I2C1STATbits.RBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag



AckI2C1();
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag



value = MasterReadI2C1();
while(I2C1STATbits.RBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag




NotAckI2C1();
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag



return;
StopI2C1();
}



thankyou in advance...have a good day..
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…