cancel
Showing results for 
Search instead for 
Did you mean: 

PROBLEM: STM32F4 Discovery Counting External Pulses by Firmware

waheed901
Associate II
Posted on August 14, 2015 at 03:43

I am trying to use TIM4 in  external counting mode, I have managed to configure it in external counting mode, but the count I am getting in watch 1 window is random. I am using ground pin of board to give input to PB7 and I expect it should give me one increment in the count when I remove it. but it is not so, can anyone tell me what wrong in my code. (Similar problem was posted in some other thread but could not find exact solution) 

I want to get count of rising pulses from external signal: 

My Program is: 

Main.c

/******************************************** */ 

/* Includes ------------------------------------------------------------------*/

#include ''stm32f4_discovery.h''

TIM_ICInitTypeDef  TIM_ICInitStructure;

uint32_t aa = 0;

/* Private function prototypes -----------------------------------------------*/

void TIM_Config(void);

/* Private functions ---------------------------------------------------------*/

int main(void)

{

  /*!< At this stage the microcontroller clock setting is already configured, 

       this is done through SystemInit() function which is called from startup

       file (startup_stm32f4xx.s) before to branch to application main.

       To reconfigure the default setting of SystemInit() function, refer to

       system_stm32f4xx.c file

     */

      

  /* TIM Configuration */

  TIM_Config();

  while (1)

{

aa=TIM4->CNT;

}

}

/**

  * @brief  Configure the TIM4 Pins.

  * @param  None

  * @retval None

  */

void TIM_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  /* TIM4 clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  /* GPIOB clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  /* TIM4 chennel2 configuration : PB.07 */

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Connect TIM pin to AF2 */

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);

TIM_TimeBaseStructure.TIM_Period = 65535;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  TIM_TIxExternalClockConfig(TIM4, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);

  TIM_Cmd(TIM4, ENABLE);

}

#ifdef  USE_FULL_ASSERT

/**

  * @brief  Reports the name of the source file and the source line number

  *         where the assert_param error has occurred.

  * @param  file: pointer to the source file name

  * @param  line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* User can add his own implementation to report the file name and line number,

     ex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

  while (1)

  {}

}

#endif

/**

  * @}

  */ 

/**

  * @}

  */ 

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

3 REPLIES 3
Posted on August 14, 2015 at 04:40

I guess I'd troubleshoot this by using another timer to generate a 50 Hz signal, and then jumper that to PB7, and use a 1 Hz ticker to periodically print out the TIM4->CNT value.

The code doesn't look objectionable, and PB7 should be free.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
waheed901
Associate II
Posted on August 14, 2015 at 09:06

Thanks Clive for the reply,

I will try to do that way, but being just a starter to the boards programming may face some difficulties.

Thanks anyway

waheed901
Associate II
Posted on August 17, 2015 at 04:01

Thanks clive,

The program is working fine,

I used function generator with 0.1 Hz frequency and found that the count is increasing after each 10 seconds by 1. 

Thanks indeed for the help and guidance.