ustimer.h File Reference

micro-second(us) timer More...

#include <inttypes.h>
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>

Functions

void us_timer_init (void)
 initiates the timer
uint32_t us_time_get (void)
 get current time
uint32_t us_time_get_difference (uint32_t time1)
 calculates the difference between a saved and the current time
double us_time_get_difference_d (uint32_t time1)
 calculates the difference between a saved and the current time in micro-seconds


Detailed Description

micro-second(us) timer

Functions for precise time measurements. Tested with

No tests with 8bit-Timers so far, but it should work with minor changes.

Todo:
ustimer could become a project on its own.
29 March 2006
Sven Kreiss

Function Documentation

uint32_t us_time_get void   ) 
 

get current time

Calculates the current time from an incremented variable and the counter register of the timer.

00046 {
00047     uint16_t timer;
00048 
00049     /*  It is really, really important to stop global interrupts before
00050      *  reading 16bit registers. See the avr-libc FAQ! */
00051     cli();
00052     timer = TCNT3;
00053     sei();
00054     
00055     return ((us_time << 16) + timer);
00056 }

uint32_t us_time_get_difference uint32_t  time1  ) 
 

calculates the difference between a saved and the current time

Uses us_time_get() to get the current time. The if-condition at the end checks whether the later time is smaller. If so, then an timer overflow is assumed and the appropriate action is taken that the correct time can still be calculated. Therefore, the maximum time one can measure is 2^32 micro-seconds.

00068 {
00069     uint32_t time2;
00070     time2 = us_time_get();
00071     if(time2 >= time1) return (time2-time1);
00072     else return (0xFFFFFFFF - time1+time2); //2^32
00073 }

double us_time_get_difference_d uint32_t  time1  ) 
 

calculates the difference between a saved and the current time in micro-seconds

Same as us_time_get_difference(), but returns the value in micro seconds.

00079 {
00080     return ((double)(us_time_get_difference(time1) * US_TIMER_PRESCALER) / (double)XTAL);
00081 }

void us_timer_init void   ) 
 

initiates the timer

Initiates 16bit-Timer3 for the precise measurement. Enables Timer overflow interrupt.

< for XTAL > 8 MHz: 8, else: 1

< cpu-freq in MHz

00022 {
00023     #define US_TIMER_PRESCALER 8    ///< for XTAL > 8 MHz: 8, else: 1
00024     #define XTAL 11.0562            ///< cpu-freq in MHz
00025 
00026     TCCR3A = 0;                     //normal mode
00027     TCCR3B = (1<<CS31);             //CS31: Prescaler 8, CS00: Prescaler 1
00028     ETIMSK = (1<<TOIE3);            //enable Timer3 overflow interrupt
00029     //TIMSK = (1<<TOIE1);           //enable Timer1 overflow interrupt
00030     TCNT3 = 0;
00031 }


Generated on Fri Aug 11 21:47:33 2006 for OpenOsci by  doxygen 1.4.6