标题:
基于消息队列的UCOSII STM32源码
[打印本页]
作者:
tangqi334
时间:
2018-10-19 10:40
标题:
基于消息队列的UCOSII STM32源码
此文件适合于STM32上移植UCOSII系统,通过消息队列的方法创建程序,完成3个LED灯的闪烁。
单片机源程序如下:
/****************************************************************************
* 本例程在 奋斗版STM32开发板V2,2.1,V3,MINI上调试通过
*
* 文件名: app.c
* 内容简述:
* 本例程操作系统采用ucos2.86a版本, 建立了2个任务
任务名 优先级
APP_TASK_START_PRIO 2 主任务
Task_Led1_PRIO 7 LED闪烁任务
当然还包含了系统任务:
OS_TaskIdle 空闲任务-----------------优先级最低
OS_TaskStat 统计运行时间的任务-------优先级次低
* 通过建立消息队列,传送3个指针型变量给Led1_Task,经过判断,触发3个led灯的闪烁控制
* 文件历史:
* 版本号 日期 作者 说明
* v0.1 2012-10-08 sun68 创建该文件
*
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define GLOBALS
#include "stdarg.h"
#include "includes.h"
//#include "globals.h"
OS_EVENT *MsgQueue;
void *MsgQueueTbl[20];
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
static OS_STK App_TaskStartStk[APP_TASK_START_STK_SIZE];
static OS_STK Task_Led1Stk[Task_Led1_STK_SIZE];
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void App_TaskCreate(void);
static void App_TaskStart(void* p_arg);
static void Task_Led1(void* p_arg);
#define LED_LED1_ON() GPIO_SetBits(GPIOB, GPIO_Pin_5 ); //LED1 亮
#define LED_LED1_OFF() GPIO_ResetBits(GPIOB, GPIO_Pin_5 ); //LED1 灭
#define LED_LED2_ON() GPIO_SetBits(GPIOD, GPIO_Pin_6 ); //LED2 亮
#define LED_LED2_OFF() GPIO_ResetBits(GPIOD, GPIO_Pin_6 ); //LED2 灭
#define LED_LED3_ON() GPIO_SetBits(GPIOD, GPIO_Pin_3 ); //LED3 亮
#define LED_LED3_OFF() GPIO_ResetBits(GPIOD, GPIO_Pin_3 ); //LED3 灭
/*
*********************************************************************************************************
* main()
*
* Description : This is the standard entry point for C code. It is assumed that your code will call
* main() once you have performed all necessary initialization.
*
* Argument : none.
*
* Return : none.
*********************************************************************************************************
*/
int main(void)
{
CPU_INT08U os_err;
//禁止CPU中断
CPU_IntDis();
//UCOS 初始化
OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel". */
//硬件平台初始化
BSP_Init(); /* Initialize BSP functions. */
//建立主任务, 优先级最高 建立这个任务另外一个用途是为了以后使用统计任务
os_err = OSTaskCreate((void (*) (void *)) App_TaskStart, //指向任务代码的指针
(void *) 0, //任务开始执行时,传递给任务的参数的指针
(OS_STK *) &App_TaskStartStk[APP_TASK_START_STK_SIZE - 1], //分配给任务的堆栈的栈顶指针 从顶向下递减
(INT8U) APP_TASK_START_PRIO); //分配给任务的优先级
//ucos的节拍计数器清0 节拍计数器是0-4294967295 对于节拍频率100hz时, 每隔497天就重新计数
OSTimeSet(0);
OSStart(); /* Start multitasking (i.e. give control to uC/OS-II). */
/* Start multitasking (i.e. give control to uC/OS-II). */
return (0);
}
/*
*********************************************************************************************************
* App_TaskStart()
*
* Description : The startup task. The uC/OS-II ticker should only be initialize once multitasking starts.
*
* Argument : p_arg Argument passed to 'App_TaskStart()' by 'OSTaskCreate()'.
*
* Return : none.
*
* Caller : This is a task.
*
* Note : none.
*********************************************************************************************************
*/
static void App_TaskStart(void* p_arg)
{
char one='1';
char two='2';
char three='3';
(void) p_arg;
//初始化ucos时钟节拍
OS_CPU_SysTickInit(); /* Initialize the SysTick. */
//使能ucos 的统计任务
#if (OS_TASK_STAT_EN > 0)
//----统计任务初始化函数
OSStatInit(); /* Determine CPU capacity. */
#endif
//建立其他的任务
App_TaskCreate();
while (1)
{
OSQPost(MsgQueue,(void *)&one);
OSTimeDlyHMSM(0, 0,1, 0);
OSQPost(MsgQueue,(void *)&two);
OSTimeDlyHMSM(0, 0,0, 500);
OSQPost(MsgQueue,(void *)&three);
OSTimeDlyHMSM(0, 0,1, 0);
}
}
//LED闪烁任务----------------------------------------
static void Task_Led1(void* p_arg)
{
INT8U err;
INT8U *msg;
(void) p_arg;
while (1)
{
msg=(INT8U*)OSQPend(MsgQueue,0,&err); //等待Led1信号量
if(*msg=='1'){
LED_LED1_ON();
LED_LED2_OFF();
LED_LED3_OFF();
}
else if(*msg=='2'){
LED_LED2_ON();
LED_LED1_OFF();
LED_LED3_OFF();
}
else if(*msg=='3'){
LED_LED3_ON();
LED_LED1_OFF();
LED_LED2_OFF();
}
OSTimeDlyHMSM(0, 0,0, 500);
}
}
/*
*********************************************************************************************************
* App_TaskCreate()
*
* Description : Create the application tasks.
*
* Argument : none.
*
* Return : none.
*
* Caller : App_TaskStart().
*
* Note : none.
*********************************************************************************************************
*/
static void App_TaskCreate(void)
{
//CPU_INT08U os_err;
MsgQueue=OSQCreate(&MsgQueueTbl[0],20); //建立消息队列
//LED1 闪烁任务------------------------------------------------------
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],
Task_Led1_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
}
/*
*********************************************************************************************************
*********************************************************************************************************
* uC/OS-II APP HOOKS
*********************************************************************************************************
*********************************************************************************************************
*/
#if (OS_APP_HOOKS_EN > 0)
/*
*********************************************************************************************************
* TASK CREATION HOOK (APPLICATION)
*
* Description : This function is called when a task is created.
*
* Argument : ptcb is a pointer to the task control block of the task being created.
*
* Note : (1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
void App_TaskCreateHook(OS_TCB* ptcb)
{
}
/*
*********************************************************************************************************
* TASK DELETION HOOK (APPLICATION)
*
* Description : This function is called when a task is deleted.
*
* Argument : ptcb is a pointer to the task control block of the task being deleted.
*
* Note : (1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
void App_TaskDelHook(OS_TCB* ptcb)
{
(void) ptcb;
}
/*
*********************************************************************************************************
* IDLE TASK HOOK (APPLICATION)
*
* Description : This function is called by OSTaskIdleHook(), which is called by the idle task. This hook
* has been added to allow you to do such things as STOP the CPU to conserve power.
*
* Argument : none.
*
* Note : (1) Interrupts are enabled during this call.
*********************************************************************************************************
*/
#if OS_VERSION >= 251
void App_TaskIdleHook(void)
{
}
#endif
/*
*********************************************************************************************************
* STATISTIC TASK HOOK (APPLICATION)
*
* Description : This function is called by OSTaskStatHook(), which is called every second by uC/OS-II's
* statistics task. This allows your application to add functionality to the statistics task.
*
* Argument : none.
*********************************************************************************************************
*/
void App_TaskStatHook(void)
{
}
/*
*********************************************************************************************************
* TASK SWITCH HOOK (APPLICATION)
*
* Description : This function is called when a task switch is performed. This allows you to perform other
* operations during a context switch.
*
* Argument : none.
*
* Note : 1 Interrupts are disabled during this call.
*
* 2 It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
* will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
* task being switched out (i.e. the preempted task).
*********************************************************************************************************
*/
#if OS_TASK_SW_HOOK_EN > 0
void App_TaskSwHook(void)
{
}
#endif
/*
*********************************************************************************************************
* OS_TCBInit() HOOK (APPLICATION)
*
* Description : This function is called by OSTCBInitHook(), which is called by OS_TCBInit() after setting
* up most of the TCB.
*
* Argument : ptcb is a pointer to the TCB of the task being created.
*
* Note : (1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
#if OS_VERSION >= 204
void App_TCBInitHook(OS_TCB* ptcb)
{
(void) ptcb;
}
#endif
#endif
复制代码
全部资料51hei下载地址:
STM32-LED闪烁-消息队列.rar
(382.32 KB, 下载次数: 19)
2018-10-19 10:40 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1