Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C interfacing problem reading data from 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
Activity points
1,769
hii guys, i am working on I2C with sht sensor today with my programming i am able to display some values but i am not sure that it is from sensor and the radings are change per each cycle



#include <P24F16KA102.h>
#include "shtI2C.h"
#include "display.h"
#include "stdio.h"
#define sht21 0b10000000
#define writeuser 0b11100110
#define readuser 0b11100111
#define reset 0b11111110
#define temphold 0b11100011
#define humidhold 0b11100101
#define read 0b10000001
#define softreset 0b11111110
#define I2C_7BIT_ADD
#define I2CON
void sht21_init(void)
{

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;


unsigned int config1 = 0;
unsigned int config2 = 0;

CloseI2C1();
Delay10ms(10);
IEC1bits.MI2C1IE = 0; //Disable I2C interrupt
Delay10ms(10);

config1 = I2C_7BIT_ADD
config2 = 4; // baud rate 100KHz
OpenI2C1(config1,config2); //configure I2C1
Delay10ms(10);
}
void WriteByte(unsigned long int address)
{
// long int address;
// START
IdleI2C1();
StartI2C1();
while(I2C1CONbits.SEN ); //Wait till Start sequence is completed
IFS1bits.MI2C1IF = 0;//Clear interrupt flag

// Control Byte
MasterWriteI2C1(0x80); //Write Slave address and set master for transmission
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

/*MasterWriteI2C1(softreset); //Write Slave address and set master for transmission
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);
*/

// Address MSB
MasterWriteI2C1((address >> 8) & 0x00ff);
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

// Address LSB
MasterWriteI2C1(address & 0x00ff);
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

// data byte
MasterWriteI2C1(humidhold);
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);




// STOP
StopI2C1();

// make sure write cycle is complete by polling acknowledgement from slave
do
{
// start
IdleI2C1();
StartI2C1();
while(I2C1CONbits.SEN ); //Wait till Start sequence is completed
IFS1bits.MI2C1IF = 0; //Clear interrupt flag

// Control Byte
MasterWriteI2C1(0x80); //Write Slave address and set master for transmission
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
}while(I2C1STATbits.ACKSTAT);
}

void Read(unsigned long int address,int length,long int * pData)
{
int i;

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

// Control Byte
MasterWriteI2C1(0x80); //Write Slave address and set master for transmission
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

// Address MSB
MasterWriteI2C1((address >> 8) & 0x00ff);
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0;//Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

// Address LSB
MasterWriteI2C1(address & 0x00ff);
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0;//Clear interrupt flag
while(I2C1STATbits.ACKSTAT);







// START for reading
IdleI2C1();
RestartI2C1();
while(I2C1CONbits.SEN ); //Wait till Start sequence is completed
IFS1bits.MI2C1IF = 0; //Clear interrupt flag

// Control Byte
MasterWriteI2C1(0x81); //Write Slave address and set master for transmission
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);

// data byte
MasterWriteI2C1(humidhold);
while(I2C1STATbits.TBF); //Wait till address is transmitted
while(!IFS1bits.MI2C1IF); //Wait for ninth clock cycle
IFS1bits.MI2C1IF = 0; //Clear interrupt flag
while(I2C1STATbits.ACKSTAT);


*(pData + 0) = MasterReadI2C1(); // read first byte
// for(i = 1;i < length;i++) // read further bytes if requested
// {
AckI2C1();
IFS1bits.MI2C1IF = 0;//Clear interrupt flag
while(I2C1STATbits.ACKSTAT);
//*(pData + i) = MasterReadI2C1();
//}
NotAckI2C1();
StopI2C1();
}



i am unable to read a correct values from sensor
 

i am using SHT21 with PIC2416KA102 and using C_30 compiler

---------- Post added at 17:29 ---------- Previous post was at 17:08 ----------

i am using I2C with interrupts ,i able to display some values but its not correct values
 

have you the pulup resistors connected to SDA and SCL?
 

Had you obtained results simulated, or directly at device ?

+++
 

yes i connected pullup resistors to SDA and SCL,yes i obtain result by simulating but its not the right values
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top