cancel
Showing results for 
Search instead for 
Did you mean: 

STMF4 Timer external clock mode 1

francesco b
Associate
Posted on December 15, 2016 at 10:54

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);

}

}
2 REPLIES 2
Seb
ST Employee
Posted on December 15, 2016 at 12:15

Have little experience in using a timer with external clock this way as I like to use peripheral for various usages at same time. Until someone gives the right way to do it, 2 alternative ways:

If the frequency of the rising edge is reasonable, use a TIMER with internal clock reference, then inject 1 to 4 signals to the CC channels of the timer. The timer will 'input capture' when a rise or fall edge occured, and using the corresponding interrupt, you can increase a counter (a global variable). This way, you can also check the duty cycle, compare between up to 4 signals, generate delayed pulses using the 'output compare' mode. (this can be fun to use an op-amp with dynamic switches to run an integrator for example).

Another alternative way is to use EXTI (this is the simplest way).

Good luck!

Posted on December 15, 2016 at 13:49

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

I don't Cube: is this OK for AF?

JW