1
0
Fork 0
dht_test/main.c

157 lines
4.2 KiB
C

/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
static void delay(int count)
{
for (int i=0; i < count; i++) {
__asm__("nop");
}
}
static void gpio_setup(void)
{
/* Enable GPIOA clock. */
/* Manually: */
// RCC_APB2ENR |= RCC_APB2ENR_IOPCEN;
/* Using API functions: */
rcc_periph_clock_enable(RCC_GPIOB);
/* Set GPIO5 (in GPIO port A) to 'output push-pull'. */
/* Manually: */
// GPIOA_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((5 - 8) * 4) + 2));
// GPIOA_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((5 - 8) * 4));
/* Using API functions: */
gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
}
static void tim_setup(void)
{
nvic_enable_irq(NVIC_TIM3_IRQ);
nvic_set_priority(NVIC_TIM3_IRQ, 1); // interupts will not work without it???
rcc_periph_clock_enable(RCC_TIM3);
rcc_periph_reset_pulse(RST_TIM3);
timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
timer_set_prescaler(TIM3, 1152); // set timer tickrate to 500khz
//timer_set_oc_value(TIM3, TIM_OC1, 0x1000);
timer_set_period(TIM3, 0xffff);
timer_one_shot_mode(TIM3);
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_IN_TI3);
//timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING); // TIM_IC_RISING
//timer_ic_enable(TIM3, TIM_IC3);
timer_enable_counter(TIM3);
timer_enable_irq(TIM3, TIM_DIER_UIE);
timer_enable_irq(TIM3, TIM_DIER_CC1IE); // end start condition
timer_enable_irq(TIM3, TIM_DIER_CC3IE);
// start condition
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
gpio_clear(GPIOB, GPIO0);
}
enum DHT11_STATE {
DHT11_START,
DHT11_RESPONSE,
DHT11_BIT0,
DHT11_BIT1,
DHT11_END
};
int dht11_state = DHT11_START;
void tim3_isr(void)
{
if (timer_get_flag(TIM3, TIM_SR_UIF)) // timer reached end
{
timer_clear_flag(TIM3, TIM_SR_UIF);
//gpio_toggle(GPIOB, GPIO1);
gpio_toggle(GPIOB, GPIO2);
//timer_set_counter(TIM3, 0);
//timer_clear_flag(TIM3, TIM_SR_UIF);
//timer_set_oc_value(TIM3, TIM_OC1, 1000);
} else if (timer_get_flag(TIM3, TIM_SR_CC1IF))
{
timer_clear_flag(TIM3, TIM_SR_CC3IF);
if (dht11_state == DHT11_START){
gpio_set(GPIOB, GPIO0);
gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0);
// setup timer to wait for start response from dht11
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING); // TIM_IC_RISING
timer_ic_enable(TIM3, TIM_IC3);
dht11_state = DHT11_START;
}
} else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value
{
timer_clear_flag(TIM3, TIM_SR_CC3IF);
if (timer_get_flag(TIM3, TIM_SR_CC3OF))
{
timer_clear_flag(TIM3, TIM_SR_CC3OF);
}
}
}
static void dht_start_signal(void)
{
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
gpio_clear(GPIOB, GPIO0);
delay(205000);
//delay(30000);
gpio_set(GPIOB, GPIO0);
delay(100);
gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0);
}
volatile int bla=0;
int main(void)
{
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
gpio_setup();
tim_setup();
/* Blink the LED (PA5) on the board. */
while (1) {
/* Using API function gpio_toggle(): */
gpio_toggle(GPIOB, GPIO1); /* LED on/off */
delay(4000000);
dht_start_signal();
}
return 0;
}