一、开发环境
开发板:STM32F407ZE
二、功能:
TIM3中断每1秒亮1次LED1 TIM2中断每1秒亮2次LED2 TIM5中断每1秒亮3次LED3 TIM1中断每1秒亮4次LED4
三、源码
#include "tim.h"
#include "led.h"
#include "stm32f4xx_tim.h"
#include "misc.h"
void tim_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;
TIM_TimeBaseInitStruct.TIM_Period = 5000-1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;
TIM_TimeBaseInitStruct.TIM_Period = 2500-1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;
TIM_TimeBaseInitStruct.TIM_Period = 1666-1;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 16800-1;
TIM_TimeBaseInitStruct.TIM_Period = 1250-1;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM5, ENABLE);
TIM_Cmd(TIM1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = TIM5_IRQn;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_Init(&NVIC_InitStruct);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
{
PFOUT(9) ^= 1;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
PFOUT(10) ^= 1;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) == SET)
{
PEOUT(13) ^= 1;
TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
}
}
void TIM1_UP_TIM10_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
{
PEOUT(14) ^= 1;
TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
}
}
|