2009-07-09 02:01 AM
pwm on timer4 does not work ?!?!?
2011-05-17 03:17 AM
Hello everyone I have sat trying to get pwm to work on timer4 channel 1, GPIOB6(have tried with timer 3 also). But the pin is just dead, (i can toggle the pin in gpio mode).
Code:
<BR>void main(void) <BR>{ <BR>GPIO_InitTypeDef GPIO_InitStructure; <BR>TIM_TimeBaseInitTypeDef TIM4_TimeBaseInitStruct; <BR>TIM_OCInitTypeDef pwm4; <BR> RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); <BR> RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4,ENABLE); <BR> <BR> <BR> TIM4_TimeBaseInitStruct.TIM_Prescaler = 720; // 10us resolution <BR> TIM4_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; <BR> TIM4_TimeBaseInitStruct.TIM_Period = 20000; // 200 ms <BR> TIM4_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; <BR> <BR> TIM_TimeBaseInit(TIM4,&TIM4_TimeBaseInitStruct); <BR> TIM_ClearFlag(TIM4, TIM_FLAG_Update); <BR> <BR> pwm4.TIM_OCMode=TIM_OCMode_PWM1; <BR> pwm4.TIM_Channel=TIM_Channel_1; <BR> pwm4.TIM_Pulse=TIM_OPMode_Single; <BR> pwm4.TIM_OCPolarity=TIM_OCPolarity_High; <BR> TIM_OCInit(TIM4,&pwm4); <BR> <BR> TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); <BR> TIM_ARRPreloadConfig(TIM4,ENABLE); <BR> <BR> TIM_SetCounter(TIM4,0); <BR> TIM_SetAutoreload(TIM4, 0x00000FFF); <BR> TIM_SetCompare1(TIM4, 0x000000FF); <BR> TIM_ClearITPendingBit(TIM4,TIM_FLAG_Update); <BR> TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); <BR> TIM_PrescalerConfig(TIM4,720,TIM_PSCReloadMode_Update); <BR> <BR> TIM_Cmd(TIM4,ENABLE); <BR>//while <BR>.. <BR>. <BR>} <BR>
[ This message was edited by: 3tnica on 07-01-2009 16:07 ]2011-05-17 03:17 AM
Have tried to get pwm to work on timer1 aswell but its stonedead aswell, I have tried to set the channels to both out and AF configuration, but i guess it should be AF. Does anyone have any idea what could be wrong. Help would be highly appreciated.
Best Regards2011-05-17 03:17 AM
Hi
This code initalize a 1kHz 25% PWM at timer4 channel3 // definition #define TIMER4_PERIODE ( 0x8CA0 ) /* 1ms -> 1kHz */ #define TIMER4_CH3_COMPARE ( 0x8CA0 / 4 ) /* 25% PWM */ void Timer4Init( void ) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; // port B clock enable already done at ''peri.c'' // RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); // configure timer4 channel3 as alternate function push-pull for PWM output GPIO_InitStructure.GPIO_Pin = PB_DO_FAN_OUT; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init( GPIOB, &GPIO_InitStructure ); // enable TIM4 clock RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4, ENABLE ); // TIM4 configuration TIM_TimeBaseStructure.TIM_Period = TIMER4_PERIODE; // overlow get detected at reload value of timer TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 36MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM4, &TIM_TimeBaseStructure ); // PWM1 mode configuration for channel13 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = TIMER4_CH3_COMPARE; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC3Init( TIM4, &TIM_OCInitStructure ); // enable capture compare unit of channel 3 TIM_OC3PreloadConfig( TIM4, TIM_OCPreload_Enable ); // enable reload mode TIM_ARRPreloadConfig( TIM4, ENABLE ); // TIM4 enable counter TIM_Cmd( TIM4, ENABLE ); }[ This message was edited by: loeffel on 08-01-2009 10:51 ]2011-05-17 03:17 AM
Hello,
added these lines to ur code and got it working, also updated my library saw that it was from 2006 and newest was 2008 =/ TIM_SetAutoreload(TIM4, 0x00000FFF); //New tim CC 1 value TIM_SetCompare1(TIM4, 0x00006FF); Thankyou for the help2011-05-17 03:17 AM
Trying ur example and can not get it to work.
Does alternate function on PB_DO_FAN_OUT mean that tim4_ch3 is used instead of as gpio? Should also say that i am using the latest libraries from ST if that matters. Best regards2011-05-17 03:17 AM
Hi
PB_DO_FAN_OUT stands for #define PB_DO_FAN_OUT GPIO_Pin_8 I use A STM32F101VB at this MCU the Timer4 Channel3 AF Output is at PortB Pin8. At the datasheet of your MCU you will find the port and pin you have to use.2011-05-17 03:17 AM
i am trying to use TIM3 Channel 1 as PWM from PC6 as AF; but the brigthness doesnt change. what am i doing wrong?? pls help...
here is the code: RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM3 , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //this pin functions as AF; when remapping GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); /*GPIOC Configuration: TIM3 channel 1 */ /* make sure that C port is opened in RCC*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); TIM_Cmd(TIM3, DISABLE); TIM_ARRPreloadConfig(TIM3, ENABLE); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 999; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; TIM_OCInitStructure.TIM_Pulse = 500; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInit(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); TIM_SetCompare1(TIM3,0x10);