cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F405rg pulse counter

robert239955_st
Associate II
Posted on March 18, 2015 at 12:33

Hi

I'm using a STM32F405RG  in a fan controller, I've two fans tacho outputs connected via opto-isolators to Pins, I wish to count the number of falling edges per second.

PA5 (Tim2 CH1 ext clock source mode 1, trigger on TI1FP1) all other Tim2 pins disabled

PB6 (Tim4 CH1 ext clock source mode 1, trigger on TI1FP1)  all other Tim4 pins disabled

both have interrupts enabled for Trigger event TIMx_DIER = 0x0040

on a falling edge as this is cleaner than rising edge, TIMx_SMCR = 0x8057

Timers are running in interrupt mode (HAL_TIM_Start_IT(&htimX))

A third timer reads the htimX.Instance->CNT once a second and returns a value in Hz.

The problem is that this value is unreliable, a 10Hz square wave input mimics an expected input. 

5Hz reads as 293.

1Hz reads as 1.

10Hz reads as 4698.

My understanding is that in ext Time mode 1, an interrupt is generated for each edge detected, 

I've tried using the filter setting to reduce the likelihood of multiple triggers per edge

but to no avail. Using a timer to disable the interrupt for 200uS after an edge also proved unfruitful.

Page 590 of the reference doc RM0090 describes the feature I'm trying to use.

Any help would be appreciated.

#stm32f405rg-pulse-counter-timx #stm32f405
1 REPLY 1
robert239955_st
Associate II
Posted on March 19, 2015 at 15:03


void

edgeCountSetup(TIM_HandleTypeDef *htim){

//setup timers for edge counting on falling edge

htim->Instance->CR1 &= ~0xFFF9;

//disable timer while writing changes


htim->Instance->CR2 &= 0xFF20;

// TI1 input, update event = TRGO

htim->Instance->CR2 |= 0x0020;

htim->Instance->DIER &=0x0040;

// disable other interupts

htim->Instance->DIER |=0x0040;

// set ext trigger interrupt


htim->Instance->SMCR &= 0x8050;

htim->Instance->SMCR |= 0x8057;

// falling adge, Filter Timer In 1, ext Clk Mode 1


htim->Instance->SR &= 0x0000;

// clear flags

htim->Instance->EGR &= 0x0000;

// clear events

htim->Instance->CCER &= 0x0000;

// disable capture compare

htim->Instance->CNT &=0x0000;

// reset count

htim->Instance->PSC &=0x0000;

// prescale 1:1

htim->Instance->ARR |=0xFFFFFFF0;

// counter is blocked if this is 0

htim->Instance->OR &= 0x0000;

// option reg cleared



htim->Instance->CR1 |= 0x0001;

//enable timer


}

This is used to configure timer 2 and timer 5, As said previously, the htim.Instance->CNT does respond to an incoming signal but not in a single increment per falling edge. The fall/rise time of my signal is about 20uS, Setting up 2 other timers in one shot mode to disable the trigger interrupt for 50uS has not cleaned up the results.