头文件
#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
运行代码文章来源:https://www.toymoban.com/news/detail-466043.html
#include "pwm.h"
void hal_pwm_init()
{
//RCC章节初始化
//GPIOB使能 RCC_MP_AHB4ENSETR[1] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//TIM4使能 RCC_MP_ARB1ENSETR[2] = 1
RCC->MP_APB1ENSETR |= (0x1 << 2);
//GPIO章节初始化
//设置复用功能 GPIOB_MODER[13:12] = 10
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x1 << 13);
//设置引脚对应复用功能为TIM4 GPIOB_AFRL[27:24] = 0010
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x1 << 25);
//TIM4章节初始化 方波频率1000HZ 占空比
//设置分频 TIM4_PSC = 208
TIM4->PSC = 208;
//设置方波周期 TIM4_ARR = 1000
TIM4->ARR = 1000;
//设置占空比 TIM4_CCR1 = 300
TIM4->CCR1 = 700;
//设置PWM模式 TIM4_CCMR1[16][6:4] = 0110
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 &= (~(0x7 << 4));
TIM4->CCMR1 |= (0x3 << 5);
//设置输出比较加载使能 TIM4_CCMR1[3] = 1
TIM4->CCMR1 |= (0x1 << 3);
//设置通道1输出 TIM4_CCMR1[1:0] = 00
TIM4->CCMR1 &= ~(0x3);
//设置通道1输出 TIM4_CCER[3] = 0
TIM4->CCER &= (~(0x1 << 3));
//设置起始状态为高电平 TIM4->CCER[1] = 1
TIM4->CCER &= (~(0x1 << 1));
//设置输出使能 TIM4_CCER[0] = 1
TIM4->CCER |= 0x1;
//设置自动预加载使能 TIM4_CR1[7] = 1
TIM4->CR1 |= (0x1 << 7);
//设置边沿对齐 TIM4_CR1[6:5] = 00
TIM4->CR1 &= (~(0x3 << 5));
//设置设置递减计数方式 TIM4_CR1[4] = 1
TIM4->CR1 |= (0x1 << 4);
//设置计数器使能 TIM4_CR1[0] = 0
TIM4->CR1 &= ~(0x1);
}
void hal_fan_init()
{
//RCC章节初始化
//GPIOB使能 RCC_MP_AHB4ENSETR[4] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//TIM4使能 RCC_MP_ARB1ENSETR[0] = 1
RCC->MP_APB2ENSETR |= 0x1;
//GPIO章节初始化
//设置复用功能 GPIOB_MODER[19:18] = 10
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x1 << 19);
//设置引脚对应复用功能为TIM4 GPIOB_AFRH[7:4] = 0001
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
TIM1->BDTR |= (0x1 << 15);
//设置分频 TIM1_PSC = 208
TIM1->PSC = 208;
//设置方波周期 TIM1_ARR = 1000
TIM1->ARR = 1000;
//设置占空比 TIM1_CCR1 = 300
TIM1->CCR1 = 600;
//设置PWM模式 TIM1_CCMR1[16][6:4] = 0110
TIM1->CCMR1 &= (~(0x1 << 16));
TIM1->CCMR1 &= (~(0x7 << 4));
TIM1->CCMR1 |= (0x3 << 5);
//设置输出比较加载使能 TIM1_CCMR1[3] = 1
TIM1->CCMR1 |= (0x1 << 3);
//设置通道1输出 TIM1_CCMR1[1:0] = 00
TIM1->CCMR1 &= ~(0x3);
//设置通道1输出 TIM1_CCER[3] = 0
TIM1->CCER &= (~(0x1 << 3));
//设置起始状态为高电平 TIM1->CCER[1] = 1
TIM1->CCER &= (~(0x1 << 1));
//设置输出使能 TIM1_CCER[0] = 1
TIM1->CCER |= 0x1;
//设置自动预加载使能 TIM1_CR1[7] = 1
TIM1->CR1 |= (0x1 << 7);
//设置边沿对齐 TIM1_CR1[6:5] = 00
TIM1->CR1 &= (~(0x3 << 5));
//设置设置递减计数方式 TIM1_CR1[4] = 1
TIM1->CR1 |= (0x1 << 4);
//设置计数器使能 TIM1_CR1[0] = 0
TIM1->CR1 &= ~(0x1);
}
void hal_monitor_init()
{
//RCC章节初始化
//GPIOB使能 RCC_MP_AHB4ENSETR[5] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//TIM4使能 RCC_MP_ARB1ENSETR[3] = 1
RCC->MP_APB2ENSETR |= (0x1 << 3);
//GPIO章节初始化
//设置复用功能 GPIOB_MODER[13:12] = 10
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x1 << 13);
//设置引脚对应复用功能为TIM4 GPIOB_AFRL[27:24] = 0001
GPIOF->AFRL &= (~(0xf << 24));
GPIOF->AFRL |= (0x1 << 24);
TIM16->BDTR |= (0x1 << 15);
//TIM4章节初始化 方波频率1000HZ 占空比
//设置分频 TIM16_PSC = 208
TIM16->PSC = 208;
//设置方波周期 TIM16_ARR = 1000
TIM16->ARR = 1000;
//设置占空比 TIM16_CCR1 = 300
TIM16->CCR1 = 700;
//设置PWM模式 TIM16_CCMR1[16][6:4] = 0110
TIM16->CCMR1 &= (~(0x1 << 16));
TIM16->CCMR1 &= (~(0x7 << 4));
TIM16->CCMR1 |= (0x3 << 5);
//设置输出比较加载使能 TIM16_CCMR1[3] = 1
TIM16->CCMR1 |= (0x1 << 3);
//设置通道1输出 TIM16_CCMR1[1:0] = 00
TIM16->CCMR1 &= ~(0x3);
//设置通道1输出 TIM16_CCER[3] = 0
TIM16->CCER &= (~(0x1 << 3));
//设置起始状态为高电平 TIM16->CCER[1] = 1
TIM16->CCER &= (~(0x1 << 1));
//设置输出使能 TIM16_CCER[0] = 1
TIM16->CCER |= 0x1;
//设置自动预加载使能 TIM16_CR1[7] = 1
TIM16->CR1 |= (0x1 << 7);
//设置边沿对齐 TIM16_CR1[6:5] = 00
TIM16->CR1 &= (~(0x3 << 5));
//设置设置递减计数方式 TIM16_CR1[4] = 1
TIM16->CR1 |= (0x1 << 4);
//设置计数器使能 TIM16_CR1[0] = 0
TIM16->CR1 &= ~(0x1);
}
主函数文章来源地址https://www.toymoban.com/news/detail-466043.html
#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_fan_init();
while(1)
{
}
return 0;
}
到了这里,关于A7核PWM实验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!