因为有时候上位机处理不及时,MCU需要缓冲一些数据,我自己琢磨着怎么做这个,所以用代码实现了如下;
/********************************************************函数原型:函数功能:堆栈初始化输入参数:输出参数:说 明:*********************************************************/#define StackSize 8unsigned char StackTemp[StackSize];void StackInitinal(void){u8 Num;for(Num =0; Num <StackSize; Num++){StackTemp[Num] =0;}}
/********************************************************函数原型:函数功能:事件堆栈输入参数:输出参数:说 明:*********************************************************/void PushINStack(unsigned char Event) //压栈{ unsigned char PushINSta =0, Num =StackSize, NullNumber =0; if(Event ==0) {return; }while(Num--) //查询开始 {if(StackTemp[Num] ==Event)//查询是否有一致的 { PushINSta =2; break; } else if(StackTemp[Num] ==0)//位置是否为空 { NullNumber =Num;//记录空的位置 PushINSta =1; } } if(PushINSta==1) { PushINSta =0; StackTemp[NullNumber] =Event;//事件存入空的位置 } else if(StackTemp[StackSize-1] !=0) { if(PushINSta ==0) { StackTemp[StackSize-1] =Event;return; } }}
/********************************************************函数原型:函数功能:事件出栈输入参数:输出参数:说 明:*********************************************************/unsigned char PushOUTStack(void) //出栈{ unsigned char Num =0, Push =0, temp =0, temp2 =0; if(StackTemp[Num] !=0)//第0个寄存器内有数据 { temp =StackTemp[Num]; StackTemp[Num] =0; Push =1; } if(Push ==1) //整理压栈数据 { while(Push ==1) { temp2 =Num+1; if(temp2 <(StackSize)) { StackTemp[Num] = StackTemp[temp2];StackTemp[temp2] =0; } else { Push =0; } Num =temp2; } } return temp;}
应用的时候,
...
PushINStack(Event ); //存入事件栈
...
temp =PushOUTStack(); //提取数组内最前面的数;
新闻热点
疑难解答