找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2213|回复: 0
收起左侧

基于消息队列的UCOSII STM32源码

[复制链接]
ID:411722 发表于 2018-10-19 10:40 | 显示全部楼层 |阅读模式
此文件适合于STM32上移植UCOSII系统,通过消息队列的方法创建程序,完成3个LED灯的闪烁。

单片机源程序如下:
  1. /****************************************************************************
  2. * 本例程在 奋斗版STM32开发板V2,2.1,V3,MINI上调试通过           
  3.   
  4. *
  5. * 文件名: app.c
  6. * 内容简述:
  7. *       本例程操作系统采用ucos2.86a版本, 建立了2个任务
  8.                         任务名                                                                                         优先级
  9.                         APP_TASK_START_PRIO                               2                主任务                 
  10.             Task_Led1_PRIO                                    7                        LED闪烁任务
  11.                  当然还包含了系统任务:
  12.                     OS_TaskIdle                  空闲任务-----------------优先级最低
  13.                         OS_TaskStat                  统计运行时间的任务-------优先级次低
  14. * 通过建立消息队列,传送3个指针型变量给Led1_Task,经过判断,触发3个led灯的闪烁控制
  15. * 文件历史:
  16. * 版本号  日期       作者    说明
  17. * v0.1    2012-10-08 sun68  创建该文件
  18. *
  19. */

  20. /*
  21. *********************************************************************************************************
  22. *                                             INCLUDE FILES
  23. *********************************************************************************************************
  24. */
  25. #define GLOBALS

  26. #include "stdarg.h"

  27. #include "includes.h"
  28. //#include "globals.h"

  29. OS_EVENT  *MsgQueue;
  30. void *MsgQueueTbl[20];

  31. /*
  32. *********************************************************************************************************
  33. *                                            LOCAL DEFINES
  34. *********************************************************************************************************
  35. */



  36. /*
  37. *********************************************************************************************************
  38. *                                       LOCAL GLOBAL VARIABLES
  39. *********************************************************************************************************
  40. */

  41. static  OS_STK App_TaskStartStk[APP_TASK_START_STK_SIZE];

  42. static  OS_STK Task_Led1Stk[Task_Led1_STK_SIZE];

  43.                

  44. /*
  45. *********************************************************************************************************
  46. *                                      LOCAL FUNCTION PROTOTYPES
  47. *********************************************************************************************************
  48. */
  49. static  void App_TaskCreate(void);

  50. static  void App_TaskStart(void* p_arg);

  51. static  void Task_Led1(void* p_arg);



  52. #define LED_LED1_ON()   GPIO_SetBits(GPIOB, GPIO_Pin_5 );                 //LED1  亮
  53. #define LED_LED1_OFF()  GPIO_ResetBits(GPIOB, GPIO_Pin_5 );            //LED1  灭

  54. #define LED_LED2_ON()   GPIO_SetBits(GPIOD, GPIO_Pin_6 );                 //LED2  亮
  55. #define LED_LED2_OFF()  GPIO_ResetBits(GPIOD, GPIO_Pin_6 );            //LED2  灭

  56. #define LED_LED3_ON()   GPIO_SetBits(GPIOD, GPIO_Pin_3 );                 //LED3  亮
  57. #define LED_LED3_OFF()  GPIO_ResetBits(GPIOD, GPIO_Pin_3 );            //LED3  灭
  58. /*
  59. *********************************************************************************************************
  60. *                                                main()
  61. *
  62. * Description : This is the standard entry point for C code.  It is assumed that your code will call
  63. *               main() once you have performed all necessary initialization.
  64. *
  65. * Argument : none.
  66. *
  67. * Return   : none.
  68. *********************************************************************************************************
  69. */

  70. int main(void)
  71. {
  72.    CPU_INT08U os_err;

  73.    //禁止CPU中断
  74.    CPU_IntDis();
  75.    
  76.    //UCOS 初始化
  77.    OSInit();                                                   /* Initialize "uC/OS-II, The Real-Time Kernel".         */
  78.    
  79.    //硬件平台初始化
  80.    BSP_Init();                                                 /* Initialize BSP functions.  */
  81.    
  82.     //建立主任务, 优先级最高  建立这个任务另外一个用途是为了以后使用统计任务
  83.    os_err = OSTaskCreate((void (*) (void *)) App_TaskStart,                                            //指向任务代码的指针
  84.                           (void *) 0,                                                                                  //任务开始执行时,传递给任务的参数的指针
  85.                (OS_STK *) &App_TaskStartStk[APP_TASK_START_STK_SIZE - 1],        //分配给任务的堆栈的栈顶指针   从顶向下递减
  86.                (INT8U) APP_TASK_START_PRIO);                                                                //分配给任务的优先级
  87.    
  88.    //ucos的节拍计数器清0    节拍计数器是0-4294967295    对于节拍频率100hz时, 每隔497天就重新计数
  89.    OSTimeSet(0);
  90.    OSStart();                                                  /* Start multitasking (i.e. give control to uC/OS-II).  */
  91.                                                  /* Start multitasking (i.e. give control to uC/OS-II).  */

  92.    return (0);
  93. }




  94. /*
  95. *********************************************************************************************************
  96. *                                          App_TaskStart()
  97. *
  98. * Description : The startup task.  The uC/OS-II ticker should only be initialize once multitasking starts.
  99. *
  100. * Argument : p_arg       Argument passed to 'App_TaskStart()' by 'OSTaskCreate()'.
  101. *
  102. * Return   : none.
  103. *
  104. * Caller   : This is a task.
  105. *
  106. * Note     : none.
  107. *********************************************************************************************************
  108. */

  109. static  void App_TaskStart(void* p_arg)
  110. {
  111.    char one='1';
  112.    char two='2';
  113.    char three='3';

  114.    (void) p_arg;
  115.    //初始化ucos时钟节拍
  116.    OS_CPU_SysTickInit();                                       /* Initialize the SysTick.       */

  117. //使能ucos 的统计任务
  118. #if (OS_TASK_STAT_EN > 0)
  119.    //----统计任务初始化函数  
  120.    OSStatInit();                                               /* Determine CPU capacity.                              */
  121. #endif
  122.    //建立其他的任务
  123.    App_TaskCreate();

  124.    while (1)
  125.    {
  126.           OSQPost(MsgQueue,(void *)&one);           
  127.           OSTimeDlyHMSM(0, 0,1, 0);
  128.           OSQPost(MsgQueue,(void *)&two);           
  129.           OSTimeDlyHMSM(0, 0,0, 500);       
  130.           OSQPost(MsgQueue,(void *)&three);           
  131.           OSTimeDlyHMSM(0, 0,1, 0);  
  132.    }
  133. }


  134. //LED闪烁任务----------------------------------------
  135. static  void Task_Led1(void* p_arg)
  136. {
  137.    INT8U err;
  138.    INT8U *msg;
  139.    (void) p_arg;            
  140.    while (1)
  141.    {
  142.       msg=(INT8U*)OSQPend(MsgQueue,0,&err);                            //等待Led1信号量  
  143.           if(*msg=='1'){
  144.                   LED_LED1_ON();        
  145.               LED_LED2_OFF();
  146.                   LED_LED3_OFF();
  147.           }          
  148.           else if(*msg=='2'){
  149.                   LED_LED2_ON();        
  150.               LED_LED1_OFF();
  151.                   LED_LED3_OFF();
  152.           }          
  153.           else if(*msg=='3'){
  154.                   LED_LED3_ON();        
  155.               LED_LED1_OFF();
  156.                   LED_LED2_OFF();
  157.           }          
  158.           OSTimeDlyHMSM(0, 0,0, 500);  
  159.    }
  160. }

  161. /*
  162. *********************************************************************************************************
  163. *                                            App_TaskCreate()
  164. *
  165. * Description : Create the application tasks.
  166. *
  167. * Argument : none.
  168. *
  169. * Return   : none.
  170. *
  171. * Caller   : App_TaskStart().
  172. *
  173. * Note     : none.
  174. *********************************************************************************************************
  175. */

  176. static  void App_TaskCreate(void)
  177. {
  178.    //CPU_INT08U os_err;

  179.    MsgQueue=OSQCreate(&MsgQueueTbl[0],20);                                 //建立消息队列
  180.    

  181.                
  182.    //LED1 闪烁任务------------------------------------------------------
  183.    OSTaskCreateExt(Task_Led1,(void *)0,(OS_STK *)&Task_Led1Stk[Task_Led1_STK_SIZE-1],Task_Led1_PRIO,Task_Led1_PRIO,(OS_STK *)&Task_Led1Stk[0],
  184.                     Task_Led1_STK_SIZE,
  185.                     (void *)0,
  186.                     OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  187. }

  188. /*
  189. *********************************************************************************************************
  190. *********************************************************************************************************
  191. *                                          uC/OS-II APP HOOKS
  192. *********************************************************************************************************
  193. *********************************************************************************************************
  194. */

  195. #if (OS_APP_HOOKS_EN > 0)
  196. /*
  197. *********************************************************************************************************
  198. *                                      TASK CREATION HOOK (APPLICATION)
  199. *
  200. * Description : This function is called when a task is created.
  201. *
  202. * Argument : ptcb   is a pointer to the task control block of the task being created.
  203. *
  204. * Note     : (1) Interrupts are disabled during this call.
  205. *********************************************************************************************************
  206. */

  207. void App_TaskCreateHook(OS_TCB* ptcb)
  208. {
  209. }

  210. /*
  211. *********************************************************************************************************
  212. *                                    TASK DELETION HOOK (APPLICATION)
  213. *
  214. * Description : This function is called when a task is deleted.
  215. *
  216. * Argument : ptcb   is a pointer to the task control block of the task being deleted.
  217. *
  218. * Note     : (1) Interrupts are disabled during this call.
  219. *********************************************************************************************************
  220. */

  221. void App_TaskDelHook(OS_TCB* ptcb)
  222. {
  223.    (void) ptcb;
  224. }

  225. /*
  226. *********************************************************************************************************
  227. *                                      IDLE TASK HOOK (APPLICATION)
  228. *
  229. * Description : This function is called by OSTaskIdleHook(), which is called by the idle task.  This hook
  230. *               has been added to allow you to do such things as STOP the CPU to conserve power.
  231. *
  232. * Argument : none.
  233. *
  234. * Note     : (1) Interrupts are enabled during this call.
  235. *********************************************************************************************************
  236. */

  237. #if OS_VERSION >= 251
  238. void App_TaskIdleHook(void)
  239. {
  240. }
  241. #endif

  242. /*
  243. *********************************************************************************************************
  244. *                                        STATISTIC TASK HOOK (APPLICATION)
  245. *
  246. * Description : This function is called by OSTaskStatHook(), which is called every second by uC/OS-II's
  247. *               statistics task.  This allows your application to add functionality to the statistics task.
  248. *
  249. * Argument : none.
  250. *********************************************************************************************************
  251. */

  252. void App_TaskStatHook(void)
  253. {
  254. }

  255. /*
  256. *********************************************************************************************************
  257. *                                        TASK SWITCH HOOK (APPLICATION)
  258. *
  259. * Description : This function is called when a task switch is performed.  This allows you to perform other
  260. *               operations during a context switch.
  261. *
  262. * Argument : none.
  263. *
  264. * Note     : 1 Interrupts are disabled during this call.
  265. *
  266. *            2  It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
  267. *                   will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
  268. *                  task being switched out (i.e. the preempted task).
  269. *********************************************************************************************************
  270. */

  271. #if OS_TASK_SW_HOOK_EN > 0
  272. void App_TaskSwHook(void)
  273. {
  274. }
  275. #endif

  276. /*
  277. *********************************************************************************************************
  278. *                                     OS_TCBInit() HOOK (APPLICATION)
  279. *
  280. * Description : This function is called by OSTCBInitHook(), which is called by OS_TCBInit() after setting
  281. *               up most of the TCB.
  282. *
  283. * Argument : ptcb    is a pointer to the TCB of the task being created.
  284. *
  285. * Note     : (1) Interrupts may or may not be ENABLED during this call.
  286. *********************************************************************************************************
  287. */

  288. #if OS_VERSION >= 204
  289. void App_TCBInitHook(OS_TCB* ptcb)
  290. {
  291.    (void) ptcb;
  292. }
  293. #endif

  294. #endif
复制代码

全部资料51hei下载地址:
STM32-LED闪烁-消息队列.rar (382.32 KB, 下载次数: 19)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表