U32 G_ulTimer_Tick;

#pragma vector=BASICTIMER_VECTOR
__interrupt void Interrupt_BasicTimer(void)
{
        S_MSG_TAG        mess;
        
        G_ulTimer_Tick++;

        /*post mail to main loop for ui timer  process*/
        mess.id=MSG_SYSTEM_CLASS;
        Mail_PostMessage(mess);                
}

void Main_LockLoop(void)
{
        S_MSG_TAG *message=NULL;

        /*enable key and base timer interrupt*/
        Enable_Interrupt();

        while(1){

        Mail_GetMessage(&message);
        if(message!=NULL){
        switch(message->id){
        case  MSG_KEY_CLASS:                        
        break;
                                                
        case  MSG_UART_CLASS_WRITE:
        break;

        case  MSG_UART_CLASS_READ:
        break;
                                                
        case  MSG_UI_CLASS:                                                                
        break;
        case  MSG_UI_CLASS_DIPLAY:                                
        break;

        case MSG_SYSTEM_CLASS:
        //1000*60*60*24=86400000  one day
        if(G_ulTimer_Tick>86400000)
        System_SetTick(0);
        /* 这里可以添加相应的定时处理函数，通过 G_ulTimer_Tick*/
        
        break;
        case MSG_USB_CLASS:
        break;
        default:
        break;
        }
        memset(message,0,sizeof(S_MSG_TAG));
        message=NULL;
        }
                        
        }
}


static S_MSG_QUEUE_TAG MSG_Queue={0};
#define        MSG_QUEUE_SIZE        64
#define        MSG_SIZE                        2

typedef  struct {
        INT8U        id;             /*messageid*/
        INT8U        msg[MSG_SIZE];
}S_MSG_TAG;


typedef  struct {
        INT8U        head;             /*queue head*/
        INT8U        tail;              /*queue tail */
        INT8U        count;            /*queue count*/
        S_MSG_TAG        data[MSG_QUEUE_SIZE];
}S_MSG_QUEUE_TAG;

void Mail_PostMessage(S_MSG_TAG message)
{
        /*disable interrupt*/
        _DINT();        
        if(MSG_Queue.count<=MSG_QUEUE_SIZE){
                MSG_Queue.head++;
                if(MSG_Queue.head==MSG_QUEUE_SIZE)
                        MSG_Queue.head=0;
                MSG_Queue.data[MSG_Queue.head].id=message.id;
                MSG_Queue.data[MSG_Queue.head].msg[0]=message.msg[0];
                MSG_Queue.data[MSG_Queue.head].msg[1]=message.msg[1];
                MSG_Queue.count++;
        }
        else{
                MSG_Queue.count=0;
                MSG_Queue.head=0;
                MSG_Queue.tail=0;
        }
        /*enable interrupt*/
        _EINT();        
}

void Mail_GetMessage(S_MSG_TAG**mess)
{
                /*disable interrupt*/
                _DINT();
                mess=mess;/*compile warning*/
                if(MSG_Queue.head!=MSG_Queue.tail){
                        MSG_Queue.tail++;
                        if(MSG_Queue.tail==MSG_QUEUE_SIZE)
                                MSG_Queue.tail=0;
                        *mess=&MSG_Queue.data[MSG_Queue.tail];
                        if(MSG_Queue.count>0)
                                MSG_Queue.count--;
                }
                /*enable interrupt*/
                _EINT();
}
basictimer 配置1ms或者4ms都可以，然后启动消息机制，如果定时的，可以通过tick来处理！
