AnsweredAssumed Answered

PROBLEM: STM32F4 External Pulse counting by using HAL Libraries

Question asked by W.A on Aug 13, 2015
Latest reply on Aug 14, 2015 by W.A
I am trying to use TIM1 in  external counting mode, but some how the count is changing even I have not connected any signal to PE9 (even not to any pin). can any one tell me whats going on with the code?

I am using STM32F4 Discovery with HAL libraries 

My Program is: 

Main.c

/********************************************
  * main.c 
  * 
  **************************  */


#include "main.h"
TIM_HandleTypeDef      TimHandle;
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

uint32_t               uwIC2Value1 = 0;
uint32_t               uwIC2Value2 = 0;
uint32_t               uwDiffCapture = 0;
uint16_t               uhCaptureIndex = 0;
uint32_t               uwFrequency = 0;
uint32_t               uwFrequency1 = 0;
uint32_t               uwFrequency2 = 0;
uint32_t               uwFrequency3 = 0;
uint32_t               uw = 0;
uint32_t               uw2 = 0;

static void SystemClock_Config(void);
void MX_TIM1_Init(void);
void MX_GPIO_Init(void);

int main(void)
{

   HAL_Init();
  SystemClock_Config();
  MX_TIM1_Init();
  MX_GPIO_Init();
 
  while (1)
  {
          if(uhCaptureIndex == 0)
    {
      /* Get the 1st Input Capture value */
      uw=TIM1->CNT;
      uhCaptureIndex = 1;
    }
    else if(uhCaptureIndex == 1)
    {
      /* Get the 2nd Input Capture value */
      uw2=TIM1->CNT;
           
          if (uw2 > uw)
      {
        uwDiffCapture = (uw2 - uw); 
      }
      else  /* (uwIC2Value2 <= uwIC2Value1) */
      {
        uwDiffCapture = ((0xFFFF - uw) + uw2); 
      }
               uw=uw2;
               uwFrequency=uwDiffCapture;
          }
     }
}

static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;

  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  /*RCC_OscInitStruct.PLL.PLLM = 25;*/
     RCC_OscInitStruct.PLL.PLLM = 8;
  /*RCC_OscInitStruct.PLL.PLLN = 360;*/
     RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2      clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}

void MX_TIM1_Init(void)
{
 
     /*##-1- Configure the TIM peripheral #########*/ 

  TimHandle.Instance = TIMx;
  TimHandle.Init.Period            = 0xFFFF;
  TimHandle.Init.Prescaler         = 0;
  TimHandle.Init.ClockDivision= TIM_CLOCKDIVISION_DIV1;
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  TimHandle.Init.RepetitionCounter = 0;
  HAL_TIM_Base_Init(&TimHandle);
     
  sClockSourceConfig.ClockSource=TIM_CLOCKSOURCE_ETRMODE2;
sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
sClockSourceConfig.ClockFilter = 0;
HAL_TIM_ConfigClockSource(&TimHandle, &sClockSourceConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &sMasterConfig);
 __TIM1_CLK_ENABLE();
    TIM1->CNT=0; 
  //  TIM1->CR1|=0x0001; 
     }  
void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef   GPIO_InitStruct;
 
  /*##-1- Enable peripherals and GPIO Clocks ####*/
  /* TIMx Peripheral clock enable */
  TIMx_CLK_ENABLE(); // TIM 1 clc enable
    
  /* Enable GPIO channels Clock */
  TIMx_CHANNEL_GPIO_PORT(); // Port E enable
  
  /* Configure  (TIMx_Channel) in Alternate function, push-pull and 100MHz speed */
  GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL2;  //GPIO_PIN_CHANNEL2 GPIO_PIN_11


      // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      // GPIO_InitStruct.Pull = GPIO_PULLUP;
  
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
     
     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF_TIMx;
  HAL_GPIO_Init(GPIO_PORT, &GPIO_InitStruct);


  /*##-2- Configure the NVIC for TIMx ############*/
  /* Sets the priority grouping field */
  HAL_NVIC_SetPriority(TIMx_IRQn, 0, 1);  //# TIMx_IRQn       TIM1_CC_IRQn
  
  /* Enable the TIM4 global Interrupt */
  HAL_NVIC_EnableIRQ(TIMx_IRQn);    // TIMx_IRQHandler        TIM1_CC_IRQHandler
}


The main.h


#ifndef __MAIN_H
#define __MAIN_H


/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "stm32f4_discovery.h"


/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* User can use this section to tailor TIMx instance used and associated 
   resources */
/* Definition for TIMx clock resources */
#define TIMx                           TIM1
#define TIMx_CLK_ENABLE()              __HAL_RCC_TIM1_CLK_ENABLE()


/* Definition for TIMx Pins */
#define TIMx_CHANNEL_GPIO_PORT()       __HAL_RCC_GPIOE_CLK_ENABLE()
#define GPIO_PORT                      GPIOE
#define GPIO_PIN_CHANNEL2              GPIO_PIN_11
#define GPIO_AF_TIMx                   GPIO_AF1_TIM1


/* Definition for TIMx's NVIC */
#define TIMx_IRQn                      TIM1_CC_IRQn
#define TIMx_IRQHandler                TIM1_CC_IRQHandler




/* Exported functions ------------------------------------------------------- */


#endif /* __MAIN_H */





Outcomes