cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with timer external clock STM32F303VC

uri_104
Associate
Posted on March 14, 2015 at 20:24

Hello everybody. I try configure the timer 3 with external clock, but I have a problem. When done a signal in PD2 (ETR), the timer don't increase. I think that problem is for any enable clock... I use HAL libraries with ST cube but the register I configure with manual form.

I use STM32F3 Discovery

This is my code.

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

#include ''stm32f3xx_hal.h''

/* Private variables ---------------------------------------------------------*/

TIM_HandleTypeDef htim3;

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

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void TIM3_Config(void);

int main(void)

{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  TIM3_Config();

  while (1)

  {

        GPIOA->ODR=TIM3->CNT;

  }

}

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                              |RCC_CLOCKTYPE_PCLK1;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);

  __SYSCFG_CLK_ENABLE();

}

void TIM3_Config(void)

{    

    __TIM3_CLK_ENABLE();

        TIM3->CR1=0x0001;

        TIM3->CR2=0x0000;

        TIM3->SMCR=0x5000;

        TIM3->DIER=0x0001;

        TIM3->EGR=0x0000;

        TIM3->CNT=0x0005;  

        TIM3->PSC=0x0000;

}

void MX_GPIO_Init(void)

{

  GPIO_InitTypeDef GPIO_InitStruct;

      __GPIOA_CLK_ENABLE();

    __GPIOD_CLK_ENABLE();

 

  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|

                                                GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|

                                                GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}
1 REPLY 1
uri_104
Associate
Posted on March 15, 2015 at 14:27

SOLVED

#include ''stm32f3xx_hal.h''
TIM_HandleTypeDef htim3;
void
SystemClock_Config(
void
);
static
void
MX_GPIO_Init(
void
);
static
void
MX_TIM3_Init(
void
);
int
main(
void
)
{
HAL_Init();
SystemClock_Config();
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
HAL_NVIC_SetPriority(RCC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RCC_IRQn);
MX_GPIO_Init();
MX_TIM3_Init();
while
(1)
{
GPIOA->ODR=TIM3->CNT;
}
}
void
SystemClock_Config(
void
)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
__SYSCFG_CLK_ENABLE();
}
void
MX_TIM3_Init(
void
)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 32; 
//umbral máximo
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim3);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
sClockSourceConfig.ClockFilter = 0;
HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
__TIM3_CLK_ENABLE();
TIM3->CNT=0; 
//valor inicio
TIM3->CR1|=0x0001; 
//timer 3 en ON. 
}
void
MX_GPIO_Init(
void
)
{
GPIO_InitTypeDef GPIO_InitStruct;
__GPIOA_CLK_ENABLE();
__GPIOD_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 
|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11 
|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}