STM32F4|STM32F4 TIM6 TIM7 基本定时器

以下为 TIM.C

#include "tim.h" #include "led.h" #include "usart.h"void TIM6_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE); TIM_TimeBaseInitStructure.TIM_Period = arr; TIM_TimeBaseInitStructure.TIM_Prescaler=psc; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM6,&TIM_TimeBaseInitStructure); TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE); TIM_Cmd(TIM6,ENABLE); NVIC_InitStructure.NVIC_IRQChannel=TIM6_DAC_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x03; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); } void TIM7_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE); TIM_TimeBaseInitStructure.TIM_Period = arr; TIM_TimeBaseInitStructure.TIM_Prescaler=psc; TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM7,&TIM_TimeBaseInitStructure); TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE); TIM_Cmd(TIM7,ENABLE); NVIC_InitStructure.NVIC_IRQChannel=TIM6_DAC_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x03; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); }void TIM6_DAC_IRQHandler(void) { if(TIM_GetITStatus(TIM6,TIM_IT_Update)==SET) { GPIO_ToggleBits(GPIOF,GPIO_Pin_9 ); TIM_ClearITPendingBit(TIM6,TIM_IT_Update); }if(TIM_GetITStatus(TIM7,TIM_IT_Update)==SET) { GPIO_ToggleBits(GPIOF,GPIO_Pin_10); TIM_ClearITPendingBit(TIM7,TIM_IT_Update); }}

以下为 tim.h
#ifndef _TIMER_H #define _TIMER_H #include "sys.h"void TIM6_Int_Init(u16 arr,u16 psc); void TIM7_Int_Init(u16 arr,u16 psc); #endif

主函数进行初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); LED_Init(); TIM6_Int_Init(5000-1,8400-1); TIM7_Int_Init(10000-1,8400-1);

    推荐阅读