风扇/蜂鸣器/马达实验
//pwm.h
#ifndef __PWM_H__
#define __PWM_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
//蜂鸣器初始化
void hal_pwm_init(void);
//马达初始化
void hal_motor_init(void);
//风扇初始化
void hal_fan_init(void);
#endif
//pwm.c
#include "pwm.h"
//PB6-->TIM4_CH1
void hal_pwm_init(void)
{
/*****RCC初始化*****/
//1.使能GPIOB组时钟
RCC->MP_AHB4ENSETR |= 0X1<<1;
//2.使能TIM4定时器时钟
RCC->MP_APB1ENSETR |= 0x1<<2;
/******GPIO初始化*******/
//PB6引脚复用模式
GPIOB->MODER &=(~(0X3 << 12));
GPIOB->MODER |= 0X2<<12;
//复用功能为TIME——CH
GPIOB->AFRL &=(~(0Xf<<24));
GPIOB->AFRL |= 0X1<<25;
/*******TIM4_CH1初始化*******/
//设置分频器 208+1
TIM4->PSC = 208;
//自动重载计数器进行赋值1000
TIM4->ARR |= 0X200;
//比较/捕获寄存器进行赋值300
TIM4->CCR1 |= 0Xc0;
//设置PWM1模式
TIM4->CCMR1 &= (~(0X1<<16));
TIM4->CCMR1 &= (~(0X7<<4));
TIM4->CCMR1 |= 0X3<<5;
//设置TIM4_CH1预加载使能
TIM4->CCMR1 |= 0X1<<3;
//设置TIM4_CH1输出模式
TIM4->CCMR1 &=(~(0X3));
//设置TIM4_CH1输出模式使能
TIM4->CCER &= (~(0X1<<3));
//设置TIM4_CH1输出模式极性高电平
TIM4->CCER &= (~(0X3));
//设置TIM4_CH1比较捕获寄存器输出使能
TIM4->CCER |= 0X1;
//设置TIM4_CH1自动重载计数器预加载使能
TIM4->CR1 |= 0X1<<7;
//设置TIM4_CH1边沿对齐
TIM4->CR1 &= (~(0X3<<5));
//设置TIM4_CH1递减计数器
TIM4->CR1 |= 0X1<<4;
//设置TIM4_CH1计数器使能
TIM4->CR1 &= ~0X1;
}
//马达初始化
void hal_motor_init(void)
{
/*****RCC初始化*****/
//1.使能GPIOF组时钟
RCC->MP_AHB4ENSETR |= 0X1<<5;
//2.使能TIM16定时器时钟
RCC->MP_APB2ENSETR |= 0x1<<3;
/******GPIO初始化*******/
//PF6引脚复用模式
GPIOF->MODER &=(~(0X3 << 12));
GPIOF->MODER |= 0X2<<12;
//复用功能为TIME——CH
GPIOF->AFRL &=(~(0Xf<<24));
GPIOF->AFRL |= 0X1<<24;
/*******TIM16_CH1初始化*******/
//设置分频器 系统提供时钟源209
TIM16->PSC = 208;
//自动重载计数器进行赋值1000
TIM16->ARR |= 0X200;
//比较/捕获寄存器进行赋值300
TIM16->CCR1 |= 0Xc0;
//设置PWM1模式
TIM16->CCMR1 &= (~(0X1<<16));
TIM16->CCMR1 &= (~(0X7<<4));
TIM16->CCMR1 |= 0X3<<5;
//设置TIM16_CH1预加载使能
TIM16->CCMR1 |= 0X1<<3;
//设置TIM16_CH1输出模式
TIM16->CCMR1 &=(~(0X3));
//设置TIM16_CH1输出模式使能
TIM16->CCER &= (~(0X1<<3));
//设置TIM16_CH1输出模式极性高电平
TIM16->CCER &= (~(0X3));
//设置TIM16_CH1比较捕获寄存器输出使能
TIM16->CCER |= 0X1;
//设置TIM16_CH1自动重载计数器预加载使能
TIM16->CR1 |= 0X1<<7;
//设置TIM16_CH1计数器使能
TIM16->CR1 &= ~0X1;
TIM16->BDTR |= 0x1<<15;
}
//风扇初始化
void hal_fan_init(void)
{
/*****RCC初始化*****/
//1.使能GPIOE组时钟
RCC->MP_AHB4ENSETR |= 0X1<<4;
//2.使能TIM1定时器时钟
RCC->MP_APB2ENSETR |= 0x1;
/******GPIO初始化*******/
//PE9引脚复用模式
GPIOE->MODER &=(~(0X3 << 18));
GPIOE->MODER |= 0X2<<18;
//复用功能为TIME——CH
GPIOE->AFRH &=(~(0Xf<<4));
GPIOE->AFRH |= 0X1<<4;
/*******TIM1_CH1初始化*******/
//设置分频器 系统提供时钟源209
TIM1->PSC = 208;
//自动重载计数器进行赋值1000
TIM1->ARR |= 0X200;
//比较/捕获寄存器进行赋值300
TIM1->CCR1 |= 0Xc0;
//设置PWM1模式
TIM1->CCMR1 &= (~(0X1<<16));
TIM1->CCMR1 &= (~(0X7<<4));
TIM1->CCMR1 |= 0X3<<5;
//设置TIM1_CH1预加载使能
TIM1->CCMR1 |= 0X1<<3;
//设置TIM1_CH1输出模式
TIM1->CCMR1 &=(~(0X3));
//设置TIM1_CH1输出模式使能
TIM1->CCER &= (~(0X1<<3));
//设置TIM1_CH1输出模式极性高电平
TIM1->CCER &= (~(0X3));
//设置TIM1_CH1比较捕获寄存器输出使能
TIM1->CCER |= 0X1;
//设置TIM1_CH1自动重载计数器预加载使能
TIM1->CR1 |= 0X1<<7;
//设置TIM1_CH1边沿对齐
TIM1->CR1 &= (~(0X3<<5));
//设置TIM1_CH1递减计数器
TIM1->CR1 |= 0X1<<4;
//设置TIM1_CH1计数器使能
TIM1->CR1 &= ~0X1;
TIM1->BDTR |= 0x1<<15;
}
//main.c
#include "pwm.h"
extern void printf(const char *fmt, ...);
//extern char buffer[50] = {0};
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_fan_init();
hal_motor_init();
while(1)
{
}
}