kopia lustrzana https://github.com/SP8EBC/ParaTNC
analogue windspeed
rodzic
4f8a3b2a49
commit
44e4bad919
|
@ -20,6 +20,8 @@ extern float rte_wx_temperature_min_dallas_valid, rte_wx_temperature_max_dallas_
|
|||
extern float rte_wx_temperature_ms, rte_wx_temperature_ms_valid;
|
||||
extern float rte_wx_pressure, rte_wx_pressure_valid;
|
||||
|
||||
extern uint16_t rte_wx_windspeed_pulses;
|
||||
|
||||
extern uint8_t rte_wx_tx20_excessive_slew_rate;
|
||||
|
||||
extern dht22Values rte_wx_dht, rte_wx_dht_valid;
|
||||
|
|
BIN
skalowania.ods
BIN
skalowania.ods
Plik binarny nie jest wyświetlany.
|
@ -121,6 +121,7 @@ void TIM1_TRG_COM_TIM17_IRQHandler(void) {
|
|||
|
||||
void DMA1_Channel7_IRQHandler() {
|
||||
NVIC_ClearPendingIRQ(DMA1_Channel7_IRQn);
|
||||
DMA_ClearITPendingBit(DMA1_IT_GL7);
|
||||
|
||||
analog_anemometer_dma_irq();
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ float rte_wx_temperature_min_dallas_valid = 0.0f, rte_wx_temperature_max_dallas_
|
|||
float rte_wx_temperature_ms = 0.0f, rte_wx_temperature_ms_valid = 0.0f;
|
||||
float rte_wx_pressure = 0.0f, rte_wx_pressure_valid = 0.0f;
|
||||
|
||||
uint16_t rte_wx_windspeed_pulses = 0;
|
||||
|
||||
uint8_t rte_wx_tx20_excessive_slew_rate = 0;
|
||||
|
||||
dht22Values rte_wx_dht, rte_wx_dht_valid; // quality factor inside this structure
|
||||
|
|
|
@ -8,17 +8,37 @@
|
|||
#include "drivers/analog_anemometer.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stm32f10x_tim.h>
|
||||
#include <stm32f10x_dma.h>
|
||||
#include "drivers/gpio_conf.h"
|
||||
#include "rte_wx.h"
|
||||
|
||||
#define MINUM_PULSE_LN 15
|
||||
#define MAXIMUM_PULSE_SLEW_RATE 4000
|
||||
|
||||
// an array where DMA will store values of the timer latched by compare-capture input
|
||||
uint16_t analog_anemometer_windspeed_pulses_time[ANALOG_ANEMOMETER_SPEED_PULSES_N];
|
||||
|
||||
uint16_t analog_anemometer_pulses_durations[ANALOG_ANEMOMETER_SPEED_PULSES_N];
|
||||
|
||||
// a static copy of impulse-meters/second contact
|
||||
uint16_t analog_anemometer_pulses_per_ms_constant = 0;
|
||||
|
||||
uint8_t analog_anemometer_timer_has_been_fired = 0;
|
||||
|
||||
DMA_InitTypeDef DMA_InitStruct;
|
||||
|
||||
void analog_anemometer_init(uint16_t pulses_per_ms, uint16_t mvolts_for_1deg,
|
||||
uint16_t mvolts_for_359deg, uint8_t reversed) {
|
||||
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
|
||||
DMA_InitTypeDef DMA_InitStruct;
|
||||
|
||||
analog_anemometer_pulses_per_ms_constant = pulses_per_ms;
|
||||
|
||||
// initializing arrays;
|
||||
memset(analog_anemometer_windspeed_pulses_time, 0x00, ANALOG_ANEMOMETER_SPEED_PULSES_N);
|
||||
memset(analog_anemometer_pulses_durations, 0x00, ANALOG_ANEMOMETER_SPEED_PULSES_N);
|
||||
|
||||
// enabling the clock for TIM17
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM17EN;
|
||||
|
@ -63,10 +83,10 @@ void analog_anemometer_init(uint16_t pulses_per_ms, uint16_t mvolts_for_1deg,
|
|||
DMA_InitStruct.DMA_BufferSize = ANALOG_ANEMOMETER_SPEED_PULSES_N;
|
||||
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
|
||||
DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
|
||||
DMA_InitStruct.DMA_MemoryBaseAddr = analog_anemometer_windspeed_pulses_time;
|
||||
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)analog_anemometer_windspeed_pulses_time;
|
||||
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
||||
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
||||
DMA_InitStruct.DMA_PeripheralBaseAddr = &TIM17->CCR1;
|
||||
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&TIM17->CCR1;
|
||||
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
||||
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||
|
||||
|
@ -81,9 +101,100 @@ void analog_anemometer_init(uint16_t pulses_per_ms, uint16_t mvolts_for_1deg,
|
|||
}
|
||||
|
||||
void analog_anemometer_timer_irq(void) {
|
||||
|
||||
analog_anemometer_timer_has_been_fired = 1;
|
||||
}
|
||||
|
||||
void analog_anemometer_dma_irq(void) {
|
||||
DMA_ClearITPendingBit(DMA1_IT_GL7);
|
||||
int i = 0;
|
||||
uint16_t pulse_ln = 0;
|
||||
uint16_t previous_pulse_ln = 0;
|
||||
uint16_t minimum_pulse_ln = 60000;
|
||||
uint16_t maximum_pulse_ln = 0;
|
||||
|
||||
// checking if timer overflowed (raised an iterrupt)
|
||||
if (analog_anemometer_timer_has_been_fired == 1) {
|
||||
rte_wx_windspeed_pulses = 1;
|
||||
|
||||
// reseting array to default values
|
||||
for (i = 0; i < ANALOG_ANEMOMETER_SPEED_PULSES_N; i++)
|
||||
analog_anemometer_windspeed_pulses_time[i] = 0;
|
||||
|
||||
DMA_Init(DMA1_Channel7, &DMA_InitStruct);
|
||||
DMA1_Channel7->CCR |= DMA_CCR7_EN;
|
||||
DMA1_Channel7->CCR |= DMA_CCR7_TCIE;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// calculating pulses duration time
|
||||
for (i = 0; i < ANALOG_ANEMOMETER_SPEED_PULSES_N - 1; i++) {
|
||||
pulse_ln = analog_anemometer_windspeed_pulses_time[i + 1] -
|
||||
analog_anemometer_windspeed_pulses_time[i];
|
||||
|
||||
analog_anemometer_pulses_durations[i] = pulse_ln;
|
||||
}
|
||||
|
||||
// debouncing captured times
|
||||
for (i = 0; i < ANALOG_ANEMOMETER_SPEED_PULSES_N; i++) {
|
||||
if (analog_anemometer_pulses_durations[i] < MINUM_PULSE_LN)
|
||||
analog_anemometer_pulses_durations[i] = 0;
|
||||
}
|
||||
|
||||
// limiting slew rate
|
||||
for (i = 1; i < ANALOG_ANEMOMETER_SPEED_PULSES_N; i++) {
|
||||
previous_pulse_ln = analog_anemometer_pulses_durations[i - 1];
|
||||
pulse_ln = analog_anemometer_pulses_durations[i];
|
||||
|
||||
// skipping pulses erased by debouncing
|
||||
if (pulse_ln == 0 || previous_pulse_ln == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int32_t diff = pulse_ln - previous_pulse_ln;
|
||||
|
||||
// if current pulse is much longer than previous
|
||||
if ( diff > MAXIMUM_PULSE_SLEW_RATE ) {
|
||||
analog_anemometer_pulses_durations[i] = previous_pulse_ln + MAXIMUM_PULSE_SLEW_RATE;
|
||||
}
|
||||
// if previous pulse is much longer than current
|
||||
else {
|
||||
analog_anemometer_pulses_durations[i - 1] = pulse_ln + MAXIMUM_PULSE_SLEW_RATE;
|
||||
}
|
||||
}
|
||||
|
||||
// find maximum and minimum values within pulses duration
|
||||
for (i = 0; i < ANALOG_ANEMOMETER_SPEED_PULSES_N; i++) {
|
||||
|
||||
pulse_ln = analog_anemometer_pulses_durations[i];
|
||||
|
||||
// skipping pulses erased by debouncing
|
||||
if (pulse_ln == 0)
|
||||
continue;
|
||||
|
||||
// find maximum and minimum values within pulses duration
|
||||
if (pulse_ln < minimum_pulse_ln)
|
||||
minimum_pulse_ln = pulse_ln;
|
||||
|
||||
if (pulse_ln > maximum_pulse_ln)
|
||||
maximum_pulse_ln = pulse_ln;
|
||||
|
||||
}
|
||||
|
||||
// calculating the target pulse duration
|
||||
rte_wx_windspeed_pulses = (uint16_t)((maximum_pulse_ln + minimum_pulse_ln) / 2);
|
||||
|
||||
// resetting the timer
|
||||
analog_anemometer_timer_has_been_fired = 0;
|
||||
|
||||
for (i = 0; i < ANALOG_ANEMOMETER_SPEED_PULSES_N; i++)
|
||||
analog_anemometer_windspeed_pulses_time[i] = 0;
|
||||
|
||||
for (i = 0; i < ANALOG_ANEMOMETER_SPEED_PULSES_N; i++)
|
||||
analog_anemometer_pulses_durations[i] = 0;
|
||||
|
||||
DMA_Init(DMA1_Channel7, &DMA_InitStruct);
|
||||
DMA1_Channel7->CCR |= DMA_CCR7_EN;
|
||||
DMA1_Channel7->CCR |= DMA_CCR7_TCIE;
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue