#include "../include/beep.h"
// 蜂鸣器 --> PB6--> TIM4_CH1 --> APB1
void hal_beep_tim4_init(void)
{
// 1. 设置GPIOB,TIM4的时钟使能 RCC_MP_AHB4ENSETR[1] RCC_MP_APB1ENSETR[2]
RCC->MP_AHB4ENSETR = (0x1 << 1);
RCC->MP_APB1ENSETR = (0x1 << 2);
// 2. 设置PB6引脚为复用功能 GPIOB_MODER[13:12]
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x2 << 12);
// 3. 设置PB6引脚为TIM4_CH1功能 GPIOB_AFRL[27:24]
GPIOB->AFRL &= ~(0xF << 24);
GPIOB->AFRL |= (0x2 << 24);
// 4. 设置预分频寄存器,TIM4_PSC[15:0] = 208 // CK_PSC = 209MHz
// 提供给TIM4的时钟源的频率是209MHz
TIM4->PSC = 209-1;
// 5. 设置PWM方波的最终的周期 TIM4_ARR[16:0] = 1000
// 得到一个1000-2000Hz的方波
TIM4->ARR = 1000;
// 6. 设置PWM方波的占空比 TIM4_CCR1[16:0] = 700
TIM4->CCR1 = 700;
// 7. 设置TIM4_CH1通道为PWM1模式
// TIM4_CCMR1[16] = 0b0 TIM4_CCMR1[6:4] = 0b110 // pwm模式1 = 0b0110
TIM4->CCMR1 &= ~((0x1 << 16) | (0x7 << 4));
TIM4->CCMR1 |= (0x6 << 4);
// 8. 设置TIM4_CH1通道使能TIMx_CCR1预加载使能寄存器
// TIM4_CCMR1[3] = 0x1
TIM4->CCMR1 |= (0x1 << 3);
// 9. 设置TIM4_CH1通道输出PWM方波的极性,
// TIM4_CCER[1] = 0x1 or 0x0
TIM4->CCER &= ~(0x1 << 1);
// 10. 设置TIM4_CH1通道的输出使能位,输出PWM方波
// TIM4_CCER[0] = 0x1
TIM4->CCER |= (0x1 << 0);
// 11. 设置TIM4_CH1通道的TIMx_ARR预装载寄存器的缓冲区的使能
// TIM4_CR1[7] = 0x1
TIM4->CR1 |= (0x1 << 7);
// 12. 设置定时器的计数方式,边沿对齐
// TIM4_CR1[6:5] = 0x0
TIM4->CR1 &= ~(0x3 << 5);
// 12 设置定时器计数的方向,采用递减计数/递增计数
// TIM4_CR1[4] = 0x0
TIM4->CR1 &= ~(0x1 << 4);
// 13. 使能TIM4_CH1计数器
// TIM4_CR1[0] = 0x1
TIM4->CR1 |= (0x1 << 0);
}
#include "../include/beep.h"
// 风扇 --> PE9--> TIM1_CH1 --> APB1
void hal_fan_tim1_init(void)
{
// 1. 设置GPIOE,TIM1的时钟使能 RCC_MP_AHB4ENSETR[4] RCC_MP_APB2ENSETR[0]
RCC->MP_AHB4ENSETR = (0x1 << 4);
RCC->MP_APB2ENSETR = (0x1 << 0);
// 2. 设置PE9引脚为复用功能 GPIOE_MODER[19:18]
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x2 << 18);
// 3. 设置PE9引脚为TIM1_CH1功能 GPIOE_AFRH[7:4]
GPIOE->AFRH &= ~(0xF << 4);
GPIOE->AFRH |= (0x1 << 4);
// 4. 设置预分频寄存器,TIM1_PSC[15:0] = 208 // CK_PSC = 209MHz
// 提供给TIM1的时钟源的频率是209MHz
TIM1->PSC = 209 - 1;
// 5. 设置PWM方波的最终的周期 TIM1_ARR[16:0] = 1000
// 得到一个1000-2000Hz的方波
TIM1->ARR = 1000;
// 6. 设置PWM方波的占空比 TIM1_CCR1[16:0] = 700
TIM1->CCR1 = 700;
// 7. 设置TIM1_CH1通道为PWM1模式
// TIM1_CCMR1[16] = 0b0 TIM1_CCMR1[6:4] = 0b110 // pwm模式1 = 0b0110
TIM1->CCMR1 &= ~((0x1 << 16) | (0x7 << 4));
TIM1->CCMR1 |= (0x6 << 4);
// 8. 设置TIM1_CH1通道使能TIMx_CCR1预加载使能寄存器
// TIM1_CCMR1[3] = 0x1
TIM1->CCMR1 |= (0x1 << 3);
// 9. 设置TIM1_CH1通道输出PWM方波的极性,
// TIM1_CCER[1] = 0x1 or 0x0
TIM1->CCER &= ~(0x1 << 1);
// 10. 设置TIM1_CH1通道的输出使能位,输出PWM方波
// TIM1_CCER[0] = 0x1
TIM1->CCER |= (0x1 << 0);
// 11. 设置TIM1_CH1通道的TIMx_ARR预装载寄存器的缓冲区的使能
// TIM1_CR1[7] = 0x1
TIM1->CR1 |= (0x1 << 7);
// 12. 设置定时器的计数方式,边沿对齐
// TIM1_CR1[6:5] = 0x0
TIM1->CR1 &= ~(0x3 << 5);
// 12 设置定时器计数的方向,采用递减计数/递增计数
// TIM1_CR1[4] = 0x0
TIM1->CR1 &= ~(0x1 << 4);
// 13. 使能TIM1_CH1计数器
// TIM1_CR1[0] = 0x1
TIM1->CR1 |= (0x1 << 0);
TIM1->BDTR |= (0x1 << 15);
}
#include "../include/beep.h"
// 马达 --> PF6--> TIM6_CH1 --> APB2
void hal_motor_tim16_init(void)
{
// 1. 设置GPIOB,TIM16的时钟使能 RCC_MP_AHB4ENSETR[5] RCC_MP_APB2ENSETR[3]
RCC->MP_AHB4ENSETR = (0x1 << 5);
RCC->MP_APB2ENSETR = (0x1 << 3);
// 2. 设置PF6引脚为复用功能 GPIOF_MODER[13:12]
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x2 << 12);
// 3. 设置PF6引脚为TIM16_CH1功能 GPIOF_AFRL[27:24]
GPIOF->AFRL &= ~(0xF << 24);
GPIOF->AFRL |= (0x1 << 24);
// 4. 设置预分频寄存器,TIM16_PSC[15:0] = 208 // CK_PSC = 209MHz
// 提供给TIM16的时钟源的频率是209MHz
TIM16->PSC = 209 - 1;
// 5. 设置PWM方波的最终的周期 TIM16_ARR[16:0] = 1000
// 得到一个1000-2000Hz的方波
TIM16->ARR = 1000;
// 6. 设置PWM方波的占空比 TIM16_CCR1[16:0] = 700
TIM16->CCR1 = 700;
// 7. 设置TIM16_CH1通道为PWM1模式
// TIM16_CCMR1[16] = 0b0 TIM16_CCMR1[6:4] = 0b110 // pwm模式1 = 0b0110
TIM16->CCMR1 &= ~((0x1 << 16) | (0x7 << 4));
TIM16->CCMR1 |= (0x6 << 4);
// 8. 设置TIM16_CH1通道使能TIMx_CCR1预加载使能寄存器
// TIM16_CCMR1[3] = 0x1
TIM16->CCMR1 |= (0x1 << 3);
// 9. 设置TIM16_CH1通道输出PWM方波的极性,
// TIM16_CCER[1] = 0x1 or 0x0
TIM16->CCER &= ~(0x1 << 1);
// 10. 设置TIM16_CH1通道的输出使能位,输出PWM方波
// TIM16_CCER[0] = 0x1
TIM16->CCER |= (0x1 << 0);
// 11. 设置TIM16_CH1通道的TIMx_ARR预装载寄存器的缓冲区的使能
// TIM16_CR1[7] = 0x1
TIM16->CR1 |= (0x1 << 7);
// 12. 设置定时器的计数方式,边沿对齐
// TIM16_CR1[6:5] = 0x0
TIM16->CR1 &= ~(0x3 << 5);
// 12 设置定时器计数的方向,采用递减计数/递增计数
// TIM16_CR1[4] = 0x0
TIM16->CR1 &= ~(0x1 << 4);
// 13. 使能TIM16_CH1计数器
// TIM16_CR1[0] = 0x1
TIM16->CR1 |= (0x1 << 0);
TIM16->BDTR |= (0x1 << 15);
}