本次实验使用pwm激活风扇、蜂鸣器、马达三个组件。?
PWM:脉冲宽度调制定时器,通过PWM产生一个方波,作为震荡源驱动蜂鸣器等器件正常工作
查询芯片手册可知蜂鸣器:TIM4_CH1---->PB6? ?风扇:TIM1_CH1---->PE9??马达:TIM16_CH1---->PF6? ??
一、pwm.h——头文件的包络和函数的声明
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
/******蜂鸣器******/
//RCC章节初始化
void fm_rcc_init();
//GPIO章节初始化
void fm_gpio_init();
//TIM4章节初始化
void fm_tim4_init();
/******马达******/
//RCC章节初始化
void md_rcc_init();
//GPIO章节初始化
void md_gpio_init();
//TIM16章节初始化
void md_tim16_init();
/******风扇******/
//RCC章节初始化
void fs_rcc_init();
//GPIO章节初始化
void fs_gpio_init();
//TIM1章节初始化
void fs_tim1_init();
#endif
二、pwm.c——功能函数的声明(以蜂鸣器为例)
rcc章节的初始化
void fm_rcc_init()
{
//GPIOB使能
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//TIM4EN使能
RCC->MP_APB1ENSETR |= (0x1 << 2);
}
GPIO章节初始化——设置引脚的复用功能
void fm_gpio_init()
{
//设置PB6引脚为复用模式
GPIOB->MODER &= (~(0x3 <<12));
GPIOB->MODER |= (0x1 << 13);
//设置PB6引脚功能为AF2--->TIM4_CH1
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x1 << 25);
}
TIM4章节初始化
?
1)配置TIM4_PSC寄存器
TIM4->PSC &= (~(0xffff << 0));
TIM4->PSC |= (0xD0 << 0);
2)配置TIM4_ARR寄存器——影响周期和频率
TIM4->ARR &= (~(0xffff << 0));
TIM4->ARR |= (0x3E8 << 0);
3)配置TIM4_CCR1寄存器
TIM4->CCR1 &= (~(0xffff << 0));
TIM4->CCR1 |= (0x12C << 0);
4)配置TIM4_CCMR1寄存器
//1.设置为pwm1模式
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 &= (~(0x1 << 4));
TIM4->CCMR1 |= (0x3 << 5);
//2.设置预加载使能
TIM4->CCMR1 |= (0x1 << 3);
//3.设置为输出模式
TIM4->CCMR1 &= (~(0x3 << 0));
5)配置TIM4_CCER寄存器
//1.配置起始状态高电平
TIM4->CCER &= (~(0x1 << 1));
//2.配置输出使能
TIM4->CCER |= (0x1 << 0);
6)配置tim计算器方式--TIM4_CR
//1.自动重载计数器预加载使能
TIM4->CR1 |= (0x1 << 7);
//2.设置边沿对齐模式
TIM4->CR1 &= (~(0x3 << 5));
//3.设置递减计数器
TIM4->CR1 |= (0x1 << 4);
//4.设置计数器使能
TIM4->CR1 |= (0x1 << 0);
马达和风扇的配置与蜂鸣器相似,但需要将复用事件改为AF1(0b0001),并加入BDTR寄存器的配置
//配置TIM16_BDTR寄存器
TIM16->BDTR |= (0x1 << 15);
完整代码
#include "pwm.h"
//蜂鸣器RCC章节初始化
void fm_rcc_init()
{
//GPIOB使能
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//TIM4EN使能
RCC->MP_APB1ENSETR |= (0x1 << 2);
}
//蜂鸣器GPIO章节初始化
void fm_gpio_init()
{
//设置PB6引脚为复用模式
GPIOB->MODER &= (~(0x3 <<12));
GPIOB->MODER |= (0x1 << 13);
//设置PB6引脚功能为AF2--->TIM4_CH1
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x1 << 25);
}
//蜂鸣器TIM4章节初始化
void fm_tim4_init()
{
//配置TIM4_PSC寄存器
TIM4->PSC &= (~(0xffff << 0));
TIM4->PSC |= (0xD0 << 0);
//配置TIM4_ARR寄存器——影响周期和频率
TIM4->ARR &= (~(0xffff << 0));
TIM4->ARR |= (0x3E8 << 0);
//配置TIM4_CCR1寄存器
TIM4->CCR1 &= (~(0xffff << 0));
TIM4->CCR1 |= (0x12C << 0);
//配置TIM4_CCMR1寄存器
//1.设置为pwm1模式
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 &= (~(0x1 << 4));
TIM4->CCMR1 |= (0x3 << 5);
//2.设置预加载使能
TIM4->CCMR1 |= (0x1 << 3);
//3.设置为输出模式
TIM4->CCMR1 &= (~(0x3 << 0));
//配置TIM4_CCER寄存器
//1.配置起始状态高电平
TIM4->CCER &= (~(0x1 << 1));
//2.配置输出使能
TIM4->CCER |= (0x1 << 0);
//配置tim计算器方式--TIM4_CR1
//1.自动重载计数器预加载使能
TIM4->CR1 |= (0x1 << 7);
//2.设置边沿对齐模式
TIM4->CR1 &= (~(0x3 << 5));
//3.设置递减计数器
TIM4->CR1 |= (0x1 << 4);
//4.设置计数器使能
TIM4->CR1 |= (0x1 << 0);
}
//TIM16--->PF6
//马达RCC章节初始化
void md_rcc_init()
{
//TIM16使能
RCC->MP_APB2ENSETR |= (0x1 << 3);
//GPIOF使能
RCC->MP_AHB4ENSETR |= (0x1 << 5);
}
//马达GPIO章节初始化
void md_gpio_init()
{
//设置PF6引脚为复用模式
GPIOF->MODER &= (~(0x3 <<12));
GPIOF->MODER |= (0x1 << 13);
//设置PF6引脚功能为AF1--->TIM16_CH1
GPIOF->AFRL &= (~(0xf << 24));
GPIOF->AFRL |= (0x1 << 24);
}
//马达TIM16章节初始化
void md_tim16_init()
{
//配置TIM16_PSC寄存器
TIM16->PSC &= (~(0xffff << 0));
TIM16->PSC |= (0xD0 << 0);
//配置TIM16_ARR寄存器——影响周期和频率
TIM16->ARR &= (~(0xffff << 0));
TIM16->ARR |= (0x3E8 << 0);
//配置TIM16_CCR1寄存器
TIM16->CCR1 &= (~(0xffff << 0));
TIM16->CCR1 |= (0x12C << 0);
//配置TIM16_BDTR寄存器
TIM16->BDTR |= (0x1 << 15);
//配置TIM16_CCMR1寄存器
//1.设置为pwm1模式
TIM16->CCMR1 &= (~(0x1 << 16));
TIM16->CCMR1 &= (~(0x1 << 4));
TIM16->CCMR1 |= (0x3 << 5);
//2.设置预加载使能
TIM16->CCMR1 |= (0x1 << 3);
//3.设置为输出模式
TIM16->CCMR1 &= (~(0x3 << 0));
//配置TIM16_CCER寄存器
//1.配置起始状态高电平
TIM16->CCER &= (~(0x1 << 1));
//2.配置输出使能
TIM16->CCER |= (0x1 << 0);
//配置tim计算器方式--TIM16_CR1
//1.自动重载计数器预加载使能
TIM16->CR1 |= (0x1 << 7);
//2.设置边沿对齐模式
TIM16->CR1 &= (~(0x3 << 5));
//3.设置递减计数器
TIM16->CR1 |= (0x1 << 4);
//4.设置计数器使能
TIM16->CR1 |= (0x1 << 0);
}
//TIM1--->PE9
//风扇RCC章节初始化
void fs_rcc_init()
{
//TIM1使能
RCC->MP_APB2ENSETR |= (0x1 << 0);
//GPIOE使能
RCC->MP_AHB4ENSETR |= (0x1 << 4);
}
//风扇GPIO章节初始化
void fs_gpio_init()
{
//设置PE9引脚为复用模式
GPIOE->MODER &= (~(0x3 <<18));
GPIOE->MODER |= (0x1 << 19);
//设置PE9引脚功能为AF1--->TIM1_CH1
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
}
//风扇TIM1章节初始化
void fs_tim1_init()
{
//配置TIM1_PSC寄存器
TIM1->PSC &= (~(0xffff << 0));
TIM1->PSC |= (0xD0 << 0);
//配置TIM1_ARR寄存器——影响周期和频率
TIM1->ARR &= (~(0xffff << 0));
TIM1->ARR |= (0x3E8 << 0);
//配置TIM1_CCR1寄存器
TIM1->CCR1 &= (~(0xffff << 0));
TIM1->CCR1 |= (0x12C << 0);
//配置TIM1_BDTR寄存器
TIM1->BDTR |= (0x1 << 15);
//配置TIM1_CCMR1寄存器
//1.设置为pwm1模式
TIM1->CCMR1 &= (~(0x1 << 16));
TIM1->CCMR1 &= (~(0x1 << 4));
TIM1->CCMR1 |= (0x3 << 5);
//2.设置预加载使能
TIM1->CCMR1 |= (0x1 << 3);
//3.设置为输出模式
TIM1->CCMR1 &= (~(0x3 << 0));
//配置TIM1_CCER寄存器
//1.配置起始状态高电平
TIM1->CCER &= (~(0x1 << 1));
//2.配置输出使能
TIM1->CCER |= (0x1 << 0);
//配置tim计算器方式--TIM1_CR1
//1.自动重载计数器预加载使能
TIM1->CR1 |= (0x1 << 7);
//2.设置边沿对齐模式
TIM1->CR1 &= (~(0x3 << 5));
//3.设置递减计数器
TIM1->CR1 |= (0x1 << 4);
//4.设置计数器使能
TIM1->CR1 |= (0x1 << 0);
}
三、main.c——函数的调用
#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()
{
//蜂鸣器的初始化
fm_rcc_init();
fm_gpio_init();
fm_tim4_init();
//马达的初始化
md_rcc_init();
md_gpio_init();
md_tim16_init();
//风扇的初始化
fs_rcc_init();
fs_gpio_init();
fs_tim1_init();
while(1)
{
}
return 0;
}
|