CT1711数字传感器 例程

最近在做一款测温手环使用的测温芯片是CT1711,这是一款低功耗的测温芯片,实测整机功耗在休眠状态下10ua,在20ma的电池下可以工作20天左右,性价比比较高。不废话直接上程序。

注:每款单片机的延时可能不一样,自行调整

void CT1711_init(void)
{
  GPIO_ResetBits(GPIOB, GPIO_Pin_1);
  delay_us(165); // 500 us
  GPIO_SetBits(GPIOB, GPIO_Pin_1);
  
}

BitStatus CT1711_Read_Bit(void)
{
  BitStatus bi_data;
  GPIO_ResetBits(GPIOB, GPIO_Pin_1);
  GPIO_SetBits(GPIOB, GPIO_Pin_1);
  delay_10us(); //20us
  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
  {
    bi_data = 1;
  } else {
    bi_data = 0;
  }
  
  GPIO_SetBits(GPIOB, GPIO_Pin_1);
  delay_us(2); // 30us
  
  return bi_data;
}

unsigned char CT1711_Read_Byte(void)
{
  unsigned char byte = 0;
  int i;
  for(i=8;i>0;i--)
  {
    byte <<= 1;
    byte |= CT1711_Read_Bit();
  }
  return byte;
}

float CT1711_Read_Temp_Degree(void)

{
  float temp = 0.00;
  unsigned char bit_cc0,bit_cc1,bit_sign;
  char temp_byte0,temp_byte1,temp_byte2;
  int temp_val;
  
  CT1711_init();
  delay_ms(110);
  bit_cc0 = CT1711_Read_Bit();
  delay_10us();
  bit_cc1 = CT1711_Read_Bit();
  delay_10us();
  bit_cc0 = bit_cc0&0x01;
  bit_cc1 = bit_cc1&0x01;
  if((bit_cc0 == 0x00) &&(bit_cc1 == 0x00))
  {
    bit_sign = CT1711_Read_Bit();
    delay_10us();
    temp_byte0 = CT1711_Read_Byte();
    delay_10us();
    temp_byte1 = CT1711_Read_Byte();
    delay_10us();
    temp_val = (temp_byte0 << 8) + temp_byte1;
    if(bit_sign == 0x01)
    {
      temp_val = ~temp_val;
      temp_val &= 0xffff;
      temp_val++;
      temp = (-3.90625*temp_val/1000);
    } else {
      temp = ((3.90625*(float)temp_val)/1000);
    }
    return temp;
  }
  
}

void main(void)
{
  float CT1711_temp=0.0;
  while (1)
  {
      CT1711_temp = CT1711_Read_Temp_Degree();
      delay_ms(1000);
    }

}

发布了11 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/ljl578040826/article/details/104692795