当计时器与MR0匹配时,在中断服务程序的末尾运行中断服务程序。我不会从服务例程返回主程序。为什么我的程序无法从服务例程返回?

请参阅下面的答案以获取完整的代码

最佳答案

 the  code  is                                                                              /* Timer.h */
#include "LPC214x.h"
#include "main.h"

#define VIC_EN      5
#define VIC_TIMER0  4
#define MR0         0
void timer_init(void);
void timer_isr(void);


 /* Timer.c */
 volatile uint8_t flag;
 void timer_init()
 {
 //disable and reset timer counters
 T0TCR = BV(1);

 //use T0 as TIMER:
  T0CTCR = 0x00;

 //set prescalar
 T0PR = 15000000-1;

 //setup MR0 for 5 sec
  T0MR0 = 4; //4+1

 //enable intr on MR0, reset
  T0MCR |= BV(0) | BV(1);

 //enable T0 intr in VIC
 VICVectAddr1 = (uint32_t)timer_isr;
 VICVectCntl1 = BV(VIC_TIMER0) | VIC_EN;
 VICIntSelect &= ~BV(VIC_TIMER0);
 VICIntEnable |= BV(VIC_TIMER0);

 //enable timer counter
 T0TCR = BV(0);
 }

void timer_isr()
{
   flag=1;

 //clear intr in TIMER regrs
 T0IR |= BV(0);

 //clear intr in VIC
 VICVectAddr = 0x00000000;
 }

/* Main.c*/

int main (void)
{
int cnt=0;
char str[32];
timer_init();
lcd_init();
lcd_putstring(LCD_LINE1," *TIMER* ");
_delay_ms(1000);
str_printf(str,"Count:%d",cnt);
//lcd_putstring(LCD_LINE2,str);

while(1)
{
    while(flag==0);
    flag = 0;
    cnt++;
    str_printf(str,"Count:%d",cnt);
    lcd_putstring(LCD_LINE2,str);
}
return 0;
}

10-07 15:20