中断C定时器

时间:2016-10-12 06:02:28

标签: c timer interrupt

定时器2和定时器3之间似乎存在冲突。这是一个MIPS板,而不是使用汇编语言来编程;我正在使用C. Timer1用于正常工作的计数。 Timer2用于闪烁的LED,可正常工作。 Timer3用于切换计数方向。但是timer2和timer3之间存在冲突。有谁知道冲突在哪里?我必须注释掉DelayInit3();为了使代码正确执行。

void __ISR(_TIMER_2_VECTOR, ipl2) Timer2Handler(void)
{
// clear the interrupt flag
mT2ClearIntFlag();
    PORTToggleBits(IOPORT_B, BIT_10); 
 }
 void __ISR(_TIMER_23_VECTOR, ipl2) Timer23Handler(void)
 {
    // clear the interrupt flag
    mT3ClearIntFlag();
    if (direction != 0){
        direction < 1;
    }
    else{
        direction != 0;
    }
}

int main()
{
DeviceInit();
DelayInit1();
DelayInit2();
//  DelayInit3();

}
void DelayInit1()   
{
unsigned int tcfg1;

/* Configure Timer 1. This sets it up to count a 10Mhz with a period of 0xFFFF
  */
    tcfg1 =     T1_ON|T1_IDLE_CON|T1_SOURCE_INT|T1_PS_1_8|T1_GATE_OFF|T1_SYNC_EXT_OFF;
OpenTimer1(tcfg1, 0xFFFF);

}


void DelayInit2()
{
unsigned int tcfg2;

// Config Timer 2. This sets it to count 312500 Hz with a period of T2_TICK
tcfg2 = T2_ON | T2_SOURCE_INT | T2_PS_1_32;
OpenTimer2(tcfg2, T2_TICK);

// Now enable system-wide multi-vector interrupt handling
INTEnableSystemMultiVectoredInt();

// Configure timer 2 interrupt with a priority of 2
ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);

// Clear interrupt flag
mT2ClearIntFlag();
}

void DelayInit3()
{
unsigned int tcfg3;

// Config Timer 3. This sets it to count 312500 Hz with a period of T3_TICK
tcfg3 = T3_ON | T3_SOURCE_INT | T3_PS_1_256;
OpenTimer23(tcfg3, T23_TICK);

// Now enable system-wide multi-vector interrupt handling
INTEnableSystemMultiVectoredInt();

// Configure timer 3 interrupt with a priority of 2
ConfigIntTimer23(T23_INT_ON | T23_INT_PRIOR_2);

// Clear interrupt flag
mT3ClearIntFlag();
}

2 个答案:

答案 0 :(得分:0)

您还应该在每个定时器结束时切换位。你正在切换的顺序是错误的。在每个计时器结束时,您将两次切换BIT10,即将其恢复到初始位置。

你可以使用这样的代码。

count = 0;  // in Init.
while(1)
{
    if (IFS0bits.T2IF == 1)
    {
        //if timer == period, toggle the LED
        count++;
        PORTToggleBits(IOPORT_B, BIT_10);
        if (count %2 == 0)
        {
          DelayMs(2);
          PORTToggleBits(IOPORT_B, BIT_11);
        }
        if (count > 3)
           count = 0;
        mT2ClearIntFlag();
    }
 }

答案 1 :(得分:0)

您正在将Bit 10以2 ms的延迟切换到它的第一个状态。即使这样有效,你也不会注意到。