problem in I2C output with sensor

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
Visit site
Activity points
1,769
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
 

Hi,

I am not an expert on PIC24FXX, but when i see your code i have several remarks to do:
1) after the end of write i do not see the stop condition?
2) Before reading, do you need to make a restart or a simply start? (also testing the idle condition)
3) At the end of the read do you need to make a stop condion ?
Hope it helps you
regards
JoseMiguel
 
Furthermore, what will happen if for example an acknowledge won't be received? You need to take fault situations under consideration. A simple method is to send a stop condition and then send a start condition again. But you need to verify this from the I2C data register status.
When you send data to the sensor is an ACK received? If yes then you are lucky and something is wrong with the logic part of your code. If no, then you should check again all connections, maybe see the sent data on osciloscope and search all your hardware again, as well as the commands of your sensor (do you drive the correct commands on the bus?).
Something important is that you need pull up resistors on both SDA and SCL, because they are both open collector pins. Are you using those resistors?
About the code I wouldn't know about I2C registers drive on PIC, maybe someone who uses PIC could tell.

Regards,
Alexis
 
Hi,
Do you have a small and low cost logic analyser for seeing the datas, acknowledges flow and so on?
It helps you a lot and you will earn lot of time.
(You need to have these pull-ups and you need to respect the I2C protocol)
regards
JoseMiguel
 

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…