cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Generating Problem

ali jounaki
Associate
Posted on November 10, 2017 at 09:52

hi

i have a problem in pwm.

i want to generate a pwm signal by nucleo stm32f103rb board,i use keil for the compiling,

and i don't get the pwm.

this is my code :

&sharpinclude <stm32f10x.h>

TIM_TimeBaseInitTypeDef TIM_structure;

TIM_OCInitTypeDef pwm_structure;

void hse_config(void);

void timer_config(void);

void pwm_config(void);

void rcc_config(void);

void gpio_config(void);

int main() {

hse_config();

rcc_config();

gpio_config();

timer_config();

pwm_config();

while(1){

}

}

void hse_config(void) {

RCC_HSEConfig(RCC_HSE_ON);

RCC_WaitForHSEStartUp();

RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY==RESET));

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

}

void timer_config(void) {

TIM_structure.TIM_Prescaler=71;

TIM_structure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_structure.TIM_Period=99;

TIM_structure.TIM_ClockDivision=0;

TIM_structure.TIM_RepetitionCounter=0;

TIM_TimeBaseInit(TIM2,&TIM_structure);

}

void pwm_config(void) {

pwm_structure.TIM_OCMode=TIM_OCMode_PWM1;

pwm_structure.TIM_OutputState=TIM_OutputState_Enable;

pwm_structure.TIM_Pulse=48;

pwm_structure.TIM_OCNPolarity=TIM_OCPolarity_High;

TIM_OC2Init(TIM2,&pwm_structure);

TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM2,ENABLE);

TIM_Cmd(TIM2,ENABLE);

}

void rcc_config(void){

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);

}

void gpio_config(void){

GPIO_InitTypeDef GPIO_structure;

GPIO_structure.GPIO_Pin=GPIO_Pin_3;

GPIO_structure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_structure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_structure);

GPIO_PinRemapConfig(GPIO_FullRemap_TIM2,ENABLE);

}

please help me in this case.

thank you.

#pwm #nucleo-f103rb #stm32f103
0 REPLIES 0