分析框图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d438927133454b46662cdfdc30ab81db.png)
头文件
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
void hal_pwm_init();
void hal_fan_init();
void hal_monitor_init();
#endif
源代码
#include "pwm.h"
void hal_pwm_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 1);
RCC->MP_APB1ENSETR |= (0x1 << 2);
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x1 << 13);
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x1 << 25);
TIM4->PSC = 209 - 1;
TIM4->ARR = 1000;
TIM4->CCR1 = 700;
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 |= (0x7 << 4);
TIM4->CCMR1 &= (~(0x1 << 4));
TIM4->CCMR1 |= (0x1 << 3);
TIM4->CCMR1 &= (~(0x3 << 0));
TIM4->CCER &= (~(0x1 << 3));
TIM4->CCER &= (~(0x1 << 1));
TIM4->CCER |= (0x1 << 0);
TIM4->CR1 |= (0x1 << 7);
TIM4->CR1 &= (~(0x3 << 5));
TIM4->CR1 |= (0x1 << 4);
TIM4->CR1 |= (0x1 << 0);
}
void hal_fan_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 4);
RCC->MP_APB2ENSETR |= (0x1);
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x1 << 19);
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
TIM1->PSC = 209 - 1;
TIM1->ARR = 1000;
TIM1->CCR1 = 700;
TIM1->CCMR1 &= (~(0x1 << 16));
TIM1->CCMR1 |= (0x7 << 4);
TIM1->CCMR1 &= (~(0x1 << 4));
TIM1->CCMR1 |= (0x1 << 3);
TIM1->CCMR1 &= (~(0x3 << 0));
TIM1->CCER &= (~(0x1 << 3));
TIM1->CCER &= (~(0x1 << 1));
TIM1->CCER |= (0x1 << 0);
TIM1->CR1 |= (0x1 << 7);
TIM1->CR1 &= (~(0x3 << 5));
TIM1->CR1 |= (0x1 << 4);
TIM1->CR1 |= (0x1 << 0);
TIM1->BDTR |= (0x1 << 15);
}
void hal_monitor_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 5);
RCC->MP_APB2ENSETR |= (0x1 << 3);
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x1 << 13);
GPIOF->AFRL &= (~(0xf << 24));
GPIOF->AFRL |= (0x1 << 24);
TIM16->PSC &= (~(0xffff));
TIM16->PSC |= (0xD << 4);
TIM16->ARR &= (~(0xffff));
TIM16->ARR |= (0x3E8);
TIM16->CCR1 &= (~(0xffff));
TIM16->CCR1 |= (0x384);
TIM16->CCMR1 &= (~(0x1 << 16));
TIM16->CCMR1 &= (~(0x7 << 4));
TIM16->CCMR1 |= (0x3 << 5);
TIM16->CCMR1 |= (0x1 << 3);
TIM16->CCMR1 &= (~(0x3));
TIM16->CCER &= (~(0x1 << 3));
TIM16->CCER &= (~(0x1 << 1));
TIM16->CCER |= (0x1);
TIM16->CR1 |= (0x1 << 7);
TIM16->CR1 |= (0x1 << 0);
TIM16->BDTR |= (0x1 << 15);
}
主函数
#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;
}