STM32L151 PWM output example

Publisher:SparklingMoonLatest update time:2018-09-10 Source: eefocusKeywords:STM32L151 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I recently did a project that required me to use PWM to control the speed of a 4-wire fan. After debugging, I recorded it.



typedef struct {

u16 pwm_change_flag;

u16 pwm_value;

}Pwm_Fan_t;

 

 

Pwm_Fan_t Pwm_Fan;

 

void Pwm_Param_Init(void)

{

Pwm_Fan.pwm_change_flag=0;

Pwm_Fan.pwm_value=80;

}

 

 

//PD14->TIM4_CH3

void PWM_GPIO_Config(void)  

  GPIO_InitTypeDef GPIO_InitStructure; 

 

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);

 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; 

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Multiplexed push-pull output 

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_40MHz; 

  GPIO_Init(GPIOD, &GPIO_InitStructure); 

}

 

 

/****************************************************** *************** 

August 14, 2017 14:37:57: The CPU fan is modulated using 25Khz pwm

 *************************************************** *************/  

void TIM4_Config(u16 pwm)

{  

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  

TIM_OCInitTypeDef TIM_OCInitStructure;  

/* PWM signal level jump value*/     

u16 CCR3;  

 

if(pwm>160-1)

pwm=160-1;

CCR3=pwm;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);  

GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4); //For L series, don't miss this sentence

TIM_Cmd(TIM4, DISABLE);                          

/* Time base configuration */                                            

TIM_TimeBaseStructure.TIM_Period =160 - 1;//0xEA5F;  

TIM_TimeBaseStructure.TIM_Prescaler = 8-1; //Set prescaler: prescaler = 2, which is 72/3 = 24MHz  

TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Set the clock division factor: no division  

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Upward counting overflow mode  

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);  

 

/* PWM1 Mode configuration: Channel3 */  

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Configure to PWM mode 1  

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                

TIM_OCInitStructure.TIM_Pulse = CCR3; //Set the jump value. When the counter counts to this value, the level jumps.  

TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low; //When the timer count value is less than CCR1, it is low level  

TIM_OC3Init(TIM4, &TIM_OCInitStructure); //Enable channel 3     

TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);  

TIM_ARRPreloadConfig(TIM4, ENABLE); //Enable TIM4 reload register ARR  

/* TIM4 enable counter */  

TIM_Cmd(TIM4, ENABLE); //Enable TIM4   


Keywords:STM32L151 Reference address:STM32L151 PWM output example

Previous article:STM32l151 low power chip serial communication
Next article:STM32L0 low power design 4: How to generate 1 second clock of RTC module

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号