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()
{
//蜂鸣器初始化
hal_pwm_beep_init1();
//马达
hal_pwm_motor_init1();
//风扇
hal_pwm_blower_init1();
while(1)
{
}
return 0;
}
pwm.h
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_tim.h"
#include "stm32mp1xx_rcc.h"
//pwm方波
//蜂鸣器初始化
void hal_pwm_beep_init();
//马达
void hal_pwm_motor_init();
//风扇
void hal_pwm_blower_init();
//普通输出模式
//蜂鸣器初始化
void hal_pwm_beep_init1();
//马达
void hal_pwm_motor_init1();
//风扇
void hal_pwm_blower_init1();
#endif
pwm.c
#include"pwm.h"
//蜂鸣器初始化
void hal_pwm_beep_init(){
//RCC初始化 PB6 使能ahb4 和apb1总线
RCC->MP_AHB4ENSETR |= (0x1 << 1);
RCC->MP_APB1ENSETR |= (0x1 << 2);
//GPIO初始化
//设置PE6引脚为复用模式 mode[13:12] 10
GPIOB->MODER &=(~(0x3 << 12));
GPIOB->MODER |= (0x1 <<13);
//设置复用功能为TIM4 为AF2 [27:24] 0010
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x1 << 25);
//TIM4初始化
TIM4->CR1 &=(~((0xf << 4)+0x1));
TIM4->CR1 |=(0x1 << 7);
TIM4->CR1 |=(0x1 << 4);
TIM4->CR1 |=(0x1);
//PWM方波频率为1000HZ
//系统提供的时钟源为209MHZ
//设置分频值 系统会自动加1
TIM4->PSC =209-1;
//设置自动重载计数器的值
TIM4->ARR = 1000;
//设置递减
TIM4->CCR1 = 300;
//设置输出捕获模式
TIM4->CCMR1 &=(~(0x3));
TIM4->CCMR1 &=(~(0X1 << 16));
TIM4->CCMR1 &=(~(0X7 << 4));
TIM4->CCMR1 |=(0x6 << 4);
TIM4->CCMR1 |=(0x1 << 3);
//设置捕获、比较寄存器
TIM4->CCER |=(0x3);
TIM4->CCER |=(~(0x1 << 3));
}
//马达
void hal_pwm_motor_init(){
//RCC初始化 PF6 使能ahb4 和apb1总线
RCC->MP_AHB4ENSETR |= (0x1 << 5);
RCC->MP_APB2ENSETR |= (0x1 << 3);
//GPIO初始化
//设置PF6引脚为复用模式 mode[13:12] 10
GPIOF->MODER &=(~(0x3 << 12));
GPIOF->MODER |= (0x1 <<13);
//设置复用功能为TIM4 为AF1 [27:24] 0001
GPIOF->AFRL &= (~(0xf << 24));
GPIOF->AFRL |= (0x1 << 24);
//TIM4初始化
TIM16->CR1 &=(~((0xf << 4)+0x1));
TIM16->CR1 |=(0x1 << 7);
TIM16->CR1 |=(0x1 << 4);
TIM16->CR1 |=(0x1);
//PWM方波频率为1000HZ
//系统提供的时钟源为209MHZ
//设置分频值 系统会自动加1
TIM16->PSC =209-1;
//设置自动重载计数器的值
TIM16->ARR = 1000;
//设置递减
TIM16->CCR1 = 300;
//设置输出捕获模式
TIM16->CCMR1 &=(~(0x3));
TIM16->CCMR1 &=(~(0X1 << 16));
TIM16->CCMR1 &=(~(0X7 << 4));
TIM16->CCMR1 |=(0x6 << 4);
TIM16->CCMR1 |=(0x1 << 3);
//设置捕获、比较寄存器
TIM16->CCER |=(0x3);
TIM16->CCER |=(~(0x1 << 3));
TIM16->BDTR |=(0x1 <<15);
}
//风扇
void hal_pwm_blower_init(){
//RCC初始化 PE9 使能ahb4 和apb1总线
RCC->MP_AHB4ENSETR |= (0x1 << 4);
RCC->MP_APB2ENSETR |= (0x1);
//GPIO初始化
//设置PE9引脚为复用模式 mode[13:12] 10
GPIOE->MODER &=(~(0x3 << 18));
GPIOE->MODER |= (0x1 <<19);
//设置复用功能为TIM4 为AF1 [7:4] 0001
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
//TIM4初始化
TIM1->CR1 &=(~((0xf << 4)+0x1));
TIM1->CR1 |=(0x1 << 7);
TIM1->CR1 |=(0x1 << 4);
TIM1->CR1 |=(0x1);
//PWM方波频率为1000HZ
//系统提供的时钟源为209MHZ
//设置分频值 系统会自动加1
TIM1->PSC =209-1;
//设置自动重载计数器的值
TIM1->ARR = 1000;
//设置递减
TIM1->CCR1 = 300;
//设置输出捕获模式
TIM1->CCMR1 &=(~(0x3));
TIM1->CCMR1 &=(~(0X1 << 16));
TIM1->CCMR1 &=(~(0X7 << 4));
TIM1->CCMR1 |=(0x6 << 4);
TIM1->CCMR1 |=(0x1 << 3);
//设置捕获、比较寄存器
TIM1->CCER |=(0x3);
TIM1->CCER |=(~(0x1 << 3));
TIM1->BDTR |=(0x1 <<15);
}
//蜂鸣器初始化
void hal_pwm_beep_init1(){
//RCC初始化 PB6 使能ahb4
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//GPIO初始化
//设置PE6引脚为复用模式 mode[13:12] 10
GPIOB->MODER &=(~(0x3 << 12));
GPIOB->MODER |= (0x1 <<12);
GPIOB->OTYPER &=(~(0x1 << 6));
GPIOB->OSPEEDR &=(~(0x1 << 13));
GPIOB->OSPEEDR |=(0x1 <<12);
GPIOB->PUPDR &=(~(0x3 <<12));
GPIOB->ODR |=(0x1 << 6);
}
//马达
void hal_pwm_motor_init1(){
//RCC初始化 PF6 使能ahb4
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//GPIO初始化
//设置PF6引脚为复用模式 mode[13:12] 10
GPIOF->MODER &=(~(0x3 << 12));
GPIOF->MODER |= (0x1 <<12);
GPIOF->OTYPER &=(~(0x1 << 6));
GPIOF->OSPEEDR &=(~(0x1 << 13));
GPIOF->OSPEEDR |=(0x1 <<13);
GPIOF->PUPDR &=(~(0x3 <<12));
GPIOF->ODR |=(0x1 << 6);
}
//风扇
void hal_pwm_blower_init1(){
//RCC初始化 PE9 使能ahb4
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//GPIO初始化
//设置PE9引脚为复用模式 mode[13:12] 10
GPIOE->MODER &=(~(0x3 << 18));
GPIOE->MODER |= (0x1 <<18);
GPIOE->OTYPER &=(~(0x1 << 9));
GPIOE->OSPEEDR &=(~(0x1 << 18));
GPIOE->OSPEEDR |=(0x1 <<19);
GPIOE->PUPDR &=(~(0x3 <<18));
GPIOE->ODR |=(0x1 << 9);
}