sensor_dht22: minor, add space around operators
This commit is contained in:
parent
4a1470401c
commit
1dc728e40e
|
@ -12,10 +12,10 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from Aosong DHT22 temperature and relative humidity sensor (code)
|
||||
* @file sensor_dht22.c
|
||||
/** library to query measurements from Aosong DHT22 temperature and relative humidity sensor
|
||||
* @file
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @date 2017-2020
|
||||
* @note peripherals used: GPIO and timer @ref sensor_dht22_timer
|
||||
* @note the DHT22 protocol is very similar but nit completely compatible with the DHT22 protocol: only 1 ms initial host pull low is required (vs. 18 ms), the data is encoded as int16_t (vs. uint8_t), and the signal has more jitter
|
||||
*/
|
||||
|
@ -83,7 +83,7 @@ void sensor_dht22_setup(void)
|
|||
rcc_periph_clock_enable(RCC_TIM(SENSOR_DHT22_TIMER)); // enable clock for timer peripheral
|
||||
rcc_periph_reset_pulse(RST_TIM(SENSOR_DHT22_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(SENSOR_DHT22_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(SENSOR_DHT22_TIMER), 2-1); // set the prescaler so this 16 bits timer allows to wait for 18 ms for the start signal ( 1/(72E6/2/(2**16))=1.820ms )
|
||||
timer_set_prescaler(TIM(SENSOR_DHT22_TIMER), 2 - 1); // set the prescaler so this 16 bits timer allows to wait for 18 ms for the start signal ( 1/(72E6/2/(2**16))=1.820ms )
|
||||
timer_ic_set_input(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_IN_TI(SENSOR_DHT22_CHANNEL)); // configure ICx to use TIn
|
||||
timer_ic_set_filter(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_OFF); // use no filter input (precise timing needed)
|
||||
timer_ic_set_polarity(TIM(SENSOR_DHT22_TIMER), TIM_IC(SENSOR_DHT22_CHANNEL), TIM_IC_FALLING); // capture on rising edge
|
||||
|
@ -103,13 +103,13 @@ void sensor_dht22_setup(void)
|
|||
|
||||
bool sensor_dht22_measurement_request(void)
|
||||
{
|
||||
if (sensor_dht22_state!=SENSOR_DHT22_OFF) { // not the right state to start (wait up until timeout to reset state)
|
||||
if (sensor_dht22_state != SENSOR_DHT22_OFF) { // not the right state to start (wait up until timeout to reset state)
|
||||
return false;
|
||||
}
|
||||
if (gpio_get(TIM_CH_PORT(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL), TIM_CH_PIN(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL))==0) { // signal should be high per default
|
||||
if (gpio_get(TIM_CH_PORT(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL), TIM_CH_PIN(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL)) == 0) { // signal should be high per default
|
||||
return false;
|
||||
}
|
||||
if (TIM_CR1(TIM(SENSOR_DHT22_TIMER))&(TIM_CR1_CEN)) { // timer should be off
|
||||
if (TIM_CR1(TIM(SENSOR_DHT22_TIMER)) & (TIM_CR1_CEN)) { // timer should be off
|
||||
return false;
|
||||
}
|
||||
sensor_dht22_reset(); // reset states
|
||||
|
@ -126,15 +126,15 @@ bool sensor_dht22_measurement_request(void)
|
|||
struct sensor_dht22_measurement_t sensor_dht22_measurement_decode(void)
|
||||
{
|
||||
struct sensor_dht22_measurement_t measurement = { NAN, NAN }; // measurement to return
|
||||
if (sensor_dht22_bit<40) { // not enough bits received
|
||||
if (sensor_dht22_bit < 40) { // not enough bits received
|
||||
return measurement;
|
||||
}
|
||||
if ((uint8_t)(sensor_dht22_bits[0]+sensor_dht22_bits[1]+sensor_dht22_bits[2]+sensor_dht22_bits[3])!=sensor_dht22_bits[4]) { // error in checksum (not really parity bit, as mentioned in the datasheet)
|
||||
if ((uint8_t)(sensor_dht22_bits[0] + sensor_dht22_bits[1] + sensor_dht22_bits[2] + sensor_dht22_bits[3]) != sensor_dht22_bits[4]) { // error in checksum (not really parity bit, as mentioned in the datasheet)
|
||||
return measurement;
|
||||
}
|
||||
// calculate measured values (stored as uint16_t deci-value)
|
||||
measurement.humidity = (int16_t)((sensor_dht22_bits[0]<<8)+sensor_dht22_bits[1])/10.0;
|
||||
measurement.temperature = (int16_t)((sensor_dht22_bits[2]<<8)+sensor_dht22_bits[3])/10.0;
|
||||
measurement.humidity = (int16_t)((sensor_dht22_bits[0] << 8) + sensor_dht22_bits[1]) / 10.0;
|
||||
measurement.temperature = (int16_t)((sensor_dht22_bits[2] << 8) + sensor_dht22_bits[3]) / 10.0;
|
||||
|
||||
return measurement;
|
||||
}
|
||||
|
@ -154,31 +154,31 @@ void TIM_ISR(SENSOR_DHT22_TIMER)(void)
|
|||
} else if (timer_get_flag(TIM(SENSOR_DHT22_TIMER), TIM_SR_CCIF(SENSOR_DHT22_CHANNEL))) { // edge detected on input capture
|
||||
uint16_t time = TIM_CCR(SENSOR_DHT22_TIMER,SENSOR_DHT22_CHANNEL); // save captured bit timing (this clear also the flag)
|
||||
timer_set_counter(TIM(SENSOR_DHT22_TIMER), 0); // reset timer counter
|
||||
time = (time*1E6)/(rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_DHT22_TIMER))+1)); // calculate time in us
|
||||
time = (time * 1E6) / (rcc_ahb_frequency / (TIM_PSC(TIM(SENSOR_DHT22_TIMER)) + 1)); // calculate time in us
|
||||
switch (sensor_dht22_state) {
|
||||
case (SENSOR_DHT22_HOST_STARTED): // the host query data and the slave is responding
|
||||
sensor_dht22_state = SENSOR_DHT22_SLAVE_START; // set new state
|
||||
break;
|
||||
case (SENSOR_DHT22_SLAVE_START): // the slave sent the start signal
|
||||
if (time >= ((80+80)*(1-SENSOR_DHT22_JITTER)) && time <= ((80+80)*(1+SENSOR_DHT22_JITTER))) { // response time should be 80 us low and 80 us high
|
||||
if (time >= ((80 + 80) * (1 - SENSOR_DHT22_JITTER)) && time <= ((80 + 80) * (1 + SENSOR_DHT22_JITTER))) { // response time should be 80 us low and 80 us high
|
||||
sensor_dht22_state = SENSOR_DHT22_SLAVE_BIT; // set new state
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
case (SENSOR_DHT22_SLAVE_BIT): // the slave sent a bit
|
||||
if (sensor_dht22_bit>=40) { // no bits should be received after 40 bits
|
||||
if (sensor_dht22_bit >= 40) { // no bits should be received after 40 bits
|
||||
goto error;
|
||||
}
|
||||
if (time >= ((50+26)*(1-SENSOR_DHT22_JITTER)) && time <= ((50+28)*(1+SENSOR_DHT22_JITTER))) { // bit 0 time should be 50 us low and 26-28 us high
|
||||
sensor_dht22_bits[sensor_dht22_bit/8] &= ~(1<<(7-(sensor_dht22_bit%8))); // clear bit
|
||||
} else if (time >= ((50+70)*(1-SENSOR_DHT22_JITTER)) && time <= ((50+70)*(1+SENSOR_DHT22_JITTER))) { // bit 1 time should be 50 us low and 70 us high
|
||||
sensor_dht22_bits[sensor_dht22_bit/8] |= (1<<(7-(sensor_dht22_bit%8))); // set bit
|
||||
if (time >= ((50 + 26) * (1 - SENSOR_DHT22_JITTER)) && time <= ((50 + 28) * (1 + SENSOR_DHT22_JITTER))) { // bit 0 time should be 50 us low and 26-28 us high
|
||||
sensor_dht22_bits[sensor_dht22_bit / 8] &= ~(1 << (7 - (sensor_dht22_bit % 8))); // clear bit
|
||||
} else if (time >= ((50 + 70) * (1 - SENSOR_DHT22_JITTER)) && time <= ((50 + 70) * (1 + SENSOR_DHT22_JITTER))) { // bit 1 time should be 50 us low and 70 us high
|
||||
sensor_dht22_bits[sensor_dht22_bit / 8] |= (1 << (7 - (sensor_dht22_bit % 8))); // set bit
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
sensor_dht22_bit++;
|
||||
if (sensor_dht22_bit>=40) { // all bits received
|
||||
if (sensor_dht22_bit >= 40) { // all bits received
|
||||
sensor_dht22_reset(); // reset states
|
||||
sensor_dht22_bit = 40; // signal decoder all bits have been received
|
||||
sensor_dht22_measurement_received = true; // signal user all bits have been received
|
||||
|
|
|
@ -12,10 +12,10 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from Aosong DHT22 (aka. AM2302) temperature and relative humidity sensor (API)
|
||||
* @file sensor_dht22.h
|
||||
/** library to query measurements from Aosong DHT22 (aka. AM2302) temperature and relative humidity sensor
|
||||
* @file
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @date 2017-2020
|
||||
* @note peripherals used: timer channel @ref sensor_dht22_timer (add external pull-up resistor)
|
||||
*/
|
||||
#pragma once
|
||||
|
|
Loading…
Reference in New Issue