//头文件
#ifndef __PWM_H__
#define __PWM_H__
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_tim.h"
/***********蜂鸣器初始化************/
void sp_pwm(void);
/***********风扇初始化*************/
void fan_pwm(void);
/***********马达初始化*************/
void motor_pwm(void);
#endif
//功能函数
#include"pwm.h"
/***********蜂鸣器PB6初始化************/
void sp_pwm()
{
/*---------RCC章节-----------*/
//GPIOB使能
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//TIM4使能
RCC->MP_APB1ENSETR |= (0x1 << 2);
/*----------GPIO章节----------*/
//设置PB6引脚为复用功能
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x2 << 12);
//设置PB6复用功能为TIM4_CH1
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x2 << 24);
/*----------TIM4章节----------*/
//设置分频
TIM4->PSC = 209;
//设置自动重载计数器值
TIM4->ARR = 1000;
//设置捕获计数器的值
TIM4->CCR1 = 700;
//CR1寄存器初始化控制寄存器
//边沿对齐
TIM4->CR1 &= (~(0x3 << 5));
//设置递减计数器
TIM4->CR1 |= (0x1 << 4);
//设置比较捕获寄存器初始化
//设置pwm模式
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 &= (~(0x7 << 4));
TIM4->CCMR1 |= (0x6 << 4);
//自动重载计数器预加载使能
TIM4->CR1 |= (0x1 << 7);
//输出比较寄存器预加载使能
TIM4->CCMR1 |= (0x1 << 3);
//配置捕获/比较寄存器输出模式
TIM4->CCMR1 &= (~0x3);
//比较/捕获寄存器
TIM4->CCER &= (~(0x1 << 3));
TIM4->CCER &= (~(0X1 << 1));
TIM4->CCER |= 0x1;
//设置计数器使能
TIM4->CR1 |= 0x1;
}
/***********风扇PE9初始化*************/
void fan_pwm()
{
/*---------RCC章节-----------*/
//GPIO组使能
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//TIM1使能
RCC->MP_APB2ENSETR |= 0x1;
/*---------GPIO章节----------*/
//设置PE9引脚为复用功能
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x2 << 18);
//设置复用功能为TIM1_CH1
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
/*-------------TIME1_CH1章节----------*/
//设置分频
TIM1->PSC =209;
//设置自动重载计数器的值
TIM1->ARR =1000;
//设置比较/捕获计数器的值
TIM1->CCR1=700;
//BDTR寄存器
TIM1->BDTR |= (0x1 << 15);
//设置计数器采用边沿对齐方式
TIM1->CR1 &= (~(0x3 << 5));
//设置计数器为递减计数器
TIM1->CR1 |= (0x1 << 4);
//配置输出比较模式为pwm
TIM1->CCMR1 &= (~(0x1 << 16));
TIM1->CCMR1 &= (~(0x7 << 4));
TIM1->CCMR1 |= (0x6 << 4);
//配置捕获/比较寄存器输出模式
TIM1->CCMR1 &= (~0x3);
//设置比较/捕获寄存器通道1输出极性
TIM1->CCER &= (~(0x1 << 3));
//设置起始状态为高电平
TIM1->CCER &= (~(0x1 << 1));
//设置比较/捕获寄存器为输出信号
TIM1->CCER |= 0x1;
//自动重载计数器预加载使能
TIM1->CR1 |= (0x1 << 7);
//配置输出比较寄存器预加载使能
TIM1->CCMR1 |= (0x1 << 3);
//设置计数器使能
TIM1->CR1 |= 0x1;
}
/***********马达PF6初始化*************/
void motor_pwm()
{
/*---------RCC章节-----------*/
//GPIO组使能
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//TIM16使能
RCC->MP_APB2ENSETR |= (0x1 << 3);
/*---------GPIO章节----------*/
//设置PF6引脚为复用功能
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x2 << 12);
//设置复用功能为TIM16_CH1
GPIOF->AFRL &= (~(0xf << 24));
GPIOF->AFRL |= (0x1 << 24);
/*-------------TIME16_CH1章节----------*/
//设置分频
TIM16->PSC =209;
//设置自动重载计数器的值
TIM16->ARR =1000;
//设置比较/捕获计数器的值
TIM16->CCR1=700;
//BDTR寄存器
TIM16->BDTR |= (0x1 << 15);
//配置输出比较模式为pwm
TIM16->CCMR1 &= (~(0x1 << 16));
TIM16->CCMR1 &= (~(0x7 << 4));
TIM16->CCMR1 |= (0x6 << 4);
//配置捕获/比较寄存器输出模式
TIM16->CCMR1 &= (~0x3);
//设置比较/捕获寄存器通道1输出极性
TIM16->CCER &= (~(0x1 << 3));
//设置起始状态为高电平
TIM16->CCER &= (~(0x1 << 1));
//设置比较/捕获寄存器为输出信号
TIM16->CCER |= 0x1;
//配置输出比较寄存器预加载使能
TIM16->CCMR1 |= (0x1 << 3);
//自动重载计数器预加载使能
TIM16->CR1 |= (0x1 << 7);
//计数器使能
TIM16->CR1 |= 0x1;
}
//主函数
#include "pwm.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
sp_pwm();
fan_pwm();
motor_pwm();
while(1)
{
}
return 0;
}
//实验结果
1、蜂鸣器响
2、风扇转动
3、马达声音响