单片机中基于时间片的多任务的实现
2019-04-15 18:57 发布
生成海报
前言: 最近朋友问我一个单片机问题,他写了一个单片机程序实现了两组LED灯的闪烁,他说着两组LED灯的闪烁有先后顺序,他问我能不能设计一个程序,让着两组LED灯的独立闪烁。我当时想了想,用两个定时器可以实现它这个功能。但是由于我做硬件比较少,觉得配置定时器太麻烦,就在技术群里咨询了一下,以杭州的大神(网名:迷你强)给了建议:用时间片来实现多任务,并举了个详细的例子。于是今天(2016年12月13日)我借了个STM32F103ZET的板子设计并验证了时间片是可行的多任务方案。废话好多,下面进入正题。先讲讲时间片的概念吧 以下这段来自百度:时间片即 CPU 分配给各个程序的时间,每个线程被分配一个时间段,称作它的时间片,即该进程允许运行的时间,使各个程序从表面上看是同时进行的。如果在时间片结束时进程还在运行,则 CPU 将被剥夺并分配给另一个进程。如果进程在时间片结束前阻塞或结束,则 CPU 当即进行切换。而不会造成 CPU 资源浪费。在宏观上:我们可以同时打开多个应用程序,每个程序并行不悖,同时运行。但在微观上:由于只有一个 CPU ,一次只能处理程序要求的一部分,如何处理公平,一种方法就是引入时间片,每个程序轮流执行。 OK ,不懂可以先往下看。 1. 首先,定义一个表示的任务的结构体: [cpp] view plain copy typedef struct _Task_Struct { u8 isRun; u16 TimerSlice; u16 SliceNumber; void (*TaskPointer)( void * parameter); }Task_Struct,*Task_Struct_Pointer; 2. 然后定义要执行的任务,即函数,这里简单起见,定义了四个 task : [cpp] view plain copy void task1( void * parameter) { printf("LED is blinking.
" ); } void task2( void * parameter) { printf("LCD is Running.
" ); } void task3( void * parameter) { printf("A file is downloading.
" ); } void task4( void * parameter) { printf("A key is pressing.
" ); } 3.定义任务数组,并初始化,用于保存任务的信息: [cpp] view plain copy Task_Struct tasks[] = { {0,200,200,task1}, {0,60,60,task2}, {0,100,100,task3}, {0,10,10,task4} }; 4. 保存所有执行的任务数量: [cpp] view plain copy u32 task_count = sizeof (tasks) / sizeof (Task_Struct); 5. 随便初始化一个定时器,我这里用了定时器 3 : [cpp] view plain copy #define IN #define OUT [cpp] view plain copy void Timer3_Init(IN u16 period,IN u16 prescaler) { TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct; NVIC_InitTypeDef* NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); TIM_TimeBaseInitStruct->TIM_Prescaler = prescaler; TIM_TimeBaseInitStruct->TIM_Period = period; TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3,TIM_TimeBaseInitStruct); TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); NVIC_InitStructure->NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure->NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure->NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure->NVIC_IRQChannelSubPriority = 3; NVIC_Init(NVIC_InitStructure); TIM_Cmd(TIM3,ENABLE); } 6. 定时器中断: [cpp] view plain copy void TIM3_IRQHandler( void ) u8 i = 0; if (RESET != TIM_GetITStatus(TIM3,TIM_IT_Update)) { TIM_ClearITPendingBit(TIM3,TIM_IT_Update); for (i=0; i < task_count; ++i) { if (tasks[i].TimerSlice) { --tasks[i].TimerSlice; if (0 == tasks[i].TimerSlice) { tasks[i].isRun = 0x01; tasks[i].TimerSlice = tasks[i].SliceNumber; } } } } 在定时器中断中对任务结构体的 TimerSlice 递减,定时器没中断一次 TimerSlice 减一。当 TimerSlice 减到 0 时,将任务结构体的 isRun 置位,表示任务可执行,该标志在 Task_Process 函数中会用到。 7. 任务调度函数: [cpp] view plain copy void Task_Process() u8 i = 0; for (i=0; i < task_count; ++i) { if (tasks[i].isRun) { tasks[i].TaskPointer(NULL); tasks[i].isRun = 0; } } 8. 在 main 函数中调用 Task_Proccess 函数即可, [cpp] view plain copy int main( void ) { u8 t = 0; SystemInit(); delay_init(); NVIC_Configuration(); uart_init(9600); Timer3_Init(7999,7199); while (1) { Task_Process(); } return 0; } 到此,一个简单的时间片就实现了。蟹蟹大家,如有不对之处,请大家批评指正;如有雷同,纯属巧合。再次感谢!!! 程序运行结果如下: A key is pressing. LCD is Running. A key is pressing. LED is blinking. LCD is Running. A file is downloading. LCD is Running. A key is pressing. LCD is Running. A file is downloading. A key is pressing. LCD is Running. LED is blinking. LCD is Running. A file is downloading. A key is pressing. LCD is Running. A key is pressing. LCD is Running. A file is downloading. LED is blinking. LCD is Running. A key is pressing. LCD is Running. A file is downloading. A key is pressing. LCD is Running. 可见,程序实现了多任务的并行。
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮