From 35c9b48bf71e9307b8e61eb224d9db77b0f49a4e Mon Sep 17 00:00:00 2001 From: guoge Date: Fri, 8 Nov 2024 13:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8hx711=E7=9A=84=E8=84=89=E5=86=B2?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=8F=82=E6=95=B0=E7=9A=84=E7=99=BE=E4=BD=8D?= =?UTF-8?q?=E8=AE=BE=E5=AE=9Adelay=E7=9A=84=E8=AE=A1=E6=95=B0=E5=80=BC.?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=BA=E9=9B=B6.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hx71x.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/hx71x.c b/src/hx71x.c index 2df965218ff9..24bff8fb0bcf 100644 --- a/src/hx71x.c +++ b/src/hx71x.c @@ -20,9 +20,9 @@ //hx71x唤醒信号 static struct task_wake s_Hx71x_Wake; -static uint32_t s_delayCnt = 32; +static uint32_t s_delayCnt = 2; static uint32_t s_delayCnt2 = 0; -static uint32_t s_delayclk= 0; +static uint32_t s_delayclk= 123; struct hx71x_s { struct timer hx71x_timer; //定时器,启动 @@ -60,9 +60,9 @@ static inline void hx71x_delay_waitdata(uint32_t var) { uint32_t end = var; - for( int i=0; isample_ticks = 100000000; hx71x->sample_times = 0; hx71x->sample_cnt = 0; - - s_delayCnt = (CONFIG_CLOCK_FREQ / 1000000) * 0.06; //假设每个循环cnt要3个时钟周期. 大概延时0.2us. - - uint32_t t1 = timer_read_time(); - uint32_t t2 = timer_read_time(); - hx71x_delay_waitdata(3); - uint32_t t3 = timer_read_time(); - s_delayclk = t3 - t2 - (t2-t1); } DECL_COMMAND(command_config_hx71x, "config_hx71x oid=%c sck_pin=%u dout_pin=%u"); @@ -124,9 +116,19 @@ void command_query_hx71x(uint32_t * args) struct hx71x_s *dev = hx71x_oid_lookup(args[0]); dev->sample_ticks = args[1]; dev->sample_times = args[2]; - dev->pulse_cnt = args[3]; + long cnt = args[3]; + dev->pulse_cnt = cnt % 100; dev->sample_cnt = 0; + //s_delayCnt = (CONFIG_CLOCK_FREQ / 1000000) * 0.06 + 1; //假设每个循环cnt要3个时钟周期. 大概延时0.2us. + s_delayCnt = cnt / 100; //用第三个参数的百位为作为延时计数. + + uint32_t t1 = timer_read_time(); + uint32_t t2 = timer_read_time(); + hx71x_delay_waitdata(s_delayclk); + uint32_t t3 = timer_read_time(); + s_delayclk = t3 - t2 - (t2-t1); + sched_del_timer(&dev->hx71x_timer); //删除旧定时器 //添加新定时器.(需要先关闭中断) irq_disable(); @@ -169,13 +171,13 @@ long HX711_Read(struct hx71x_s *dev) { gpio_out_write(dev->sck_out, 1); // hx71x_udelay(1); - hx71x_delay_waitdata(0); + hx71x_delay_waitdata(s_delayclk); count = count << 1; gpio_out_write(dev->sck_out, 0); // hx71x_udelay(1); - hx71x_delay_waitdata(0); + hx71x_delay_waitdata(s_delayclk); if( gpio_in_read(dev->dt_in) ) count++; @@ -192,10 +194,10 @@ long HX711_Read(struct hx71x_s *dev) { gpio_out_write(dev->sck_out, 1); // hx71x_udelay(1); - hx71x_delay_waitdata(0); + hx71x_delay_waitdata(s_delayclk); gpio_out_write(dev->sck_out, 0); // hx71x_udelay(1); - hx71x_delay_waitdata(0); + hx71x_delay_waitdata(s_delayclk); } //完成采样,计数+1. @@ -237,8 +239,8 @@ hx71x_query_task(void) //读取并发送返回数据 long weight = HX711_Get_Weight(dev); -// sendf("hx71x_state oid=%c value=%u cnt=%u next_clock=%u", oid, weight, dev->sample_cnt, next_waketime); - sendf("hx71x_state oid=%c value=%u cnt=%u next_clock=%u", oid, weight, s_delayclk, next_waketime); + sendf("hx71x_state oid=%c value=%u cnt=%u next_clock=%u", oid, weight, dev->sample_cnt, next_waketime); +// sendf("hx71x_state oid=%c value=%u cnt=%u next_clock=%u", oid, weight, s_delayclk, next_waketime); } } DECL_TASK(hx71x_query_task);