cortex-A7核PWM实验

pwm.h:

#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();

//PF6---->TIM16_CH1
void hal_monitor_init();

//PE9--->TIM1_CH1
void hal_fan_init();

#endif

pwm.c:

#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引脚为复用功能模式 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章节初始化
	//1.系统提供时钟源为209MHZ,进行分频209-1 PSC = 209 - 1;
	TIM4->PSC |= 209 - 1;
	//2.设置自动重载计数器中的值1000,产生PWM方波频率1000HZ ARR = 1000
	TIM4->ARR = 1000;
	//3.设置捕获比较寄存器中的值300 CCR1 = 300
	TIM4->CCR1 = 300;
	//4.设置TIM4_CH1通道,默认输出极性为高电平 CCER[3] = 0 CCER[1] = 0
	TIM4->CCER &= (~(0x1 << 3));
	TIM4->CCER &= (~(0x1 << 1));
	//5.设置TIM4_CH1通道,捕获比较寄存器输出使能 CCER[0] = 1
	TIM4->CCER |= (0x1 << 0);
	//6.设置TIM4_CH1通道,输出PWM模式 CCMR1[16][6:4] = 0110
	TIM4->CCMR1 &= (~(0x1 << 16));
	TIM4->CCMR1 &= (~(0x7 << 4));
	TIM4->CCMR1 |= (0x3 << 5);
	//7.设置TIM4_CH1通道,设置捕获比较寄存器预加载使能 CCMR1[3] = 1
	TIM4->CCMR1 |= (0x1 << 3);
	//8.设置TIM4_CH1通道为输出模式 CCMR1[1:0] = 00 
	TIM4->CCMR1 &= (~(0x3 << 0));
	//8.设置TIM4_CH1通道,自动重载计数器预加载使能 CR1[7] = 1
	TIM4->CR1 |= (0x1 << 7);
	//9.设置TIM4_CH1通道,边沿对齐模式 CR1[6:5] = 00 
	TIM4->CR1 &= (~(0x3 << 5));
	//10.设置TIM4_CH1通道为递减计数方式 CR1[4] = 1
	TIM4->CR1 |= (0x1 << 4);
	//11.设置TIM4_CH1通道计数器使能 CR1[0] = 1
	TIM4->CR1 |= (0x1 << 0);
}

//PF6 --->TIM16_CH1
void hal_monitor_init()
{
	//RCC章节初始化
	//1.使能GPIOF组控制器 MP_AHB4ENSETR[5] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//2.使能TIM16组控制器 MP_APB2ENSETR[3] = 1
	RCC->MP_APB2ENSETR |= (0x1 << 3);

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

	//TIM16章节初始化
	//1.系统提供时钟源为209MHZ,进行分频209-1 PSC = 209 - 1;
	TIM16->PSC |= 209 - 1;
	//2.设置自动重载计数器中的值1000,产生PWM方波频率1000HZ ARR = 1000
	TIM16->ARR = 1000;
	//3.设置捕获比较寄存器中的值300 CCR1 = 300
	TIM16->CCR1 = 300;
	//4.设置TIM16_CH1通道,默认输出极性为高电平 CCER[3] = 0 CCER[1] = 0
	TIM16->CCER &= (~(0x1 << 3));
	TIM16->CCER &= (~(0x1 << 1));
	//5.设置TIM16_CH1通道,捕获比较寄存器输出使能 CCER[0] = 1
	TIM16->CCER |= (0x1 << 0);
	//6.设置TIM16_CH1通道,输出PWM模式 CCMR1[16][6:4] = 0110
	TIM16->CCMR1 &= (~(0x1 << 16));
	TIM16->CCMR1 &= (~(0x7 << 4));
	TIM16->CCMR1 |= (0x3 << 5);
	//7.设置TIM16_CH1通道,设置捕获比较寄存器预加载使能 CCMR1[3] = 1
	TIM16->CCMR1 |= (0x1 << 3);
	//8.设置TIM16_CH1通道为输出模式 CCMR1[1:0] = 00 
	TIM16->CCMR1 &= (~(0x3 << 0));
	//8.设置TIM16_CH1通道,自动重载计数器预加载使能 CR1[7] = 1
	TIM16->CR1 |= (0x1 << 7);
	//9.设置TIM16_CH1通道计数器使能 CR1[0] = 1
	TIM16->CR1 |= (0x1 << 0);
	//10.设置TIM16_CH1通道主输出使能 BDTR[15] = 1
	TIM16->BDTR |= (0x1 << 15);
}

//PE9 -----> TIM1_CH1
void hal_fan_init()
{
	//RCC章节初始化
	//1.使能GPIOE组控制器 MP_AHB4ENSETR[4] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 4);
	//2.使能TIM1组控制器 MP_APB2ENSETR[0] = 1
	RCC->MP_APB2ENSETR |= (0x1 << 0);

	//GPIO章节初始化
	//1.设置PE9引脚为复用功能模式 MODER[19:18] = 10
	GPIOE->MODER &= (~(0x3 << 18));
	GPIOE->MODER |= (0x1 << 19);
	//2.设置PE9引脚复用功能为TIM1_CH1功能 AFRH[7:4] = 0001
	GPIOE->AFRH &= (~(0xf << 4));
	GPIOE->AFRH |= (0x1 << 4);

	//TIM1章节初始化
	//1.系统提供时钟源为209MHZ,进行分频209-1 PSC = 209 - 1;
	TIM1->PSC |= 209 - 1;
	//2.设置自动重载计数器中的值1000,产生PWM方波频率1000HZ ARR = 1000
	TIM1->ARR = 1000;
	//3.设置捕获比较寄存器中的值300 CCR1 = 300
	TIM1->CCR1 = 500;
	//4.设置TIM1_CH1通道,默认输出极性为高电平 CCER[3] = 0 CCER[1] = 0
	TIM1->CCER &= (~(0x1 << 3));
	TIM1->CCER &= (~(0x1 << 1));
	//5.设置TIM1_CH1通道,捕获比较寄存器输出使能 CCER[0] = 1
	TIM1->CCER |= (0x1 << 0);
	//6.设置TIM1_CH1通道,输出PWM模式 CCMR1[16][6:4] = 0110
	TIM1->CCMR1 &= (~(0x1 << 16));
	TIM1->CCMR1 &= (~(0x7 << 4));
	TIM1->CCMR1 |= (0x3 << 5);
	//7.设置TIM1_CH1通道,设置捕获比较寄存器预加载使能 CCMR1[3] = 1
	TIM1->CCMR1 |= (0x1 << 3);
	//8.设置TIM1_CH1通道为输出模式 CCMR1[1:0] = 00 
	TIM1->CCMR1 &= (~(0x3 << 0));
	//8.设置TIM1_CH1通道,自动重载计数器预加载使能 CR1[7] = 1
	TIM1->CR1 |= (0x1 << 7);
	//9.设置TIM1_CH1通道,边沿对齐模式 CR1[6:5] = 00 
	TIM1->CR1 &= (~(0x3 << 5));
	//10.设置TIM1_CH1通道为递减计数方式 CR1[4] = 1
	TIM1->CR1 |= (0x1 << 4);
	//11.设置TIM1_CH1通道计数器使能 CR1[0] = 1
	TIM1->CR1 |= (0x1 << 0);
	TIM1->BDTR |= (0x1 << 15);
}

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()
{

	while(1)
	{
	//	hal_beep_init();
	//	hal_monitor_init();
		hal_fan_init();
	}
	return 0;
}

你可能感兴趣的:(arm开发,嵌入式硬件,单片机)