利用系统变量sys_tick
官方提供ds18b20读取文件:
s16 ds18b20_read(void)
{
u8 val[2];
u8 i = 0;
s16 x = 0;
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_CONVERT);
delay_us(750000);
ow_reset();
ow_byte_wr( OW_SKIP_ROM );
ow_byte_wr ( DS18B20_READ );
for ( i=0 ;i<2; i++) {
val[i] = ow_byte_rd();
}
x = val[1];
x <<= 8;
x |= val[0];
return x;
}
其中会有750ms的延时,这种情况下会出现堵塞现象。
改进
extern uint32_t sys_tick;
uint32_t ds18b20_tick = 0;
uint8_t conv_flag = 0;
//
s16 ds18b20_read(void)
{
u8 val[2];
u8 i = 0;
s16 x = 0;
if(conv_flag == 0)
{
ow_reset();
ow_byte_wr(OW_SKIP_ROM);
ow_byte_wr(DS18B20_CONVERT);
ds18b20_tick = sys_tick;
conv_flag = 1;
}
if(sys_tick - ds18b20_tick >= 750)
{
conv_flag = 0;
ow_reset();
ow_byte_wr( OW_SKIP_ROM );
ow_byte_wr ( DS18B20_READ );
for ( i=0 ;i<2; i++) {
val[i] = ow_byte_rd();
}
x = val[1];
x <<= 8;
x |= val[0];
return x;
}
return 0;
}
main.c
uint32_t sys_tick = 0;
int main()
{
u32 z=0;
float x=0.0;
while(1)
{
if(z!=0)
{
z=(z&0x07ff);
x=z/16.;
sprintf((char*)str,"$%.2f\r\n",x);
UartSendString(str);
}
}
}