AnsweredAssumed Answered

how can i use pwm in cmsis

Question asked by hossein hosseini on Mar 26, 2018
Latest reply on Mar 26, 2018 by Clive One

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.

 

 

#include "stm32f10x.h"
#include "stm32f10x_gpio.h" // for Enable LCD
#include "stm32f10x_rcc.h" //for Enable LCD
#include "stm32f10x_tim.h"
#include "LCDLib.h"
#include "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 );


}

 

Outcomes