2013-10-04 11:36 PM
I'm an absolutely beginner in microcontrollers.
I want to create a PWM signal wave signal to drive main power switch at 100Khz TIM1CH1&TIM1CH1N of STM32F103VC. I change duty (CCR1_Val) but the duty stay same at 50%. Can anybody tell me what I'm doing wrong? Thank You Photo to Attachments vu16 CCR1_Val = 90 ; TIM_DeInit(TIM1); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 360 - 1 ; // 72 MHz / 360 = 200 kHz TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); /* Channel 1, 2,3 and 4 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ; TIM_OCMode_PWM1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCNIdleState_Set; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; // 25% TIM_OC1Init(TIM1,&TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); /* TIM1 counter enable */ TIM_Cmd(TIM1,ENABLE); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM1,ENABLE);2013-10-05 02:02 AM
Is this a copy/paste from source code which actually compiles?
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ; TIM_OCMode_PWM1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; JW2013-10-05 04:53 AM
TOGGLE mode would tend to do that as the Period is effectively doubled, and will be even, ie 50/50
If you want PWM mode then used that, and not TOGGLE mode2013-10-06 12:22 AM
thanks for the reply
waclawek yes2013-10-06 12:29 AM
many thanks for the reply clive1
yes , but witch TIM_OCMode_PWM1 & TIM_OCMode_PWM2 have same signal (photo 2). I need pwm dual signal same photo 1. Thank You ________________ Attachments : 1.PNG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0dt&d=%2Fa%2F0X0000000bbo%2FSqLP._E6kq1xuA7NtkxYMdLWY6ZE4qQ2qH2ZCjmN7EE&asPdf=false2.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0bj&d=%2Fa%2F0X0000000bbn%2FajgXrzZb3FuWFiP0j_1p76CBAHJUsFL.ai_OaAwSmTI&asPdf=false2013-10-06 01:43 AM
But that's not a negated signal, I think here you'd need to use two channels and modulate the pulse width between 0 (OFF) and the 25% on alternating channels at the Update interrupt. 100 KHz should be possible. Alternatively you could use two timers.
I seem to recall posting an elegant solution to a similar A B timing diagram before, but a quick search isn't pulling it up. I'll need to view my own source trees.2013-10-07 12:24 AM
Isn't this what dead-time insertion is to accomplish in the advanced timers (TIM1 and TIM8)? Refer to the TIMx_BDTR register and ''Complementary outputs and dead-time insertion'' chapter in manual.
JW2013-10-07 09:16 AM
Thank You JW
I am a first step in STM32 & is difficult for understand the switch register from documentation. I more efficient The Samples. Thanks again2013-10-07 09:19 AM
One might suppose, but I've never had much joy with the dead-time settings, so an example doing this would be welcome.
2013-10-07 09:27 AM
For what it's worth this was the other example I was thinking about.
// STM32 PWM 1 KHz (TIM1 CH1 PA.08, CH3 PA.10) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); // CH1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1); // CH3
}
/**************************************************************************************/
#define ONTIME 800 // 800 us, 80%
int State = 0;
void TIM1_UP_TIM10_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
if (State == 0)
{
TIM1->CCR1 = ONTIME; // On 80 or 90% typical
TIM1->CCR3 = 0; // Off
}
else
{
TIM1->CCR1 = 0; // Off
TIM1->CCR3 = ONTIME; // On 80 or 90% typical
}
State ^= 1;
}
}
/**************************************************************************************/
void TIM1_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 KHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Enable TIM1 Preload register on ARR */
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // ?? 1 / 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0; // Off
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
/* Output Compare PWM1 Mode configuration: Channel1 PA.08, Channel3 PA.10 */
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
/* TIM Interrupts enable */
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
}
/**************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM1 update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
TIM1_Configuration();
while(1); // Don't want to exit
}