BiNa2605
Full Member level 3
Hi all,
Does anyone work with HTS221 of ST using MikroC? I used I2C to communicate with that chip but when I read the sensing signals from HTS221, I think I have errors. I always read value 79 from any register :bang:.
I used RB0, RB1 and configure them as SCK and SDA in I2C. I uploaded whole of my codes and the signals that I captured by an oscilloscope. Any recommendations and suggestions will be high appreciated. Many thanks.
HTS221 datasheet:
**broken link removed**
References:
C language: https://github.com/ControlEverythingCommunity/HTS221/blob/master/C/HTS221.c
Arduino : https://github.com/ControlEverythingCommunity/HTS221/blob/master/Arduino/HTS221.ino
Does anyone work with HTS221 of ST using MikroC? I used I2C to communicate with that chip but when I read the sensing signals from HTS221, I think I have errors. I always read value 79 from any register :bang:.
I used RB0, RB1 and configure them as SCK and SDA in I2C. I uploaded whole of my codes and the signals that I captured by an oscilloscope. Any recommendations and suggestions will be high appreciated. Many thanks.
Code:
#define W_SAD 0xBE
#define R_SAD 0xBF
sbit SDA at RB0_bit;
sbit SCL at RB1_bit;
sbit SDA_D at TRISB0_bit;
void GPIO_Init();
void HTS221_Init();
void HTS221_Start();
void HTS221_Stop();
void HTS221_ACK();
void HTS221_NACK();
void HTS221_Write(unsigned char address);
unsigned char HTS221_uRead(unsigned char address);
char HTS221_cRead(unsigned char address);
void main() {
unsigned char data0_temp[8], data0_humid[8];
unsigned char ucSensor;
char data1_temp[8], data1_humid[8];
char Temperature[2], Humidity[2];
int H0=0, H1=0, H2=0, H3=0;
int T0=0, T1=0, T2=0, T3=0;
int temp=0, humi=0;
float fTemp=0, fHumidity=0;
int raw=0;
GPIO_Init();
HTS221_Init();
while(1)
{
PORTB.B5 = 1;
//Reading calibration values
//H0_rH_x2 0x30 register address - u8
data0_humid[0] = HTS221_uRead(0x30);
H0 = data0_humid[0]/2;
//H1_rH_x2 0x31 register address - u8
data0_humid[1] = HTS221_uRead(0x31);
H1 = data0_humid[1]/2;
//H0_T0_OUT 0x36 0x37 - s16
data1_humid[0] = HTS221_cRead(0x36); //Lower Byte
data1_humid[1] = HTS221_cRead(0x37); //Higher Byte
H2 = data1_humid[1]*256+data1_humid[0];
//H1_T0_OUT 0x3A 0x3B - s16
data1_humid[2] = HTS221_cRead(0x3A); //Lower Byte
data1_humid[3] = HTS221_cRead(0x3B); //Higher Byte
H3= data1_humid[3]*256+data1_humid[2];
//T0_degC_x8 0x32 register address - u8
data0_temp[0] = HTS221_uRead(0x32);
T0 = data0_temp[0];
//T1_degC_x8 0x33 register address - u8
data0_temp[1] = HTS221_uRead(0x33);
T1 = data0_temp[1];
//T1/T0 msb 0x35 - u2
data0_temp[2] = HTS221_uRead(0x35);
raw = data0_temp[2];
T0 = ((raw & 0x03)*256) + T0;
T1 = ((raw & 0x0C)*64) + T1;
//T0_OUT 0x3C 0x3D - s16
data1_temp[0] = HTS221_cRead(0x3C); //Lower Byte
data1_temp[1] = HTS221_cRead(0x3D); //Higher Byte
T2 = data1_temp[1]*256 + data1_temp[0];
//T1_OUT 0x3E 0x3F - s16
data1_temp[2] = HTS221_cRead(0x3E); //Lower Byte
data1_temp[3] = HTS221_cRead(0x3F); //Higher Byte
T3 = data1_temp[3]*256 + data1_temp[2];
//Reading Humidity and Temperature output
//H_OUT 0x28 0x29 - s16
Humidity[0] = HTS221_cRead(0x28);
Humidity[1] = HTS221_cRead(0x29);
//T_OUT 0x2A 0x2B - s16
Temperature[0] = HTS221_cRead(0x2A);
Temperature[1] = HTS221_cRead(0x2B);
humi = Humidity[1]*256+Humidity[0];
temp = Temperature[1]*256+Temperature[0];
if(temp > 32767)
{
temp-= 65536;
}
fHumidity = ((1.0 * H1) - (1.0 * H0)) * (1.0 * humi - 1.0 * H2) / (1.0 * H3 - 1.0 * H2) + (1.0 * H0);
fTemp = ((T1 - T0) / 8.0) * (temp - T2) / (T3 - T2) + (T0 / 8.0); // Temperature in Celcius
UART1_Write(Humidity[1]);
}
}
//Initialize GPIO and I2C protocol for measuring sensing signal
void GPIO_Init()
{
ANSELC = 0;
ANSELB = 0;
TRISB = 0;
//PORTB = 0;
TRISC = 0;
//PORTC = 0;
OSCCON = 0b01011011; //Internal Oscillator 1Mhz
//I2C1_Init(100000); //Initialize I2C communication
//I2C1_Start();
UART1_Init(9600);
delay_ms(300);
}
//HTS221 Start I2C communication
void HTS221_Start()
{
SDA_D = 0; // Data output
SDA = 1;
SCL = 1;
delay_us(10);
SDA = 0;
//delay_us(10);
}
//HTS221 Stop I2C communication
void HTS221_Stop()
{
SDA_D = 0;
SDA = 0;
SCL = 1;
delay_us(10);
SDA = 1;
//delay_us(100);
}
//HTS221 Ackowledgement
void HTS221_ACK()
{
SDA_D = 0;
SDA = 0;
SCL = 0;
delay_us(1);
SCL = 1;
delay_us(2);
SCL = 0;
delay_us(1);
SDA = 1;
}
//HTS221 Not Ackowledgement
void HTS221_NACK()
{
SDA_D = 0;
SDA = 1;
SCL = 0;
delay_us(1);
SCL = 1;
delay_us(2);
SCL = 0;
delay_us(1);
SDA = 0;
}
//Write data to HTS221 based on register address
void HTS221_Write(unsigned char CMD)
{
unsigned char i;
unsigned char j = CMD;
SDA_D = 0; //Write function so SDA_D is OUTPUT
for(i=0x80; i>0; i/=2)
{
SCL = 0;
//delay_us(1);
if(i & j) //AND SCL and DATA if
{
SDA = 1; //SDA = 1 value
}
else
{
SDA = 0; //SDA = 0 value
}
SCL = 1;
//delay_us(1);
}
}
//Read data from HTS221 based on register address, return unsigned char value
unsigned char HTS221_uRead(unsigned char CMD)
{
unsigned char ucData=0;
int i=0;
HTS221_Start();
HTS221_Write(W_SAD);
HTS221_Write(CMD);
HTS221_Start(); //Repeat Start
HTS221_Write(R_SAD);
SDA_D = 1;
SCL = 0;
delay_us(1);
SCL = 1;
while(SDA==1);
delay_us(200);
for(i=0; i<8; i++)
{
ucData <<= 1;
SCL = 0;
if(SDA == 1)
ucData = ucData | 0x01;
SCL = 1;
}
HTS221_Stop();
return ucData;
}
//Read data from HTS221 based on register address, return unsigned char value
char HTS221_cRead(unsigned char CMD)
{
char cData=0;
int i=0;
HTS221_Start();
HTS221_Write(W_SAD);
HTS221_Write(CMD);
HTS221_Start(); //Repeat Start
HTS221_Write(R_SAD);
SDA_D = 1;
SCL = 0;
delay_us(1);
SCL = 1;
while(SDA==1);
delay_us(200);
for(i=0; i<8; i++)
{
cData <<= 1;
SCL = 0;
if(SDA == 1)
cData = cData | 0x01;
SCL = 1;
}
HTS221_Stop();
return cData;
}
//Initialize HTS211 Humidity and Temperature sensor
void HTS221_Init()
{
HTS221_Start();
HTS221_Write(W_SAD);
HTS221_Write(0x10); //AV_CONF
HTS221_Write(0x1B); //Temperature average samples = 16; Humidity average samples = 32
HTS221_Stop();
//Configure Active Mode, 1Hz @ODR, not continuous update 0x20
HTS221_Start();
HTS221_Write(W_SAD);
HTS221_Write(0x20);
HTS221_Write(0x85);
HTS221_Stop();
}
HTS221 datasheet:
**broken link removed**
References:
C language: https://github.com/ControlEverythingCommunity/HTS221/blob/master/C/HTS221.c
Arduino : https://github.com/ControlEverythingCommunity/HTS221/blob/master/Arduino/HTS221.ino