1
0

works better

This commit is contained in:
Arti Zirk 2021-07-07 23:37:56 +03:00
parent bf5f6c16fc
commit ebff80e119

27
main.c
View File

@ -123,36 +123,40 @@ void tim3_isr(void)
// And make the GPIO input so that we can wait for response // And make the GPIO input so that we can wait for response
gpio_set(DHT_PORT, DHT_PIN); gpio_set(DHT_PORT, DHT_PIN);
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DHT_PIN); gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, DHT_PIN);
// 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);// TIM_IC_RISING timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_RISING);// TIM_IC_RISING //timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_RISING);// TIM_IC_RISING
timer_ic_enable(TIM3, TIM_IC3);
timer_clear_flag(TIM3, TIM_SR_CC3IF); timer_clear_flag(TIM3, TIM_SR_CC3IF);
timer_ic_enable(TIM3, TIM_IC3);
//puts("waiting for response"); //puts("waiting for response");
} }
gpio_toggle(GPIOB, GPIO2); //gpio_toggle(GPIOB, GPIO2);
} }
else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value
{ {
gpio_toggle(GPIOB, GPIO2);
//puts("fall");
if (g_dht11_state == DHT11_END_START) { if (g_dht11_state == DHT11_END_START) {
g_dht11_state = DHT11_RESPONSE; g_dht11_state = DHT11_RESPONSE;
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_FALLING);// 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) - g_dht_vals[g_dht_i-1]; // store length from last sample
g_dht_i++; g_dht_i++;
gpio_toggle(GPIOB, GPIO2); //gpio_toggle(GPIOB, GPIO2);
} }
timer_clear_flag(TIM3, TIM_SR_CC3IF);
gpio_toggle(GPIOB, GPIO2);
// if (timer_get_flag(TIM3, TIM_SR_CC3OF)) // if (timer_get_flag(TIM3, TIM_SR_CC3OF))
// { // {
@ -166,6 +170,7 @@ void tim3_isr(void)
static void dht_start(void) static void dht_start(void)
{ {
timer_disable_counter(TIM3); timer_disable_counter(TIM3);
timer_ic_disable(TIM3, TIM_IC3);
g_dht11_state = DHT11_BEGIN_START; g_dht11_state = DHT11_BEGIN_START;
g_dht_i = 0; g_dht_i = 0;
@ -173,6 +178,8 @@ static void dht_start(void)
timer_set_counter(TIM3, 0); timer_set_counter(TIM3, 0);
timer_set_oc_value(TIM3, TIM_OC1, 1000*20); // 20ms timer_set_oc_value(TIM3, TIM_OC1, 1000*20); // 20ms
timer_clear_flag(TIM3, TIM_SR_CC1IF); timer_clear_flag(TIM3, TIM_SR_CC1IF);
timer_clear_flag(TIM3, TIM_SR_CC3IF);
// Do DHT11_BEGIN_START aka drive DATA Low // Do DHT11_BEGIN_START aka drive DATA Low
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);