#include "pwm.h"
//蜂鸣器初始化
void pwm_init()
{
/********RCC章节初始化**********/
//1.使能GPIOB控制器
(RCC->MP_AHB4ENSETR)|=(0X1<<1);
//2.使能TIM4定时器
(RCC->MP_APB1ENSETR)|=(0X1<<1);
/********GPIO章节初始化*********/
//1.设置PB6引脚为复用功能模式
(GPIOB->MODER)&=(~(0X3<<12));
(GPIOB->MODER)|=(0X2<<12);
//2.设置PB6引脚的复用功能为TIM4_CH1
(GPIOB->AFRL)|=(0X2<<24);
/********TIM4章节初始化**********/
//1.系统提供的时钟源为209MHz,设置分频器的值为209-1
TIM4->PSC=208;
//2.产生一个方波,方波频率1000Hz,设置自动重载计数器值1000
TIM1->ARR&=(~(0XFFFF));
TIM4->ARR=1000;
//3.设置方波占空比,设置比较捕获寄存器值300
TIM4->CCR1=300;
//4.设置TIM4_CH1通道为PWM模式
(TIM4->CCMR1)|=(0X6<<4);
//5.设置TIM4_CH1通道捕获比较预加载使能
(TIM4->CCMR1)|=(0X1<<3);
//6.设置TIM4_CH1通道为输出模式
(TIM4->CCMR1)&=(~0X3);
//7.设置TIM4_CH1通道为输出极性
(TIM4->CCER)|=(0x1<<3);
//8.设置TIM4_CH1通道输出极性起始状态为高电平
(TIM4->CCER)&=(~(0x1<<1));
//9.设置TIM4_CH1捕获比较输出使能
(TIM4->CCER)|=(0x1);
//10.设置TIM4_CH1通道自动重载预加载使能
(TIM4->CR1)|=(0x1<<7);
//11.设置TIM4_CH1通道计数器采用边沿对齐方式
(TIM4->CR1)&=(~(0x3<<5));
//12.设置TIM4_CH1通道采用递减方式进行计数
(TIM4->CR1)|=(0x1<<4);
//13.设置TIM4_CH1通道计数器使能
(TIM4->CR1)|=(0x1);
}
//风扇初始化
void fan_init()
{
/********RCC章节初始化**********/
//1.使能GPIOE控制器
(RCC->MP_AHB4ENSETR)|=(0X1<<4);
//2.使能TIM1定时器
(RCC->MP_APB2ENSETR)|=(0X1);
/********GPIO章节初始化*********/
//1.设置PE9引脚为复用功能模式
(GPIOE->MODER)&=(~(0X3<<18));
(GPIOE->MODER)|=(0X2<<18);
//2.设置PE9引脚的复用功能为TIM1_CH1
(GPIOE->AFRL)&=(~(0XF<<4));
(GPIOE->AFRL)|=(0X1<<4);
/********TIM1章节初始化**********/
//1.系统提供的时钟源为209MHz,设置分频器的值为209-1
TIM1->PSC=208;
//2.产生一个方波,方波频率1000Hz,设置自动重载计数器值1000
TIM1->ARR=1000;
//3.设置方波占空比,设置比较捕获寄存器值300
TIM1->CCR1=300;
//4.设置TIM4_CH1通道为PWM模式
(TIM1->CCMR1)&=(~(0XF<<4));
(TIM1->CCMR1)|=(0X6<<4);
//5.设置TIM4_CH1通道捕获比较预加载使能
(TIM1->CCMR1)|=(0X1<<3);
//6.设置TIM4_CH1通道为输出模式
(TIM1->CCMR1)&=(~0X3);
//7.设置TIM4_CH1通道为输出极性
(TIM1->CCER)|=(0x1<<3);
//8.设置TIM4_CH1通道输出极性起始状态为高电平
(TIM1->CCER)&=(~(0x1<<1));
//9.设置TIM4_CH1捕获比较输出使能
(TIM1->CCER)|=(0x1<<0);
//10.设置TIM4_CH1通道自动重载预加载使能
(TIM1->CR1)|=(0x1<<7);
//11.设置TIM4_CH1通道计数器采用边沿对齐方式
(TIM1->CR1)&=(~(0x3<<5));
//12.设置TIM4_CH1通道采用递减方式进行计数
(TIM1->CR1)|=(0x1<<4);
//13.设置TIM4_CH1通道计数器使能
(TIM1->CR1)|=(0x1<<0);
//14.输出使能
(TIM1->BDTR)|=(0X1<<15);
}
//马达初始化
void monitor_init()
{
/********RCC章节初始化**********/
//1.使能GPIOF控制器
(RCC->MP_AHB4ENSETR)|=(0X1<<5);
//2.使能TIM16定时器
(RCC->MP_APB2ENSETR)|=(0X1<<3);
/********GPIO章节初始化*********/
//1.设置PF6引脚为复用功能模式
(GPIOB->MODER)&=(~(0X3<<12));
(GPIOB->MODER)|=(0X2<<12);
//2.设置PF6引脚的复用功能为TIM16_CH1
(GPIOB->AFRL)&=(~(0XF<<24));
(GPIOB->AFRL)|=(0X2<<24);
/********TIM16章节初始化**********/
//1.系统提供的时钟源为209MHz,设置分频器的值为209-1
TIM16->PSC=208;
//2.产生一个方波,方波频率1000Hz,设置自动重载计数器值1000
TIM16->ARR=1000;
//3.设置方波占空比,设置比较捕获寄存器值300
TIM16->CCR1=300;
//4.设置TIM4_CH1通道为PWM模式
(TIM16->CCMR1)&=(~(0XF<<4));
(TIM16->CCMR1)|=(0X6<<4);
//5.设置TIM4_CH1通道捕获比较预加载使能
(TIM16->CCMR1)|=(0X1<<3);
//6.设置TIM4_CH1通道为输出模式
(TIM16->CCMR1)&=(~0X3);
//7.设置TIM4_CH1通道为输出极性
(TIM16->CCER)|=(0x1<<3);
//8.设置TIM4_CH1通道输出极性起始状态为高电平
(TIM16->CCER)&=(~(0x1<<1));
//9.设置TIM4_CH1捕获比较输出使能
(TIM16->CCER)|=(0x1<<0);
//10.设置TIM4_CH1通道自动重载预加载使能
(TIM16->CR1)|=(0x1<<7);
//11.设置TIM4_CH1通道计数器采用边沿对齐方式
(TIM16->CR1)&=(~(0x3<<5));
//12.设置TIM4_CH1通道采用递减方式进行计数
(TIM16->CR1)|=(0x1<<4);
//13.设置TIM4_CH1通道计数器使能
(TIM16->CR1)|=(0x1<<0);
//14.输出使能
(TIM16->BDTR)|=(0X1<<15);
}
22071.10.23
该代码段展示了如何对STM32的PWM进行初始化配置,包括蜂鸣器、风扇和马达的初始化。涉及的定时器有TIM4、TIM1和TIM16,通过设置GPIO、RCC和定时器寄存器来实现PWM输出,以控制不同设备的工作状态。
摘要由CSDN通过智能技术生成