练习1:
PWM控制蜂鸣器,风扇,震动马达
pwm.h
#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
#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_ch1GPIOB->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_ch1GPIOE->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_ch1GPIOF->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
#include "pwm.h"int main()
{hal_beep_init();hal_fan_init();hal_monitor_init();while(1){}return 0;
}