www.pudn.com > MiniOS.rar > ex4.c


// 
// example 4: task synchronization 
// descriptions: The first task wait for a semaphore,when the semaphore is Realeased, 
//               this task turn on LEDs one by one.  
//               The second task sends a semaphore at the rate of 0.5 second.  
// author: Taiyun Wang  
// date: 2003/2/22 
/////////////////////////////////////////////////////////////////////////// 
 
#include "sposvar.h" 
#include "spos.h" 
 
int err;					//Error No 
int t1stack[25];			//Task 1 stack 
int t2stack[25];			//Task 2 stack 
HEvent sem;					//Event handle 
 
volatile unsigned int *P_IOB_BUFFER =(unsigned int*)(0x7006);	//Port B data register 
volatile unsigned int *P_IOB_DIR =(unsigned int*)(0x7007);		//Port B direction register 
volatile unsigned int *P_IOB_ATTRIB = (unsigned int*)(0x7008);	//Port B attribute register 
 
main() 
{ 
	void Task1(); 
	SpSInit(); 
	*P_IOB_DIR = 0XFFFF;						//Set Port B output 
	*P_IOB_ATTRIB = 0XFFFF;						//Set Port B attribute 
	err = SpSTaskCreate(Task1,0,t1stack+24,1);	//Create first task 
	sem = SpSSemCreate(0);						//Create semaphore 
	SpSStart();									//Start kernel 
} 
void Task1()				//Task one 
{ 
    void Task2(); 
    unsigned int i = 1; 
	err = SpSTaskCreate(Task2,0,t2stack+24,2);	//Create second task 
   	while(1) { 
		for(i = 1;i<0x100;i<<=1) { 
           SpSSemPend(sem,0);					//Waiting semaphore 
           *P_IOB_BUFFER = i; 
		} 
   } 
} 
 
void Task2() 
{ 
	while(1) { 
        SpSSemPost(sem);						//Realease semaphore 
        SpSTimeDly(64);							//Delay 64 tick 
 	} 
}