FreeRTOS-stm32f407学习笔记-中断实验

这个示例展示了如何在嵌入式系统中使用FreeRTOS创建任务,并通过定时中断来控制任务执行。`main.c`文件初始化了中断任务,并在`interrupt_task()`中利用定时器产生中断,每5次中断关闭并重新开启中断,同时控制LED闪烁。`timer.c`文件配置了TIM3和TIM5定时器,用于中断触发。
摘要由CSDN通过智能技术生成

由于对于中断的了解并不多,这里仅作相关源码(已测可用)的展示

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); 
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值