How to set PWM output with STM32 microcontroller

surroundings:

Host: XP

Development environment: MDK4.23

MCU: STM32F103CBT6

instruction:

Use the internal 8M crystal oscillator, multiply the frequency to 64M for the TIM3 timer, and generate a 640K, 50% square wave on PA6 (channel 1).

Source code:

Initialize the clock:

//Initialize the RCC clock

voidinit_rcc(void)

{

//Reset peripheral RCC register to default value

RCC_DeInit();

// Enable internal crystal oscillator

RCC_HSICmd(ENABLE);

//Wait for the job to stabilize

while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY)==RESET);

//LSI startup, provided to watchdog clock

RCC_LSICmd(ENABLE);//Open LSI

while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET);//Wait until LSI is stable

if(1)

{

//enable prefetch cache

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

//Set the number of FLASH memory delay clock cycles. (set to delay 2 clock cycles)

FLASH_SetLatency(FLASH_Latency_2);

//Set the AHB clock (HCLK) (set to = system clock at this time)

RCC_HCLKConfig(RCC_SYSCLK_Div1);

//Set the AHB clock (HCLK2) of SDIO (set to =HCLK at this time), 64M

RCC_PCLK2Config(RCC_HCLK_Div1);

//Set the APB1 peripheral clock (HCLK1) (set to =HCLK/2 at this time), 32M

RCC_PCLK1Config(RCC_HCLK_Div2);

//ADCCLK=PCLK2/8, 1M

RCC_ADCCLKConfig(RCC_PCLK2_Div8);

//The clock source of the PLL and the multiple of the frequency multiplier, here is set to 64MHz

RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);

//enable PLL

RCC_PLLCmd(ENABLE);

//Check if PLL is ready

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);

//Set PLL as system clock

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

//Check if the clock source of the system is PLL

while(RCC_GetSYSCLKSource() != 0x08);

}

}

Initialize the IO port and timer:

//Set IO port

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//set timer 3

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

//Reset TImer to default value

TIM_DeInit(TIM3);

//Use the internal clock to provide the clock source to TIM3

TIM_InternalClockConfig(TIM3);

timInitStruct.TIM_ClockDivision=TIM_CKD_DIV2;

timInitStruct.TIM_Prescaler=0;//Counting frequency is 64M

timInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//Count up

timInitStruct.TIM_RepetitionCounter=0;

timInitStruct.TIM_Period=100;//This value is actually TIMX-"ARR, you can reset it when the delay starts

TIM_TimeBaseInit(TIM3, &timInitStruct);

//set PWM output

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse=50;

TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;

TIM_OC1Init(TIM3, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

//ARR preload buffer enable

TIM_ARRPreloadConfig(TIM3, ENABLE);

// start the timer

TIM_Cmd(TIM3, ENABLE);

Output waveform:

Ego E cigarette

Suizhou simi intelligent technology development co., LTD , https://www.msmsmart.com