⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ledflow4.c

📁 文件: LedFlow.c LedFlow2.c LedFlow3.c LedFlow4.c LedFlow.mcp 等相关文件。 这是一个学习uC/os-II的实用源程序
💻 C
字号:
#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
********************************************************************************************************/
			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -