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.

Coding > Help with timers

#1368 - Epo - Fri Jan 17, 2003 6:02 am

Hi,

Does anyone have some kinf of routine that pauses for so long using timers; a Wait() function, where you pass the number of miliseconds.

I keep trying to use REG_TM3D and REG_TM3CNT.. but i am getting no results =(

Thanks,
- Epo

#1369 - mo - Fri Jan 17, 2003 6:26 am

http://216.167.73.47/~dovoto/English/tutorial_5.html#timers

#1394 - Gordon - Sat Jan 18, 2003 4:27 am

Epo wrote:
Hi,

Does anyone have some kinf of routine that pauses for so long using timers; a Wait() function, where you pass the number of miliseconds.

I keep trying to use REG_TM3D and REG_TM3CNT.. but i am getting no results =(

Thanks,
- Epo

only thing you need is the gba.h file
Code:
/////////////////Bits////////////////
#define BIT00 1
#define BIT01 2
#define BIT02 4
#define BIT03 8
#define BIT04 16
#define BIT05 32
#define BIT06 64
#define BIT07 128
#define BIT08 256
#define BIT09 512
#define BIT10 1024
#define BIT11 2048
#define BIT12 4096
#define BIT13 8192
#define BIT14 16384
#define BIT15 32768

///////////////////Definitions////////////////////
#define REG_TM0D       *(volatile u16*)0x4000100      //Timer 1 Data
#define REG_TM0CNT     *(volatile u16*)0x4000102      //Timer 1 Control
#define REG_TM1D       *(volatile u16*)0x4000104      //Timer 2 Data
#define REG_TM1CNT     *(volatile u16*)0x4000106      //Timer 2 Control
#define REG_TM2D       *(volatile u16*)0x4000108      //Timer 3 Data
#define REG_TM2CNT     *(volatile u16*)0x400010A      //Timer 3 Control
#define REG_TM3D       *(volatile u16*)0x400010C      //Timer 4 Data
#define REG_TM3CNT     *(volatile u16*)0x400010E      //Timer 4 Control
#define FREQUENCY_0      0x00
#define FREQUENCY_64   BIT00
#define FREQUENCY_256   BIT01
#define FREQUENCY_1024   BIT00 | BIT01

#define TIMER_CASCADE   BIT02
#define TIMER_IRQ      BIT06
#define TIMER_ENABLE   BIT07

/********************************************  How to use
   
  1) when to enable it
     call S_FeTimerEnable()
  2) timer = S_FeTimerNew();
  3) set time stemp
     call S_FeTimerSetTimeStemp(..)
  4) check to see is over or not
     call FeTimerIsOver(a_timerPtr);
 
 ***********************************************************/
 
typedef struct _FeTimer
{
 //Time variables
 u32 m_secondStemp;
 u32 m_milliSecondStemp;
 u32 m_seconds;
 u32 m_milliSeconds;
 u16 m_formId;           
 bool m_switch;   // true : enable, false: disable
 u16 m_status;    // conditional flag using depends on the user
 //
} FeTimer, *FeTimerPtr;

/**
 * Create a new timer
 */
FeTimerPtr S_FeTimerNew();

/**
 * Enable the clock for all timers
 */
void S_FeTimerEnable();

/**
 * disable the clock for all timers
 */
void S_FeTimerDisable();

/**
 * Set the time stemp
 * @param a_timerPtr pointer to the timer
 * @param a_second number of seconds
 * @param a_milliSeconds number of milliSeconds
 */
void FeTimerSetTimeStemp(FeTimerPtr a_timerPtr, int a_second, int a_milliSeconds);

/**
 * Check to see the timer is over or not
 * @param a_timerPtr pointer to the timer
 * @return true: is over, otherwise is false
 */
bool FeTimerIsOver(FeTimerPtr a_timerPtr);

Code:

FeTimerPtr S_FeTimerNew()
{
   FeTimerPtr timerPtr;
   
   timerPtr = (FeTimerPtr) malloc( sizeof(FeTimer) );
   FE_ASSERT( timerPtr != NULL, "NULL");
   timerPtr->m_secondStemp = 0;
   timerPtr->m_milliSecondStemp = 0;
   timerPtr->m_seconds = 0;
   timerPtr->m_milliSeconds = 0;
   timerPtr->m_eventPtr = NULL;
   timerPtr->m_formId = 0xffff;
   timerPtr->m_switch = false;
   
   return timerPtr;
}

void S_FeTimerEnable()
{
   //Enable timers
   REG_TM3D   = 0; // reset time to 0
   REG_TM2D   = 0; // reset time to 0
   REG_TM2CNT = FREQUENCY_256 | TIMER_ENABLE;
   REG_TM3CNT = TIMER_CASCADE | TIMER_ENABLE;
}

void S_FeTimerDisable()
{
     REG_TM2CNT = 0;
   REG_TM3CNT = 0;
   REG_TM3D   = 0; // reset time to 0
   REG_TM2D   = 0; // reset time to 0
}

void FeTimerSetTimeStemp(FeTimerPtr a_timerPtr, int a_seconds, int a_milliSeconds)
{
     a_timerPtr->m_secondStemp      = REG_TM3D;
     a_timerPtr->m_milliSecondStemp = REG_TM2D / (65536/1000);
     a_timerPtr->m_switch = true;
     a_timerPtr->m_seconds = a_seconds;
     a_timerPtr->m_milliSeconds = a_milliSeconds;
}

bool FeTimerIsOver(FeTimerPtr a_timerPtr)
{
    u32 seconds;
    u32 milliSeconds;
    u32 reg_seconds;
    u32 reg_milliSeconds;
   
    if ( a_timerPtr->m_switch == true)
    {
       seconds       = a_timerPtr->m_seconds;
       // Total milliseconds
       milliSeconds  = a_timerPtr->m_milliSeconds + seconds*1000;
       // diff in second
      reg_seconds      = REG_TM3D - a_timerPtr->m_secondStemp;
      reg_milliSeconds = REG_TM2D / (65536/1000);
      reg_milliSeconds += reg_seconds*1000;
      // diff in millisecond
      reg_milliSeconds -= a_timerPtr->m_milliSecondStemp;
   
      if ( milliSeconds <= reg_milliSeconds )
      { 
         a_timerPtr->m_seconds = 0;
         a_timerPtr->m_milliSeconds = 0;
         a_timerPtr->m_switch = false;
         return true;
      }
    }
   
    return false;
}


bye now!
Gordon wong