2016-10-12 87 views
0

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

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(); 
} 

回答

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(); 
    } 
} 
0

您将第10位延迟2 ms切换到第一个状态。即使这样做,你也不会注意到。