BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 7:55 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: General Purpose Timers
PostPosted: Sun May 15, 2011 8:44 am 
Offline
Citizen

Joined: Fri May 06, 2011 8:19 am
Posts: 11
I'm a newbie to Mbasic and the Atom Pro...

Short of setting up some interrupt scheme, I can't find any built-in function to provide general purpose program timing. There are all sorts of ways to suspend or wait for a period of time, but I can't see how to count elapsed time. As an example, I'm trying to do something like the following code (I've made up the command "Timer1"):
------------------------------------------------------------------
irDirection var word
Timer1 Reset ;initialize Timer1

Main
Do
; go to a subroutine and get some sensor value named irDirection from an i2c sensor
While irDirection < 90 or Timer1 < 5000 ;exit loop if irDirection is less than 90 or if the loop has executed for greater than 5000ms

goto Main
------------------------------------------------------------------

What would be the best approach to accomplish this type of code?
Do I need to tie-in an external clock chip which I'd have to read?


Top
 Profile  
 
 Post subject: Re: General Purpose Timers
PostPosted: Sun May 15, 2011 9:12 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
Which Atom Pro are you using?

If it is the Arc32, you can initialize and use the HServoTime function. It is described in the data sheet for the Arc32.

If you are using a Basic Atom Pro 24/28 you can setup and use TimerA or TimerV and if you are not using HSERVO or the like TimerW. If you are on a BAP40/BAP64 /(Arc32 and don't wish to use HSERVO), you can setup and use TimerB1, or TimverV or TimerZ.

Example using TimerA (Code extracted from Lynxmotion Phoenix code... :
Code:
;====================================================================
lTimerCnt         var long   

;--------------------------------------------------------------------
;[TIMER INTERRUPT INIT]
TIMERINT   con   TIMERAINT
ONASMINTERRUPT TIMERAINT, HANDLE_TIMERA_ASM

;--------------------------------------------------------------------
;[InitServoDriver] - Initializes the servo driver including the main timer used in the phoenix code
InitTimer:
;Timer
; Timer A init, used for timing of messages and some times for timing code...
  TIMERINT   con   TIMERAINT

  WTIMERTICSPERMSMUL con 64   ; BAP28 is 16mhz need a multiplier and divider to make the conversion with /8192
  WTIMERTICSPERMSDIV con 125  ;
  TMA = 0   ; clock / 8192               ; Low resolution clock - used for timeouts...
  ENABLE TIMERINT
return

;==============================================================================
;[Handle_Timer_asm] - Handle timer A overflow in assembly language.  Currently only
;used for timings for debugging the speed of the code
;Now used to time how long since we received a message from the remote.
;this is important when we are in the NEW message mode, as we could be hung
;out with the robot walking and no new commands coming in.
;==============================================================================
BEGINASMSUB
HANDLE_TIMERA_ASM
   push.l    er1                  ; first save away ER1 as we will mess with it.
   bclr    #6,@IRR1:8               ; clear the corresponding bit in the interrupt pending mask
   mov.l    @LTIMERCNT:16,er1      ; Add 256 to our counter
   add.l   #256,er1
   mov.l    er1, @LTIMERCNT:16
   pop.l    er1
   rte
ENDASMSUB

return      ; Put a basic statement before...
;==============================================================================
;[GetCurrentTime] - Gets the Timer value from our overflow counter as well as the TCA counter.  It
;                makes sure of consistency. That is it is very possible that
;                after we grabbed the timers value it overflows, before we grab the other part
;                so we check to make sure it is correct and if necessary regrab things.
;==============================================================================
GetCurrentTime:
  lCurrentTime = lTimerCnt + TCA
   
  ; handle wrap
  if lTimerCnt <> (lCurrentTime & 0xffffff00) then
   lCurrentTime = lTimerCnt + TCA
  endif

return lCurrentTime
;-------------------------------------------------------------------------------------
;[ConvertTimeMS]
_ttconv   var   long
ConvertTimeMS[_ttconv]:
   return (_ttconv * WTIMERTICSPERMSMUL)/WTIMERTICSPERMSDIV


The code for the Bap40 using TimerB1 is almost identical. Except the names of the registers and interrupt, and the conversion factor to milliseconds as the Bap28 runs at 16mhz and the Bap40/Arc32 run at 20mhz.

Hope that helps
Kurt


Top
 Profile  
 
 Post subject: Re: General Purpose Timers
PostPosted: Sun May 15, 2011 11:41 am 
Offline
Citizen

Joined: Fri May 06, 2011 8:19 am
Posts: 11
Many thanks. I'll try it tonight.
I'm using the bap40.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO