AnsweredAssumed Answered

STMF4 Timer external clock mode 1

Question asked by francesco on Dec 15, 2016
Latest reply on Dec 15, 2016 by S. Marsanne

Hi everyone,

 

i want to count the positive edges of a pulsesignal on PA6 using a Timer with external clock.

To test the functionality i generate the signal and outputport is PA8. PA8 is linked with PA6.

 

After several tries it still doesn't count and i don't know why anymore.

 

Thanks if anyone knows a hint

 

 Here the most important parts of the code:

 

 

GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;          //Signal assigned to AF2 TIM3

 

GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

__TIM3_CLK_ENABLE();

hTim3.Instance = TIM3;
hTim3.Init.Prescaler = 40;
hTim3.Init.CounterMode = TIM_COUNTERMODE_UP;
hTim3.Init.Period = 700;
hTim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
hTim3.Init.RepetitionCounter = 0;

HAL_TIM_Base_Init(&hTim3);

TIM_ClockConfigTypeDef TimClock;

TimClock.ClockSource = TIM_CLOCKSOURCE_TI1; //ETRMODE1 oder TI1
TimClock.ClockPolarity = TIM_CLOCKPOLARITY_RISING; //polarity of TIx clock source
TimClock.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;

HAL_TIM_ConfigClockSource(&hTim3,&TimClock);

 

TIM_SlaveConfigTypeDef SlaveConfig;

SlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1; //External Source als Clock
SlaveConfig.InputTrigger = TIM_TS_TI1FP1; //
SlaveConfig.TriggerFilter = 0; //No filter needed
SlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING; //Counting on rising edge
SlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1; //No Prescaler

HAL_TIM_SlaveConfigSynchronization(&hTim3, &SlaveConfig);

HAL_TIM_Base_Start(&hTim3);

 

while (1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); 
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); 
HAL_Delay(50);
iz++;
ic = __HAL_TIM_GET_COUNTER(&hTim3);
if (ic==50){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
}
}

Outcomes