cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discover Pulse Counting

Traud.Jason
Associate II
Posted on November 11, 2015 at 22:54

My end goal is that I want to count pulses that occur on one of the inputs on the STM32F4 Discovery dev board and I'm having issues.

I found this post that seems to be doing what I want, but it's not quite working. I have data spitting out of UART4 but it's non-nonsensical. Perhaps it's just counting up and disregarding the voltage on PD12? https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https%3a%2f%2fmy%2est%2ecom%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Discovery%2fSTM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D... Platform: STM32F407VG

void TIM4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* GPIOD clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOA Configuration: TIM4 CH1 (PD12) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Input/Output controlled by peripheral
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // Button to ground expectation
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect TIM4 pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
// Time initialization
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Control with dead zone.
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //Counter direction
TIM_TimeBaseInitStructure.TIM_Prescaler = 84-1; //Timer clock = sysclock /(TIM_Prescaler+1) = 2M
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF;
// Period = (TIM counter clock / TIM output clock) - 1 = 40Hz
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM4,&TIM_ICInitStructure);
// Turn on the counter
TIM_Cmd(TIM4,ENABLE);
}

int main()
{
char str[120];
char ToABuffer[10]; // Debug prints
System_Setup();
UART4_Write_Byte(0xFF) ;
mainDelayTimer = 5000 ;
while(1){
//UART_Write_Byte(0xFF) ;
if (mainDelayTimer < 1) {
mainDelayTimer = 5000 ;
x_dot = TIM_GetCounter(TIM4);
itoa(ToABuffer, x_dot, 10) ;
UART4_Write_String(ToABuffer) ;
UART4_Write_String(''\r\n'') ;
}
}
}

3 REPLIES 3
Posted on November 12, 2015 at 01:18

You're not counting pulses, you are using input capture which time stamps the input vs the incrementing clock.

ie TIM4->CCR1 = TIM4->CNT; // at the event, the counter is ticking at 1MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Traud.Jason
Associate II
Posted on November 12, 2015 at 15:37

After adding in: 

TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI1, TIM_ICPolarity_Rising, 0);

It seems to be working properly. If this is the incorrect way of trying to count the number of posedge pulses seen on a pin, what would be the proper way of achieving this?

Posted on November 12, 2015 at 19:33

Well somewhat, but the initialization of the channel doesn't seem necessary, and you want to use a maximal count without the prescaler. The examples in the

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentview...

don't do that, except for a few later respondents with off-topic posts. May be you want to review the initial posts again, and use those as a framework?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..