From b74f68994dba3d0eb20f632e632d3959295d0556 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Tue, 2 Jan 2018 19:14:50 +0200 Subject: [PATCH] Improve bit manipulation macros --- include/common.h | 11 +++++++---- src/sonar.c | 6 +++--- src/temperature.c | 6 +++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/common.h b/include/common.h index e96b2d8..7f71f5c 100644 --- a/include/common.h +++ b/include/common.h @@ -1,9 +1,12 @@ #ifndef COMMON_H #define COMMON_H -#define SET(X, Y) X |= (1 << Y) -#define CLR(X, Y) X &= ~(1 << Y) -#define CHK(X, Y) X & (1 << Y) -#define TOG(X, Y) X ^= (1 << Y) +#include + +#define BIT(mask) (1 << (mask)) +#define SET(var, mask) ((var) |= (uint8_t)BIT(mask)) +#define CLR(var, mask) ((var) &= (uint8_t)~(BIT(mask))) +#define CHK(var, mask) ((var) & (uint8_t)BIT(mask)) +#define TOG(var, mask) ((var) ^= (uint8_t)BIT(mask) #endif /* COMMON_H */ diff --git a/src/sonar.c b/src/sonar.c index 8c63c4f..3156b9e 100644 --- a/src/sonar.c +++ b/src/sonar.c @@ -6,13 +6,13 @@ volatile uint32_t overflow_counter = 0; volatile uint32_t trig_counter = 0; volatile uint32_t no_of_cycles = 0; -void init_sonar() +void init_sonar(void) { SET(TRIG_DDR, TRIG_BIT); CLR(ECHO_DDR, ECHO_BIT); } -void trigger_sonar() +void trigger_sonar(void) { CLR(TRIG_PORT, TRIG_BIT); _delay_us(1); @@ -28,7 +28,7 @@ ISR(TIMER1_OVF_vect) TCNT1 = 0; } -int read_sonar() +int read_sonar(void) { int dist_in_cm = 0; init_sonar(); diff --git a/src/temperature.c b/src/temperature.c index 9fa3349..2fb8795 100644 --- a/src/temperature.c +++ b/src/temperature.c @@ -16,7 +16,7 @@ Please refer to LICENSE file for licensing information. /* * ds18b20 init */ -uint8_t ds18b20_reset() +uint8_t ds18b20_reset(void) { uint8_t i; @@ -53,7 +53,7 @@ void ds18b20_writebit(uint8_t bit) // wait 60uS and release the line _delay_us(60); - DS18B20_DDR &= ~(1 << DS18B20_BIT); + CLR(DS18B20_DDR, DS18B20_BIT); } /* @@ -109,7 +109,7 @@ uint8_t ds18b20_readbyte(void) /* * get temperature */ -double ds18b20_gettemp() +double ds18b20_gettemp(void) { uint8_t temperature_l; uint8_t temperature_h;