TI官方例程代码中的任务状态机用于处理各种不同的非环路控制任务,这种状态机编程对于多任务管理比较有用
- // Timing sync for background loops
- // Timer period definitions found in PeripheralHeaderIncludes.h
- CpuTimer0Regs.PRD.all = mSec1; // A tasks,200000,1ms,CPU frequency=200MHz
- CpuTimer1Regs.PRD.all = mSec10; // B tasks,2000000,10ms
- CpuTimer2Regs.PRD.all = mSec100; // C tasks,20000000,100ms
-
- // Tasks State-machine init
- Alpha_State_Ptr = &A0;
然后在main函数里的while循环中写下如下代码:- //--------------------------------- FRAMEWORK -------------------------------------
- for(;;) //infinite loop
- {
- // State machine entry & exit point
- //===========================================================
- (*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)
- //===========================================================
- }
复制代码
在main函数之外有:
- <font microsoft="" yahei";"=""><ul type="1" class="litype_1" style="margin-left: 14px; padding-right: 20px; padding-left: 20px; list-style: none; font-style: italic;"><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">//--------------------------------- FRAMEWORK ------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">void A0(void) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">{ </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> // loop rate synchronizer for A-tasks </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> if(CpuTimer0Regs.TCR.bit.TIF == 1) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> { </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> CpuTimer0Regs.TCR.bit.TIF = 1; // clear flag </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> //----------------------------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> (*A_Task_Ptr)(); // jump to an A Task (A1,A2,A3,...) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> //----------------------------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> } </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> Alpha_State_Ptr = &B0; // Comment out to allow only A tasks </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">} </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">void B0(void) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">{ </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> // loop rate synchronizer for B-tasks </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> if(CpuTimer1Regs.TCR.bit.TIF == 1) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> { </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> CpuTimer1Regs.TCR.bit.TIF = 1; // clear flag </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> //----------------------------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> (*B_Task_Ptr)(); // jump to a B Task (B1,B2,B3,...) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> //----------------------------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> } </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> Alpha_State_Ptr = &C0; // Allow C state tasks </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">} </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">void C0(void) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">{ </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> // loop rate synchronizer for C-tasks </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> if(CpuTimer2Regs.TCR.bit.TIF == 1) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> { </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> CpuTimer2Regs.TCR.bit.TIF = 1; // clear flag </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> //----------------------------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> (*C_Task_Ptr)(); // jump to a C Task (C1,C2,C3,...) </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> //----------------------------------------------------------- </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> } </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black"> Alpha_State_Ptr = &A0; // Back to State A0 </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">} </font>
- </li></ul></font>
复制代码
任务A:- void A1(void)
- {
- do something
- //the next time CpuTimer0 'counter' reaches Period value go to A2
- A_Task_Ptr = &A2;
- }
-
- void A2(void)
- {
- do something
- //the next time CpuTimer0 'counter' reaches Period value go to A3
- A_Task_Ptr = &A3;
- }
-
- void A3(void)
- {
- do something
- //the next time CpuTimer0 'counter' reaches Period value go to A4
- A_Task_Ptr = &A4;
- }
-
- void A4(void)
- {
- do nothing
- //the next time CpuTimer0 'counter' reaches Period value go to A1
- A_Task_Ptr = &A1;
- }
复制代码
任务B和C的细分也类似于A,同上
|