gpt4 book ai didi

c - 中断C定时器

转载 作者:行者123 更新时间:2023-11-30 16:56:12 36 4
gpt4 key购买 nike

定时器2和定时器3之间似乎有冲突。这是一 block 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();
}

最佳答案

您还应该在每个计时器结束时切换这些位。您切换的顺序是错误的。每次定时器结束时,您都会切换 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();
}
}

关于c - 中断C定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39991482/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com