CT1711数字传感器 例程
时间:2023-12-01 18:37:02
最近用来做测温手镯的测温芯片是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);
}
}