这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 关于定时器的匹配值

共2条 1/1 1 跳转至

关于定时器的匹配值

院士
2006-09-17 18:14:16     打赏
关于定时器的匹配值



关键词: 关于     定时器     匹配    

院士
2006-12-22 22:43:00     打赏
2楼
问 芯片使用lpc2214,在定时器1中使用了4个匹配值,程序当中动态的改变,但是问题是:有的时候设置的匹配值明明大于当前的TC值,但是就是不能产生匹配中断(匹配中断是开着的),不知道为什么? 1: 或者两个匹配值基本相同,会不会在各自匹配的时候置位相应的IR位 2: re.是否使用我们的工程模板,其它的中断是否可以正常产生。

只有匹配值等于TC值时才能发生匹配事件,可以产生中断。 3: 好像还是不行我发现当两个匹配值非常近的时候,好像只能匹配一个产生中断,另外一个就无法匹配了,中断程序使用的fiq,进程序后有如下操作:

1、读取IR值到一个临时变量,然后将该变量的值写入IR来清除中断
2、然后按位查询每个中断位,有中断就进行处理
3、退出中断

另外,我还尝试在退出中断之前从新进行以上步骤,再确认没有了中断之后再退出,但是问题依然存在,很奇怪。

当两个匹配值接近到0xff左右的时候,容易发生数值较大的匹配值无法匹配。我将IR清零了之后,按理说就可以再相应新的匹配中断了啊,但是还是不可以,郁闷好几天了。

我用的是keil 4: 请把中断服务程序代码贴出来看看。 5: 程序MR3是作为虚拟串口的
MR2-MR0作为其他串口的超时

程序注释少了点,麻烦了!

void FIQ_Handler(void) __fiq
{
    unsigned char ucTemp;
    unsigned char ucTmpPin,ucTmpIR;
    static unsigned char s_ucCounter,s_ucParity,s_ucTmpByte,s_test;    
    T0TC = 0;
    T0TCR = 1;
    T1TCR = 0;
    ucTmpIR = T1IR;
    //while (ucTmpIR & 0x0f)
    {
        T1IR = ucTmpIR;
        T1TCR = 1;
        if ((ucTmpIR & (1<<3)) == (1<<3))
        {
            //T1IR = 1<<3;
            if (gucSimUARTMode == UART_MODE_SEND)    //·&cent;&Euml;&Iacute;&Ecirc;&yacute;&frac34;&Yacute;
            {
                switch(s_ucCounter)
                {
                    case 0:    //&AElig;&eth;&Ecirc;&frac14;&Icirc;&raquo;
                    {
                         if (gusSimUARTSendLength == 0)    //·&cent;&Euml;&Iacute;&frac12;á&Ecirc;&oslash;
                        {
                            gucSimUARTMode = UART_MODE_RECV;
                            SIM_UART_RXD_INT;
                            EXTINT |= (1<<0);
                            SIM_UART_TXD_SET;
                            T1MCR &= ~(7<<9);
                            //T1IR = 1<<3;
                        }
                        else    //·&cent;&Euml;&Iacute;&AElig;&eth;&Ecirc;&frac14;&Icirc;&raquo;&pound;&not;&sup2;&cent;&Egrave;&iexcl;&sup3;&ouml;&Ograve;&raquo;&cedil;&ouml;&acute;&yacute;·&cent;×&Ouml;&frac12;&Uacute;
                        {    
                            T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                            SIM_UART_TXD_CLR;
                            s_ucTmpByte = *gpucSimUARTSendStart++;
                            gusSimUARTSendLength--;
                            s_ucCounter++;
                        }
                        break;
                    }
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:    //&Ecirc;&yacute;&frac34;&Yacute;&Icirc;&raquo;
                    {        
                        T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                        ucTmpPin = s_ucTmpByte & 0x01;
                        if (ucTmpPin == 0)
                        {
                             SIM_UART_TXD_CLR;
                        }
                        else
                        {
                            SIM_UART_TXD_SET;
                        }
                        s_ucTmpByte >>= 1;
                        s_ucParity += ucTmpPin;
                        s_ucCounter++;
                        break;
                    }
                    case 9:
                    {    
                        switch (gucSimUARTParity)
                        {    
                            case ODD:
                            {             
                                T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                                 if ((s_ucParity & 1) == 0)
                                {
                                     SIM_UART_TXD_SET;
                                }
                                else
                                {
                                      SIM_UART_TXD_CLR;
                                }
                                s_ucCounter++;
                                break;
                            }
                            case EVEN:
                            {          
                                T1MR3 = T1TC + (gulSimUARTBaudRate << 1);  
                                 if ((s_ucParity & 1) == 0)
                                {
                                     SIM_UART_TXD_CLR;
                                }
                                else
                                {
                                      SIM_UART_TXD_SET;
                                }
                                s_ucCounter++;
                                break;
                            }
                            default:    //&Icirc;&THORN;&ETH;&pound;&Ntilde;é&Icirc;&raquo;&pound;&not;·&cent;&Iacute;&pound;&Ouml;&sup1;&Icirc;&raquo;
                            {
                                if (gucSimUARTStopBit == STOP_BIT1)
                                {          
                                    T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                                }
                                else
                                {          
                                     T1MR3 = T1TC + (gulSimUARTBaudRate << 2);
                                }
                                SIM_UART_TXD_SET;
                                 s_ucCounter = 0;
                            }
                        }
                         break;
                    }
                    case 10:    //&Iacute;&pound;&Ouml;&sup1;&Icirc;&raquo;
                    {
                        if (gucSimUARTStopBit == STOP_BIT1)
                        {      
                            T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                        }
                        else
                        {         
                             T1MR3 = T1TC + (gulSimUARTBaudRate << 2);
                        }
                        SIM_UART_TXD_SET;
                        s_ucCounter = 0;
                         break;
                    }
                    default:
                    {
                        gucSimUARTMode = UART_MODE_RECV;
                        SIM_UART_TXD_SET;
                        s_ucCounter = 0;
                        T1MCR &= ~(7<<9);
                        //T1IR = 1<<3;
                    }
                }
            }
            else
            {
                if (gucSimUARTNewByte == 0)    //&Ouml;&iexcl;&frac12;á&Ecirc;&oslash;
                {
                    if ((gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength > 0) &&
                        (gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength <= SIM_UART_RECV_BUF_SIZE))
                    {      
                        gstSimUARTRecv[gucSimUARTRecvCurFrame].ucReady = 1;
                        ucTemp = (gucSimUARTRecvCurFrame + 1) % SIM_UART_MAX_FRAME_NUM;
                        gstSimUARTRecv[ucTemp].usStart = (    gstSimUARTRecv[gucSimUARTRecvCurFrame].usStart +
                                                            gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength)
                                                            % SIM_UART_RECV_BUF_SIZE;
                        gucSimUARTRecvCurFrame = ucTemp;
                    }
                    gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength = 0;
                    gstSimUARTRecv[gucSimUARTRecvCurFrame].ucReady = 0;
                    SIM_UART_RXD_INT;
                    EXTINT |= (1<<0);
                    SIM_UART_RXD_EN_INT;
                    T1MCR &= ~(7<<9);
                    T1IR = 1<<3;
                }
                else //&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;&ETH;&Acirc;&micro;&Auml;×&Ouml;&frac12;&Uacute;
                {
                    ucTmpPin = SIM_UART_RXD_READ;
                    switch (s_ucCounter)
                    {
                        case 0:    //&AElig;&eth;&Ecirc;&frac14;&Icirc;&raquo;
                        {
                            if (ucTmpPin == 0)    //&Otilde;&yacute;&Egrave;·&micro;&Auml;&AElig;&eth;&Ecirc;&frac14;&Icirc;&raquo;
                            {         
                                T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                                s_ucParity = 0;
                                s_ucTmpByte = 0;
                                s_ucCounter++;
                            }
                            else
                            {            
                                T1MR3 = T1TC + gulSimUARTTimeOut + gulSimUARTBaudRate;
                                SIM_UART_RXD_INT;
                                EXTINT |= (1<<0);
                                SIM_UART_RXD_EN_INT;
                                gucSimUARTNewByte = 0;
                            }
                            break;
                        }
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:    //&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;8&cedil;&ouml;&Ecirc;&yacute;&frac34;&Yacute;&Icirc;&raquo;
                        {        
                            T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                            s_ucTmpByte = (s_ucTmpByte>>1) + ucTmpPin;
                            s_ucParity += ucTmpPin;
                            s_ucCounter++;
                            break;
                        }
                        case 9:    //&frac12;&Oacute;&Ecirc;&Otilde;&ETH;&pound;&Ntilde;é&Icirc;&raquo;
                        {
                            if (gucSimUARTParity == NONE)    //&Atilde;&raquo;&Oacute;&ETH;&ETH;&pound;&Ntilde;é&Icirc;&raquo;&pound;&not;&cedil;&Atilde;&Icirc;&raquo;&frac34;&Iacute;&Ecirc;&Ccedil;&Iacute;&pound;&Ouml;&sup1;&Icirc;&raquo;
                            {      
                                if (gucSimUARTStopBit == STOP_BIT1)
                                {     
                                    T1MR3 = T1TC + gulSimUARTTimeOut + gulSimUARTBaudRate;
                                }
                                else
                                {      
                                     T1MR3 = T1TC + gulSimUARTTimeOut + gulSimUARTBaudRate * 3;
                                }
                                if (ucTmpPin == 0x80)    //&Otilde;&yacute;&Egrave;·&micro;&Auml;&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&Aacute;&Euml;&Iacute;&pound;&Ouml;&sup1;&Icirc;&raquo;&pound;&not;±&pound;&acute;&aelig;&Ecirc;&yacute;&frac34;&Yacute;
                                {
                                    gucSimUARTRecvBuf[(gstSimUARTRecv][gucSimUARTRecvCurFrame].usStart +
                                                     gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength)
                                                     % SIM_UART_RECV_BUF_SIZE]=s_ucTmpByte;
                                    gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength++;
                                }
                                SIM_UART_RXD_INT;
                                EXTINT |= (1<<0);
                                SIM_UART_RXD_EN_INT;
                                gucSimUARTNewByte = 0;
                                s_ucCounter = 0;
                            }
                            else
                            {      
                                T1MR3 = T1TC + (gulSimUARTBaudRate << 1);
                                s_ucParity += ucTmpPin;
                                s_ucCounter++;
                            }
                            break;
                        }
                        case 10://&Iacute;&pound;&Ouml;&sup1;&Icirc;&raquo;
                        {    
                            if (gucSimUARTStopBit == STOP_BIT1)
                            {       
                                T1MR3 = T1TC + gulSimUARTTimeOut + gulSimUARTBaudRate;
                            }
                            else
                            {      
                                 T1MR3 = T1TC + gulSimUARTTimeOut + gulSimUARTBaudRate * 3;
                            }
                            if ((ucTmpPin == 0x80) &&
                                (((gucSimUARTParity == ODD) && (s_ucParity == 0x80)) ||
                                ((gucSimUARTParity == EVEN) && (s_ucParity == 0))))    //&frac12;&Oacute;&Ecirc;&Otilde;&micro;&frac12;&Otilde;&yacute;&Egrave;·&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;
                            {
                                gucSimUARTRecvBuf[(gstSimUARTRecv][gucSimUARTRecvCurFrame].usStart +
                                                 gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength)
                                                 % SIM_UART_RECV_BUF_SIZE]=s_ucTmpByte;
                                gstSimUARTRecv[gucSimUARTRecvCurFrame].usLength++;
                            }
                            SIM_UART_RXD_INT;
                            EXTINT |= (1<<0);
                            SIM_UART_RXD_EN_INT;
                            gucSimUARTNewByte = 0;
                            s_ucCounter = 0;
                            break;
                        }
                        default:
                        {                  
                            T1MR3 = T1TC + gulSimUARTTimeOut + gulSimUARTBaudRate * 3;
                            SIM_UART_RXD_INT;
                            EXTINT |= (1<<0);
                            SIM_UART_RXD_EN_INT;
                            gucSimUARTNewByte = 0;
                            s_ucCounter = 0;
                            //T1IR = 1<<3;
                        }
                    }
                }
            }
            gulTC2 = T1TC;
            gulMCR = T1MCR;
            gulMR0 = T1MR0;
            gulMR1 = T1MR1;
            gulMR2 = T1MR2;
            gulMR3 = T1MR3;
            gulIR = T1IR;
        }
        if ((ucTmpIR & 1) == 1) //RS485&frac12;&Oacute;&Ecirc;&Otilde;×&Ouml;&frac12;&Uacute;&sup3;&not;&Ecirc;±
        {    
            if (gucRS485Mode == UART_MODE_RECV)
            {
                if ((gstRS485Recv[gucRS485RecvCurFrame].usLength > 0) &&
                    (gstRS485Recv[gucRS485RecvCurFrame].usLength <= RS485_RECV_BUF_SIZE))
                {
                    gstRS485Recv[gucRS485RecvCurFrame].ucReady = 1;
                    ucTemp = (gucRS485RecvCurFrame + 1) % RS485_MAX_FRAME_NUM;
                    gstRS485Recv[ucTemp].usStart = (    gstRS485Recv[gucRS485RecvCurFrame].usStart +
                                                        gstRS485Recv[gucRS485RecvCurFrame].usLength)
                                                        % RS485_RECV_BUF_SIZE;
                    gucRS485RecvCurFrame = ucTemp;
                }
                gstRS485Recv[gucRS485RecvCurFrame].usLength = 0;
                gstRS485Recv[gucRS485RecvCurFrame].ucReady = 0;
            }
            else
            {
                gucRS485Mode = UART_MODE_RECV;
                RS485_RECV;
            }
            T1MCR &= ~7;
            //T1IR = 1;
        }
        if ((ucTmpIR & (1<<1)) == (1<<1))    //CDMA&frac12;&Oacute;&Ecirc;&Otilde;×&Ouml;&frac12;&Uacute;&sup3;&not;&Ecirc;±
        {
            if ((gstCDMARecv[gucCDMARecvCurFrame].usLength > 0) &&
                (gstCDMARecv[gucCDMARecvCurFrame].usLength <= CDMA_RECV_BUF_SIZE))
            {
                gstCDMARecv[gucCDMARecvCurFrame].ucReady = 1;
                ucTemp = (gucCDMARecvCurFrame + 1) % CDMA_MAX_FRAME_NUM;
                gstCDMARecv[ucTemp].usStart = (    gstCDMARecv[gucCDMARecvCurFrame].usStart +
                                                gstCDMARecv[gucCDMARecvCurFrame].usLength)
                                                % CDMA_RECV_BUF_SIZE;
                gucCDMARecvCurFrame = ucTemp;
            }     
            gstCDMARecv[gucCDMARecvCurFrame].usLength = 0;
            gstCDMARecv[gucCDMARecvCurFrame].ucReady = 0;
            T1MCR &= ~(7<<3);
            //T1IR = 1<<1;
        }
        if ((ucTmpIR & (1<<2)) == (1<<2))    //CDMA·&cent;&Euml;&Iacute;×&Ouml;&frac12;&Uacute;&sup3;&not;&Ecirc;±
        {       
            //T1IR = 1<<2;
            gucCDMASending = 0;
            T1MCR &= ~(7<<6);
        }
        //ucTmpIR = T1IR;
    }
    gultest = T0TC;
    T0TCR = 0;
    gulTC3 = T1TC;
} 6: 问题多个串口同时收发的时候就会有问题,就是所谓的匹配值相近的时候出现问题,程序当中动态的设置MR值,TC一直计数 7: 请各位帮忙看看,郁闷好多天了

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]