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.

C/C++ > Timers

#105673 - NighTiger - Tue Oct 10, 2006 9:03 pm

Hi guys,
I tried to understand when 256 ms are incremented in the timers.

I had enabled the following timers:

Code:

  enableTimer2(_FREQUENCY_256_ | _TIMER_ENABLE_);
  enableTimer3(_TIMER_CASCADE_ | _TIMER_ENABLE_);


How can I obtain the 256 ms?
tnx

#105728 - gmiller - Wed Oct 11, 2006 3:12 pm

NighTiger wrote:
Hi guys,
I tried to understand when 256 ms are incremented in the timers.

I had enabled the following timers:

Code:

  enableTimer2(_FREQUENCY_256_ | _TIMER_ENABLE_);
  enableTimer3(_TIMER_CASCADE_ | _TIMER_ENABLE_);


How can I obtain the 256 ms?
tnx


The 256 timer "counts" every 256 clock ticks (the clock running at 16.7 Mhz) so expecting 256 ms is not going to deliver what you expect.

If you are looking for how to get the 32 bit value you are generating between the two data registers then the lower 16 bits would be Timer2's data register and the upper 16 bits would be timer3's data register. When you get these values remember that when you get the data timer2 is always running so it is possible that Timer2 could overflow after you read it then increment Timer3. I generally do not use multiple timers to get more bits. I use an interrupt and then increment a 32 bit timer by 65536 (adding bt skipping the low order 16 bits) and the the combination is just a "OR" of my 32 bit value with the 16 bit data register. This can be done many ways and I might be a little parnoid about the combination taking too much time.

#105886 - hickscorp - Thu Oct 12, 2006 10:45 pm

I dunno if it can help, but looking at your problem with math logic will probably give you some idea:
From what you say, it seems that you want a timer to tick each 256ms. It approximately means that the timer would tick 4 times a second (One second is 1000ms, so 1000 / 256 = 3.9, let's say 4).
The problem is: your code seems to ask for 256 ticks a second, not a tick each 256ms. Don't get confused, HTz are ticks per seconds, milliseconds are 1/1000 seconds :)

But i don't have a solution for your thing, maybe looking at the TIMER_DIV_x consts?

Bye!

#105974 - NighTiger - Fri Oct 13, 2006 7:50 pm

Tnx guys I have resolved.

#106047 - NighTiger - Sat Oct 14, 2006 6:01 pm

I have a problem.
I show you:

Code:

    if (_TM2D_ > (16780000/1024) && mbh > 0)
    {
      _MOSAIC_ = _SET_BGMOSAIC(mbh, mbv, 0, 0);
      mbh--, mbv--;
     _TM2D_ = 0;
    }
  }


I think that this code is correct.
But it seems that the demo alway enters in the if statement.
Does anybody know why? :-o