根据正点原子FreeRTOS视频整理

单片机:STM32F207VC

FreeRTOS源码版本:v10.0.1

portDISABLE_INTERRUPTS();  /*关中断*/
portENABLE_INTERRUPTS();  /*开中断*/

 

工程列表:

FreeRTOS-03中断测试

 

实验说明:

1. 定时器3控制一个灯闪烁,抢占优先级为4;定时器5控制一个灯闪烁,抢占优先级为5.

2. InterruptTask()函数,一段时间后关闭中断,过一段时间再开启中断,如此反复。

3. 实验现象应该是,关闭中断后,优先级为4的灯继续闪烁,优先级为5的灯停止闪烁;

    开启中断后,2个灯都在闪烁。

 

1. main.c

  1 /*
  2  * 实验说明:
  3  * 1. 定时器3控制一个灯闪烁,抢占优先级为4;定时器4控制1个灯闪烁,抢占优先级为5.
  4  * 2. InterruptTask()函数,一段时间后关闭中断,过一段时间再开启中断,如此反复。
  5  * 备注:
  6  * 1. portDISABLE_INTERRUPTS();关中断
  7  * 2. portENABLE_INTERRUPTS();开中断
  8  * 3. 关中断后,优先级低于configMAX_SYSCALL_INTERRUPT_PRIORITY的任务,会被关掉。
  9  * 4. 数值越大,优先级越低。
 10  */
 11 #include "main.h"
 12 #include "gpio.h"
 13 #include "delay.h"
 14 #include "sys.h"
 15 #include "timer.h"
 16 
 17 #include "stm32f2xx_gpio.h"
 18 
 19 #include "FreeRTOS.h"
 20 #include "task.h"
 21 
 22 #define START_TASK_PRIO             1     /*任务优先级*/
 23 #define START_STK_SIZE              128   /*任务堆栈大小*/
 24 TaskHandle_t StartTask_Handle;            /*任务句柄*/
 25 void StartTask(void *pvParameters);       /*任务函数*/
 26 
 27 #define INTERRUPT_TASK_PRIO         2
 28 #define INTERRUPT_STK_SIZE          256
 29 TaskHandle_t InterruptTask_Handle;
 30 void InterruptTask(void *pvParameters);
 31 
 32 
 33 
 34 /***** 声明 *****/
 35 static void SystemInitial(void);
 36 
 37 
 38 void StartTask(void *pvParameters)
 39 {
 40   taskENTER_CRITICAL();           /*进入临界区*/
 41   
 42   xTaskCreate((TaskFunction_t )InterruptTask,             /*任务函数*/
 43                             (const char *   )"InterruptTask",              /*任务名称*/
 44                             (uint16_t       )INTERRUPT_STK_SIZE,        /*任务堆栈大小*/
 45                             (void *         )NULL,                      /*传递给任务函数的参数*/
 46                             (UBaseType_t    )INTERRUPT_TASK_PRIO,       /*任务优先级*/
 47                             (TaskHandle_t   )&InterruptTask_Handle);    /*任务句柄*/             
 48 
 49   vTaskDelete(StartTask_Handle);    /*删除开始任务*/
 50   taskEXIT_CRITICAL();              /*推出临界区*/
 51 } 
 52 
 53 void InterruptTask(void *pvParameters)
 54 {
 55   static uint8_t i = 0;
 56   
 57   while (1)
 58   {
 59     i++;
 60     if (5==i)
 61     {
 62       portDISABLE_INTERRUPTS();
 63     }
 64     else if (10 == i)
 65     {
 66       i = 0;
 67       portENABLE_INTERRUPTS();
 68     }
 69     else
 70       ;
 71     
 72     DelayXms(1000);
 73   }
 74 }
 75 
 76 static void SystemInitial(void)
 77 {
 78   /*组4,16级抢占优先级,无响应优先级*/
 79   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
 80   
 81   DelayInitial();
 82   GPIO_Initial();
 83   TimerInitial();
 84 }
 85 
 86 int main(void)
 87 {
 88   SystemInitial();
 89  
 90   
 91   /*创建开始任务*/
 92   xTaskCreate((TaskFunction_t )StartTask,           /*任务函数*/
 93                             (const char *   )"StartTask",            /*任务名称*/
 94                             (uint16_t       )START_STK_SIZE,      /*任务堆栈大小*/
 95                             (void *         )NULL,                /*传递给任务函数的参数*/
 96                             (UBaseType_t    )START_TASK_PRIO,     /*任务优先级*/
 97                             (TaskHandle_t   )&StartTask_Handle);  /*任务句柄*/             
 98               
 99   /*开启任务调度*/
100   vTaskStartScheduler();
101 }
102 
103 /***************************END OF FILE***************************/
View Code

相关文章:

  • 2021-09-18
  • 2021-07-11
  • 2021-07-12
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2022-02-14
  • 2021-12-23
猜你喜欢
  • 2021-07-24
  • 2021-05-02
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
相关资源
相似解决方案