Skip to content

Commit

Permalink
在hx711的脉冲数量参数的百位设定delay的计数值.默认为零.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoGeTiertime committed Nov 8, 2024
1 parent c14bf9b commit 35c9b48
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/hx71x.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; //定时器,启动
Expand Down Expand Up @@ -60,9 +60,9 @@ static inline void
hx71x_delay_waitdata(uint32_t var)
{
uint32_t end = var;
for( int i=0; i<s_delayCnt; i++ )
for( uint32_t i=0; i<s_delayCnt; i++ )
end += i;
s_delayCnt2 = end;
s_delayCnt2 += end;
}

void command_config_hx71x(uint32_t *args)
Expand All @@ -79,14 +79,6 @@ void command_config_hx71x(uint32_t *args)
hx71x->sample_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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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++;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 35c9b48

Please sign in to comment.