2014-06-05 05:06 PM
Hey ST,
I'm using STM32F4DISCOVERY board, and tried to write a counter for rising and falling edges using timer input capture mode. Couldn't capture anything though. Any ideas what's wrong would be appreciated.Also, does anyone know what TIM_ICFilter, GPIO_OType does?Thanks in advance.//Headers//PA5 -> TIM2#include ''stm32f4xx_dma.h''#include ''stm32f4xx_exti.h''#include ''stm32f4xx_gpio.h''#include ''stm32f4xx_rcc.h''#include ''stm32f4xx_tim.h''#include ''semihosting.h''//Prototypesvoid GPIO_Config(void);void TIM_IC_Config(void);//Global Variablesuint32_t count;int main(void){ GPIO_Config(); TIM_IC_Config(); while(1) { count = TIM_GetCapture1(TIM2); printf(''\nTIM: %d'',count); }}void GPIO_Config(void){ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //tim1? cannot unable? GPIO_InitTypeDef gpio; gpio.GPIO_Pin = GPIO_Pin_5; gpio.GPIO_Mode = GPIO_Mode_AF; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio.GPIO_Speed = GPIO_Speed_50MHz; gpio.GPIO_OType = GPIO_OType_PP; //What is this actually? GPIO_Init(GPIOA, &gpio); GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM2);}void TIM_IC_Config(void){ //TIM2 Channel 1 TIM_ICInitTypeDef tim; tim.TIM_Channel = TIM_Channel_1; tim.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //Both up and down edge tim.TIM_ICSelection = TIM_ICSelection_DirectTI; //??? direct vs indirect tim.TIM_ICPrescaler = TIM_ICPSC_DIV2; tim.TIM_ICFilter = 0x00; //? TIM_ICInit(TIM2,&tim);}2014-06-05 07:10 PM
TIM1 is on APB2, enabling a clock on APB1 for it won't help
PP - Push Pull Driver, transistors drive pin HIGH or LOW, in output mode Input Capture latches a time base count, a time base which you haven't initialized, or enabled. This would allow you to determine where edges are in the time domain, allowing the measurement for period and duty. If you want to COUNT external pulses, you need to configure the timer with an external clocking mode, and read the value from TIMx->CNT2014-06-06 08:58 AM
Hi Clive, thanks so much for the info. I'll try out the external clocking mode and test it out.
2014-06-06 10:37 AM
Hi Clive,
Thanks so much for the tip. I got the external edge counter working (below). i have another problem though: i'm trying to count rising edges above 0V as positive, while counting rising edges below 0V as negative (attached picture - rising edges in 0-3V range +1, rising edges in -3V to 0V range -1). Thanks, Randy //Headers //PA5 -> TIM2 #include ''stm32f4xx_dma.h'' #include ''stm32f4xx_exti.h'' #include ''stm32f4xx_gpio.h'' #include ''stm32f4xx_rcc.h'' #include ''stm32f4xx_tim.h'' #include ''semihosting.h'' //Prototypes void GPIO_Config(void); void TIM_IC_Config(void); //Global Variables uint32_t count; int main(void) { GPIO_Config(); TIM_IC_Config(); while(1) { count = TIM2->CNT; printf(''\nTIM: %d'',count); } } void GPIO_Config(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); GPIO_InitTypeDef gpio; gpio.GPIO_Pin = GPIO_Pin_5; gpio.GPIO_Mode = GPIO_Mode_AF; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio.GPIO_Speed = GPIO_Speed_50MHz; gpio.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOA, &gpio); GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM2); } void TIM_IC_Config(void) { //TIM2 Channel 1 TIM_TimeBaseInitTypeDef tim; tim.TIM_Period = 65535; tim.TIM_Prescaler = 0; tim.TIM_ClockDivision = 0; tim.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2,&tim); TIM_TIxExternalClockConfig(TIM2,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising,0); TIM_Cmd(TIM2,ENABLE); } ________________ Attachments : edge_counts.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HztN&d=%2Fa%2F0X0000000bQu%2Fo3tiekR6ueIM4RWvWi.55kXUUJhZtMpoHY2IQ15Msho&asPdf=false