cortex-A7核PWM实验

蜂鸣器、马达、风扇

fun.h

#ifndef __FUN_H__
#define __FUN_H__

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


//蜂鸣器初始化
void hal_pwm_init();

//风扇初始化
void hal_fan_init();

//马达初始化
void hal_monitor_init();

#endif

pwm.c

#include "fun.h"

// 蜂鸣器初始化  PB6--->TIM4_CH1
void hal_pwm_init()
{
    // RCC初始化
    // RCC_GPIOB[1] = 1
    RCC->MP_AHB4ENSETR |= (0x1 << 1);

    // RCC_TIM4[2] = 1
    RCC->MP_APB1ENSETR |= (0x1 << 2);

    // GPIO初始化
    // GPIOB[13 12] = 10 (复用)
    GPIOB->MODER &= (~(0x3 << 12));
    GPIOB->MODER |= (0x1 << 13);

    // GPIOB->AFRL[27 24] = 0010 (AF2)
    GPIOB->AFRL &= (~(0xf << 24));
    GPIOB->AFRL |= (0x1 << 25);

    // TIM4初始化 方波率1000HZ 占空比(自定义)
    // ARR 自动重载计数器 1000
    TIM1->ARR = 1000;

    // PSC 分频器209 此寄存器会自动加1 所以填写208
    TIM1->PSC = 208;

    // CR1 初始化/使能相关内容
    // 设置自动重载预加载使能
    TIM4->CR1 &= (~(0x1 << 7));
    TIM4->CR1 |= (0x1 << 7);

    // 设置边沿对齐方式 CR1[6:5] = 00
    TIM4->CR1 &= (~(0x3 << 5));

    // 设置计数方式为递减计数方式 CR1[4] = 1
    TIM4->CR1 &= (~(0X1 << 4));
    TIM4->CR1 |= (0x1 << 4);

    // 设置计数使能 CR1[0] = 0
    TIM4->CR1 |= (0x1 << 0);

    // 设置捕获比较寄存器的模式 CCMR1
    // 设置输出捕获模式 CCMR1 CCMR1[16][6:4] = 0110
    TIM4->CCMR1 &= (~(0x3 << 4));
    TIM4->CCMR1 |= (0x6 << 4);

    // 输出捕获预加载使能 CCMR1[3] = 1
    TIM4->CCMR1 &= (~(0x1 << 3));
    TIM4->CCMR1 |= (0X1 << 3);

    // 配置通道1为输出模式 CCER[3] = 0
    TIM4->CCMR1 &= (~(0x3 << 0));

    // CCER 设置捕获/比较寄存器为输出模式 CCER[3] = 0
    TIM4->CCER &= (~(0x1 << 3));

    // 设置捕获/比较寄存器起始状态为低/高电平 CCER[1] = 0
    TIM4->CCER &= (~(0x1 << 1));

    // 设置捕获/比较寄存器使能 CCER[0] = 1
    TIM4->CCER &= (~(0x1 << 0));
    TIM4->CCER |= (0x1 << 0);

    TIM1->CCR1 = 700;
}

// 马达初始化
void hal_monitor_init()
{
    // RCC初始化
    // RCC_GPIOF[1] = 1
    RCC->MP_AHB4ENSETR |= (0x1 << 5);

    // RCC_TIM16[3] = 1
    RCC->MP_APB2ENSETR |= (0x1 << 3);

    // GPIO初始化
    // GPIOB[13 12] = 10 (复用)
    GPIOF->MODER &= (~(0x3 << 12));
    GPIOF->MODER |= (0x1 << 13);

    // GPIOF->AFRL[27 24] = 0001 (AF1)
    GPIOF->AFRL &= (~(0xf << 24));
    GPIOF->AFRL |= (0x1 << 24);

    // PSC 分频器209 此寄存器会自动加1 所以填写208
    TIM16->PSC = 208;

    // TIM16初始化 方波率1000HZ 占空比(自定义)
    // ARR 自动重载计数器 1000
    TIM16->ARR = 1000;

    TIM16->CCR1 = 500;

    // 设置捕获比较寄存器的模式 CCMR1
    // 设置输出捕获模式 CCMR1 CCMR1[16][6:4] = 0110
    TIM16->CCMR1 &= (~(0x1 << 16));
    TIM16->CCMR1 &= (~(0x7 << 4));
    TIM16->CCMR1 |= (0X3 << 5);

    // 输出捕获预加载使能 CCMR1[3] = 1
    TIM16->CCMR1 &= (~(0x1 << 3));
    TIM16->CCMR1 |= (0X1 << 3);

    // 配置通道1为输出模式 CCER[3] = 0
    TIM16->CCMR1 &= (~(0x3 << 0));

    // CCER 设置捕获/比较寄存器为输出模式 CCER[3] = 0
    TIM16->CCER &= (~(0x1 << 3));

    // 设置捕获/比较寄存器起始状态为低/高电平 CCER[1] = 0
    TIM16->CCER &= (~(0x1 << 1));

    // 设置捕获/比较寄存器使能 CCER[0] = 1
    TIM16->CCER &= (~(0x1 << 0));
    TIM16->CCER |= (0x1 << 0);

    // CR1 初始化/使能相关内容
    // 设置自动重载预加载使能
    TIM16->CR1 &= (~(0x1 << 7));
    TIM16->CR1 |= (0x1 << 7);

    // 设置边沿对齐方式 CR1[6:5] = 00
    TIM16->CR1 &= (~(0x3 << 5));

    // 设置计数方式为递减计数方式 CR1[4] = 1
    TIM16->CR1 &= (~(0X1 << 4));
    TIM16->CR1 |= (0x1 << 4);

    // 设置计数使能 CR1[0] = 0
    TIM16->CR1 |= (0x1 << 0);

    TIM16->BDTR &= (~(0x1 << 15));
    TIM16->BDTR |= (0x1 << 15);
}


// 风扇初始化
void hal_fan_init()
{
    // RCC初始化
    // RCC_GPIOB[1] = 1
    RCC->MP_AHB4ENSETR |= (0x1 << 4);

    // RCC_TIM4[2] = 1
    RCC->MP_APB2ENSETR |= (0x1 << 0);

    // GPIO初始化
    // GPIOB[13 12] = 10 (复用)
    GPIOE->MODER &= (~(0x3 << 18));
    GPIOE->MODER |= (0x1 << 18);

    // GPIOB->AFRL[27 24] = 0010 (AF2)
    GPIOE->AFRH &= (~(0xf << 4));
    GPIOE->AFRH |= (0x1 << 4);

    // TIM4初始化 方波率1000HZ 占空比(自定义)
    // ARR 自动重载计数器 1000
    TIM1->ARR = 1000;

    // PSC 分频器209 此寄存器会自动加1 所以填写208
    TIM1->PSC = 208;

    TIM1->CCR1 = 100;

    // 设置捕获比较寄存器的模式 CCMR1
    // 设置输出捕获模式 CCMR1 CCMR1[16][6:4] = 0110
    TIM1->CCMR1 &= (~(0x1 << 16));
    TIM1->CCMR1 |= (0x6 << 4);

    // 输出捕获预加载使能 CCMR1[3] = 1
    TIM1->CCMR1 &= (~(0x1 << 3));
    TIM1->CCMR1 |= (0X1 << 3);

    // 配置通道1为输出模式 CCER[3] = 0
    TIM1->CCMR1 &= (~(0x3 << 0));

    // CCER 设置捕获/比较寄存器为输出模式 CCER[3] = 0
    TIM1->CCER &= (~(0x1 << 3));

    // 设置捕获/比较寄存器起始状态为低/高电平 CCER[1] = 0
    TIM1->CCER |= (0x1 << 1);

    // 设置捕获/比较寄存器使能 CCER[0] = 1
    TIM1->CCER &= (~(0x1 << 0));
    TIM1->CCER |= (0x1 << 0);

    TIM1->BDTR |= (0x1 << 15);

    // CR1 初始化/使能相关内容
    // 设置自动重载预加载使能
    TIM1->CR1 &= (~(0x1 << 7));
    TIM1->CR1 |= (0x1 << 7);

    // 设置边沿对齐方式 CR1[6:5] = 00
    TIM1->CR1 &= (~(0x3 << 5));

    // 设置计数方式为递减计数方式 CR1[4] = 1
    TIM1->CR1 &= (~(0X1 << 4));
    TIM1->CR1 |= (0x1 << 4);

    // 设置计数使能 CR1[0] = 0
    TIM1->CR1 |= (0x1 << 0);
}

main.c

#include "fun.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()
{

	//hal_pwm_init();
	hal_monitor_init();
	hal_fan_init();

	while (1)
	{

	}
	return 0;
}

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