ARM_PWM总线_实现蜂鸣器 风扇 马达

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()
{	
	//蜂鸣器 PB6------>TIM4_CH1
	//hal_pwm_beep_init();
	//风扇  PE9--->TIM1_CH1
	hal_pwm_fan_init();
	//马达 PF6--->TIM16_CH1
	hal_pwm_motor_init();


	while(1)
	{
	}
	return 0;
}

include/pwm.h

#ifndef __PW_H__
#define __PW_H__

#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_tim.h"


//蜂鸣器 PB6------>TIM4_CH1
void hal_pwm_beep_init();
//风扇  PE9--->TIM1_CH1
void hal_pwm_fan_init();
//马达 PF6--->TIM16_CH1
void hal_pwm_motor_init();

#endif

src/pwm.c

#include "pwm.h"

//蜂鸣器 PB6------>TIM4_CH1
void hal_pwm_beep_init()
{
	//RCC章节初始化
	//1.使能TIM4组寄存器  MP_APB1ENSETR[2] = 1
	RCC->MP_APB1ENSETR |= (0x1 << 2);
	//2.使能GPIO组控制器  MP_AHB4ENSETR[1] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 1);
	


	//GPIO章节初始化
	//1.设置PB6引脚为复用功能模式 MODER[13:12] = 10
	GPIOB->MODER &= (~(0x3 << 12));
	GPIOB->MODER |= (0x1 << 13);
	//2.设置PB6引脚复用功能为TIM4_CH1功能 AFRL[27:24] = 0010
	GPIOB->AFRL &= (~(0xf << 24));
	GPIOB->AFRL |= (0x1 << 25);
	


	//TIM4章节初始化 pwm方波频率为1000hz
	
	//1.系统提供时钟源为209mhz
	//PSC = 209 -1
	TIM4->PSC = 208;
	//2.自动重载计数器值 ARR = 1000
	TIM4->ARR = 1000;
	//3.捕获比较寄存器里面的值 CCR1  = 300
	TIM4->CCR1 = 300;

	//4.设置pwm模式CCMR1[16][6:4] = 0110
	TIM4->CCMR1 &= (~(0x1 << 16));
 	TIM4->CCMR1 &= (~(0x7 << 4));
	TIM4->CCMR1 |= (0X3 << 5);
	//CCMR1[3]=1--->设置比较寄存器预加载使能
	TIM4->CCMR1 |= (0x1 << 3);
	//CCMR1[1:0]=00-->设置通道1为输出模式
	TIM4->CCMR1 &= (~(0x3));

	//5.设置捕获/比较寄存器输出极性CCER[3] = 1 CCER[1]=1/0 CCER[0]=1
	TIM4->CCER |= (0x1 << 3);
	TIM4->CCER |= (0X1 << 1);
	TIM4->CCER |= (0X1);
	

	//1.CR1[7] = 1--->设置自动重载预加载使能
	TIM4->CR1 |= (0x1 << 7);
	//2.CR1[6:5] = 00---->设置计数器边沿对齐模式 
	TIM4->CR1 &= (~(0x3 << 5));
	//3.CR1[4] = 1--->设置递减计数方式
	TIM4->CR1 |= (0x1 << 4);
	//4.CR1[0] = 1 设置计数器使能	
	TIM4->CR1 |= (0x1);	
}


//风扇  PE9--->TIM1_CH1
void hal_pwm_fan_init()
{
	//RCC章节初始化
	//2.使能GPIO组控制器  MP_AHB4ENSETR[4] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 4);
	//1.使能TIM4组寄存器  MP_APB2ENSETR[0] = 1
	RCC->MP_APB2ENSETR |= (0x1);


	//GPIO章节初始化
	//1.设置PB6引脚为复用功能模式 MODER[13:12] = 10
	GPIOE->MODER &= (~(0x3 << 18));
	GPIOE->MODER |= (0x2 << 18);

	//2.设置PB6引脚复用功能为TIM4_CH1功能 AFRL[27:24] = 0010
	GPIOE->AFRH &= (~(0xf << 4));
	GPIOE->AFRH |= (0x1 << 4);



	//TIM4章节初始化 pwm方波频率为1000hz
	
	//1.系统提供时钟源为209mhz
	//PSC = 209 -1
	TIM1->PSC = 208;
	//2.自动重载计数器值 ARR = 1000
	TIM1->ARR = 1000;
	//3.捕获比较寄存器里面的值 CCR1  = 300
	TIM1->CCR1 = 300;

	//4.设置pwm模式CCMR1[16][6:4] = 0110
	TIM1->CCMR1 &= (~(0x1 << 16));
 	TIM1->CCMR1 &= (~(0x7 << 4));
	TIM1->CCMR1 |= (0X6 << 4);
	//CCMR1[3]=1--->设置比较寄存器预加载使能
	TIM1->CCMR1 |= (0x1 << 3);
	//CCMR1[1:0]=00-->设置通道1为输出模式
	TIM1->CCMR1 &= (~(0x3));

	//5.设置捕获/比较寄存器输出极性CCER[3] = 1 CCER[1]=1/0 CCER[0]=1
	TIM1->CCER &= (~(0x1 << 3));
	TIM1->CCER |= (0X1 << 1);
	TIM1->CCER |= (0X1);
	

	//1.CR1[7] = 1--->设置自动重载预加载使能
	TIM1->CR1 |= (0x1 << 7);
	//2.CR1[6:5] = 00---->设置计数器边沿对齐模式 
	TIM1->CR1 &= (~(0x3 << 5));
	//3.CR1[4] = 1--->设置递减计数方式
	TIM1->CR1 |= (0x1 << 4);
	//4.CR1[0] = 1 设置计数器使能	
	TIM1->CR1 |= (0x1);	
	//设置主输出使能
	TIM1->BDTR |= (0X1 << 15);
	

}


//马达 PF6--->TIM16_CH1
void hal_pwm_motor_init()
{
	//RCC章节初始化
	//2.使能GPIO组控制器  MP_AHB4ENSETR[4] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//1.使能TIM4组寄存器  MP_APB2ENSETR[0] = 1
	RCC->MP_APB2ENSETR |= (0x1 << 3);


	//GPIO章节初始化
	//1.设置PB6引脚为复用功能模式 MODER[13:12] = 10
	GPIOF->MODER &= (~(0x3 << 12));
	GPIOF->MODER |= (0x2 << 12);

	GPIOF->AFRL &= (~(0xf << 24));
	GPIOF->AFRL |= (0x1 << 24);



	//TIM4章节初始化 pwm方波频率为1000hz
	
	//1.系统提供时钟源为209mhz
	//PSC = 209 -1
	TIM16->PSC = 208;
	//2.自动重载计数器值 ARR = 1000
	TIM16->ARR = 1000;
	//3.捕获比较寄存器里面的值 CCR1  = 300
	TIM16->CCR1 = 300;

	//4.设置pwm模式CCMR1[16][6:4] = 0110
	TIM16->CCMR1 &= (~(0x1 << 16));
 	TIM16->CCMR1 &= (~(0x7 << 4));
	TIM16->CCMR1 |= (0X6 << 4);
	//CCMR1[3]=1--->设置比较寄存器预加载使能
	TIM16->CCMR1 |= (0x1 << 3);
	//CCMR1[1:0]=00-->设置通道1为输出模式
	TIM16->CCMR1 &= (~(0x3));

	//5.设置捕获/比较寄存器输出极性CCER[3] = 1 CCER[1]=1/0 CCER[0]=1
	TIM16->CCER &= (~(0x1 << 3));
	TIM16->CCER |= (0X1 << 1);
	TIM16->CCER |= (0X1);
	

	//1.CR1[7] = 1--->设置自动重载预加载使能
	TIM16->CR1 |= (0x1 << 7);
	//2.CR1[6:5] = 00---->设置计数器边沿对齐模式 
	TIM16->CR1 &= (~(0x3 << 5));
	//3.CR1[4] = 1--->设置递减计数方式
	TIM16->CR1 |= (0x1 << 4);
	//4.CR1[0] = 1 设置计数器使能	
	TIM16->CR1 |= (0x1);	
	//设置主输出使能
	TIM16->BDTR |= (0X1 << 15);
	




}

 

 

你可能感兴趣的:(arm开发)