vinay shabad
Junior Member level 3
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
#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