微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 微波和射频技术 > 天线设计和射频技术 > Re: HTS221 Humidity and Temperature sensing problem with MikroC and PIC

Re: HTS221 Humidity and Temperature sensing problem with MikroC and PIC

时间:04-04 整理:3721RD 点击:
@KlausST: I tried with MikroC I2C library:



Hi,

... and now you get data...

Klaus

Yes, with the library from MikroC I can do but I should configure the I2C clock on another pins. I will try again.

@KlausST: Hi Sir, I think I got data, just add delay time after writing a byte.

Which PIC are you using ? If you are not using hardware I2C module then use Soft_I2C library of mikroC.

@Okada: I finished for configuring I2C on two random pins. Cause I used pin C3 and C4 for SPI purpose so I cannot use I2C library from MikroC. Thanks

Yes, i know you can't use I2C1_() library which is hardware I2C library but you can use Soft_I2C_() library which is software I2C library.

@Okada: I will try later. Thanks for your suggestions. That sounds great.

@Okada: your suggestion is really useful for me. Just few minutes to configure and then start, you know the expected results are very good. I can read these values by UART and I will try to read by UHF RFID later.
I have a question, in my case when I try to configure I2C software even though I saw the received on the Oscilloscope but when I read by UART all of the values are 0.

Show the code.

This is my code, I did successfully with I2C library and Soft_I2C library of MikroC.

Code:
#define W_SAD 0xBE
#define R_SAD 0xBF

#define SCL     TRISB.F1 // I2C bus
#define SDA     TRISB.F0 //
#define SCL_IN  RB1    //
#define SDA_IN  RB0    //

void i2c_dly(void);
void i2c_start(void);
void i2c_stop(void);
void GPIO_Init();
void HTS221_Write(unsigned char CMD);
void HTS221_Init();
void HTS221_Init2();
unsigned char HTS221_uRead(unsigned char CMD);
unsigned char i2c_rx(char ack);
unsigned char HTS221_RD(unsigned char CMD);

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();
    //GPIO_Init2();
    //HTS221_Init2();

    while(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_uRead(0x36);  //Lower Byte
           data1_humid[1] = HTS221_uRead(0x37);  //Higher Byte
           H2 = data1_humid[1]*256+data1_humid[0];
           //H1_T0_OUT 0x3A 0x3B - s16
           data1_humid[2] = HTS221_uRead(0x3A);  //Lower Byte
           data1_humid[3] = HTS221_uRead(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_uRead(0x3C);   //Lower Byte
           data1_temp[1] = HTS221_uRead(0x3D);   //Higher Byte
           T2 = data1_temp[1]*256 + data1_temp[0];
           //T1_OUT 0x3E 0x3F - s16
           data1_temp[2] = HTS221_uRead(0x3E);   //Lower Byte
           data1_temp[3] = HTS221_uRead(0x3F);   //Higher Byte
           T3 = data1_temp[3]*256 + data1_temp[2];

           //Reading Humidity and Temperature output
           //H_OUT 0x28 0x29 - s16
           Humidity[0] = HTS221_uRead(0x28);
           Humidity[1] = HTS221_uRead(0x29);
           //T_OUT 0x2A 0x2B - s16
           Temperature[0] = HTS221_uRead(0x2A);
           Temperature[1] = HTS221_uRead(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(0xFF);
           UART1_Write(0xFF);
           UART1_Write(data0_humid[0]);
           UART1_Write(data0_humid[1]);
           UART1_Write(data1_humid[0]);
           UART1_Write(data1_humid[1]);
           UART1_Write(Humidity[0]);
           UART1_Write(Humidity[1]);
           UART1_Write(0xBB);
           UART1_Write(0xBB);
    }
}
void i2c_dly(void)
{
}
void i2c_start(void)
{
  SDA = 1;             // i2c start bit sequence
  i2c_dly();
  SCL = 1;
  i2c_dly();
  delay_us(80);
  SDA = 0;
  i2c_dly();
  SCL = 0;
}

void i2c_stop(void)
{
  SDA = 0;             // i2c stop bit sequence
  i2c_dly();
  SCL = 1;
  delay_us(80);
  i2c_dly();
  SDA = 1;
  i2c_dly();
}

void GPIO_Init()
{
  ANSELC = 0;
  ANSELB = 0;
  TRISB = 0;
  PORTB = 0;
  TRISC = 0;

  OSCCON = 0b01011011;        //Internal Oscillator 1Mhz

  UART1_Init(9600);
  delay_ms(300);
}
//Write data to HTS221 based on register address
void HTS221_Write(unsigned char CMD)
{
     unsigned char i;
     static bit b;
     for(i=0; i<8; i++)
     {
          if(CMD & 0x80)
          {
             SDA = 1;      //SDA = 1 value
          }
          else
          {
             SDA = 0;      //SDA = 0 value
          }
          SCL = 1;
          i2c_dly();
          SCL = 0;
          CMD <<= 1;
     }

     SDA = 1;
     SCL = 1;
     if(SDA_IN) SDA = 0;

     SCL = 0;
     while(SDA_IN);
     delay_us(50);
}


//I2C read function
unsigned char i2c_rx(char ack)
{
  char x;
  unsigned char d=0;
  
  SDA = 1;
  while(SDA_IN);
  for(x=0; x<8; x++) {
    d <<= 1;
    do {
      SCL = 1;
    }
    while(SCL_IN==0);    // wait for any SCL clock stretching
    i2c_dly();
    if(SDA_IN) d |= 0x01;
    SCL = 0;
  }
  
  if(ack) SDA = 0;
  else SDA = 1;

  SCL = 1;
  i2c_dly();             // send (N)ACK bit
  SCL = 0;
  SDA = 1;
  
  delay_us(50);
  return d;
}

//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;
     i2c_start();
     HTS221_Write(W_SAD);
     HTS221_Write(CMD);

     i2c_start();       //Repeat Start
     HTS221_Write(R_SAD);
     ucData = i2c_rx(0u);
     i2c_stop();
     return ucData;
}

//Initialize HTS211 Humidity and Temperature sensor
void HTS221_Init()
{
     i2c_start();
     HTS221_Write(W_SAD);
     HTS221_Write(0x10);     //AV_CONF
     HTS221_Write(0x1B);     //Temperature average samples = 16; Humidity average samples = 32
     i2c_stop();

     //Configure Active Mode, 1Hz @ODR, not continuous update 0x20
     i2c_start();
     HTS221_Write(W_SAD);
     HTS221_Write(0x20);
     HTS221_Write(0x85);
     i2c_stop();
}

void HTS221_Init2()
{
     I2C1_Start();
     I2C1_Wr(W_SAD);
     I2C1_Wr(0x10);
     I2C1_Wr(0x1B);
     I2C1_Stop();
     
     I2C1_Start();
     I2C1_Wr(W_SAD);
     I2C1_Wr(0x20);
     I2C1_Wr(0x85);
     I2C1_Stop();
}

//Read value from HTS221
unsigned char HTS221_RD(unsigned char CMD)
{
    unsigned char ucData=0;
    //For 1st byte
    I2C1_Start();
    I2C1_Wr(W_SAD);
    I2C1_Wr(CMD);
    
    I2C1_Repeated_Start();
    I2C1_Wr(R_SAD);
    ucData = I2C1_Rd(0);
    I2C1_Stop();
}

Were you successful in communicating with the HTS221 ? Did you got the correct temperature and humidity readings ? Did you use Sofy_I2C or Soft_Spi ?

yes, I got the correct temperature and humidity reading using I2C1_() library and Soft_I2C software. They are work well but I mean when I try to configure by myself, it cannot work. I think I'm going to close that topic here.

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top