Works!
This commit is contained in:
parent
d23915d79a
commit
1c587d5ba3
67
main.c
67
main.c
@ -34,14 +34,15 @@ enum DHT11_STATE {
|
|||||||
DHT11_BEGIN_START, // We drive DATA Low for min 18ms
|
DHT11_BEGIN_START, // We drive DATA Low for min 18ms
|
||||||
DHT11_END_START, // We drive DATA High and set it to input
|
DHT11_END_START, // We drive DATA High and set it to input
|
||||||
DHT11_RESPONSE, // Wait for DHT to drive DATA Low
|
DHT11_RESPONSE, // Wait for DHT to drive DATA Low
|
||||||
DHT11_DATA,
|
DHT11_DATA, // Receiving data
|
||||||
DHT11_END
|
DHT11_END, // Got last bit
|
||||||
|
DHT11_TIMEOUT, // Not response or data got
|
||||||
};
|
};
|
||||||
|
|
||||||
int g_dht11_state = DHT11_STOP;
|
int g_dht11_state = DHT11_STOP;
|
||||||
|
|
||||||
int g_dht_i = 0;
|
int g_dht_i = 0;
|
||||||
uint16_t g_dht_vals[100] = {0};
|
uint16_t g_dht_vals[43] = {0};
|
||||||
|
|
||||||
static void delay(int count)
|
static void delay(int count)
|
||||||
{
|
{
|
||||||
@ -106,7 +107,7 @@ void tim3_isr(void)
|
|||||||
} else {
|
} else {
|
||||||
puts("overflow");
|
puts("overflow");
|
||||||
}
|
}
|
||||||
g_dht11_state = DHT11_STOP;
|
g_dht11_state = DHT11_TIMEOUT;
|
||||||
timer_disable_counter(TIM3);
|
timer_disable_counter(TIM3);
|
||||||
|
|
||||||
//timer_set_counter(TIM3, 0);
|
//timer_set_counter(TIM3, 0);
|
||||||
@ -127,7 +128,6 @@ void tim3_isr(void)
|
|||||||
|
|
||||||
// setup timer to wait for start response from dht11
|
// setup timer to wait for start response from dht11
|
||||||
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
|
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
|
||||||
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_RISING);// TIM_IC_RISING
|
|
||||||
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||||
timer_ic_enable(TIM3, TIM_IC3);
|
timer_ic_enable(TIM3, TIM_IC3);
|
||||||
//puts("waiting for response");
|
//puts("waiting for response");
|
||||||
@ -144,17 +144,23 @@ void tim3_isr(void)
|
|||||||
//gpio_toggle(GPIOB, GPIO2);
|
//gpio_toggle(GPIOB, GPIO2);
|
||||||
//timer_ic_disable(TIM3, TIM_IC3);
|
//timer_ic_disable(TIM3, TIM_IC3);
|
||||||
//timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
//timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||||
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);// TIM_IC_RISING
|
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_RISING);
|
||||||
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3); // store captured high time value
|
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3); // store captured high time value
|
||||||
//timer_ic_enable(TIM3, TIM_IC3);
|
//timer_ic_enable(TIM3, TIM_IC3);
|
||||||
//gpio_toggle(GPIOB, GPIO2);
|
//gpio_toggle(GPIOB, GPIO2);
|
||||||
} else {
|
} else {
|
||||||
//gpio_toggle(GPIOB, GPIO2);
|
//gpio_toggle(GPIOB, GPIO2);
|
||||||
g_dht11_state = DHT11_DATA;
|
g_dht11_state = DHT11_DATA;
|
||||||
g_dht_vals[g_dht_i] = TIM_CCR3(TIM3) - g_dht_vals[g_dht_i-1]; // store length from last sample
|
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3); // store length from last sample
|
||||||
g_dht_i++;
|
|
||||||
//gpio_toggle(GPIOB, GPIO2);
|
//gpio_toggle(GPIOB, GPIO2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (g_dht_i > 41) {
|
||||||
|
// we have the whole packet
|
||||||
|
g_dht11_state = DHT11_END;
|
||||||
|
timer_disable_counter(TIM3);
|
||||||
|
}
|
||||||
|
|
||||||
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
||||||
gpio_toggle(GPIOB, GPIO2);
|
gpio_toggle(GPIOB, GPIO2);
|
||||||
|
|
||||||
@ -192,6 +198,35 @@ static void dht_start(void)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void dht_parse_data(){
|
||||||
|
int8_t data[5] = {0};
|
||||||
|
uint8_t checksum = 0;
|
||||||
|
int data_index = 0;
|
||||||
|
int data_bit = 7;
|
||||||
|
for (int i=2; i < 42; i++){ // First 2 pulses are start of response, ignore them
|
||||||
|
uint16_t bit_time = g_dht_vals[i] - g_dht_vals[i-1];
|
||||||
|
if (bit_time > 100) {
|
||||||
|
data[data_index] |= 1 << data_bit;
|
||||||
|
}
|
||||||
|
data_bit--;
|
||||||
|
if (data_bit < 0) {
|
||||||
|
data_bit = 7;
|
||||||
|
data_index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++){
|
||||||
|
checksum += data[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checksum == data[4]) {
|
||||||
|
SEGGER_RTT_printf(0, "Humidity: %d.%d%%; Temperature: %d.%dC\n",
|
||||||
|
data[0], data[1], data[2], data[3]);
|
||||||
|
} else {
|
||||||
|
SEGGER_RTT_printf(0, "Checksum did not match, %x != %x\n", checksum, data[4]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void dht_start_signal(void)
|
static void dht_start_signal(void)
|
||||||
{
|
{
|
||||||
gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
||||||
@ -206,6 +241,7 @@ static void dht_start_signal(void)
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
bool wait = false;
|
||||||
puts("Start");
|
puts("Start");
|
||||||
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
|
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
|
||||||
gpio_setup();
|
gpio_setup();
|
||||||
@ -215,11 +251,20 @@ int main(void)
|
|||||||
while (1) {
|
while (1) {
|
||||||
/* Using API function gpio_toggle(): */
|
/* Using API function gpio_toggle(): */
|
||||||
gpio_toggle(GPIOB, GPIO1); /* LED on/off */
|
gpio_toggle(GPIOB, GPIO1); /* LED on/off */
|
||||||
delay(10000000);
|
|
||||||
dht_start();
|
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
SEGGER_RTT_printf(0, "Poll: %d\n", i);
|
SEGGER_RTT_printf(0, "Poll: %d\n", i);
|
||||||
|
delay(10000000);
|
||||||
|
dht_start();
|
||||||
|
wait = true;
|
||||||
|
while (wait) {
|
||||||
|
switch (g_dht11_state) {
|
||||||
|
case DHT11_END:
|
||||||
|
dht_parse_data();
|
||||||
|
// fall through
|
||||||
|
case DHT11_TIMEOUT:
|
||||||
|
wait = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user