建立一个AVR单片机RTOS(8)—占先式内核(完善的服务)
//清空所有等待该信号的等待任务
}
else //当要求的信号量为独占型
{
unsigned char i;
for (i = 0; i < OS_TASKS && !(Sem[Index].OSTaskPendTbl & (0x01
if(i < OS_TASKS) //如果有任务需要
{
Sem[Index].OSTaskPendTbl &= ~(0x01
OSRdyTbl |= 0x01
}
else
{
Sem[Index].OSEventState =1; //使信号量有效
}
}
}
//从任务发送一个信号量,并进行调度
void OSTaskSemPost(unsigned char Index)
{
OSSemPost(Index);
OSSched();
}
//清除一个信号量,只对共享型的有用。
//对于独占型的信号量,在任务占用后,就交得不可以用了。
void OSSemClean(unsigned char Index)
{
Sem[Index].OSEventState =0; //要求的信号量无效
}
void TCN0Init(void) //计时器0
{
TCCR0 = 0;
TCCR0 |= (1 TIMSK |= (1 TCNT0 = 100; //置计数起始值 } SIGNAL(SIG_OVERFLOW0) { IntNum++; //中断嵌套+1 sei(); //在中断中,重开中断 unsigned char i; for(i=0;i { if(TCB[i].OSWaitTick && TCB[i].OSWaitTick!=0xffff) { TCB[i].OSWaitTick--; if(TCB[i].OSWaitTick==0) //当任务时钟到时,必须是由定时器减时的才行 { OSRdyTbl |= (0x01 OSCoreState|=0x02; //要求任务切换的标志位 } } } TCNT0=100; cli(); IntNum--; //中断嵌套-1 IntSwitch(); //进行任务调度 } unsigned char __attribute__ ((progmem)) proStrA[]="Task "; unsigned char strA[20]; SIGNAL(SIG_UART_RECV) //串口接收中断 { strA[0]=UDR; } /////////////////////////////////////串口发送 unsigned char *pstr_UART_Send; unsigned int nUART_Sending=0; void UART_Send(unsigned char *Res,unsigned int Len) //发送字符串数组 { if(Len>0) { pstr_UART_Send=Res; //发送字串的指针 nUART_Sending=Len; //发送字串的长度 UCSRB=0xB8; //发送中断使能 } } //SIGNAL在中断期间,其它中断禁止 SIGNAL(SIG_UART_DATA) //串口发送数据中断 { IntNum++; //中断嵌套+1,不充许中断 if(nUART_Sending) //如果未发完 { UDR=*pstr_UART_Send; //发送字节 pstr_UART_Send++; //发送字串的指针加1 nUART_Sending--; //等待发送的字串数减1 } if(nUART_Sending==0) //当已经发送完 { OSSemPost(0); OSCoreState|=0x02; //要求任务切换的标志位 UCSRB=0x98; } cli(); //关发送中断 IntNum--; IntSwitch(); //进行任务调度 } void UARTInit() //初始化串口 { #define fosc 8000000 //晶振8 MHZ UBRRL=(fosc/16/(baud+1))%256; #define baud 9600 //波特率 OSCCAL=0x97; //串口波特率校正值,从编程器中读出 //UCSRB=(1 UCSRB=0x98; //UCSRB=0x08; UBRRL=(fosc/16/(baud+1))%256; UBRRH=(fosc/16/(baud+1))/256; UCSRC=(1 UCSRB=0xB8; UDR=0; } //打印unsigned int到字符串中00000 void strPUT_uInt(unsigned char *Des,unsigned int i) { unsigned char j; Des=Des+4; for(j=0;j<5;j++) { *Des=i%10+’0’; i=i/10; Des--; } } void strPUT_Star(unsigned char *Des,unsigned char i) { unsigned char j; for(j=0;j { *Des++=’*’; } *Des++=13; } unsigned int strPUT_TaskState(unsigned char *Des,unsigned char TaskID,unsigned char Num) { //unsigned int i=0; *(Des+4)=’0’+TaskID; strPUT_uInt(Des+6,Num); strPUT_Star(Des+12,TaskID); return 12+TaskID+1; } void Task0() { unsigned int j=0; while(1) { PORTB=j++; if(OSTaskSemPend(0,0xffff)) { unsigned int m; m=strPUT_TaskState(strA,OSTaskRunningPrio,j); UART_Send(strA,m); } OSTimeDly(200); } } void Task1() { unsigned int j=0; while(1) { PORTC=j++; if(OSTaskSemPend(0,0xffff)) { unsigned int m; m=strPUT_TaskState(strA,OSTaskRunningPrio,j); UART_Send(strA,m); } OSTimeDly(100); } } void Task2() { unsigned int j=0; while(1) { if(OSTaskSemPend(0,0xffff)) { unsigned int m; m=strPUT_TaskState(strA,OSTaskRunningPrio,j); UART_Send(strA,m); } PORTD=j++; OSTimeDly(50); } } void TaskScheduler() { OSSched(); while(1) {} } int main(void) { strlcpy_P(strA,proStrA,20); UARTInit(); TCN0Init(); OSRdyTbl=0; IntNum=0; OSTaskCreate(Task0,&Stack[99],0); OSTaskCreate(Task1,&Stack[199],1); OSTaskCreate(Task2,&Stack[299],2); OSTaskCreate(TaskScheduler,&Stack[399],OS_TASKS); OSStartTask(); }
AVR单片机RTOS占先式内 相关文章:
- 建立一个AVR单片机RTOS(7)—占先式内核(只带延时服务)(12-03)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)