2021-05-01 20:06:51 +03:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-05-01 20:06:51 +03:00
|
|
|
#include <libopencm3/cm3/nvic.h>
|
|
|
|
#include <libopencm3/stm32/rcc.h>
|
|
|
|
#include <libopencm3/stm32/gpio.h>
|
|
|
|
#include <libopencm3/stm32/timer.h>
|
|
|
|
|
2021-06-27 18:32:43 +03:00
|
|
|
#include <SEGGER_RTT.h>
|
2021-06-26 11:34:10 +03:00
|
|
|
|
2021-07-21 23:04:05 +03:00
|
|
|
#define puts(s) SEGGER_RTT_WriteString(0, s);SEGGER_RTT_WriteString(0,"\n")
|
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
#define DHT_PORT GPIOB
|
|
|
|
#define DHT_PIN GPIO0
|
|
|
|
|
2021-08-22 22:31:36 +03:00
|
|
|
#define SEGMENT_PORT GPIOA
|
2023-04-30 21:38:25 +03:00
|
|
|
#define SEGMENT_A GPIO5
|
|
|
|
#define SEGMENT_B GPIO3
|
|
|
|
#define SEGMENT_C GPIO6
|
2021-08-22 22:31:36 +03:00
|
|
|
#define SEGMENT_D GPIO0
|
|
|
|
#define SEGMENT_E GPIO1
|
2023-04-30 21:38:25 +03:00
|
|
|
#define SEGMENT_F GPIO7
|
|
|
|
#define SEGMENT_G GPIO4
|
|
|
|
#define SEGMENT_DOT GPIO2
|
2021-08-22 22:31:36 +03:00
|
|
|
#define SEGMENT_ALL GPIO0 | GPIO1 | GPIO2 | GPIO3 | GPIO4 | GPIO5 | GPIO6 | GPIO7 | GPIO4
|
|
|
|
#define COMMON_PORT GPIOB
|
|
|
|
#define COMMON_ALL GPIO15 | GPIO14 | GPIO13 | GPIO12 | GPIO11
|
|
|
|
#define COMMON_1 GPIO15
|
|
|
|
#define COMMON_2 GPIO14
|
|
|
|
#define COMMON_3 GPIO13
|
|
|
|
#define COMMON_4 GPIO12
|
|
|
|
#define COMMON_L GPIO11
|
|
|
|
|
|
|
|
const uint16_t segment_numbers[] = {
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F, // 0
|
|
|
|
SEGMENT_B | SEGMENT_C, // 1
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_D | SEGMENT_E | SEGMENT_G, // 2
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_G, // 3
|
|
|
|
SEGMENT_B | SEGMENT_C | SEGMENT_F | SEGMENT_G, // 4
|
|
|
|
SEGMENT_A | SEGMENT_C | SEGMENT_D | SEGMENT_F | SEGMENT_G, // 5
|
|
|
|
SEGMENT_A | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F | SEGMENT_G, // 6
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_C, // 7
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F | SEGMENT_G, // 8
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_F | SEGMENT_G, // 9
|
|
|
|
SEGMENT_A | SEGMENT_B | SEGMENT_C | SEGMENT_E | SEGMENT_F | SEGMENT_G, // A 10
|
|
|
|
SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_F | SEGMENT_G, // B 11
|
|
|
|
SEGMENT_A | SEGMENT_D | SEGMENT_E | SEGMENT_F, // C 12
|
|
|
|
SEGMENT_B | SEGMENT_C | SEGMENT_D | SEGMENT_E | SEGMENT_G, // D 13
|
|
|
|
SEGMENT_A | SEGMENT_D | SEGMENT_E | SEGMENT_F | SEGMENT_G, // E 14
|
|
|
|
SEGMENT_A | SEGMENT_E | SEGMENT_F | SEGMENT_G, // F 15
|
|
|
|
SEGMENT_B | SEGMENT_C | SEGMENT_E | SEGMENT_F | SEGMENT_G, // H 16
|
|
|
|
SEGMENT_G // - 17
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint16_t common_numbers[] = { COMMON_1, COMMON_2, COMMON_3, COMMON_4 };
|
|
|
|
|
|
|
|
volatile int segment_values[] = {17, 17, 17, 17};
|
|
|
|
volatile int common_counter = 0;
|
|
|
|
|
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
enum DHT11_STATE {
|
|
|
|
DHT11_STOP, // Stop statemachine
|
2021-07-08 16:28:32 +03:00
|
|
|
DHT11_START, // We drive DATA Low for minimum 18ms and then drive it HIGH and start waiting for response
|
2021-06-28 00:13:08 +03:00
|
|
|
DHT11_RESPONSE, // Wait for DHT to drive DATA Low
|
2021-07-08 01:02:05 +03:00
|
|
|
DHT11_DATA, // Receiving data
|
|
|
|
DHT11_END, // Got last bit
|
2021-07-08 16:28:32 +03:00
|
|
|
DHT11_TIMEOUT, // Not response or got partial data only
|
2021-06-28 00:13:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
int g_dht11_state = DHT11_STOP;
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// 2 pulses are the DHT Response and last 40 are data pulses for 5 bytes
|
|
|
|
// so we have total of 42 pulses
|
|
|
|
#define DHT11_BIT_COUNT 42
|
|
|
|
int g_dht_i = 0; // how many bits we have captured
|
|
|
|
uint16_t g_dht_vals[DHT11_BIT_COUNT] = {0}; // captured bit times in us
|
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
|
2021-05-01 20:06:51 +03:00
|
|
|
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: */
|
2021-08-22 22:31:36 +03:00
|
|
|
rcc_periph_clock_enable(RCC_GPIOA);
|
2021-05-01 20:06:51 +03:00
|
|
|
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: */
|
2021-07-08 16:28:32 +03:00
|
|
|
// DHT Data pin
|
2021-06-28 00:13:08 +03:00
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DHT_PIN);
|
2021-07-08 16:28:32 +03:00
|
|
|
// Maple mini led
|
2021-05-01 20:06:51 +03:00
|
|
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
|
2021-07-08 16:28:32 +03:00
|
|
|
// A random DEBUG pin we can toggle and measure with logic
|
2021-05-01 20:06:51 +03:00
|
|
|
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
2021-08-22 22:31:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
// 7segments
|
|
|
|
gpio_set_mode(SEGMENT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, SEGMENT_ALL);
|
|
|
|
// commons
|
|
|
|
gpio_set_mode(COMMON_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, COMMON_ALL);
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// Onetime config of timer things that stay the same
|
2021-05-01 20:06:51 +03:00
|
|
|
static void tim_setup(void)
|
|
|
|
{
|
2021-08-22 22:31:36 +03:00
|
|
|
// 7 segment driver timer
|
|
|
|
nvic_enable_irq(NVIC_TIM2_IRQ);
|
|
|
|
nvic_set_priority(NVIC_TIM2_IRQ, 2);
|
|
|
|
rcc_periph_clock_enable(RCC_TIM2);
|
|
|
|
rcc_periph_reset_pulse(RST_TIM2);
|
|
|
|
|
|
|
|
timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
|
|
|
timer_set_prescaler(TIM2, (rcc_apb1_frequency*2) / 1000000 - 1); // set timer tickrate to 1mhz
|
2021-08-22 22:39:34 +03:00
|
|
|
timer_set_period(TIM2, 2000); // 2ms
|
2021-08-22 22:31:36 +03:00
|
|
|
timer_enable_irq(TIM2, TIM_DIER_UIE);
|
|
|
|
|
|
|
|
// start displaying 7 segment numbers
|
|
|
|
timer_enable_counter(TIM2);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-08-22 22:31:36 +03:00
|
|
|
|
|
|
|
// DHT11 Reading timer
|
|
|
|
nvic_enable_irq(NVIC_TIM3_IRQ);
|
|
|
|
nvic_set_priority(NVIC_TIM3_IRQ, 1);
|
2021-05-01 20:06:51 +03:00
|
|
|
rcc_periph_clock_enable(RCC_TIM3);
|
|
|
|
rcc_periph_reset_pulse(RST_TIM3);
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// configure the timer to be a normal up counting timer, nothing fancy
|
2021-05-01 20:06:51 +03:00
|
|
|
timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
|
2021-06-28 00:13:08 +03:00
|
|
|
timer_set_prescaler(TIM3, (rcc_apb1_frequency*2) / 1000000 - 1); // set timer tickrate to 1mhz
|
2021-07-08 16:28:32 +03:00
|
|
|
timer_set_period(TIM3, 0xffff); // ~64 ms, DHT11 packet is never longer than 26ms
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
timer_enable_irq(TIM3, TIM_DIER_UIE); // overflow
|
2021-06-28 00:13:08 +03:00
|
|
|
timer_enable_irq(TIM3, TIM_DIER_CC3IE); // triggered when DHT drives DATA pin
|
2021-08-22 22:31:36 +03:00
|
|
|
// timer is enabled in dht_start()
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_segment_value(int common, int value, bool decimal) {
|
|
|
|
gpio_clear(COMMON_PORT, COMMON_ALL);
|
|
|
|
gpio_set(SEGMENT_PORT, SEGMENT_ALL);
|
|
|
|
gpio_clear(SEGMENT_PORT, segment_numbers[value] | (decimal ? SEGMENT_DOT : 0));
|
|
|
|
gpio_set(COMMON_PORT, common_numbers[common]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tim2_isr(void) {
|
|
|
|
if (timer_get_flag(TIM2, TIM_SR_UIF)) {
|
|
|
|
timer_clear_flag(TIM2, TIM_SR_UIF);
|
|
|
|
show_segment_value(common_counter, segment_values[common_counter],
|
|
|
|
common_counter == 1 ? true : false);
|
|
|
|
common_counter++;
|
|
|
|
if (common_counter > 3){
|
|
|
|
common_counter = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void tim3_isr(void)
|
|
|
|
{
|
2021-06-28 00:13:08 +03:00
|
|
|
// Timer overflow that should only happen if DHT11 does not answer
|
|
|
|
if (timer_get_flag(TIM3, TIM_SR_UIF)) {
|
2021-05-01 20:06:51 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_UIF);
|
2021-07-08 16:28:32 +03:00
|
|
|
puts("overflow");
|
2021-07-08 01:02:05 +03:00
|
|
|
g_dht11_state = DHT11_TIMEOUT;
|
2021-06-28 00:13:08 +03:00
|
|
|
timer_disable_counter(TIM3);
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
else if (timer_get_flag(TIM3, TIM_SR_CC3IF)) // PB0 changed value
|
|
|
|
{
|
2021-06-28 00:13:08 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// handle DHT11_END_START
|
|
|
|
if (g_dht11_state == DHT11_START) {
|
|
|
|
// Drive DATA pin high for a split second so that we have a nice sharp edge
|
2021-06-28 00:13:08 +03:00
|
|
|
gpio_set(DHT_PORT, DHT_PIN);
|
2021-07-08 16:28:32 +03:00
|
|
|
// Make the GPIO input so that we can wait for response
|
|
|
|
// there is a external pull-up connected to the DATA pin
|
|
|
|
// probably could use the internal pull-up also...
|
2021-07-07 23:37:56 +03:00
|
|
|
gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, DHT_PIN);
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// setup timer input capture to wait for start response from dht11
|
|
|
|
// need to disable input capture before we can configure it.
|
|
|
|
timer_ic_disable(TIM3, TIM_IC3);
|
|
|
|
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_IN_TI3);
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_ic_set_polarity(TIM3, TIM_IC3, TIM_IC_FALLING);
|
2021-07-08 16:28:32 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3IF); // otherwise we will get a invalid interrupt
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_ic_enable(TIM3, TIM_IC3);
|
2021-07-08 16:28:32 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
//puts("waiting for response");
|
2021-07-08 16:28:32 +03:00
|
|
|
g_dht11_state = DHT11_RESPONSE;
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// DHT11 drove DATA low and has started talking to us
|
|
|
|
else if (g_dht11_state == DHT11_RESPONSE) {
|
|
|
|
// store time when DHT11 pulled DATA low
|
|
|
|
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3);
|
2021-06-28 00:13:08 +03:00
|
|
|
g_dht11_state = DHT11_DATA;
|
|
|
|
}
|
2021-07-08 01:02:05 +03:00
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// DHT11 is sending data to us
|
|
|
|
else {
|
|
|
|
// store length of the data bit
|
|
|
|
g_dht_vals[g_dht_i++] = TIM_CCR3(TIM3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did we just capture our last bit?
|
|
|
|
if (g_dht_i >= DHT11_BIT_COUNT) {
|
|
|
|
// yes we did
|
2021-07-08 01:02:05 +03:00
|
|
|
g_dht11_state = DHT11_END;
|
|
|
|
timer_disable_counter(TIM3);
|
|
|
|
}
|
|
|
|
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
2021-07-08 16:28:32 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO2);
|
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// Should only happen if TIM_SR_CC3IF is too slow
|
|
|
|
// at 72mhz it should never happen but could at lower cpu speeds
|
|
|
|
if (timer_get_flag(TIM3, TIM_SR_CC3OF))
|
|
|
|
{
|
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3OF);
|
|
|
|
//puts("we are too slow to read and clear timer input capture flags");
|
|
|
|
g_dht11_state = DHT11_TIMEOUT;
|
|
|
|
timer_disable_counter(TIM3);
|
2021-06-28 00:13:08 +03:00
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-06-28 00:13:08 +03:00
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// Setup the timer and trigger DHT11 Start
|
2021-06-28 00:13:08 +03:00
|
|
|
static void dht_start(void)
|
|
|
|
{
|
|
|
|
timer_disable_counter(TIM3);
|
2021-07-08 16:28:32 +03:00
|
|
|
g_dht11_state = DHT11_STOP;
|
2021-06-28 00:13:08 +03:00
|
|
|
g_dht_i = 0;
|
|
|
|
|
|
|
|
// Setup DHT11_END_START trigger on output compare 1
|
|
|
|
timer_set_counter(TIM3, 0);
|
2021-07-08 16:28:32 +03:00
|
|
|
|
|
|
|
// capture compare channel must be disabled before we can configure it
|
|
|
|
timer_ic_disable(TIM3, TIM_IC3);
|
|
|
|
// set channel as output so that we can get an interrupt when OC3 values matches the timer counter
|
|
|
|
timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_OUT);
|
|
|
|
// tick rate should be 1Mhz (via timer prescaler), so this should trigger in 20ms
|
|
|
|
timer_set_oc_value(TIM3, TIM_OC3, 1000*20);
|
|
|
|
// clear status flags so that we dont get any stray interrupts
|
2021-07-07 23:37:56 +03:00
|
|
|
timer_clear_flag(TIM3, TIM_SR_CC3IF);
|
2021-07-08 16:28:32 +03:00
|
|
|
// and make sure that we can get interrupts
|
|
|
|
timer_ic_enable(TIM3, TIM_IC3);
|
2021-07-07 23:37:56 +03:00
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// Begin the statemachine
|
|
|
|
g_dht11_state = DHT11_START;
|
2021-06-28 00:13:08 +03:00
|
|
|
|
|
|
|
// 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_clear(DHT_PORT, DHT_PIN);
|
|
|
|
|
|
|
|
// And start counting
|
|
|
|
timer_enable_counter(TIM3);
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// Parse received DHT11 bit timing data from g_dht_vals and print out pretty values
|
2021-07-21 23:04:05 +03:00
|
|
|
static void dht_parse_data(void) {
|
2021-07-08 01:02:05 +03:00
|
|
|
int8_t data[5] = {0};
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
int data_index = 0;
|
|
|
|
int data_bit = 7;
|
2021-07-08 16:28:32 +03:00
|
|
|
|
|
|
|
// create bytes from bits
|
|
|
|
for (int i=2; i < DHT11_BIT_COUNT; i++){ // First 2 pulses are start of response, ignore them
|
2021-07-08 01:02:05 +03:00
|
|
|
uint16_t bit_time = g_dht_vals[i] - g_dht_vals[i-1];
|
2021-07-08 16:28:32 +03:00
|
|
|
|
|
|
|
// ~80us is 0 and ~120us is 1
|
2021-07-08 01:02:05 +03:00
|
|
|
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]);
|
2021-08-22 22:31:36 +03:00
|
|
|
if (segment_values[3] == 0xC) {
|
|
|
|
segment_values[3] = 16; // now showing humidity
|
|
|
|
segment_values[0] = (data[0] / 10) % 10;
|
|
|
|
segment_values[1] = data[0] % 10;
|
|
|
|
segment_values[2] = data[1] % 10;
|
|
|
|
} else {
|
|
|
|
segment_values[3] = 0xC; // now showing temperature
|
|
|
|
segment_values[0] = (data[2] / 10) % 10;
|
|
|
|
segment_values[1] = data[2] % 10;
|
|
|
|
segment_values[2] = data[3] % 10;
|
|
|
|
}
|
2021-07-08 01:02:05 +03:00
|
|
|
} else {
|
|
|
|
SEGGER_RTT_printf(0, "Checksum did not match, %x != %x\n", checksum, data[4]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:28:32 +03:00
|
|
|
// Just toggle the data pin, used only for debugging
|
2021-07-21 23:04:05 +03:00
|
|
|
//static void dht_start_signal(void)
|
|
|
|
//{
|
|
|
|
// gpio_set_mode(DHT_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DHT_PIN);
|
|
|
|
// gpio_clear(DHT_PORT, DHT_PIN);
|
|
|
|
// delay(100000);
|
|
|
|
// //delay(30000);
|
|
|
|
// gpio_set(DHT_PORT, DHT_PIN);
|
|
|
|
// delay(100);
|
|
|
|
// gpio_set_mode(DHT_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DHT_PIN);
|
|
|
|
//}
|
2021-05-01 20:06:51 +03:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2021-06-27 18:32:43 +03:00
|
|
|
int i = 0;
|
2021-07-08 01:02:05 +03:00
|
|
|
bool wait = false;
|
2021-06-28 00:13:08 +03:00
|
|
|
puts("Start");
|
2021-05-01 20:06:51 +03:00
|
|
|
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
|
|
|
|
gpio_setup();
|
2021-06-28 00:13:08 +03:00
|
|
|
tim_setup();
|
2021-06-26 11:34:10 +03:00
|
|
|
|
2021-05-01 20:06:51 +03:00
|
|
|
/* Blink the LED (PA5) on the board. */
|
|
|
|
while (1) {
|
|
|
|
/* Using API function gpio_toggle(): */
|
2021-08-22 22:31:36 +03:00
|
|
|
//gpio_toggle(GPIOB, GPIO1); /* LED on/off */
|
|
|
|
SEGGER_RTT_printf(0, "Poll: %d, seg: %d\n", i, i & 0xf);
|
|
|
|
//show_segment_value(i & 3, 16, false);
|
2021-06-27 18:32:43 +03:00
|
|
|
i++;
|
2021-07-08 01:02:05 +03:00
|
|
|
delay(10000000);
|
2021-08-22 22:31:36 +03:00
|
|
|
//delay(1000000);
|
2021-07-08 01:02:05 +03:00
|
|
|
dht_start();
|
|
|
|
wait = true;
|
|
|
|
while (wait) {
|
|
|
|
switch (g_dht11_state) {
|
|
|
|
case DHT11_END:
|
|
|
|
dht_parse_data();
|
|
|
|
// fall through
|
|
|
|
case DHT11_TIMEOUT:
|
|
|
|
wait = false;
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 20:06:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|