驱动开发板蜂鸣器风扇、马达进行工作
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_tim.h"
//蜂鸣器:PB6 ---> TIM4_CH1
void hal_beep_init();
//风扇:PE9 ---> TIM1_CH1
void hal_fan_init();
//马达:PF6 ---> TIM16_CH1
void hal_monitor_init();
#endif
#include "pwm.h"
//蜂鸣器:PB6 ---> TIM4_CH1
void hal_beep_init()
{
//RCC章节初始化
//1.使能GPIOB组控制器 MP_AHB4ENSETR[1] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//2.使能TIM4组控制器 MP_APB1ENSETR[2] = 1
RCC->MP_APB1ENSETR |= (0X1 << 2);
//GPIO章节初始化
//1.设置PB6引脚为复用功能 GPIOB->MODER[13:12] = 10
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x1 << 13);
//2.设置PB6引脚复用功能为TIM4_CH1功能 GPIOB->AFRL[27:24] = 0010
GPIOB->AFRL &= (~(0xf << 24));
GPIOB->AFRL |= (0x1 << 25);
//TIM1章节
//PWM方波频率为 : 1000HZ
//思路: 1.准备值(分频器,自动重载计数器,比较捕获寄存器)
//2.使能/输出
TIM4->ARR = 1000; //设置自动重载计数器中的值
TIM4->PSC = 209-1; //设置系统提供的时钟源进行分频
TIM4->CCR1 = 300; //设置捕获/比较寄存器中的值
//CR1寄存器初始化
TIM4->CR1 |= (0x1 << 7); //设置自动重载计数器预加载使能 CR1[7] = 1
TIM4->CR1 &= (~(0x3 << 5)); //设置计数器为边沿对齐模式 CR1[6:5] = 00
TIM4->CR1 |= (0x1 << 4); //设置计数器为递减计数器方式 CR1[4] = 1
TIM4->CR1 |= (0x1 << 0); //设置计数器使能 CR1[0] = 1
//CCMR1寄存器初始化
TIM4->CCMR1 &= (~(0x1 << 4)); //设置PWM模式选择 CCMR1[16][6:4] = 0110
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 |= (0x3 << 5);
TIM4->CCMR1 |= (0x1 << 3); //设置输出比较预加载使能 CCMR1[3] = 1
TIM4->CCMR1 &= (~(0x3 << 0)); //设置捕获/比较选择输出
//CCER、CCR1寄存器初始化
TIM4->CCER &= (~(0x5 << 1)); //设置捕获比较寄存器默认输出极性为高电平 CCER[3][1] = 00(高电平)/01(低电平)
TIM4->CCER |= (0x1 << 0); //设置捕获/比较输出使能 CCER[0] = 1
}
//风扇:PE9 ---> TIM1_CH1
void hal_fan_init()
{
//RCC章节初始化
//1.使能GPIOE组控制器 MP_AHB4ENSETR[4] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//2.使能TIM1组控制器 MP_AHB2ENSETR[0] = 1
RCC->MP_APB2ENSETR |= (0X1 << 0);
//GPIO章节初始化
//1.设置PE9引脚为复用功能 [19:18] = 10
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x1 << 19);
//2.设置PE9引脚复用功能为TIM1_CH1功能 AF1 0001 GPIOE->AFRH[7:4] = 0001
GPIOE->AFRH &= (~(0xf << 4));
GPIOE->AFRH |= (0x1 << 4);
//TIM1章节
//PWM方波频率为 : 1000HZ
//思路: 1.准备值(分频器,自动重载计数器,比较捕获寄存器)
TIM1->ARR = 1000; //设置自动重载计数器中的值
TIM1->PSC = 800; //设置系统提供的时钟源进行分频
TIM1->CCR1 = 300; //设置捕获/比较寄存器中的值
//CR1寄存器初始化
TIM1->CR1 |= (0x1 << 7); //设置自动重载计数器预加载使能 CR1[7] = 1
TIM1->CR1 &= (~(0x3 << 5)); //设置计数器为边沿对齐模式 CR1[6:5] = 00
TIM1->CR1 |= (0x1 << 4); //设置计数器为递减计数器方式 CR1[4] = 1
TIM1->CR1 |= (0x1 << 0); //设置计数器使能 CR1[0] = 1
//CCMR1寄存器初始化
TIM1->CCMR1 &= (~(0x1 << 4)); //设置PWM模式选择 CCMR1[16][6:4] = 0110
TIM1->CCMR1 &= (~(0x1 << 16));
TIM1->CCMR1 |= (0x3 << 5);
TIM1->CCMR1 |= (0x1 << 3); //设置输出比较预加载使能 CCMR1[3] = 1
TIM1->CCMR1 &= (~(0x3 << 0)); //设置捕获/比较选择输出
//CCER、CCR1寄存器初始化
TIM1->CCER &= (~(0x5 << 1)); //设置捕获比较寄存器默认输出极性为高电平 CCER[3][1] = 00(高电平)/01(低电平)
TIM1->CCER |= (0x1 << 0); //设置捕获/比较输出使能 CCER[0] = 1
TIM1->BDTR |= (0x1 << 15); //BDTR使能
}
//马达:PF6 ---> TIM16_CH1
void hal_monitor_init()
{
//RCC章节初始化
//1.使能GPIOF组控制器 MP_AHB2ENSETR[5] = 1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//2.使能TIM16组控制器 MP_APB2ENCLRR[3] = 1
RCC->MP_APB2ENSETR |= (0X1 << 3);
//GPIO章节初始化
//1.设置PF6引脚为复用功能 GPIOF->MODER[13:12] = 10
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x1 << 13);
//2.设置PF6引脚复用功能为TIM16_CH1功能 AF1 0001 GPIOF->AFRL [27:24] = 0001
GPIOF->AFRL &= (~(0xf << 24));
GPIOF->AFRL |= (0x1 << 24);
//TIM16章节
//PWM方波频率为 : 1000HZ
//思路: 1.准备值(分频器,自动重载计数器,比较捕获寄存器)
TIM16->ARR = 1000; //设置自动重载计数器中的值
TIM16->PSC = 209-1; //设置系统提供的时钟源进行分频
TIM16->CCR1 = 300; //设置捕获/比较寄存器中的值
//CR1寄存器初始化
TIM16->CR1 |= (0x1 << 7); //设置自动重载计数器预加载使能 CR1[7] = 1
TIM16->CR1 &= (~(0x3 << 5)); //设置计数器为边沿对齐模式 CR1[6:5] = 00
TIM16->CR1 |= (0x1 << 4); //设置计数器为递减计数器方式 CR1[4] = 1
TIM16->CR1 |= (0x1 << 0); //设置计数器使能 CR1[0] = 1
//CCMR1寄存器初始化
TIM16->CCMR1 &= (~(0x1 << 4)); //设置PWM模式选择 CCMR1[16][6:4] = 0110
TIM16->CCMR1 &= (~(0x1 << 16));
TIM16->CCMR1 |= (0x3 << 5);
TIM16->CCMR1 |= (0x1 << 3); //设置输出比较预加载使能 CCMR1[3] = 1
TIM16->CCMR1 &= (~(0x3 << 0)); //设置捕获/比较选择输出
//CCER、CCR1寄存器初始化
TIM16->CCER &= (~(0x5 << 1)); //设置捕获比较寄存器默认输出极性为高电平 CCER[3][1] = 00(高电平)/01(低电平)
TIM16->CCER |= (0x1 << 0); //设置捕获/比较输出使能 CCER[0] = 1
TIM16->BDTR |= (0x1 << 15); //BDTR使能
}
#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_beep_init(); //蜂鸣器初始化
hal_fan_init();//风扇初始化
hal_monitor_init();//马达初始化
while(1)
{
}
return 0;
}