风扇/蜂鸣器/马达实验

风扇/蜂鸣器/马达实验

//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)
	{
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值