本文主要是介绍2024/5/27 ARMday10 PWM实验蜂鸣器发声,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
TIM4.c
#include "tim4.h"
void tim4_init()
{//1.设置tim4,GPIOB时钟使能RCC->MP_AHB4ENSETR |= (0x1<<1);RCC->MP_APB1ENSETR |= (0x1<<2);//2.设置PB6引脚为复位功能GPIOB->MODER &= (~(0x3<<12));GPIOB->MODER |= (0x2<<12);//3.设置PB6引脚为TIM4_CH1功能GPIOB->AFRL &= (~(0xF<<24));GPIOB->AFRL |= (0x2<<24);//4.设置预分频寄存器,TIM4_PSC[15:0]//提供给TIM4时钟源的频率是209MHzTIM4->PSC = 20900-1;//5.设置PWM方波的最终的周期 TIM4_ARR自动装载寄存器TIM4->ARR = 10000;//5.设置PWM方波的占空比 TIM4_CCR1比较寄存器TIM4->CCR1 = 3000;//6.设置TIM4_CH1通道为PWM1模式//TIM4_CCMR1[16],TIM4_CCR1[6:4],PWM模式1=0b0110TIM4->CCMR1 &= (~(0x1<<16));TIM4->CCMR1 &= (~(0x7<<4));TIM4->CCMR1 |= (0x6<<4); //7.设置TIM4_CH1通道使能TIMx_CCR1预加载使能寄存器,比较寄存器使能 // TIM4_CCR1[3]=0x1TIM4->CCMR1 |= (0x1<<3);//8.设置TIM4_CH1通道输出PWM方波的极性//TIM_CCER[3] = 0b0 TIM4_CCER[1] = 0x1 or 0x0TIM4->CCER &= (~(0x1<<3));TIM4->CCER |= (~(0x1<<1));//TIM4->CCER &= (~(0x1<<1));//9.设置TIM4_CH1通道的输出使能位,输出PWM方波//TIM4_CCER[0] = 0x1TIM4->CCER |= (0x1<<0);//10.设置TIM4_CH1通道的预装载寄存器的缓冲区的使能//TIM4_CR1[7] = 0x1TIM4->CR1 |= (0x1<<7);//11.设置定时器的计数方式,边沿对齐// TIM4_CR1[7]= 0x1TIM4->CR1 &= (~(0x3<<5));//12.设置定时器计数的方向,采用递减计数/递增计数// TIM4_CR1[4]=0x1TIM4->CR1 |= (0x1<<4);//13.使能TIM4_CH1计数器// TIM4_CR1[4]=0x1TIM4->CR1 |= (0x1<<0);
}
TIM4.h
#ifndef __TIM4_H__
#define __TIM4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_tim.h"
void tim4_init();#endif
main.c
#include "tim4.h"int main(void)
{tim4_init();while(1){}return 0;
}
这篇关于2024/5/27 ARMday10 PWM实验蜂鸣器发声的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!