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
}
Previous article:STM32l151 low power chip serial communication
Next article:STM32L0 low power design 4: How to generate 1 second clock of RTC module
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- How to design high voltage power supply test tooling?
- Negative ion generator export certification
- Metro promotes RFID technology standardization in retail industry
- What do the common codes in RFID mean?
- Help
- 【RT-Thread Reading Notes】——The harder you work, the luckier you will be
- DSP2812 FIFO usage
- Getting Started with I2C on MSP MCUs
- Question sharing: What is the difference between AFE and battery monitor in battery management chip?
- Qorvo's Response to WiFi 6