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

📄 ex5.c

📁 51单片机上可以使用的操作系统
💻 C
字号:
//
// example 5: counting semaphore
// descriptions: Semaphore initialized is 3, thus 3 LEDs can be turned on at the same time.
//               There are 8 initialized tasks,one task corresponds to one LED,they share the 
//				 resource each other. Each task occupys the semaphore some cycles, then it releases 
//               the semaphore,so other task can gain semaphore and turn on its own LED. 
// author: Taiyun Wang 
// date: 2003/2/22
///////////////////////////////////////////////////////////////////////////

#include "sposvar.h"
#include "spos.h"

typedef struct {
	int portval;
	int dlyval;
} par_t;						//parameter struct
int err;						//Error No
int stack[8][30];				//Task 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
int portb = 0;

main()
{
	void Task(void* inval);
	int i;
	par_t par[8];									//structure array
	int dlypar[8] = {53,43,31,29,23,19,17,13};		//Delay tick of every task
	SpSInit();										
	*P_IOB_DIR = 0XFFFF;							//Set Port B is output
	*P_IOB_ATTRIB = 0XFFFF;							//Set Port B attribute
	for(i = 0;i<8;i++) {
		par[i].portval = 1<<i;
		par[i].dlyval = dlypar[i];
	}
	for (i = 0; i<8;i++)
		err = SpSTaskCreate(Task,(void*)&par[i],&stack[i][29],i+1);	//Create task
	sem = SpSSemCreate(3);											//Create semaphore
	SpSStart();														//Start OS kernel
}
void Task(void* inval)			//task n(n=1...8)
{
	while(1) {
		SpSSemPend(sem,0);							//Waiting semaphore
		portb |= ((par_t*)inval)->portval;
		*P_IOB_BUFFER = portb;
		SpSTimeDly(((par_t*)inval)->dlyval);
		portb &= ~((par_t*)inval)->portval;
		*P_IOB_BUFFER = portb;
		SpSSemPost(sem);							//Release semaphore
		SpSTimeDly(((par_t*)inval)->dlyval);		//Delay 
	}
}

⌨️ 快捷键说明

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