cancel
Showing results for 
Search instead for 
Did you mean: 

how can i use pwm in cmsis

hossein hosseini
Associate II
Posted on March 26, 2018 at 08:18

hi

i have tried a lot to config  pwm in stm32f103re with IAR  but i could not do that activity.

i put the code here to get help from you guys . i am really confused.

please tell me my problem in code , and where i am wrong.

thanks a lot.

&sharpinclude 'stm32f10x.h'

&sharpinclude 'stm32f10x_gpio.h' // for Enable LCD

&sharpinclude 'stm32f10x_rcc.h' //for Enable LCD

&sharpinclude 'stm32f10x_tim.h'

&sharpinclude 'LCDLib.h'

&sharpinclude 'delay.h'

void tim_init(void);

void GPIO_init(void);

int main()

{

lcd_init();

lcdPutString ('Believe Your Dreams.');

tim_init();

GPIO_init();

while(1){

}

}

void tim_init(){

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_TimeBaseStructure.TIM_Prescaler = 8;

TIM_TimeBaseStructure.TIM_Period = 500;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

TIM_Cmd(TIM3, ENABLE);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = 400;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC4Init(TIM3, &TIM_OCInitStructure);

TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

//GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM4);

}

void GPIO_init(void){

RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOC, ENABLE );

GPIO_InitTypeDef gpioAa;

gpioAa.GPIO_Mode = GPIO_Mode_AF_PP ;

gpioAa.GPIO_Pin = GPIO_Pin_9;

gpioAa.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init( GPIOC, &gpioAa );

}

#pwm
1 REPLY 1
Posted on March 26, 2018 at 14:54

Would probably do a more complete job of initializing the pin.

Also with PC9 at TIM3_CH4 you'd need to enable the AFIO clock and Remap TIM3 pin usage. Review pin remapping details in Reference Manual RM0008

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..