棧在MCU中有著非常重要的作用。定義局部變量,函數跳轉,寄存器保護都需要使用到棧。
但是在MCU中,只會默認一定大小的棧給我們使用。那么多任務就需要多個棧了,那只能自己的創造棧了。
static unsigned int stkbuf[100]; //創造一個100個word的任務棧。這個是16位單片機寄存器要以16位為準
void NewFunBuff(VOID_FUN_PTR task,unsigned int* opt)
{
*opt = (U16)(((U32)task) >> 8); //將任務的指針放到stkbuf中
opt--;
*opt = 0x80; //設置地址偏移
((U8*)opt)--;
*opt = 0x55; //設置地址偏移
asm{
lds opt /將最終的地址得到的指針放到sP中,這個時候系統的SP就會給改變
}
asm nop
這個最重要的地方就是在返回的時候需要從sp中取出進入函數前的數據。這個時候也會取出PC指針。而在取值的過程中SP是自增的,我們需要保證取PC的時候就是取到stkbuf 中存task位置上。然后返回的時候就直接返回到了task中去了。
}
void InitSystem(void)
{
InitSystemClock();
InitSystemIo();
}
void main(void) {
/* put your own code here */
DisableInterrupts;
InitSystem();
EnableInterrupts;
//FunTask1();
NewFunBuff(FunTask1,&stkbuf[99]);
while(1) {
}
/* please make sure that you never leave main */
}
而task的指針就是Funtask1.那程序就會去跑Funtask1了、
有問題嗎?仿真吧,看看內存的變化就知道為什么了。