?1.电路图分析
蜂鸣器:TIM4_CH1---->PB6?
风扇:TIM1_CH1---->PE9?
马达:TIM16_CH1---->PF6?
2.RCC章节分析
????????RCC_MP_AHB4ENSETR寄存器:GPIOB组时钟使能?
????????RCC_MP_APB1ENSETR寄存器:TIM4组时钟使能?
3.GPIO章节分析
????????设置PB6引脚为复用功能 ????????????????????????GPIOB_MODER[13:12]=10
????????设置PB6引脚为复用功能TIM4_CH1 ???????GPIOB_AFRL[27:24]=0010???????
4.寄存器分析
? ? ? ? 4.1 TIM4_CR1寄存器配置tim计数器方式
????????????????TIM4_CR1[7] = 1 ------>自动重载计数器预加载使能
????????????????TIM4_CR1[6:5] = 00 ------>设置边沿对齐模式
????????????????TIM4_CR1[4] = 1 ------>设置递减计数器
????????????????TIM4_CR1[0] = 1 ------>设置计数器使能?
? ? ? ? 4.2 IMx_CCMR1寄存器
????????????????TIM4_CCMR1[16][6:4] = 0110------>设置为PWM1模式
????????????????TIM4_CCMR1[3] = 1 ------>设置预加载使能
????????????????TIM4_CCMR1[1:0] = 00 ------>设置为输出模式?
? ? ? ? 4.3 TIMx_CCER寄存器?
? ? ? ? 4.4 TIMx_PSC寄存器
? ? ? ? 4.5 TIMx_ARR寄存器
? ? ? ? 4.6 TIMx_CCR1寄存器
#ifndef __PWM__H__
#define __PWM__H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
//蜂鸣器初始化 PB6----->TIM4_CH1
void pwm_init();
//马达初始化 PF6----->TIM16_CH1
void monitor_init();
//风扇初始化 PE9----->TIM1_CH1
void fan_init();
#endif
#include "pwm.h"
//蜂鸣器初始化 PB6----->TIM4_CH1
void pwm_init()
{
/*********RCC初始化***********/
//1.使能GPIOB组时钟
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//2.使能TIM4定时器时钟
RCC->MP_APB1ENSETR |= (0x1 << 2);
/*********GPIO初始化*********/
//1.设置PB6引脚为复用功能模式
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x2 << 12);
//2.设置PB6引脚复用功能模式TIM4_CH1
GPIOB->AFRL &= (~(0xF << 24));
GPIOB->AFRL |= (0x2 << 24);
/*********TIM4_CH1初始化**********/
//1.设置分配器 系统提供时钟源209MHZ 209
TIM4->PSC = 209-1;
//2.设置PWM方波最终周期
TIM4->ARR = 1000;
//3.设置PWM方波占空比
TIM4->CCR1 = 700;
//4.设置TIM4_CH1通道为PWM1模式
TIM4->CCMR1 &= ~((0x1 << 16) | (0x7 << 4));
TIM4->CCMR1 |= (0x6 << 4);
//5.设置TIM4_CH1通道为预加载时能
TIM4->CCMR1 |= (0x1 << 3);
//6.设置TIM4_CH1通道为输出模式
TIM4->CCMR1 &= ~(0x3);
//7.设置TIM4_CH1通道为输出模式极性高电平
TIM4->CCER &= ~(0x1 << 1);
//8.设置TIM4_CH1通道为比较捕获寄存器输出使能
TIM4->CCER |= (0x1 << 0);
//9.设置TIM4_CH1通道自动重载计数器预加载使能
TIM4->CR1 |= (0x1 << 7);
//10.边沿对齐
TIM4->CR1 &= (~(0x3 << 5));
//11.递减计数
TIM4->CR1 |= (0x1 << 4);
//12.计数器使能
TIM4->CR1 |= (0x1 << 0);
}
//马达初始化 PF6----->TIM16_CH1
void monitor_init()
{
/*********RCC初始化***********/
//1.使能GPIOB组时钟
RCC->MP_AHB4ENSETR |= (0x1 << 5);
//2.使能TIM4定时器时钟
RCC->MP_APB2ENSETR |= (0x1 << 3);
/*********GPIO初始化*********/
//1.设置PB6引脚为复用功能模式
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x2 << 12);
//2.设置PB6引脚复用功能模式TIM4_CH1
GPIOF->AFRL &= (~(0xF << 24));
GPIOF->AFRL |= (0x1 << 24);
/*********TIM4_CH1初始化**********/
//1.设置分配器 系统提供时钟源209MHZ 209
TIM16->PSC = 209-1;
//2.设置PWM方波最终周期
TIM16->ARR = 1000;
//3.设置PWM方波占空比
TIM16->CCR1 = 700;
//4.设置TIM4_CH1通道为PWM1模式
TIM16->CCMR1 &= ~((0x1 << 16) | (0x7 << 4));
TIM16->CCMR1 |= (0x6 << 4);
//5.设置TIM4_CH1通道为预加载时能
TIM16->CCMR1 |= (0x1 << 3);
//6.设置TIM4_CH1通道为输出模式
TIM16->CCMR1 &= ~(0x3);
//7.设置TIM4_CH1通道为输出模式极性高电平
TIM16->CCER &= ~(0x1 << 1);
//8.设置TIM4_CH1通道为比较捕获寄存器输出使能
TIM16->CCER |= (0x1 << 0);
//9.设置TIM4_CH1通道自动重载计数器预加载使能
TIM16->BDTR |=0x1 << 15;
TIM16->CR1 |= (0x1 << 7);
//10.边沿对齐
TIM16->CR1 &= (~(0x3 << 5));
//11.递减计数
TIM16->CR1 |= (0x1 << 4);
//12.计数器使能
TIM16->CR1 |= (0x1 << 0);
}
//风扇初始化 PE9----->TIM1_CH1
void fan_init()
{
/*********RCC初始化***********/
//1.使能GPIOE组时钟
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//2.使能TIM4定时器时钟
RCC->MP_APB2ENSETR |= (0x1 << 0);
/*********GPIO初始化*********/
//1.设置PB6引脚为复用功能模式
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x2 << 18);
//2.设置PB6引脚复用功能模式TIM4_CH1
GPIOE->AFRH &= (~(0xF << 4));
GPIOE->AFRH |= (0x1 << 4);
/*********TIM4_CH1初始化**********/
//1.设置分配器 系统提供时钟源209MHZ 209
TIM1->PSC = 209-1;
//2.设置PWM方波最终周期
TIM1->ARR = 1000;
//3.设置PWM方波占空比
TIM1->CCR1 = 700;
//4.设置TIM4_CH1通道为PWM1模式
TIM1->CCMR1 &= ~((0x1 << 16) | (0x7 << 4));
TIM1->CCMR1 |= (0x6 << 4);
//5.设置TIM4_CH1通道为预加载时能
TIM1->CCMR1 |= (0x1 << 3);
//6.设置TIM4_CH1通道为输出模式
TIM1->CCMR1 &= ~(0x3);
//7.设置TIM4_CH1通道为输出模式极性高电平
TIM1->CCER &= ~(0x1 << 1);
//8.设置TIM4_CH1通道为比较捕获寄存器输出使能
TIM1->CCER |= (0x1 << 0);
//9.设置TIM4_CH1通道自动重载计数器预加载使能
TIM1->BDTR |=0x1 << 15;
TIM1->CR1 |= (0x1 << 7);
//10.边沿对齐
TIM1->CR1 &= (~(0x3 << 5));
//11.递减计数
TIM1->CR1 |= (0x1 << 4);
//12.计数器使能
TIM1->CR1 |= (0x1 << 0);
}
#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()
{
pwm_init();
fan_init();
monitor_init();
while(1)
{
}
return 0;
}
?
?
?
|