我有用于调试的LPC1769 LPCXpresso和LPC-Link 2。我的开发环境是IAR。
我想使用Timer体验一个简单的LED闪烁。网上有很多示例代码。因此,我选择了为MCB1700板制作的样品之一。我还在这里和那里阅读了一些定时器如何与LPC一起工作的信息。
代码已编译,我将程序下载到了设备(LPC1769)。 我以为当计时器在给定间隔内调用处理程序时,LED应该闪烁。
但是,该程序根本不会命中TIMER0_IRQHandler。
这是我main.c文件中的完整代码:
#include <lpc17xx.h>
void TIMER0_IRQHandler(void);
int main(void)
{
// (1) Timer 0 configuration (see page 490 of user manual)
LPC_SC->PCONP |= 1 << 1; // Power up Timer 0 (see page 63 of user manual)
LPC_SC->PCLKSEL0 |= 1 << 2; // Clock for timer = CCLK, i.e., CPU Clock (page 56 user manual)
// MR0 is "Match Register 0". MR0 can be enabled through the MCR to reset
// the Timer/Counter (TC), stop both the TC and PC, and/or generate an interrupt
// every time MR0 matches the TC. (see page 492 and 496 of user manual)
LPC_TIM0->MR0 = 500; //Toggle Time in mS
//LPC_TIM0->MR0 = 1 << 23; // Give a value suitable for the LED blinking
// frequency based on the clock frequency
// MCR is "Match Control Register". The MCR is used to control if an
// interrupt is generated and if the TC is reset when a Match occurs.
// (see page 492 and 496 of user manual)
LPC_TIM0->MCR |= 1 << 0; // Interrupt on Match 0 compare
LPC_TIM0->MCR |= 1 << 1; // Reset timer on Match 0
// TCR is "Timer Control Register". The TCR is used to control the Timer
// Counter functions. The Timer Counter can be disabled or reset
// through the TCR. (see page 492 and 494 of user manual)
LPC_TIM0->TCR |= 1 << 1; // Manually Reset Timer 0 (forced);
LPC_TIM0->TCR &= ~(1 << 1); // Stop resetting the timer
// (2) Enable timer interrupt;
// TIMER0_IRQn is 1, see lpc17xx.h and page 73 of user manual
NVIC_EnableIRQ(TIMER0_IRQn); // see core_cm3.h header file
// (3) Some more one-time set-up's;
LPC_TIM0->TCR |= 1 << 0; // Start timer (see page 492 and 494 of user manual)
LPC_SC->PCONP |= ( 1 << 15 ); // Power up GPIO (see lab1)
LPC_GPIO1->FIODIR |= 1 << 29; // Put P1.29 into output mode. LED is connected to P1.29
// (4) infinite loop;
while (1) // Why do we need this?
{
//LPC_GPIO1->FIOPIN ^= 1 << 29; // Toggle the LED (see lab1)
}
return 0;
}
// Here, we describe what should be done when the interrupt on Timer 0 is handled;
// We do that by writing this function, whose address is “recorded” in the vector table
// from file startup_LPC17xx.s under the name TIMER0_IRQHandler;
void TIMER0_IRQHandler(void)
{
// IR is "Interrupt Register". The IR can be written to clear interrupts. The IR
// can be read to identify which of eight possible interrupt sources are
// pending. (see page 492 and 493 of user manual)
if ( (LPC_TIM0->IR & 0x01) == 0x01 ) // if MR0 interrupt (this is a sanity check);
{
LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag (see page 492 and 493 of user manual)
LPC_GPIO1->FIOPIN ^= 1 << 29; // Toggle the LED (see lab1)
}
}
任何指针将不胜感激。
答案 0 :(得分:1)
我运行了您的代码,我可以告诉您TIMER0_IRQHandler确实在执行,至少对我而言。为了证明这一点,请尝试运行IAR IDE调试器并在以下行中设置“断点”:
void TIMER0_IRQHandler(void)
程序每次进入时都会在此函数调用处暂停。
我对lpc1769的较低级别的函数调用没有太多的经验,但是我可以看到您的代码中的问题出在这里:
LPC_TIM0->MR0 = 500; //Toggle Time in mS
//LPC_TIM0->MR0 = 1 << 23; // Give a value suitable for the LED blinking
匹配寄存器MR0没有预分频值,因此不是每500毫秒执行一次,而是每500微秒执行一次。这类似于创建PWM信号,该信号将使LED闪烁得如此之快,以至于肉眼看不见。您可以尝试学习更多有关使用LPC_TIM0-> PR设置预分频值以使计时器/计数器的值大于1uS的信息。不使用预分频器,我可以通过设置以下内容来每1秒闪烁一次LED:
LPC_TIM0->MR0 = 100000000;
还要确保您的LED电路中有一个电阻,所以不要炸它。确保将已设置的输出引脚P1.29连接到板上的正确位置(PAD 12?): LPC Xpresso Pinout
希望这会有所帮助!