2015-07-09 01:13 PM
Hi, I am using the discovery board to try OPM on timer 2. I can not get it going with any effort on my code list below. Please help!!! It is important to our new product.
void GPIO_Configuration(void);int main(void){ /* System Clocks Configuration */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE); /* Configure the GPIO ports */ GPIO_Configuration(); /* TIM2 configuration: One Pulse mode ------------------------ The external signal is connected to TIM2_CH2 pin (PA.01), The Rising edge is used as active edge, The One Pulse signal is output on TIM2_CH1 pin (PA.00) The TIM_Pulse defines the delay value The (TIM_Period - TIM_Pulse) defines the One Pulse value. The TIM2CLK is fixed to 72 MHz, the Prescaler is 1, so the TIM2 counter clock is 36 MHz. The Autoreload value is 65535 (TIM2->ARR), so the maximum frequency value to trigger the TIM2 input is 500 Hz. The TIM_Pulse defines the delay value, the delay value is fixed to 455.08 us: delay = CCR1/TIM2 counter clock = 455.08 us. The (TIM_Period - TIM_Pulse) defines the One Pulse value, the pulse value is fixed to 1.365ms: One Pulse value = (TIM_Period - TIM_Pulse)/TIM2 counter clock = 1.365 ms. ------------------------------------------------------------ */ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 16383; //delay.. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC2Init(TIM2, &TIM_OCInitStructure); //PA1 output. /* TIM2 PWM2 Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 1; //No delay, immediate reacts to input. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC3Init(TIM2, &TIM_OCInitStructure); //PA2 output. /* TIM2 configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //PA0 trigger 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 = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger); //GPIO_SetBits(GPIOA,GPIO_Pin_1); while (1) { GPIO_SetBits(GPIOC,GPIO_Pin_8|GPIO_Pin_9); //Turn 2 leds on. }}///////////////////////////////////////////////////void GPIO_Configuration(void){ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //PC8,9 to be output pp. GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_2); GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_6); GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_6); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //PA2 to be alternative func. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 to be alternative func. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure);}2015-07-09 01:25 PM
P.S. I am making 2 pulses out of ch2 and 3 by trigger from one source, ch1. Is it possible?
2015-07-09 01:41 PM
You want to enable the Clock, not Reset the peripheral
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE); Really not sure if OPM is workable across multiple channels. I suspect if the Period setting was sufficient, PWM1/PWM2 modes on them might permit differing pulses. Can you draw a timing diagram of the input vs output signals you're looking to generate?2015-07-10 08:54 AM
Thanks for the hint Clive1. It is now running but only with Repetitive mode. I can probe A1/A2 and get pulses repetitively as I want. But I need it to be triggered by my signal instead of auto run.
Once I put it in Single mode, the 2 pins probed as all High no matter how I press the button on A0....2015-07-10 09:00 AM
Here is the code with Repetitive mode working:
TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //PA0 trigger with the button. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //connect CH1 to IC1. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Repetitive); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger); TIM_Cmd(TIM2, ENABLE);2015-07-10 11:05 AM
It works now!! 2 pulses out of 1 trigger with programmable timing I want.
I was doing wrong init to GPIOA which screwed up the AF scheme...