目录
pwm.h
pwm.c
main.c
pwm.h
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_tim.h"
// 蜂鸣器初始化
void hal_pwm_init(void);// 风扇初始化
void hal_fan_init(void);// 马达初始化
void hal_monitor_init(void);#endif // __PWM_H__
pwm.c
#include "pwm.h"void hal_pwm_init(void)
{// RCC章节初始化RCC->MP_AHB4ENSETR |= (0x1 << 1);RCC->MP_APB1ENSETR |= (0x1 << 2);// GPIO章节初始化 PB6// 设置引脚为复用模式GPIOB->MODER &= ~(0x3 << 12);GPIOB->MODER |= (0x2 << 12);// 设置引脚为对应复用功能GPIOB->AFRL &= ~(0xf << 24);GPIOB->AFRL |= (0x6 << 24);// TIM4章节初始化 方波频率1000HZ 占空比70% / 30%// 设置分频TIM4->PSC |= 0xD0; // 208MHz + 1// 设置自动重载计数器TIM4->ARR &= ~(0xff);TIM4->ARR |= 0x3E8; // 1000// 初始化/使能相关内容TIM4->CR1 &= ~(0xf << 4);TIM4->CR1 |= (0x9 << 4);TIM4->CR1 |= 0x1; // 设置计数使能// 初始化/使能设置捕获/比较寄存器// 设置捕获/比较寄存器使能TIM4->CCMR1 |= (0x1 << 3);// 设置为pwm模式TIM4->CCMR1 &= ~(0x1 << 16);TIM4->CCMR1 &= ~(0x7 << 4);TIM4->CCMR1 |= (0x6 << 4);// 配置通道1为输出模式TIM4->CCMR1 &= ~(0x3);// 设置设置捕获/比较寄存器为300TIM4->CCR1 &= ~(0xff);TIM4->CCR1 |= 0x12C;// 设置捕获/比较寄存器使能TIM4->CCER &= ~(0x1 << 3); // 设置捕获/比较寄存器使能TIM4->CCER |= (0x1 << 1); // 设置捕获/比较寄存器起始状态为高电平TIM4->CCER |= 0x1; // 设置捕获/比较寄存器使能
}void hal_fan_init(void)
{// RCC章节初始化RCC->MP_AHB4ENSETR |= (0x1 << 4);RCC->MP_APB2ENSETR |= 0x1;// GPIO章节初始化 PE9// 设置引脚为复用模式GPIOE->MODER &= ~(0x3 << 18);GPIOE->MODER |= (0x2 << 18);// 设置引脚为对应复用功能GPIOE->AFRH &= ~(0xf << 4);GPIOE->AFRH |= (0x1 << 4);// TIM1章节初始化 方波频率1000HZ 占空比70% / 30%// 设置分频TIM1->PSC |= 0xD0; // 208MHz + 1// 设置中断\死亡时间寄存器TIM1->BDTR |= (0x1 << 15);// 设置自动重载计数器TIM1->ARR &= ~(0xff);TIM1->ARR |= 0x3E8; // 1000// 初始化/使能相关内容TIM1->CR1 &= ~(0xf << 4);TIM1->CR1 |= (0x9 << 4);TIM1->CR1 |= 0x1; // 设置计数使能// 初始化/使能设置捕获/比较寄存器// 设置捕获/比较寄存器使能TIM1->CCMR1 |= (0x1 << 3);// 设置为pwm模式TIM1->CCMR1 &= ~(0x1 << 16);TIM1->CCMR1 &= ~(0x7 << 4);TIM1->CCMR1 |= (0x6 << 4);// 配置通道1为输出模式TIM1->CCMR1 &= ~(0x3);// 设置设置捕获/比较寄存器为300TIM1->CCR1 &= ~(0xff);TIM1->CCR1 |= 0x12C;// 设置捕获/比较寄存器使能TIM1->CCER &= ~(0x1 << 3); // 设置捕获/比较寄存器使能TIM1->CCER |= (0x1 << 1); // 设置捕获/比较寄存器起始状态为高电平TIM1->CCER |= 0x1; // 设置捕获/比较寄存器使能
}void hal_monitor_init(void)
{// RCC章节初始化RCC->MP_AHB4ENSETR |= (0x1 << 5);RCC->MP_APB2ENSETR |= (0x1 << 3);// GPIO章节初始化 PF6// 设置引脚为复用模式GPIOF->MODER &= ~(0x3 << 12);GPIOF->MODER |= (0x2 << 12);// 设置引脚为对应复用功能GPIOF->AFRL &= ~(0xf << 24);GPIOF->AFRL |= (0x1 << 24);// TIM1章节初始化 方波频率1000HZ 占空比70% / 30%// 设置分频TIM16->PSC |= 0xD0; // 208MHz + 1// 设置中断\死亡时间寄存器TIM16->BDTR |= (0x1 << 15);// 设置自动重载计数器TIM16->ARR &= ~(0xff);TIM16->ARR |= 0x3E8; // 1000// 初始化/使能相关内容TIM16->CR1 &= ~(0xf << 4);TIM16->CR1 |= (0x9 << 4);TIM16->CR1 |= 0x1; // 设置计数使能// 初始化/使能设置捕获/比较寄存器// 设置捕获/比较寄存器使能TIM16->CCMR1 |= (0x1 << 3);// 设置为pwm模式TIM16->CCMR1 &= ~(0x1 << 16);TIM16->CCMR1 &= ~(0x7 << 4);TIM16->CCMR1 |= (0x6 << 4);// 配置通道1为输出模式TIM16->CCMR1 &= ~(0x3);// 设置设置捕获/比较寄存器为300TIM16->CCR1 &= ~(0xff);TIM16->CCR1 |= 0x12C;// 设置捕获/比较寄存器使能TIM16->CCER &= ~(0x1 << 3); // 设置捕获/比较寄存器使能TIM16->CCER |= (0x1 << 1); // 设置捕获/比较寄存器起始状态为高电平TIM16->CCER |= 0x1; // 设置捕获/比较寄存器使能
}
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_init();hal_fan_init();hal_monitor_init();while (1){}return 0;
}