Bootstrap

ucos学习笔记---002双任务时间片运行原理

002:双任务时间片运行原理

时间片切换原理:用系统定时器的中断函数实现任务的切换与调度。

void tSetSysTickPeriod(uint32_t ms)          //任务调度定时器参数配置
{
	SysTick->LOAD = ms * SystemCoreClock / 1000 -1;
	NVIC_SetPriority(SysTick_IRQn, (1<<__NVIC_PRIO_BITS)-1);
	SysTick->VAL = 0;
	SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
}
void SysTick_Handler()                      //定时器中断
{
	tTaskSched();                       //任务切换
}


int task1Flag;
void task1Entry(void * param)
{
	tSetSysTickPeriod(10);             //任务1中启用任务定时器并开始任务调度
	for(;;)
	{
		task1Flag = 0;
		delay(100);
		task1Flag = 1;
		delay(100);
	}
}
int task2Flag;
void task2Entry(void * param)
{
	for(;;)
	{
		task2Flag = 0;
		delay(100);
		task2Flag = 1;
		delay(100);
	}
}

缺点:delay()函数是软延时,占用CPU资源。?????

;