tim.c
#include "tim.h"
#include "stm32mp1xx_tim.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"//tim4初始化(蜂鸣器)
void tim4_init(){//1.使能GPIOB的外设时钟RCC->MP_AHB4ENSETR |= (0x1<<1);//使能TIM4的外设时钟RCC->MP_APB1ENSETR |= (0x1<<2);//2.配置PB6为TIM4的复用功能GPIOB->MODER &= (~(0x3<<12));GPIOB->MODER |= (0x2<<12);//3.GPIOB->AFRL |= (0x02<<24);//4.配置预分配器设置pwm周期TIM4->PSC = 20900-1;//5.配置自动装载寄存器TIM4->ARR = 10000;//6.配置比较寄存器TIM4->CCR1 = 1000;//配置为PWM输出模式TIM4->CCMR1 &= (~(0x1<<16));TIM4->CCMR1 &= (~(0x7<<4));TIM4->CCMR1 |= (0x6<<4);TIM4->CCMR1 &= (~0x3);//7.配置预加载使能寄存器TIM4->CCMR1 |= (0x1<<3);//8.配置极性由高到低TIM4->CCER &= (~(0x1<<3));TIM4->CCER &= (~(0x1<<1)); //9.配置tim4通道的输出使能位TIM4->CCER |= (0x1);//10.配置tim4自动装载缓冲使能TIM4->CR1 |= (0x1<<7);//11.配置计数器为增计数TIM4->CR1 &= (~(0x3<<5));TIM4->CR1 &= (~(0x1<<4));//13.计数器使能TIM4->CR1 |= (0x1);}//tim1初始化(风扇)
void tim1_init(){//1.使能GPIOE的外设时钟RCC->MP_AHB4ENSETR |= (0x1<<4);//使能TIM4的外设时钟RCC->MP_APB2ENSETR |= (0x1);//2.配置PE9为TIM1的复用功能GPIOE->MODER &= (~(0x3<<18));GPIOE->MODER |= (0x2<<18);//3.GPIOE->AFRH &= (~(0xf<<4));GPIOE->AFRH |= (0x1<<4);//4.配置预分配器设置pwm周期TIM1->PSC = 20900-1;//5.配置自动装载寄存器TIM1->ARR = 10000;//6.配置比较寄存器TIM1->CCR1 = 5000;//配置为PWM输出模式TIM1->CCMR1 &= (~(0x1<<16));TIM1->CCMR1 &= (~(0x7<<4));TIM1->CCMR1 |= (0x6<<4);TIM1->CCMR1 &= (~0x3);//7.配置预加载使能寄存器TIM1->CCMR1 |= (0x1<<3);//8.配置极性由高到低TIM1->CCER &= (~(0x1<<3));TIM1->CCER &= (~(0x1<<1));//9.配置tim1通道的输出使能位TIM1->CCER |= (0x1);//10.配置tim1自动装载缓冲使能TIM1->CR1 |= (0x1<<7);//11.配置计数器为增计数TIM1->CR1 &= (~(0x3<<5));TIM1->CR1 &= (~(0x1<<4));//配置为主模式TIM1->BDTR |= (0x1<<15);//13.计数器使能TIM1->CR1 |= (0x1);}//tim16初始化(震动马达)
void tim16_init(){//1.使能GPIOF的外设时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//使能TIM16的外设时钟RCC->MP_APB2ENSETR |= (0x1<<3);//2.配置PF6为TIM16的复用功能GPIOF->MODER &= (~(0x3<<12));GPIOF->MODER |= (0x2<<12);//3.GPIOF->AFRL &= (~(0xf<<24));GPIOF->AFRL |= (0x1<<24);//4.配置预分配器设置pwm周期TIM16->PSC = 20900-1;//5.配置自动装载寄存器TIM16->ARR = 10000;//6.配置比较寄存器TIM16->CCR1 = 5000;//配置为PWM输出模式TIM16->CCMR1 &= (~(0x1<<16));TIM16->CCMR1 &= (~(0x7<<4));TIM16->CCMR1 |= (0x6<<4);TIM1->CCMR1 &= (~0x3);//7.配置预加载使能寄存器TIM16->CCMR1 |= (0x1<<3);//8.配置极性由高到低TIM16->CCER &= (~(0x1<<3));TIM16->CCER &= (~(0x1<<1));//9.配置tim16通道的输出使能位TIM16->CCER |= (0x1);//10.配置tim16自动装载缓冲使能TIM16->CR1 |= (0x1<<7);//11.配置计数器为增计数TIM16->CR1 &= (~(0x3<<5));TIM16->CR1 &= (~(0x1<<4));//配置为主模式TIM16->BDTR |= (0x1<<15);//13.计数器使能TIM16->CR1 |= (0x1);}
tim.h
#ifndef __TIM_H__
#define __TIM_H__//tim4初始化
void tim4_init();//tim1初始化
void tim1_init();//tim16初始化
void tim16_init();#endif
main.c
#include "gpio.h"
#include "led.h"
#include "uart4.h"
#include "keyip.h"
#include "iic.h"
#include "si7006.h"
#include "ap3216c.h"
#include "spi.h"
#include "tim.h"int main()
{tim4_init();tim1_init();tim16_init();while (1){}return 0;
}