2018-08-03 08:04 PM
Hello everyone.
I'm studying in stm32f40g eval and using timer in one pulse mode.
First I'm use the example code TIM_OnePulse and modify it. I'm add the One pulse OUTPUT in function TIM_Config and One pulse EXTERNAL TRIGGER in function TIM_T2IConfig. I want to see the pulse output in Led4 (pin PC7), TIM3_CH2 and external trigger I use is rising edge in button wakeup, TIM5_CH1.
After modify, I download code into the kit but it doesn't work.
Please help me with this problem. Thank you.
Here is my code.
#include "main.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t uhPrescalerValue = 0;
static void TIM_Config(void);
static void TIM_T2IConfig(void);
int main(void)
{
TIM_Config();
TIM_T2IConfig();
uhPrescalerValue = (uint16_t)((SystemCoreClock / 2) / 42000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = uhPrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // enable pwm mode 2 by writing OC1M = 111 in the TIMx_CCMR1 res
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // compare enable
TIM_OCInitStructure.TIM_Pulse = 16383; // compare value
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // output compare state high
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
/* TIM4 configuration in Input Capture Mode */
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // TI2 rising edge is detected
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // capture detected each edge
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM5, &TIM_ICInitStructure);
/* One Pulse Mode selection */
TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Repetitive);
TIM_SelectInputTrigger(TIM5, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM5, TIM_SlaveMode_External1);
// TIM_Cmd(TIM3,ENABLE);
while (1)
{
}
}
static void TIM_Config(void) // ONE PULSE OUTPUT
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC->APB1ENR=RCC_APB1ENR_TIM3EN;
RCC->AHB1ENR=RCC_AHB1ENR_GPIOCEN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
}
static void TIM_T2IConfig(void) // EXTERNAL TRIGGER
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC->AHB1ENR=RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR=RCC_APB1ENR_TIM5EN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5);
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{}
}
#endif
2018-08-08 01:51 PM
.
2018-08-14 06:03 AM
Hi @phuocnguyenhuu96 ,
Could you please precise for which reason you're using two timers ?
The program is confusing: For example you're configuring channel 2 for timer 5 while in your description you mentioned that timer 5 channel 1 will be used. Please check your shared code.
Khouloud.