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

📄 ex9.c

📁 51单片机上可以使用的操作系统
💻 C
字号:
//
// example 9: Send message in interrupt
// descriptions: The interrupt function send semaphore to task 1 and task 2.
//               pay attention to the interrupt function that lie in system clock interrupt.
// 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 sem1,sem2;				//Event handle
int tick1 = 0,tick2 = 0;

volatile unsigned int *P_IOA_BUFFER =(unsigned int*)(0x7000);		//Port A data register
volatile unsigned int *P_IOA_DIR =(unsigned int*)(0x7002);		//Port A direction register
volatile unsigned int *P_IOA_ATTRIB = (unsigned int*)(0x7003);  //Port A attribute register

volatile unsigned int *P_IOB_BUFFER =(unsigned int*)(0x7005);		//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 Timer_INT(void);
	void Task1();
	void Task2();
	SpSInit();
	*P_IOA_DIR = 0XFFFF;							//Set Port A direction
	*P_IOA_ATTRIB = 0XFFFF;							//Set Port A attribute
	
	*P_IOB_DIR = 0XFFFF;							//Set Port B direction
	*P_IOB_ATTRIB = 0XFFFF;							//Set Port B attribute
	SpSSetVector(IRQ6_TMB2_VEC,Timer_INT);			//Set interrupt function
	err = SpSTaskCreate(Task1,0,t1stack+24,1);		//Create first task
	err = SpSTaskCreate(Task2,0,t2stack+24,2);		//Create second task
	sem1 = SpSSemCreate(0);							//Create semaphore
	sem2 = SpSSemCreate(0);
	SpSStart();										//Start OS kernel
}
void Task1()				//task one
{
	unsigned int i = 1;
	while(1) {
		SpSSemPend(sem1,0);							//Waiting semaphore
		*P_IOB_BUFFER = i;
		i<<=1;
		if(i == 0x0100)
			i = 1;
	}
}

void Task2()				//task two
{
	unsigned int i=0;
	while(1) {
		SpSSemPend(sem2,0);							//Waiting semaphore
		*P_IOA_BUFFER = i;
		i<<=1;
		if(i == 0)
			i = 1;
	}
}
void Timer_INT(void)
{
	tick1++;
	tick2++;
	if (tick1 == 30) {								//tick==30?
		SpSSemPost(sem1);							//Release semaphore 1 ,running task 1
		tick1 = 0;
	}
	if (tick2 == 5) {								//tick==5?
		SpSSemPost(sem2);							//Release semaphore 2 ,running task 2
		tick2 = 0;
	}
}

⌨️ 快捷键说明

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