www.pudn.com > 51ucosii.zip > MyTest.c, change:2010-05-14,size:5037b


 /*********************************************************** 
 * 标题:        操作系统试验三 
 * 
 *  1.此操作系统已经移植好,可以在51单片机上运行. 
 * 
 *  2.关于学习操作系统时,学员试着边读边理解操作系统代码,边在开发板上做试验 
 *    这可以为以后学习更复杂的操作系统(Linux等)打下扎实的基础.也为学习嵌入系统打下扎实基础。 
 * 
 *  3.在学习时,有条件可以找一本适合的 uC/OS-II参考书籍.在这环节上希望学员不要浮躁,静 
 *    心来吃到它. 
 *   
 * 
 *************************************************************/ 
 
//****************************************************************** 
//* mytest fuction 
#include <includes.h> 
 
//****************************************************************** 
//* mytest system timer initial 
#define   TIME_10mS     (OS_TICKS_PER_SEC /100) 
#define   TIME_50mS     (OS_TICKS_PER_SEC /20 ) 
#define   TIME_100mS    (OS_TICKS_PER_SEC /10 ) 
#define   TIME_200mS    (OS_TICKS_PER_SEC /5  ) 
#define   TIME_500mS    (OS_TICKS_PER_SEC /2  ) 
#define   TIME_1S       (OS_TICKS_PER_SEC) 
#define   TIME_2S       (OS_TICKS_PER_SEC * 2) 
#define   TIME_3S       (OS_TICKS_PER_SEC * 3) 
#define   TIME_4S       (OS_TICKS_PER_SEC * 4) 
#define   TIME_5S       (OS_TICKS_PER_SEC * 5) 
 
//****************************************************************** 
//* mytest system crystalloid 
#define   XA_CRYSTAL_FREQ   (INT32U)(22118400L) 
 
//****************************************************************** 
//* SCR crystalloid 
#define	  CFG_SCR_4	    SCR&0x03  
#define	  CFG_SCR_16	SCR&0x07 
#define	  CFG_SCR_64	SCR&0x0B 
 
#define	  OS_CFG_SCR	CFG_SCR_4 
 
//****************************************************************** 
//* mytest system  
#define   TASK_STK_SIZE   30 //* 128 word 
#define   NUMBER_TASKS    20    
 
//****************************************************************** 
//* mytest task stack define 
OS_STK  xdata   AnPrintfStk[TASK_STK_SIZE]; 
OS_STK  xdata   BnPrintfStk[TASK_STK_SIZE]; 
 
//****************************************************************** 
//* OS_EVENT 
OS_EVENT xdata * AnPrintfWait; 
OS_EVENT xdata * BnPrintfWait; 
 
 
 
//****************************************************************** 
 
INT8U  xdata   OSCtxSwCtrMax;  
 
//------------------------------------------------------------------- 
void  TimeTickInitial(void); 
void  AnPrintf(void *dataptr);   //* Startup Task           
void  BnPrintf(void *dataptr);   //* 8 Identical Tasks  
void  Serial_initialize(void); 
INT8U keyscan(void); 
void Delay(INT8U i); 
//static void XAInit (void); 
//static void TimerInit (INT8U tmr, INT16U freq, INT8U prio); 
//static void SeryComm0Init (INT16U baud); 
//****************************************************************** 
 
//****************************************************************** 
void main (void) 
{ 
    //XAInit(); //* XA Configuration                            
    OSInit();   //* Initialize uC/OS-II, The Real-Time Kernel  
     
    BnPrintfWait = OSSemCreate(0); 
     
     
     
    OSTaskCreate( AnPrintf,  
                  (void *)0,  
                  (void *)&AnPrintfStk[TASK_STK_SIZE-1],  
                  0x03); 
 
    OSTaskCreate( BnPrintf,  
                  (void *)0,  
                  (void *)&BnPrintfStk[TASK_STK_SIZE-1],  
                  0x04); 
  
    TimeTickConfig(); 
    Serial_initialize(); 
    OSStart();            
}  
 
void  AnPrintf(void *dataptr) 
{    
    INT8U err; 
    dataptr = dataptr; 
    while(1) 
    {   
	//	keyscan(); 
	// if(P3 != 0xff) 
	 P0 = 0x0f; 
//	 else  
	// P0 = 0xff; 
     OSSemPend(BnPrintfWait,100,&err); //等待一个信号量 OSSemPend (OS_EVENT *pevent, INT16U timeout, INT8U *err) 
 
    } 
     
} 
 
//****************************************************************** 
void  BnPrintf(void *dataptr) 
{ 
	/*char tmp[60]; 
	int i = 0; 
	 
	for(i = 0 ; i < 60 ; i++) 
	tmp[i] = i; 
 
    dataptr = dataptr; 
    EA =1;  	*/ 
    while(1) 
    { 
	   // P1 = rand(); 
		Delay(3); 
		P0 = 0x0f; 
		OSSemPost(BnPrintfWait);// 发送一个信号	 
    }	 
    
} 
void SeryInt(void) 
{ 
	 
} 
void Serial_initialize() 
{ 
    SCON  = 0x50;  //* SCON: mode 1, 8-bit UART, enable rcvr */ 
    TMOD |= 0x20;  //* TMOD: timer 1, mode 2, 8-bit reload */ 
    TH1   = 0xfd;  //* TH1:  reload value for 1200 baud @ 16MHz */ 
    TR1   = 1;     //* TR1:  timer 1 run */ 
    TI    = 1;     //* TI:   set TI to send first char of UART */ 
} 
 
 
INT8U keyscan(void) 
{ 
	INT8U sccode,recode; 
	P2 = 0xF0; 
	if((P2 & 0xF0) != 0xF0) //检测按钮 
	{ 
		Delay(3);	 //防抖延时 
		if((P2 & 0xF0) != 0xF0)	 //  再次检测按钮 
		{ 
			sccode = 0xFE; 
			while((sccode & 0x10) != 0) 
			{ 
				P2 = sccode; 
				if((P2 & 0xF0) != 0xF0) 
				{ 
					recode = (P2 & 0xF0) | 0x0F; 
					while((P2 & 0xF0) != 0xF0)Delay(1); 
					SBUF = ((~sccode) + (~recode)); 
					return((~sccode) + (~recode)); 
				} 
				else 
					sccode = (sccode << 1) | 0x01; 
			} 
		} 
	} 
} 
 
void Delay(INT8U i) 
{ 
	INT8U j,k; 
	for(i; i > 0; i--) 
		for(j = 0; j < 250; j++) 
			for(k = 0; k <250; k++); 
}