From e1868033d66f127dda2db4c9d5f636e404b05b00 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Thu, 10 May 2018 08:03:19 +0100 Subject: [PATCH] Add w1_read_bit() to improve waiting for result Rather than using a full w1_read_byte() to wait for the temperature sensor to indicate a result is ready introduce a w1_read_bit() and use that instead. This lets us service USB requests more often while waiting for the result. --- main.c | 2 +- w1.c | 37 +++++++++++++++++++++++-------------- w1.h | 1 + 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/main.c b/main.c index 61064a0..a75a4fc 100644 --- a/main.c +++ b/main.c @@ -426,7 +426,7 @@ int main(void) w1_write(0x44); /* Convert T */ temp_state = 4; } else if (temp_state == 4) { - if (w1_read_byte() == 0xFF) + if (w1_read_bit()) temp_state = 5; } else if (temp_state == 5) { if (w1_reset()) { diff --git a/w1.c b/w1.c index 8217602..add1b48 100644 --- a/w1.c +++ b/w1.c @@ -65,26 +65,35 @@ void w1_write(uint8_t val) } } +bool w1_read_bit() +{ + bool val; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + /* Pull low for 6µs */ + DDRB |= 1 << W1_PIN; + _delay_us(6); + /* Release for 9µs */ + DDRB &= ~(1 << W1_PIN); + _delay_us(9); + + /* Read the line state */ + val = ((PINB >> W1_PIN) & 1); + } + _delay_us(55); + + return val; +} + uint8_t w1_read_byte() { uint8_t i, val; val = 0; for (i = 0; i < 8; i++) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) - { - /* Pull low for 6µs */ - DDRB |= 1 << W1_PIN; - _delay_us(6); - /* Release for 9µs */ - DDRB &= ~(1 << W1_PIN); - _delay_us(9); - - /* Read the line state */ - val |= ((PINB >> W1_PIN) & 1) << i; - } - - _delay_us(55); + if (w1_read_bit()) + val |= (1 << i); } return val; diff --git a/w1.h b/w1.h index 54c9f5b..bef02a9 100644 --- a/w1.h +++ b/w1.h @@ -5,6 +5,7 @@ uint8_t w1_crc(uint8_t *buf, uint8_t len); void w1_write(uint8_t val); +bool w1_read_bit(); uint8_t w1_read_byte(); void w1_read(uint8_t *buf, uint8_t len); bool w1_reset(void); -- 2.39.2