练习1:
PWM控制蜂鸣器,风扇,震动马达
pwm.h
cpp
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
//蜂鸣器 PB6 TIME4_CH1 初始化
void hal_beep_init();
//风扇
void hal_fan_init();
//马达
void hal_monitor_init();
#endif
pwm.c
cpp
#include "pwm.h"
//蜂鸣器 PB6 TIM4_CH1
void hal_beep_init(){
//rcc章节初始化
//1.使能gpiob组控制器
RCC->MP_AHB4ENSETR |= (0x1<<1);
//2.使能tim4组控制器
RCC->MP_APB1ENSETR |= (0x1<<2);
//gpiob组控制器初始化
//1.设置复用功能模式
GPIOB->MODER &= (~(0x3<<12));
GPIOB->MODER |= (0x2<<12);
//2.设置复用功能为tim4_ch1
GPIOB->AFRL &= (~(0xf<<24));
GPIOB->AFRL |= (0x2<<24);
//tim4章节初始化
//产生方波 频率1000hz
//系统提供时钟源位209mhz
//1.设置预分频器
TIM4->PSC = 208; //因为时钟频率=fck_psc/(PSC[15:0]+1),所以这里传输208
//2.设置自动重载值
TIM4->ARR = 1000; //1000
//3.设置比较寄存器,控制占空比
TIM4->CCR1 = 300; //300
//4.设置输出比较模式
TIM4->CCMR1 &= (~(0x7<<4));
TIM4->CCMR1 &= (~(0x1<<16));
TIM4->CCMR1 |= (0x6<<4);
//5.输出比较预加载使能
TIM4->CCMR1 |= (0x1<<3);
//6.设置通道1配置为输出
TIM4->CCMR1 &= (~(0x3<<0));
//8.设置起始时低电平
TIM4->CCER |= (0x1<<1);
//9.使能捕获
TIM4->CCER |= (0x1<<0);
//10.设置自动重载预加载使能
TIM4->CR1 |= (0x1<<7);
//11.设置边沿对齐方式
TIM4->CR1 &= (~(0x3<<5));
//12.设置递减计数
TIM4->CR1 |= (0x1<<4);
//13.设置计数器使能
TIM4->CR1 |= (0x1<<0);
}
//风扇
void hal_fan_init(){
//rcc章节初始化
//1.使能gpioe组控制器
RCC->MP_AHB4ENSETR |= (0x1<<4);
//2.使能tim1组控制器
RCC->MP_APB2ENSETR |= (0x1<<0);
//gpioe组控制器初始化
//1.设置复用功能模式
GPIOE->MODER &= (~(0x3<<18));
GPIOE->MODER |= (0x2<<18);
//2.设置复用功能为tim1_ch1
GPIOE->AFRH &= (~(0xf<<4));
GPIOE->AFRH |= (0x1<<4);
//tim1章节初始化
//产生方波 频率1000hz
//系统提供时钟源位209mhz
//1.设置预分频器
TIM1->PSC = 208; //因为时钟频率=fck_psc/(PSC[15:0]+1),所以这里传输208
//2.设置自动重载值
TIM1->ARR = 1000; //1000
//3.设置比较寄存器,控制占空比
TIM1->CCR1 = 500; //500
//4.设置输出比较模式
TIM1->CCMR1 &= (~(0x7<<4));
TIM1->CCMR1 &= (~(0x1<<16));
TIM1->CCMR1 |= (0x6<<4);
//5.输出比较预加载使能
TIM1->CCMR1 |= (0x1<<3);
//6.设置通道1配置为输出
TIM1->CCMR1 &= (~(0x3<<0));
//14.设置主输出使能
TIM1->BDTR |= (0x1<<15);
//8.设置起始时低电平
TIM1->CCER |= (0x1 << 3);
TIM1->CCER |= (0x1<<1);
//9.使能捕获
TIM1->CCER |= (0x1<<0);
//10.设置自动重载预加载使能
TIM1->CR1 |= (0x1<<7);
//11.设置边沿对齐方式
TIM1->CR1 &= (~(0x3<<5));
//12.设置递减计数
TIM1->CR1 |= (0x1<<4);
//13.设置计数器使能
TIM1->CR1 |= (0x1<<0);
}
//马达
void hal_monitor_init(){
//rcc章节初始化
//1.使能gpiof组控制器
RCC->MP_AHB4ENSETR |= (0x1<<5);
//2.使能tim16组控制器
RCC->MP_APB2ENSETR |= (0x1<<3);
//gpiof组控制器初始化
//1.设置复用功能模式
GPIOF->MODER &= (~(0x3<<12));
GPIOF->MODER |= (0x2<<12);
//2.设置复用功能为tim1_ch1
GPIOF->AFRL &= (~(0xf<<24));
GPIOF->AFRL |= (0x1<<24);
//tim16章节初始化
//产生方波 频率1000hz
//系统提供时钟源位209mhz
//1.设置预分频器
TIM16->PSC = 208; //因为时钟频率=fck_psc/(PSC[15:0]+1),所以这里传输208
//2.设置自动重载值
TIM16->ARR = 1000; //1000
//3.设置比较寄存器,控制占空比
TIM16->CCR1 = 500; //500
//4.设置输出比较模式
TIM16->CCMR1 &= (~(0x7<<4));
TIM16->CCMR1 &= (~(0x1<<16));
TIM16->CCMR1 |= (0x6<<4);
//5.输出比较预加载使能
TIM16->CCMR1 |= (0x1<<3);
//6.设置通道1配置为输出
TIM16->CCMR1 &= (~(0x3<<0));
//7.设置主输出使能
TIM16->BDTR |= (0x1<<15);
//8.设置起始时低电平
TIM16->CCER |= (0x1 << 3);
TIM16->CCER |= (0x1<<1);
//9.使能捕获
TIM16->CCER |= (0x1<<0);
//10.设置自动重载预加载使能
TIM16->CR1 |= (0x1<<7);
//11.设置计数器使能
TIM16->CR1 |= (0x1<<0);
}
main.c
cpp
#include "pwm.h"
int main()
{
hal_beep_init();
hal_fan_init();
hal_monitor_init();
while(1){
}
return 0;
}