搜索
bottom↓
回复: 6

UCOS操作系统的移植.

[复制链接]

出0入0汤圆

发表于 2006-12-11 08:38:11 | 显示全部楼层 |阅读模式
我感觉咱们的论坛,用UCOS的不是很多,难道大家不喜欢用么??我的程序发信号不好用,不知道为什么?我把发信号的OSSemPost(LedSem); //发信号注释掉,可是接收信号的那个任务还是能执行,按道理我没发信号,那个任务就不应该执行.用感性区的共同讨论一下.

下面是主程序.

/*

*************************************************************************************************************

*                                                uC/OS-II

*                                          The Real-Time Kernel

*

*                                         ATmega128  Sample code

*

* File : APP.C

* By   : Jean J. Labrosse

*************************************************************************************************************

*/

#include <mega64.h>

#include "ucos_ii.h"

#include "lio.h"

#include <lcd240128.h>

/*

**************************************************************************************************************

*                                               CONSTANTS

*

* Note(s) : 1) See OS_CFG.H for the default stack size: 'OS_TASK_STK_SIZE'

**************************************************************************************************************

*/



#define  CPU_CLK_FREQ                  8000000L





#define  OS_TASK_START_STK_SIZE        OS_TASK_STK_SIZE

#define  OS_TASK_START_HARD_STK_SIZE   OS_TASK_HARD_STK_SIZE



#define  OS_TASK_1_STK_SIZE            OS_TASK_STK_SIZE

#define  OS_TASK_1_HARD_STK_SIZE       OS_TASK_HARD_STK_SIZE



#define  OS_TASK_2_STK_SIZE            OS_TASK_STK_SIZE

#define  OS_TASK_2_HARD_STK_SIZE       OS_TASK_HARD_STK_SIZE



#define  OS_TASK_3_STK_SIZE            OS_TASK_STK_SIZE

#define  OS_TASK_3_HARD_STK_SIZE       OS_TASK_HARD_STK_SIZE



/*

**************************************************************************************************************

*                                               VARIABLES

**************************************************************************************************************

*/



OS_STK  AppTaskStartStk[OS_TASK_START_STK_SIZE];

OS_STK  AppTask1Stk[OS_TASK_1_STK_SIZE];

OS_STK  AppTask2Stk[OS_TASK_1_STK_SIZE];

OS_STK  AppTask3Stk[OS_TASK_1_STK_SIZE+64];





OS_EVENT        *LedSem;



typedef struct time_struct

{

        unsigned char Date;

        unsigned char Hour;

        unsigned char Minute;

        unsigned char Second;

}TIMER;



TIMER Timer;



/*

**************************************************************************************************************

*                                           FUNCTION PROTOTYPES

**************************************************************************************************************

*/





      

static void  AppTaskStart(void *p_arg);

static void  AppTaskCreate(void);



static void  AppIOInit(void);

static void KeyScanTask(void *p_arg);

static void SecondTask(void *p_arg);

static void LedFlashTask(void *p_arg);

/*

**************************************************************************************************************

*                                                MAIN

*

* Note(s): 1) You SHOULD use OS_TASK_STK_SIZE (see OS_CFG.H) when setting OSTaskStkSize prior to calling

*             OSInit() because OS_TASK_IDLE_STK_SIZE and OS_TASK_STAT_STK_SIZE are set to this value in

*             OS_CFG.H.

**************************************************************************************************************

*/



void  main (void)

{

    /*---- Any initialization code prior to calling OSInit() goes HERE --------------------------------*/



                                                /* IMPORTANT: MUST be setup before calling 'OSInit()'  */

    OSTaskStkSize     = OS_TASK_STK_SIZE;       /* Setup the default stack size                        */

    OSTaskHardStkSize = OS_TASK_HARD_STK_SIZE;  /* Setup the default hardware stack size               */

    cls();    //清屏

    cls1();   //清屏

    fnLCMInit();

    Linexy(0,0,239,0);   //画线

    Linexy(0,17,239,17);

    Linexy(0,110,239,110);

    Linexy(0,127,239,127);

    OSInit();



    LedSem = OSSemCreate(0);    //创建信号量                /* Initialize "uC/OS-II, The Real-Time Kernel"         */

   



    /*---- Any initialization code before starting multitasking ---------------------------------------*/



    OSTaskStkSize     = OS_TASK_START_STK_SIZE;       /* Setup the total stack size                    */

    OSTaskHardStkSize = OS_TASK_START_HARD_STK_SIZE;  /* Setup the hardware stack size                 */

    OSTaskCreate(AppTaskStart, (void *)0, (OS_STK *)&AppTaskStartStk[OSTaskStkSize - 1], 0);



    /*---- Create any other task you want before we start multitasking --------------------------------*/



    OSStart();                                  /* Start multitasking (i.e. give control to uC/OS-II)  */

}



/*

*********************************************************************************************************

*                                          STARTUP TASK

*

* Description : This is an example of a startup task.  As mentioned in the book's text, you MUST

*               initialize the ticker only once multitasking has started.

*

* Arguments   : p_arg   is the argument passed to 'AppStartTask()' by 'OSTaskCreate()'.

*

* Notes       : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not

*                  used.  The compiler should not generate any code for this statement.

*********************************************************************************************************

*/



static void  AppTaskStart (void *p_arg)

{   

    int i=0;

    INT8U temp;

    INT8U number;

    p_arg = p_arg;                               /* Prevent compiler warnings                          */

                            /* Initialize the ticker                              */

    AppIOInit();                                 /* Initialize the I/Os                                */

    OSTickISR_Init();

    eprintf(90,3,0,"AVR单片机");    //显示

       for(i=0;i<20;i++)

          {

           delay_ms(20);

      

          }  

   

   

   

    AppTaskCreate();



    while (1)

         {                               /* Task body, always written as an infinite loop.     */

                number++;

                temp = (Timer.Date^Timer.Hour^Timer.Minute^Timer.Second)^number;

                OSTimeDly(OS_TICKS_PER_SEC/10);

         }

}





/*

*********************************************************************************************************

*                                    CREATE APPLICATION TASKS

*

* Description : This function creates the application tasks.

*

* Arguments   : p_arg   is the argument passed to 'AppStartTask()' by 'OSTaskCreate()'.

*

* Notes       : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not

*                  used.  The compiler should not generate any code for this statement.

*********************************************************************************************************

*/



static  void  AppTaskCreate (void)

{





    /*---- Task initialization code goes HERE! --------------------------------------------------------*/

    OSTaskStkSize     = OS_TASK_1_STK_SIZE;           /* Setup the default stack size                     */

    OSTaskHardStkSize = OS_TASK_1_HARD_STK_SIZE;      /* Setup the default hardware stack size            */

    OSTaskCreate(LedFlashTask, (void *)0, (OS_STK *)&AppTask1Stk[OSTaskStkSize - 1], 4);

   

    OSTaskStkSize     = OS_TASK_1_STK_SIZE+64;         /* Setup the default stack size                     */

    OSTaskHardStkSize = OS_TASK_1_HARD_STK_SIZE;       /* Setup the default hardware stack size            */

    OSTaskCreate(KeyScanTask, (void *)0, (OS_STK *)&AppTask3Stk[OSTaskStkSize - 1], 3);







     

    OSTaskStkSize     = OS_TASK_1_STK_SIZE;

    OSTaskHardStkSize = OS_TASK_1_HARD_STK_SIZE;   

    OSTaskCreate(SecondTask, (void *)0, (OS_STK *)&AppTask2Stk[OSTaskStkSize - 1], 2);







}







/*

*********************************************************************************************************

*                                            SETUP THE I/Os

*********************************************************************************************************

*/



static void AppIOInit (void)

  {

        Hardware_init();

        Software_init();

  }



/*

*********************************************************************************************************

*                                        SETUP THE TICK RATE

*********************************************************************************************************

*/



void  OSTickISR_Init (void)

  {

    TCCR0 = 0x05;                                            /* Set TIMER0 prescaler to CLK/1024            */

    TIMSK = 0x01;                                       /* Enable TIMER0 overflow interrupt            */

  }





/*

*********************************************************************************************************

*                                        SETUP THE TICK RATE

*********************************************************************************************************

*/



void  OSTickISR_Handler (void)

{

    TCNT0 = 256 - (CPU_CLK_FREQ / OS_TICKS_PER_SEC / 1024);

    OSTimeTick();  //检查延时状态的任务是否达到延时时间

}





static void SecondTask(void *lcndata)       //任务

{        

        OS_CPU_SR        cpu_sr;

        INT8U        err;

        lcndata=lcndata;

  

  while(1)

   {

    //OSSemPost(LedSem); //发信号

     PORTE.3=1;

     eprintf(0+20,3,0,"AVR单片机");//显示

     eprintf(0+52,3,0,"        ");

     eprintf(0+70,3,0,"   ");

     OSTimeDly(255);       

    }

}



static void LedFlashTask(void *p_arg)//;任务2

{



while(1)

{



  eprintf(0+52,3,0,"AVR单片机");

  eprintf(0+20,3,0,"        ");

  eprintf(0+70,3,0,"   ");



  OSTimeDly(60);



}





}

static void KeyScanTask(void *p_arg)    //任务3

{

          OS_CPU_SR  cpu_sr;

        INT8U        err;

        p_arg = p_arg;

       

  while(1)

   {

    OSSemPend(LedSem,0, &err); //等待信号

    PORTE.4=1;

    eprintf(0+52,3,0,"        ");

    eprintf(0+20,3,0,"        ");

    eprintf(0+70,3,0,"123");

   

     

   }



}

出0入0汤圆

 楼主| 发表于 2006-12-11 08:43:19 | 显示全部楼层
自己顶一下.

下面是os_cfg.h文件的配置

/*

*********************************************************************************************************

*                                                uC/OS-II

*                                          The Real-Time Kernel

*

*                           (c) Copyright 1992-2003, Jean J. Labrosse, Weston, FL

*                                           All Rights Reserved

*

*                                  uC/OS-II Configuration File for V2.7x

*

* File : OS_CFG.H

* By   : Jean J. Labrosse

*********************************************************************************************************

*/



#ifndef OS_CFG_H

#define OS_CFG_H



#define OS_TASK_STK_SIZE        256    /* Default size for each task stack (Total stack size)          */

#define OS_TASK_HARD_STK_SIZE    64    /* Default size for the hardware stack                          */



                                       /* ---------------------- MISCELLANEOUS ----------------------- */

#define OS_ARG_CHK_EN             0    /* Enable (1) or Disable (0) argument checking                  */

#define OS_CPU_HOOKS_EN           1    /* uC/OS-II hooks are found in the processor port files         */



#define OS_DEBUG_EN               1    /* Enable(1) debug variables                                    */



#define OS_EVENT_NAME_SIZE        0    /* Determine the size of the name of a Sem, Mutex, Mbox or Q    */



#define OS_LOWEST_PRIO            8    /* Defines the lowest priority that can be assigned ...         */

                                       /* ... MUST NEVER be higher than 63!                            */



#define OS_MAX_EVENTS             5    /* Max. number of event control blocks in your application      */

#define OS_MAX_FLAGS              5    /* Max. number of Event Flag Groups    in your application      */

#define OS_MAX_MEM_PART           5    /* Max. number of memory partitions                             */

#define OS_MAX_QS                 4    /* Max. number of queue control blocks in your application      */

#define OS_MAX_TASKS              6    /* Max. number of tasks in your application, MUST be >= 2       */



#define OS_SCHED_LOCK_EN          1    /*     Include code for OSSchedLock() and OSSchedUnlock()       */



#define OS_TASK_IDLE_STK_SIZE     OS_TASK_STK_SIZE  /* Idle task stack size                            */



#define OS_TASK_STAT_EN           0                 /* Enable (1) or Disable(0) the statistics task    */

#define OS_TASK_STAT_STK_SIZE     OS_TASK_STK_SIZE  /* Statistics task stack size                      */

#define OS_TASK_STAT_STK_CHK_EN   1                 /* Check task stacks from statistic task           */



#define OS_TICK_STEP_EN           0    /* Enable tick stepping feature for uC/OS-View                  */

#define OS_TICKS_PER_SEC         20    /* Set the number of ticks in one second                        */





                                       /* ----------------------- EVENT FLAGS ------------------------ */

#define OS_FLAG_EN                0    /* Enable (1) or Disable (0) code generation for EVENT FLAGS    */

#define OS_FLAG_WAIT_CLR_EN       1    /* Include code for Wait on Clear EVENT FLAGS                   */

#define OS_FLAG_ACCEPT_EN         1    /*     Include code for OSFlagAccept()                          */

#define OS_FLAG_DEL_EN            1    /*     Include code for OSFlagDel()                             */

#define OS_FLAG_NAME_SIZE         0    /*     Determine the size of the name of an event flag group    */

#define OS_FLAG_QUERY_EN          1    /*     Include code for OSFlagQuery()                           */





                                       /* -------------------- MESSAGE MAILBOXES --------------------- */

#define OS_MBOX_EN              0    /* Enable (1) or Disable (0) code generation for MAILBOXES      */

#define OS_MBOX_ACCEPT_EN         1    /*     Include code for OSMboxAccept()                          */

#define OS_MBOX_DEL_EN            1    /*     Include code for OSMboxDel()                             */

#define OS_MBOX_POST_EN           1    /*     Include code for OSMboxPost()                            */

#define OS_MBOX_POST_OPT_EN       1    /*     Include code for OSMboxPostOpt()                         */

#define OS_MBOX_QUERY_EN          1    /*     Include code for OSMboxQuery()                           */





                                       /* --------------------- MEMORY MANAGEMENT -------------------- */

#define OS_MEM_EN                 0    /* Enable (1) or Disable (0) code generation for MEMORY MANAGER */

#define OS_MEM_QUERY_EN           1    /*     Include code for OSMemQuery()                            */

#define OS_MEM_NAME_SIZE          0    /*     Determine the size of a memory partition name            */





                                       /* ---------------- MUTUAL EXCLUSION SEMAPHORES --------------- */

#define OS_MUTEX_EN               0    /* Enable (1) or Disable (0) code generation for MUTEX          */

#define OS_MUTEX_ACCEPT_EN        1    /*     Include code for OSMutexAccept()                         */

#define OS_MUTEX_DEL_EN           1    /*     Include code for OSMutexDel()                            */

#define OS_MUTEX_QUERY_EN         1    /*     Include code for OSMutexQuery()                          */





                                       /* ---------------------- MESSAGE QUEUES ---------------------- */

#define OS_Q_EN                   0    /* Enable (1) or Disable (0) code generation for QUEUES         */

#define OS_Q_ACCEPT_EN            1    /*     Include code for OSQAccept()                             */

#define OS_Q_DEL_EN               1    /*     Include code for OSQDel()                                */

#define OS_Q_FLUSH_EN             1    /*     Include code for OSQFlush()                              */

#define OS_Q_POST_EN              1    /*     Include code for OSQPost()                               */

#define OS_Q_POST_FRONT_EN        1    /*     Include code for OSQPostFront()                          */

#define OS_Q_POST_OPT_EN          1    /*     Include code for OSQPostOpt()                            */

#define OS_Q_QUERY_EN             1    /*     Include code for OSQQuery()                              */





                                       /* ------------------------ SEMAPHORES ------------------------ */

#define OS_SEM_EN                 1    /* Enable (1) or Disable (0) code generation for SEMAPHORES     */

#define OS_SEM_ACCEPT_EN          1    /*    Include code for OSSemAccept()                            */

#define OS_SEM_DEL_EN             1    /*    Include code for OSSemDel()                               */

#define OS_SEM_QUERY_EN           1    /*    Include code for OSSemQuery()                             */

#define OS_SEM_SET_EN             1    /*    Include code for OSSemSet()                               */





                                       /* --------------------- TASK MANAGEMENT ---------------------- */

#define OS_TASK_CHANGE_PRIO_EN    1    /*     Include code for OSTaskChangePrio()                      */

#define OS_TASK_CREATE_EN         1    /*     Include code for OSTaskCreate()                          */

#define OS_TASK_CREATE_EXT_EN     0    /*     Include code for OSTaskCreateExt()                       */

#define OS_TASK_DEL_EN            0    /*     Include code for OSTaskDel()                             */

#define OS_TASK_NAME_SIZE         0    /*     Determine the size of a task name                        */

#define OS_TASK_PROFILE_EN        1    /*     Include variables in OS_TCB for profiling                */

#define OS_TASK_QUERY_EN          0    /*     Include code for OSTaskQuery()                           */

#define OS_TASK_SUSPEND_EN        0    /*     Include code for OSTaskSuspend() and OSTaskResume()      */

#define OS_TASK_SW_HOOK_EN        1    /*     Include code for OSTaskSwHook()                          */





                                       /* --------------------- TIME MANAGEMENT ---------------------- */

#define OS_TIME_DLY_HMSM_EN       1    /*     Include code for OSTimeDlyHMSM()                         */

#define OS_TIME_DLY_RESUME_EN     0    /*     Include code for OSTimeDlyResume()                       */

#define OS_TIME_GET_SET_EN        0    /*     Include code for OSTimeGet() and OSTimeSet()             */

#define OS_TIME_TICK_HOOK_EN      1    /*     Include code for OSTimeTickHook()                        */





typedef INT16U             OS_FLAGS;   /* Date type for event flag bits (8, 16 or 32 bits)             */



#endif

出0入0汤圆

 楼主| 发表于 2006-12-11 08:45:19 | 显示全部楼层
这个程序是用atmega64和cvavr编译器

出0入0汤圆

发表于 2006-12-11 11:31:48 | 显示全部楼层
UCOS好像比较复杂,书买了不少,也学了就是没实站过。

现在用一个时间调度系统,程序很简单,就是一个程序指针,感觉还不错。

出0入0汤圆

 楼主| 发表于 2006-12-11 12:40:39 | 显示全部楼层
调度应该是最简单的,我感觉我的发信号没有问题,可是就是不好用.没发信号,那个任务也执行,不只错到哪里了???个位多发表意见.

出0入0汤圆

发表于 2006-12-12 09:08:50 | 显示全部楼层
因为

1。KeyScanTask任务3 的优先级是3,SecondTask任务2的优先级是2,所以SecondTask比KeyScanTask先执行。



2。然后你在SecondTaskOSSemPost(LedSem)发信号,信号量有了一个资源。



3。任务3SecondTask在执行OSSemPend(LedSem,0, &err)无限期等待信号时,因为刚才任务2使得信号量有了1个资源,所以就不等待,马上执行了。就出现你提到的问题。



4。解决方法,更换任务2和任务3的优先级,让KeyScanTask先执行等待信号量,然后,任务2再发信号量,就能得到正确的结果了.

出0入0汤圆

发表于 2006-12-16 13:23:29 | 显示全部楼层
3执行之后 信号量不就没有了么  为什么继续执行阿
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-9 12:20

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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