cancel
Showing results for 
Search instead for 
Did you mean: 

Config TIMx for external clock mode 1

hamed_solar
Associate II
Posted on June 29, 2016 at 11:34

Hi

I want to config TIM3 channel 2 for input pulses and generate pulse with the same frequency at PC0 of STM32F Its my code. Is it true?

void TIM3CH1_Configuration(void) 
{ 
/* TIM3 configuration: Input Capture mode ------------------------
The external signal is connected to TIM3 CH2 pin (PA.07), 
The Rising edge is used as active edge,
------------------------------------------------------------ */
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure; 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 
TIM_DeInit(TIM3); 
TIM_BaseInitStructure.TIM_Period = 1; 
TIM_BaseInitStructure.TIM_Prescaler = 0; 
TIM_BaseInitStructure.TIM_ClockDivision = 0; 
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; 
TIM_BaseInitStructure.TIM_RepetitionCounter = 0; 
TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure); 
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
/* Select the TIM3 Input Trigger: TI1FP1 */
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);// TIM_TS_TI2FP2==>PA7 - TIM_TS_TI1FP1==>PA6
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
//TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_External1);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
// /////////////////NVIC/////////////////////////////////
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn ; 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure); 
// /////////////////////////////////////////////////////// 
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{ 
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
GPIOC->BSRR = GPIO_Pin_0;
GPIOC->BRR = GPIO_Pin_0;
}
} 

Regards
7 REPLIES 7
troy1818
Senior
Posted on June 29, 2016 at 12:38

Hi,

There are so many code examples for what you are trying to do. Please check them.

On a side note I would not place:

TIM_DeInit(TIM3);  

After:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

And you must also configure the output pin of course and its corresponding peripheral clock (port).

hamed_solar
Associate II
Posted on June 29, 2016 at 13:40

Hi,

I was configed PA7 as input trigger and PC0 as output and enabled the clock of both.

Please give me the link of example for activating TIMx as input trigger on CHx.

Regards

Walid FTITI_O
Senior II
Posted on June 29, 2016 at 14:00

Hi shams.hamed,

Timer examples in STM32Cube library package relevant to the device that you are using help you to build you target project. Check both ''TIM_InputCapture'' and ''TIM_TimerBase'' examples in

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef1.html

at this path: STM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\TIM

A I have understood your need, you can identify the frequency of input signal and program you ouput with the same frequency value.

-Hannibal-

Posted on June 29, 2016 at 14:02

Assuming that I'm not going to build, test or evaluate incomplete code fragments, what are you currently observing with the provided code, and at what frequencies?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
slimen
Senior
Posted on June 29, 2016 at 14:02

Hi,

You can refer to the example under :

STM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\TIM\TIM_InputCapture

This example can help you as an implementation example to configure TIM peripheral

and get inspiration to achieve you goal.

Regards

Posted on June 29, 2016 at 14:18

The OP is not using HAL, examples exist in the SPL and on the forum.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hamed_solar
Associate II
Posted on June 29, 2016 at 16:20

Thanks all

Finally I can config TIM3_CH2 as input trigger and I create output pulse with the same frequency as input pulse. It is the correct code.

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
/* Select the TIM3 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_External1);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM3, ENABLE);
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_Trigger, ENABLE);

Its the Interrupt handler that make pulses.

void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Trigger) != RESET)
{ 
/* Clear TIM3 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM3, TIM_IT_Trigger);
GPIOC->BSRR = GPIO_Pin_0;
GPIOC->BRR = GPIO_Pin_0;
}
}

Now, I want to trigger ADC with this input pulses. But without any interrupt handler. How can I do this? Regards