.h
#ifndef __PWM__H_
#define __PWM__H_
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_tim.h"
#define rcc_mp_ahb4 RCC->MP_AHB4ENSETR |= (0X1 << 1)
#define rcc_mp_apb1 RCC->MP_APB1ENSETR |= (0x1 << 2)
void momu_init(); //风扇自转
void TIM4_init(); // 蜂鸣器
void cc_init(); //马达
#endif
.c
#include "pwm.h"
void momu_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 4);
RCC->MP_APB2ENSETR |= (0x1 << 0);
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x2 << 18);
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 |= (0x6 << 4);
TIM1->CCMR1 |= (0x1 << 3);
TIM1->CCER &= (~(0x3));
TIM1->CCER &= ~(0x1 << 1);
TIM1->CCER |= (0x1 << 0);
TIM16->BDTR |= (0x1 << 15);
TIM1->CR1 |= (0x1 << 7);
TIM1->CR1 &= (~(0x3 << 5));
TIM1->CR1 &= (0x1 << 4);
TIM1->CR1 |= (0x1 << 0);
}
void TIM4_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 1);
RCC->MP_APB1ENSETR |= (0x1 << 2);
// 2. 设置PB6引脚为复用功能 GPIOB_MODER
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x2 << 12);
// 3. 设置PB6引脚为TIM4_CH1功能 GPIOB_AFRL
GPIOB->AFRL &= (~(0xF << 24));
GPIOB->AFRL |= (0x2 << 24);
// 4. 设置预分频寄存器,TIM4_PSC[15:0] CK_PSC = 209MHz
// 提供给TIM4的时钟源的频率是209MHz
// 2. 设置PB6引脚为复用功能 GPIOB_MODER
// 2. 设置PB6引脚为复用功能 GPIOB_MODER
TIM4->PSC = 209 - 1;
// 5. 设置PWM方波的最终的周期 TIM4_ARR[16:0]
// 得到一个1000-2000Hz的方波
TIM4->ARR = 1000;
// 5. 设置PWM方波的占空比 TIM4_CCR1[16:0]
TIM4->CCR1 = 700;
// 6. 设置TIM4_CH1通道为PWM1模式
// TIM4_CCMR1[16] TIM4_CCMR1[6:4] pwm模式1 = 0b0110
// 2. 设置PB6引脚为复用功能 GPIOB_MODER
TIM4->CCMR1 &= (~(0x1 << 16));
TIM4->CCMR1 &= (~(0x7 << 4));
TIM4->CCMR1 |= (0x6 << 4);
// 7. 设置TIM4_CH1通道使能TIMx_CCR1预加载使能寄存器
// 2. 设置PB6引脚为复用功能 GPIOB_MODER
// TIM4_CCMR1[3] = 0x1
TIM4->CCMR1 |= (0x1 << 3);
// 8. 设置TIM4_CH1通道输出PWM方波的极性,
// TIM_CCER[3] = 0b0 TIM4_CCER[1] = 0x1 or 0x0
TIM4->CCER &= (~(0x1 << 3));
//TIM4->CCER |= (0x1 << 1);
TIM4->CCER &= ~(0x1 << 1);
// 9. 设置TIM4_CH1通道的输出使能位,输出PWM方波
// TIM4_CCER[0] = 0x1
TIM4->CCER |= (0x1 << 0);
// 10. 设置TIM4_CH1通道的预装载寄存器的缓冲区的使能
// TIM4_CR1[7] = 0x1
TIM4->CR1 |= (0x1 << 7);
// 11. 设置定时器的计数方式,边沿对齐
// TIM4_CR1[6:5] = 0x0
TIM4->CR1 &= (~(0x3 << 5));
// 12 设置定时器计数的方向,采用递减计数/递增计数
// TIM4_CR1[4] = 0x1 | 0x0
TIM4->CR1 |= (0x1 << 4);
// 13. 使能TIM4_CH1计数器
// TIM4_CR1[0] = 0x1
TIM4->CR1 |= (0x1 << 0);
} // 2. 设置PB6引脚为复用功能 GPIOB_MODER
void cc_init()
{
RCC->MP_AHB4ENSETR |= (0x1 << 5);
RCC->MP_APB2ENSETR |= (0x1 << 3);
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x2 << 12);
GPIOF->AFRL &= (~(0xF << 24));
GPIOF->AFRL |= (0x1 << 24);
TIM16->PSC = 209 - 1;
TIM16->ARR = 1000;
TIM16->CCR1 = 700;
TIM16->CCMR1 &= (~(0x1 << 16));
TIM16->CCMR1 &= (~(0x7 << 4));
TIM16->CCMR1 |= (0x6 << 4);
TIM16->CCMR1 |= (0x1 << 3);
TIM16->CCMR1 &= ~(0x3);
TIM16->CCER &= ~(0x1 << 1);
TIM16->CCER |= (0x1 << 0);
TIM16->BDTR |= (0x1 << 15);
TIM16->CR1 |= (0x1 << 7);
TIM16->CR1 &= (~(0x3 << 5));
TIM16->CR1 |= (0x1 << 4);
TIM16->CR1 |= (0x1 << 0);
}
main.c
#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()
{
// GPIO_init();
TIM4_init();
momu_init();
cc_init();
while(1)
{
}
return 0;
}