火浪汤B - tombtomb的BLOG http://tombtomb.52rd.net - 复制 - 收藏
驭刀降佛 发表于 2008-6-7 10:11:00

最简单的嵌入式练习:

#include "config.h"

#define  TASK_STK_SIZE  100  /* 定义任务堆栈大小 */
#define  LedFlowSpeed  5  /* 定义LED流动速度  */

OS_STK  TaskStartStk[TASK_STK_SIZE];
OS_STK  TaskKeyScanStk[TASK_STK_SIZE];

OS_EVENT *LedFlowMbox;    /* 定义邮箱用于传递按键值 */
  
#define  Led1  (1<<22)
#define  Led2  (1<<23)
#define  Led3  (1<<24)
#define  Led4  (1<<25)
#define  KEY1  (1<<16)
#define  KEY2  (1<<17)
#define  KEY3  (1<<18)
#define  KEY4  (1<<19)
#define  KEY5  (1<<20)
#define  KEY6  (1<<21)


void TaskStart(void *pdata);
void TaskKeyScan(void *pdata);
void  DelayNms(INT32U  dly);


/*********************************************************************************************************
** 函数名称: main
** 功能描述: c语言的主函数,由它启动多任务环境
** 输 入: 无
** 输 出: 无
** 全局变量: 无
** 调用模块: OSInit,OSTaskCreate,OSStart
**
** 作 者: 陈明计
** 日 期: 2003年7月8日
**-------------------------------------------------------------------------------------------------------
** 修改人: 梁浩荡
** 日 期: 2008-5-13
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int main()
{
 OSInit();
 OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE-1], 0);
 OSStart();
 return 0;
}

/*********************************************************************************************************
** 函数名称: TaskStart
** 功能描述: μCOS-II的第一个任务,通常由它初始化目标板和建立其它任务
** 输 入: 无
** 输 出: 无
** 全局变量: 无
** 调用模块:
**
** 作 者: 陈明计
** 日 期: 2003年7月8日
**-------------------------------------------------------------------------------------------------------
** 修改人: 梁浩荡
** 日 期: 2008-5-13
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

void TaskStart(void *pdata)
 {
 uint8 *KeyValue;
 uint8 err;
 pdata = pdata;          /* 避免编译警告 */
 
 LedFlowMbox = OSMboxCreate(NULL);     /* 建立邮箱     */
 if(LedFlowMbox == NULL)
  {
   while(1);
  }
 TargetInit();
 
 IO0DIR &= (~KEY1)|(~KEY2)|(~KEY3)|(~KEY4)|(~KEY5)|(~KEY6);
 IO0DIR |= Led1|Led2|Led3|Led4;
 IO0SET =  Led1|Led2|Led3|Led4;
 PINSEL0 = (PINSEL0&0x00000000) | 0x05;
 PINSEL1 = (PINSEL1&0x00000000);
 OSTaskCreate(TaskKeyScan, (void *)0, &TaskKeyScanStk[TASK_STK_SIZE-1],8);
 
 
  for(;;)
   {
   KeyValue = (uint8 *)OSMboxPend(LedFlowMbox, 0 , &err);
    if(*KeyValue==1)
     {
      IO0CLR = Led1;
      //DelayNms(200);
      OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
      IO0SET = Led1;
      OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
      //DelayNms(200);
      IO0CLR = Led1;
      //DelayNms(200);
      OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
      IO0SET = Led1;
     } else if(*KeyValue ==2)
      {
       IO0CLR = Led2;
       //DelayNms(200);
       OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
       IO0SET = Led2;
       //DelayNms(200);
       OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
       IO0CLR = Led2;
       //DelayNms(200);
       OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
       IO0SET = Led2;
      }else if(*KeyValue ==3)
       {
        IO0CLR = Led3;
        OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
        //DelayNms(200);
        IO0SET = Led3;
        OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
        //DelayNms(200);
        IO0CLR = Led3;
        //DelayNms(200);
        OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
        IO0SET = Led3;
       }else if(*KeyValue==4)
        {
         IO0CLR = Led4;
         //DelayNms(200);
         OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
         IO0SET = Led4;
         //DelayNms(200);
         OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
         IO0CLR = Led4;
         //DelayNms(200);
         OSTimeDly(OS_TICKS_PER_SEC/LedFlowSpeed);
         IO0SET = Led4;
        }
   }
 }

/*********************************************************************************************************
** 函数名称: TaskKeyScan
** 功能描述: 用于按键检测,同时传递按键的标记值
** 输 入: 无
** 输 出: 无
** 全局变量: 无
** 调用模块:
**
** 作 者: 陈明计
** 日 期: 2003年7月8日
**-------------------------------------------------------------------------------------------------------
** 修改人: 梁浩荡
** 日 期: 2008-5-13
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

void TaskKeyScan(void *pdata)
 {
 uint8 KeyValue;
 pdata = pdata;
 KeyValue = 1;
 
  for(;;)
  {
  while(((IO0PIN&KEY1)!=0) && ((IO0PIN&KEY2)!=0) && ((IO0PIN&KEY3)!=0) && ((IO0PIN&KEY4)!=0) );
    {
     OSTimeDly(OS_TICKS_PER_SEC/50);
    }
  if((IO0PIN&KEY1)==0)
   {
    OSTimeDly(OS_TICKS_PER_SEC/50);
    if((IO0PIN&KEY1)!=0) continue;
    KeyValue = 1;
    while((IO0PIN&KEY1)==0)
    {
     OSTimeDly(OS_TICKS_PER_SEC/50);
    }
   }
     else if((IO0PIN&KEY2)==0)
       {
        OSTimeDly(OS_TICKS_PER_SEC/50);
     if((IO0PIN&KEY2)!=0) continue;
     KeyValue = 2;
     while((IO0PIN&KEY2)==0)
     {
      OSTimeDly(OS_TICKS_PER_SEC/50);
     }
       }
       else if((IO0PIN&KEY3)==0)
        {
         OSTimeDly(OS_TICKS_PER_SEC/50);
      if((IO0PIN&KEY3)!=0) continue;
      KeyValue = 3;
      while((IO0PIN&KEY3)==0)
      {
       OSTimeDly(OS_TICKS_PER_SEC/50);
      }
        }
         else if((IO0PIN&KEY4)==0)  
          {
           OSTimeDly(OS_TICKS_PER_SEC/50);
        if((IO0PIN&KEY4)!=0) continue;
        KeyValue = 4;
        while((IO0PIN&KEY4)==0)
        {
         OSTimeDly(OS_TICKS_PER_SEC/50);
        }
          }
    
   OSMboxPost(LedFlowMbox,&KeyValue);
  
   OSTimeDly(OS_TICKS_PER_SEC/10);
  }
 }

/****************************************************************************
* 名称:DelayNmS()
* 功能:长软件延时
* 入口参数:dly  延时参数,值为1时,延时为1ms
* 出口参数:无
****************************************************************************/
void  DelayNms(INT32U  dly)
{  INT32U  i;

   for(; dly>0; dly--)
      for(i=0; i<7142; i++);
}

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/
   

阅读全文(853) | 评论(1)
评 论
1楼 52RD网友(游客) 发表于 2008-11-27 17:59:00
这个是陈明计写的
9 1 :
昵 称: 匿名
验证码: 7575
博 主
进入tombtomb的首页
博客名称:火浪汤B
日志总数:24
评论数量:92
访问次数:33998
建立时间:2007年10月16日
导 航
日 历
«Mar.2010»
123456
78910111213
14151617181920
21222324252627
28293031
公 告
欢迎光临!
日 志
评 论
链 接