2016-10-12 91 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切換到第一個狀態。即使這樣做,你也不會注意到。