由于对于中断的了解并不多,这里仅作相关源码(已测可用)的展示
main.c函数
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "timer.h"
#include "FreeRTOS.h"
#include "task.h"
void start_task();
TaskHandle_t StartTask_Handler;
void interrupt_task();
TaskHandle_t InterruptTask_Handler;
int main(void)
{
u8 key;
delay_init(168);
LED_Init();
KEY_Init();
uart_init(115200);
LED0=1;
LED1=1;
xTaskCreate((TaskFunction_t) start_task,
(const char * ) "start_task",
(const uint16_t) 50,
(void * ) NULL,
(UBaseType_t ) 1,
(TaskHandle_t *) &StartTask_Handler );
vTaskStartScheduler() ;
}
void start_task()
{
xTaskCreate((TaskFunction_t) interrupt_task,
(const char * ) "interrupt_task",
(const uint16_t) 50,
(void * ) NULL,
(UBaseType_t ) 2,
(TaskHandle_t *) &InterruptTask_Handler );
vTaskDelete(NULL);
}
void interrupt_task()
{
static u32 total_num=0;
while(1)
{
total_num+=1;
if(total_num%5==0)
{
printf("关闭中断");
portDISABLE_INTERRUPTS();
delay_ms(1000);
printf("开启中断");
portENABLE_INTERRUPTS();
}
LED0=~LED0;
vTaskDelay(1000);
}
}
timer.c文件
#include "timer.h"
#include "sys.h"
#include "led.h"
#include "usart.h"
void My_Timer_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = 8400-1;
TIM_TimeBaseInitStructure.TIM_Prescaler=5000-1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM5,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x04;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x04;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x05;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
printf("TIM3");
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
printf("TIM5");
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}