[应用方案] UCOS内核2.83 基础知识总结

[复制链接]
764|3
 楼主| ideafor 发表于 2016-4-22 09:12 | 显示全部楼层 |阅读模式
一:什么是邮箱
邮箱是UCOS中的一种通信机制,可以使一个任务或者中断服务子程序向另一个任务发送一个指针型的变量。通常该指针指向一个包含了“消息”的特定数据结构。
邮箱可以建多个,邮箱的最大数量由OS_CFG.H文件中的配置常数OS_MAX_EVENTS设定。一个邮箱只能保存一则消息。
二:邮箱的使用目的
1.通知一个事件的发生(发送一条消息)那么初始化时就要把该邮箱设空(NULL)。
2.如果用邮箱共享某些资源,那么就要初始化该邮箱为一个非NULL的指针,这种情况下,邮箱被当作一个二值信号量来使用。
三:程序示例
  1. Void main(void)

  2. {

  3.        OSInit();

  4.     AckMbox = OSMboxCreate( ( void * ) 0 );

  5.     TxMbox  = OSMboxCreate( ( void * ) 0 );

  6.     OSStart();

  7. }



  8. Void Task1( void * data)

  9. {

  10.        Char * txmsg;



  11.     Txmsg = ‘A’ ;

  12.     For (; ;){

  13.            OSMboxPost(TxMbox, ( void * )&txmsg );

  14.            OSMboxPend(AckMbox,0,&err) ;

  15.            Txmsg ++;

  16.            If ( txmsg == ‘Z’){

  17.                Txmsg = ‘A’ ;

  18.                }

  19.          }

  20. }



  21. Void Task2 ( void * data )

  22. {

  23.     Char * rxmsg;

  24.     INT8U err ;



  25.     For ( ; ;){

  26.        Rxmsg =  ( char *)OSMboxPend(TxMbox, 0,&err)

  27.        PC_DispChar ( 70 ,18 ,* rxmsg ,DISP_FGND_YELLOW+DISP_BGND_RED);

  28.        OSTimeDly (1);

  29.        OSMboxPost ( AckMbox , (void *) 1);

  30.        }

  31. }

  32. NO2:

  33.    Void main (void)

  34. {

  35.        OS_EVENT * MboxSem;

  36.        OSInit();

  37.        MboxSem = OSMboxCreate( ( void * ) 1 );

  38.        OSStart();

  39. }



  40. Void Task1 ( void * pdata )

  41. {

  42.        INY8U err ;

  43.        For ( ; ; ){

  44.        OSMboxPend ( MboxSem , 0 , &err );

  45.               .

  46.               .

  47.               .

  48.        OSMboxPost ( MboxSem , (void *) 1 );

  49.       }

  50. }


 楼主| ideafor 发表于 2016-4-22 09:13 | 显示全部楼层
四.主要函数的介绍:
1.OS_EVENT  *OSMboxCreate (void*msg)
Description: This function waits for a message tobe sent to a mailbox
Arguments : msg is a pointer to a messagethat you wish to deposit in the mailbox. If you set this value tothe NULL pointer (i.e. (void *)0) then the mailbox will beconsidered empty.
Returns   : != (OS_EVENT *)0  is a pointer to the eventcontrol clock (OS_EVENT) associated with the createdmailbox
            ==(OS_EVENT *)0  if no event control blocks wereavailable
2.void  *OSMboxPend (OS_EVENT *pevent, INT16Utimeout, INT8U *err)
Description: This function waits for a message tobe sent to a mailbox
Arguments : pevent is a pointer to the eventcontrol block associated  with the desiredmailbox
Notes: when use OSMboxPend , the mailbox will beempty .
Returns   : != (void *)0  is a pointer to the messagereceived
             == (void *)0  if no message was received or , if'pevent'  is a NULL pointer or, if you didn't passthe proper pointer to the event control block.
3.INT8U  OSMboxPost (OS_EVENT *pevent, void*msg)
Description: This function sends a message to amailbox
Arguments  : pevent is a pointerto the event control block associated with the desiredmailbox.
             msgis a pointer to the message to send.  You MUST NOTsend a NULL pointer.
Returns   : OS_NO_ERR  The call wassuccessful and the message was sent

捉虫天师 发表于 2016-4-22 13:43 | 显示全部楼层
原来是采用了任务调度机制
neeringstu 发表于 2016-4-22 23:03 | 显示全部楼层
感觉用邮箱还不如直接用全局数组方便呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

30

主题

149

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部