gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Trying to make a timer interrrupt

#102627 - Tails86 - Fri Sep 15, 2006 7:47 pm

I have never worked with interrupts before. I have always been taught the basic concept of an interrupt, but I have no experience of programming one.

I decided to take a whack at it and tried programming a counter that went up every 100mS. I wrote this program, but all that shows up on the screen is a 0. This shows that it is going into the interrupt at least once.

I tried to use a global variable to count how many times the interrupt has been activated instead of using another timer as an overflow, but that still didnt work.

Am I setting up the interrupt right?

Code:
#include <mygba.h>
MULTIBOOT
 #include "main.h"

void outlights(void)
{
  unsigned int interrupts = REG_IF;
    char str[20];
  if(interrupts & INT_TIMER0)
  {
       unsigned int count=REG_TM1D;
   WaitVBlank();
    DrawBox3(140,30,180,38,0x0000);
    sprintf(str,"%i",count);
    Print(140,30,str,0xFF0);   
  }
  *(unsigned short *)0x03fffff8 |= interrupts;
  REG_IF = interrupts;
}

int main(void)
{
   ham_Init();

    REG_IME=0x00;
    REG_INTERUPT=(u32)outlights;
    REG_IE |= INT_TIMER0;
    REG_IME=0x01;
   
    REG_DISPCNT = (3 | BG2_ENABLE);
   
    //start timer, overflow every .1 seconds
    REG_TM0D=63898;
    REG_TM0CNT = TIMER_FREQUENCY_1024 | TIMER_ENABLE;
    REG_TM1CNT = TIMER_OVERFLOW | TIMER_ENABLE;   
   
   while(true)
   {
      ;
   }

   return 0;
}

#102642 - Tails86 - Fri Sep 15, 2006 9:14 pm

I figured it out. I forgot to set the timer control to activate the interrupt (TIMER_IRQ_ENABLE).

#102651 - tepples - Fri Sep 15, 2006 11:03 pm

In addition, if you want to make an animation that runs every 100 milliseconds, it's usually easier to make it run once every six vblanks. You have only four timers at once, and you have to use some of them for audio if you're using audio.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.