From c27de326c68b32b4b57a74b3e45449737c19b6f3 Mon Sep 17 00:00:00 2001 From: jeenter Date: Fri, 26 Jan 2024 22:31:49 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8DRT-Thread=20Studio=E4=B8=AD?= =?UTF-8?q?=E5=BC=95=E7=94=A8"sensor.h"=E8=B7=AF=E5=BE=84=E4=B8=8D?= =?UTF-8?q?=E5=AE=8C=E6=95=B4=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B=202.?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=B0=E7=89=88=E6=9C=AC=E4=B8=AD"rt=5Fwea?= =?UTF-8?q?k"=E6=94=B9=E7=94=A8=E5=B0=8F=E5=86=99=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9B=203.=E8=AF=BB=E6=88=96=E5=86=99=E4=BC=A0?= =?UTF-8?q?=E6=84=9F=E5=99=A8=E6=97=B6=EF=BC=8C=E8=BF=9B=E5=85=A5=E4=B8=B4?= =?UTF-8?q?=E7=95=8C=E5=8C=BA=EF=BC=8C=E9=98=B2=E6=AD=A2=E6=97=B6=E5=BA=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SConscript | 2 +- src/sensor_dallas_ds18b20.c | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/SConscript b/SConscript index 68e7b87..82ef072 100644 --- a/SConscript +++ b/SConscript @@ -12,7 +12,7 @@ if GetDepend('PKG_USING_DS18B20_SAMPLE'): src += Glob('sample/ds18b20_sample.c') # add ds18b20 include path. -path = [cwd + '/inc'] +path = [cwd + '/inc'] + [cwd + '/../../rt-thread/components/drivers/include/drivers'] # add src and include to group. group = DefineGroup('ds18b20', src, depend = ['PKG_USING_DS18B20'], CPPPATH = path) diff --git a/src/sensor_dallas_ds18b20.c b/src/sensor_dallas_ds18b20.c index 343683d..64b4d93 100644 --- a/src/sensor_dallas_ds18b20.c +++ b/src/sensor_dallas_ds18b20.c @@ -20,7 +20,7 @@ #define SENSOR_TEMP_RANGE_MAX (125) #define SENSOR_TEMP_RANGE_MIN (-55) -RT_WEAK void rt_hw_us_delay(rt_uint32_t us) +rt_weak void rt_hw_us_delay(rt_uint32_t us) { rt_uint32_t delta; @@ -94,11 +94,13 @@ static uint8_t ds18b20_read_byte(rt_base_t pin) uint8_t i, j, dat; dat = 0; + rt_enter_critical(); for (i = 1; i <= 8; i++) { j = ds18b20_read_bit(pin); dat = (j << 7) | (dat >> 1); } + rt_exit_critical(); return dat; } @@ -109,6 +111,7 @@ static void ds18b20_write_byte(rt_base_t pin, uint8_t dat) uint8_t testb; rt_pin_mode(pin, PIN_MODE_OUTPUT); + rt_enter_critical(); for (j = 1; j <= 8; j++) { testb = dat & 0x01; @@ -129,6 +132,7 @@ static void ds18b20_write_byte(rt_base_t pin, uint8_t dat) rt_hw_us_delay(2); } } + rt_exit_critical(); } void ds18b20_start(rt_base_t pin) @@ -167,7 +171,7 @@ int32_t ds18b20_get_temperature(rt_base_t pin) tem = TH; tem <<= 8; tem += TL; - tem = (int32_t)(tem * 0.0625 * 10 + 0.5); + tem = (int32_t)(tem * 0.0625 * 10 + 0.5);//10倍温度,+0.5实现四舍五入 return -tem; } else @@ -175,7 +179,7 @@ int32_t ds18b20_get_temperature(rt_base_t pin) tem = TH; tem <<= 8; tem += TL; - tem = (int32_t)(tem * 0.0625 * 10 + 0.5); + tem = (int32_t)(tem * 0.0625 * 10 + 0.5);//10倍温度,+0.5实现四舍五入 return tem; } }