Measure Elapsed Time in C

C and PASCAL (or any other high-level languages) in here please

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Measure Elapsed Time in C

Postby DrCoolZic » Wed Jun 12, 2013 7:29 pm

1) I need to measure some elapsed time in a C (Pure C or AHCC) program is the following code correct

Code: Select all

#define _hz_200 *(volatile long *)0x4ba;
#define HZ_200_SEC

unsigned long start_time = _hz_200;
...
unsigned long elapsed_time = (_hz_200 - start_time) / HZ_200_SEC;  /* elapsed in second */

So here the resolution is 5ms per tick

2) For more precision I was thinking of building a real time clock using MFP timer.
Rather than reinvent code does anybody has some code already available directly in C

3) I understand that the first choice for timer is Timer A, but is it also possible to use Timer D (I do not use serial port) and if possible is it necessary to set back the Timer to initial condition

4) related to 2. Some code for building a watchdog using timer and interrupt

Code: Select all

set/arm watchdog value
while (waiting for something) {
  do something else
}
normal termination

if watchdog expired
abnormal termination


Thanks
Jean
Last edited by DrCoolZic on Thu Jun 13, 2013 3:27 pm, edited 1 time in total.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Re: Measure Elapsed Time in C

Postby DrCoolZic » Thu Jun 13, 2013 3:22 pm

Allo :megaphone:

Nobody to answer these easy questions in this wonderful community!

User avatar
Eero Tamminen
Atari God
Atari God
Posts: 1962
Joined: Sun Jul 31, 2011 1:11 pm

Re: Measure Elapsed Time in C

Postby Eero Tamminen » Thu Jun 13, 2013 8:33 pm

DrCoolZic wrote:1) I need to measure some elapsed time in a C (Pure C or AHCC) program is the following code correct

Code: Select all

#define _hz_200 *(volatile long *)0x4ba;
#define HZ_200_SEC

unsigned long start_time = _hz_200;
...
unsigned long elapsed_time = (_hz_200 - start_time) / HZ_200_SEC;  /* elapsed in second */

So here the resolution is 5ms per tick


You need to do that in supervisor mode.

Easiest would be to use the already existing clock() function for that.

If you get the AHCC's (GPL) sources, you can check the implementatation from in ahccstd/time.c.

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Re: Measure Elapsed Time in C

Postby DrCoolZic » Thu Jun 13, 2013 10:19 pm

Thanks I will look at the sources.
I now have _hz_200 method working fine (I was doing something stupid :oops: )

But I would still be interested by some C code on how to handle interrupt for Timer A to build a real time clock with high precision

User avatar
charles
10 GOTO 10
10 GOTO 10
Posts: 2530
Joined: Tue Aug 17, 2004 12:11 am
Location: ont. Canada
Contact:

Re: Measure Elapsed Time in C

Postby charles » Fri Jun 14, 2013 2:29 am

here is a multi speed , user set , timing source

you'll have to root around with the code to get it working on timer d
should work

charlie

Code: Select all

 The Pro MIDI Player
 Copyright 1986-1994 By J.D. Koftinoff Software, Ltd.
 Released under the GPL in 2007
 Please see the file 'COPYING' including in this package.
 
*/
 
/*   clocks.c
**
**   Handles all Xbtimer clock calls.
**  has the ability to set Xbtimer to whatever frequency
**  you want.
**
**   there is more accuracy for lower frequencies.
**
**   frequency range: 48 Hz to 614400 Hz.
**
**   call set_timer( long frequency ) to start the clocks
**   call unset_timer() to stop.
**
**   you can access and reset the clock by directly accessing
**      clock_ticks (long)
*/

#include <jk_sys.h>
#include <osbind.h>
#include <clocks.h>

#define SEND_INT_CLOCK 1

extern "C" void my_interrupt( void );


long clock_ticks;

#if SEND_INT_CLOCK
short clock_ticks_per_midi_clock=4;
short cur_midi_clock_tick_count=4;
#endif


asm( "
   
   MFP       =(0xfffffa00)
   S6850      =(0xfffffc00)
   MFP_CLOCK   =(2457600)


   Mgpip       =(MFP+1)
   Maer      =(MFP+3)
   Mddr      =(MFP+5)
   
   Miera      =(MFP+7)
   Mierb      =(MFP+9)
   Mipra      =(MFP+0xb)
   Miprb      =(MFP+0xd)
   Misra      =(MFP+0xf)
   Misrb      =(MFP+0x11)
   Mimra      =(MFP+0x13)
   Mimrb      =(MFP+0x15)
   Mvr      =(MFP+0x17)
   
   Mtacr      =(MFP+0x19)
   Mtbcr      =(MFP+0x1b)
   Mtcdcr       =(MFP+0x1d)
   Mtadr      =(MFP+0x1f)
   Mtbdr      =(MFP+0x21)
   Mtcdr      =(MFP+0x23)
   Mtddr      =(MFP+0x25)
   
   
   Mscr      =(MFP+0x27)
   Mucr      =(MFP+0x29)
   Mrsr      =(MFP+0x2b)
   Mtsr      =(MFP+0x2d)
   Mudr      =(MFP+0x2f)
   
   
   Skeyctl      =(S6850)
   Skeybd      =(S6850+2)
   Smidictl   =(S6850+4)
   Smidi      =(S6850+6)


   IBit0      =0x100
   IBit1      =0x104
   IBit2      =0x108
   IBit3      =0x10c
   ITimerD      =0x110
   ITimerC      =0x114
   IBit4      =0x118
   IBit5      =0x11c
   ITimerB      =0x120
   ITxERR      =0x124
   ITxDATA      =0x128
   IRxERR      =0x12c
   IRxDATA      =0x130
   ITimerA      =0x134
   IBit6      =0x138
   IBit7      =0x13c
" );

#if SEND_INT_CLOCK
asm ("
_my_interrupt:
      movl   d0,sp@-
      addql   #1,_clock_ticks
      subqw   #1,_cur_midi_clock_tick_count

      jne   _my_interrupt_end
     
      movw   _clock_ticks_per_midi_clock,_cur_midi_clock_tick_count

_my_interrupt_loop:
      movb   Smidictl,d0
      btst   #1,d0
      jeq   _my_interrupt_loop
     
      movb   #0xf8,Smidi

_my_interrupt_end:   
      movl   sp@+,d0
      bclr   #5,0xfffa0f
         /* Tell MFP the interrupt has been serviced   */
      rte
");
#else
asm ("
_my_interrupt:
      addql   #1,_clock_ticks

      bclr   #5,0xfffa0f
         /* Tell MFP the interrupt has been serviced   */
      rte
");
#endif



void set_timer( long freq)
{
int cntrl, count;
static int prescales[]= {
   0, 4, 10, 16, 50, 64, 100, 200
};

   cntrl=0;
   
   if( freq<=614400 && freq>=2400 ) {
      cntrl=1;   /* divide by 4     */
      goto done;
   }
   if( freq<2400 && freq>=960 ) {
      cntrl=2;      /* divide by 10    */
      goto done;
   }
   if( freq<960  && freq>=600 ) {
      cntrl=3;      /* divide by 16    */
      goto done;
   }
   if( freq<600  && freq>=192 ) {
      cntrl=4;      /* divide by 50    */
      goto done;
   }
   if( freq<192  && freq>=150 ) {
      cntrl=5;      /* divide by 64    */
      goto done;
   }
   if( freq<150  && freq>=96  ) {
      cntrl=6;      /* divide by 100   */
      goto done;
   }
     
   if( freq<96   && freq>=48  ) {
      cntrl=7;       /* divide by 200   */
      goto done;
   }
   
   if( cntrl==0 ) {
      return;
   }
   
done:
   /* entire range is not checked above; this is because if we
   ** use (for example) divide by 50 instead of divide by 100
   ** for the same frequency, our calculated count value will be
   ** larger, giving us a more accurate clock
   */
   
   count=2457600/(prescales[cntrl]*freq);

   Xbtimer(0, cntrl, count, my_interrupt );
}


void unset_timer()
{
   Xbtimer(0, 0, 0, NULL);
}

static short midi_byte;

static void _clock_midi_out()
{
   asm( "
      movw   sr,sp@-
      orw   #0x700,sr
_amy_loop:
      movb   Smidictl,d0
      btst   #1,d0
      jeq   _amy_loop
     
      movw   _midi_byte,d0
      movb   d0,Smidi
      movw   sp@+,sr
   " );
}

void clock_midi_out( short a )
{
   midi_byte=a;
   Supexec( _clock_midi_out );
}


 
The radioactive half-life : )
Atari is a lifestyle,not a hobby.
HOLD ON ! ! ! Im printing unreadable characters ...!

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Re: Measure Elapsed Time in C

Postby DrCoolZic » Sat Jun 15, 2013 8:50 am

Thanks Charles

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: Measure Elapsed Time in C

Postby Nyh » Tue Jun 18, 2013 7:37 am

DrCoolZic wrote:1) I need to measure some elapsed time in a C (Pure C or AHCC) program is the following code correct

What is wrong with this code?
http://www.atari-forum.com/viewtopic.php?f=70&t=18212&p=157525&hilit=0xfffa0fL#p157525

Hans Wessels

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Re: Measure Elapsed Time in C

Postby DrCoolZic » Tue Jun 18, 2013 8:11 am

Nyh wrote:
DrCoolZic wrote:1) I need to measure some elapsed time in a C (Pure C or AHCC) program is the following code correct

What is wrong with this code?
http://www.atari-forum.com/viewtopic.php?f=70&t=18212&p=157525&hilit=0xfffa0fL#p157525

Hans Wessels

Many thanks Nyh. I knew it was somewhere but I was not able to find the threads.
Two side questions:
- Can I use timer D withut "danger" (I am not using the RS232) as I am already using timer A ?
- !is AHCC "compatible with the dark magic" ?

User avatar
Nyh
Atari God
Atari God
Posts: 1496
Joined: Tue Oct 12, 2004 2:25 pm
Location: Netherlands

Re: Measure Elapsed Time in C

Postby Nyh » Tue Jun 18, 2013 8:35 am

DrCoolZic wrote:Many thanks Nyh. I knew it was somewhere but I was not able to find the threads.
Two side questions:
- Can I use timer D withut "danger" (I am not using the RS232) as I am already using timer A ?
- !is AHCC "compatible with the dark magic" ?

Yes, you can also use timer D.
I don't know if AHCC is dark magic compatible.

Hans Wessels

User avatar
DrCoolZic
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2188
Joined: Mon Oct 03, 2005 7:03 pm
Location: France
Contact:

Re: Measure Elapsed Time in C

Postby DrCoolZic » Tue Jun 18, 2013 9:39 am

Thanks I will try


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 6 guests