Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20500
  • 博文数量: 23
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-08 23:32
文章分类

全部博文(23)

文章存档

2013年(23)

我的朋友

分类: C/C++

2013-08-12 12:09:38

原文地址:ucosII之消息 作者:number007cool

程序功能如下:
任务2给串口发送数据,然后给任务4发送消息。任务4收到消息后给串口发送数据。
当任务2不给任务4发送消息的时候,任务4就不给串口发送数据。
程序如下:
#include "STM32Lib\\stm32f10x.h"
#include "hal.h"
#include "UCOSII\\ucos_ii.h"
/******************************
** 任务堆栈
*******************************/
OS_STK   Task0Stk[128];
OS_STK   Task2Stk[128];
OS_STK   Task3Stk[128];
OS_STK   Task4Stk[128];

/******************************
 任务函数
******************************/
void  Task0     (void *p_arg);
void  Task2     (void *p_arg);
void  Task3     (void *p_arg);
void  Task4     (void *p_arg);
OS_EVENT *sem;
OS_EVENT *mybox;
int main(void)
{
 u8 os_err;
 
 //由于使用UCOS,一下的初始化虽然可以在OS运行之前运行,但注意别使能任何中断.
 ChipHalInit();   //片内硬件初始化
 ChipOutHalInit();  //片外硬件初始化
 OSInit();    //UCOS INIT~
 
 os_err = OSTaskCreate( Task0,          //第一个任务0
                          (void          * ) 0,       //不带参数
                          (OS_STK        * )&Task0Stk[128 - 1],   //堆栈指针
                          (INT8U           ) 5       //优先级
                         );
 OSStart();
 
 while(1);
}
void Task0(void *p_arg)
{
 u8 os_err;
 char DATA_1[]="TASK1_ON\r\n";
 
 
 OS_CPU_SysTickInit(); //使能SYSTICK中断
 
#if (OS_TASK_STAT_EN > 0)
    OSStatInit();                                               /*这东西可以测量CPU使用量 */
#endif

 //使能其他板上中断
 //NVIC_Configuration();
 
 os_err = OSTaskCreate( Task2,          //任务1
                           &DATA_1,       //不带参数
                          (OS_STK        * )&Task2Stk[128 - 1],   //堆栈指针
                          (INT8U           ) 6       //优先级
                         );
 
 os_err = OSTaskCreate( Task3,          //任务2
                          (void          * ) 0,       //不带参数
                          (OS_STK        * )&Task3Stk[128 - 1],   //堆栈指针
                          (INT8U           ) 7       //优先级
                         );
#if 1
 os_err = OSTaskCreate( Task4,          //任务3
                          (void          * ) 0,       //不带参数
                          (OS_STK        * )&Task4Stk[128 - 1],   //堆栈指针
                          (INT8U           ) 8       //优先级
                         );
#endif
 for(;;)
 {
     //OSTaskDel(2);
  OSTimeDly(OS_TICKS_PER_SEC/2);
  //USART1_Puts("TASK0_ON\r\n");
  //LED1_ON;
  //OSTimeDly(OS_TICKS_PER_SEC/10);
  //LED1_OFF;
  //USART1_Puts("TASK0_OFF\r\n");
 }
}

void Task4(void *p_arg)
{
    INT8U err;
 for(;;)
 {
     //等待消息
     OSMboxPend(mybox,0,&err);
     USART1_Puts("4\r\n");
  OSTimeDly(OS_TICKS_PER_SEC/10);
 }
}
  
void Task2(void *p_arg)
{
    INT8U err;
    sem=OSSemCreate(0);//创建信号量
 mybox=OSMboxCreate((void *)0); //创建消息
 for(;;)
 {
  USART1_Puts("1\r\n");
     OSTimeDly(OS_TICKS_PER_SEC/10);
  //发送信号量
  //OSSemPost(sem);
  //发送消息
  OSMboxPost(mybox,(void *)0);
  OSTimeDly(OS_TICKS_PER_SEC/10);
 }
}
void Task3(void *p_arg)
{
    INT8U err;
 for(;;)
 {
     //等待信号量
     OSSemPend(sem,0,&err);
  USART1_Puts("3\r\n");
  OSTimeDly(OS_TICKS_PER_SEC/10);
 }
}
几个关键函数的原型如下:
1、创建消息
/*
*********************************************************************************************************
*                                        CREATE A MESSAGE MAILBOX
*
* Description: This function creates a message mailbox if free event control blocks are available.
*
* Arguments  : pmsg          is a pointer to a message that you wish to deposit in the mailbox.  If
*                            you set this value to the NULL pointer (i.e. (void *)0) then the mailbox
*                            will be considered empty.
*
* Returns    : != (OS_EVENT *)0  is a pointer to the event control clock (OS_EVENT) associated with the
*                                created mailbox
*              == (OS_EVENT *)0  if no event control blocks were available
*********************************************************************************************************
*/
OS_EVENT  *OSMboxCreate (void *pmsg)
2、发送消息
/*
*********************************************************************************************************
*                                       POST MESSAGE TO A MAILBOX
*
* Description: This function sends a message to a mailbox
*
* Arguments  : pevent        is a pointer to the event control block associated with the desired mailbox
*
*              pmsg          is a pointer to the message to send.  You MUST NOT send a NULL pointer.
*
* Returns    : OS_ERR_NONE          The call was successful and the message was sent
*              OS_ERR_MBOX_FULL     If the mailbox already contains a message.  You can can only send one
*                                   message at a time and thus, the message MUST be consumed before you
*                                   are allowed to send another one.
*              OS_ERR_EVENT_TYPE    If you are attempting to post to a non mailbox.
*              OS_ERR_PEVENT_NULL   If 'pevent' is a NULL pointer
*              OS_ERR_POST_NULL_PTR If you are attempting to post a NULL pointer
*
* Note(s)    : 1) HPT means Highest Priority Task
*********************************************************************************************************
*/
INT8U  OSMboxPost (OS_EVENT *pevent, void *pmsg)
3、等待消息
/*
*********************************************************************************************************
*                                           PEND ON SEMAPHORE
*
* Description: This function waits for a semaphore.
*
* Arguments  : pevent        is a pointer to the event control block associated with the desired
*                            semaphore.
*
*              timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will
*                            wait for the resource up to the amount of time specified by this argument.
*                            If you specify 0, however, your task will wait forever at the specified
*                            semaphore or, until the resource becomes available (or the event occurs).
*
*              perr          is a pointer to where an error message will be deposited.  Possible error
*                            messages are:
*
*                            OS_ERR_NONE         The call was successful and your task owns the resource
*                                                or, the event you are waiting for occurred.
*                            OS_ERR_TIMEOUT      The semaphore was not received within the specified
*                                                'timeout'.
*                            OS_ERR_PEND_ABORT   The wait on the semaphore was aborted.
*                            OS_ERR_EVENT_TYPE   If you didn't pass a pointer to a semaphore.
*                            OS_ERR_PEND_ISR     If you called this function from an ISR and the result
*                                                would lead to a suspension.
*                            OS_ERR_PEVENT_NULL  If 'pevent' is a NULL pointer.
*                            OS_ERR_PEND_LOCKED  If you called this function when the scheduler is locked
*
* Returns    : none
*********************************************************************************************************
*/
/*$PAGE*/
void  OSSemPend (OS_EVENT *pevent, INT16U timeout, INT8U *perr)
阅读(339) | 评论(0) | 转发(0) |
0

上一篇:ucosII之信号量

下一篇:ucos ii综合测试程序

给主人留下些什么吧!~~