各位大侠,我现在做了一个44B0驱动彩色LCD的项目,串口通信协议中是采用超时来判断一帧数据的结束,我用了一个定时器中断+一个超时计数来判断超时与否,但不知道为什么,虽然大多数情况下都可以正常判断帧的结束,但是有时就判断不出来,以至于把两个甚至三帧算作一帧数据,我判断最有可能是定时器的中断出了问题,不让为什么在不接收串口数据的情况下,超时计数会达不到超时时间呢?但是为什么定时器会出现不稳定呢,我很迷惑,请各位高手帮忙诊断一下,下面是这段三个中断服务程序和定时器初始化程序,Timer0为超时计算,Timer2用于定时键盘扫描: void __irq UartIsr(void) { rINTMSK|=BIT_GLOBAL|BIT_URXD0; rI_ISPC|=BIT_URXD0; if (RevFlag) { TimerCount=0; RevBuffTemp[RevCountTemp++]=rURXH0; } else { if (rURXH0==0xAA) { RevFlag=1; RevCountTemp=0; FrameOverFlag=0; // TimerStart(0); } } rINTMSK&=~(BIT_GLOBAL|BIT_URXD0); }
void __irq Timer0Isr(void) { rINTMSK|=BIT_GLOBAL|BIT_TIMER0; rI_ISPC|=BIT_TIMER0; if (RevFlag) TimerCount++; /* if (((TimerCount++)>=TimeOutValue)&&RevFlag) { TimerCount=0; RevFlag=0; FrameOverFlag=1; // TimerStop(0); }*/ rINTMSK&=~(BIT_GLOBAL|BIT_TIMER0); }
void __irq Timer2Isr(void) { rINTMSK|=BIT_GLOBAL|BIT_TIMER2; rI_ISPC|=BIT_TIMER2; KeyFlag=1; rINTMSK&=~(BIT_GLOBAL|BIT_TIMER2); } /************************Timer***********************************/ void TimerInit(U8 TimerNum,U8 Prescale,U8 Mux) { switch (TimerNum) { case 0: rTCFG0&=0xffffff00; rTCFG0|=Prescale; rTCFG1&=0xfffffff0; rTCFG1|=Mux; rTCON&=0xffffff00; rTCON|=0x8; rTCMPB0=0; rTCNTB0=0x1; rTCON&=0xffffff00; rTCON|=0xA; rTCON&=0xffffff00; rTCON|=0x8; break; case 1: rTCFG0&=0xffffff00; rTCFG0|=Prescale; rTCFG1&=0xffffff0f; rTCFG1|=(Mux<<4); rTCON&=0xfffff0ff; rTCON|=(0x8<<8); rTCMPB1=0; rTCNTB1=0x3E8; rTCON&=0xfffff0ff; rTCON|=(0xA<<8); rTCON&=0xfffff0ff; rTCON|=(0x8<<8); break; case 2: rTCFG0&=0xffff00ff; rTCFG0|=(Prescale<<8); rTCFG1&=0xfffff0ff; rTCFG1|=(Mux<<8); rTCON&=0xffff0fff; rTCON|=(0x8<<12); rTCMPB2=0; rTCNTB2=0x3E8; rTCON&=0xffff0fff; rTCON|=(0xA<<12); rTCON&=0xffff0fff; rTCON|=(0x8<<12); break; case 3: rTCFG0|=(Prescale<<8); rTCFG1=(Mux<<12); rTCON=(0x8<<16); rTCMPB3=0; rTCNTB3=0xA; rTCON=(0xA<<16); rTCON=(0x8<<16); break; case 4: rTCFG0=(Prescale<<16); rTCFG1=(Mux<<16); rTCON=(0x8<<20); rTCMPB4=0; rTCNTB4=0xA; rTCON=(0xA<<20); rTCON=(0x8<<20); break; case 5: rTCFG0=(Prescale<<16); rTCFG1=(Mux<<20); rTCON=(0x4<<24); rTCNTB5=0xA; rTCON=(0xA<<24); rTCON=(0x8<<24); break; default: break; } } |