Question
STM32F4 Discover Pulse Counting
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=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=6201 Platform: STM32F407VGvoid 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'') ;
}
}
}