uchar MAddressNew,MAddressOld; //消息的记录
uint MessageRam[MESSAGE_AMOUNT]; //储存需要处理的消息
/*****************************************************
*功能:初始化消息,插入消息,读取消息
*函数名:
*void InitMessage(void);
*void WriteMessage(uchar Priority_ITM,uint Kind_ITM);
*void ReadMessage(void);
*
*
*
*
*****************************************************/
void InitMessage(void)
{
uchar i_IM;
MAddressNew=0;
MAddressOld=0;
for(i_IM=0;i_IM<MESSAGE_AMOUNT;++i_IM)
{
MessageRam[i_IM]=NO_MESSAGE;
}
}
void WriteMessage(uchar Priority_ITM,uint Value_ITM) //这个提供的优先级而已....前面的那个是优先级....
{
switch(Priority_ITM)
{
case 1:
{
if(MAddressOld-MAddressNew>=2)
{
++MAddressNew;
MessageRam[MAddressNew]=Value_ITM;
}
else if(MAddressNew>=MAddressOld)
{
if(MAddressNew<(MESSAGE_AMOUNT-1))
{
++MAddressNew;
MessageRam[MAddressNew]=Value_ITM;
}
else if(MAddressNew==(MESSAGE_AMOUNT-1)&&(MAddressOld!=0))
{
MAddressNew=0;
MessageRam[MAddressNew]=Value_ITM;
}
}
break;
}
case 2:
{
if(MAddressOld-MAddressNew>=2)
{
MessageRam[MAddressOld]=Value_ITM;
--MAddressOld;
}
else if(MAddressNew>=MAddressOld)
{
if(MAddressOld>0)
{
MessageRam[MAddressOld]=Value_ITM;
--MAddressOld;
}
else if(MAddressOld==0)
{
MessageRam[MAddressOld]=Value_ITM;
MAddressOld=MESSAGE_AMOUNT-1;
}
}
break;
}
default :
{
break;
}
}
}
void ReadMessage(void)
{
if(MAddressOld==MAddressNew)
{
ProcessMessage=0;
}
else if((MAddressOld<(MESSAGE_AMOUNT-1)))
{
++MAddressOld;
ProcessMessage=MessageRam[MAddressOld];
MessageRam[MAddressOld]=0;
}
else if((MAddressOld==(MESSAGE_AMOUNT-1)))
{
MAddressOld=0;
ProcessMessage=MessageRam[MAddressOld];
MessageRam[MAddressOld]=0;
}
}
其实很简单的,写过数据结构的人来说,这是小kiss,你自己看看....方法都是类似的,可以根据你自己的改进...
|