vinay shabad
Junior Member level 3
hii every one,i am working on sht21 sensor with I2C communication i wrote a program for i2c but i am not getting exact out put can any one suggest me where i did wrong and correct me with your suggestions
#include <P24F16KA102.h>
#include "i2c.h"
#include "displayhum.h"
void initI2CportSHT2X() {
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
/*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
MasterWriteI2C1(0xe5);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
/*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;
}
i am working on pIC24F with this sensor
#include <P24F16KA102.h>
#include "i2c.h"
#include "displayhum.h"
void initI2CportSHT2X() {
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
/*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
MasterWriteI2C1(0xe5);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
/*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;
}
i am working on pIC24F with this sensor