Subversion Repositories HomeAutomation

Rev

Rev 149 | Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * Timebase software module. Utilizes timer0 to provide a 32bit timebase with
  3.  * millisecond-granularity to the application.
  4.  *
  5.  * @author  Martin Thomas
  6.  * @author  Jimmy Myhrman
  7.  *
  8.  * @date    2006-11-19
  9.  */
  10.  
  11. /*-----------------------------------------------------------------------------
  12.  * Includes
  13.  *---------------------------------------------------------------------------*/
  14. #include <inttypes.h>
  15. #include <avr/io.h>
  16. #include <avr/interrupt.h>
  17.  
  18. #include <timebase.h>
  19.  
  20.  
  21. /*-----------------------------------------------------------------------------
  22.  * Globals
  23.  *---------------------------------------------------------------------------*/
  24. volatile uint32_t gMilliSecTick;
  25.  
  26.  
  27. /*-----------------------------------------------------------------------------
  28.  * Interrupt Service Routines
  29.  *---------------------------------------------------------------------------*/
  30. ISR(SIG_OVERFLOW0) {
  31.     TCNT0 = TIMEBASE_RELOAD;
  32.     gMilliSecTick++;
  33. }
  34.  
  35.  
  36. /*-----------------------------------------------------------------------------
  37.  * Public Functions
  38.  *---------------------------------------------------------------------------*/
  39.  
  40. /**
  41.  * Initializes the timebase. The hardware timer interrupt source will be
  42.  * enabled, but global interrupts need to be enabled by the application.
  43.  */
  44. void Timebase_Init() {
  45.    
  46.     #if defined(__AVR_ATmega8__)
  47.     TCCR0 = (1<<CS01) | (1<<CS00); // prescaler: 64
  48.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  49.     TIFR  |= (1<<TOV0);  // clear overflow int.
  50.     TIMSK |= (1<<TOIE0); // enable overflow-interrupt
  51.     #endif
  52.    
  53.     #if defined(__AVR_ATmega88__)
  54.     TCCR0B = (1<<CS01) | (1<<CS00); // prescaler: 64
  55.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  56.     TIFR0  |= (1<<TOV0);  // clear overflow int.
  57.     TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt
  58.     #endif
  59. }
  60.  
  61.  
  62. /**
  63.  * Reads the current time.
  64.  *
  65.  * @return
  66.  *      The current time in milliseconds (32bit value).
  67.  */
  68. uint32_t Timebase_CurrentTime(void) {
  69.     uint8_t sreg;
  70.     uint16_t res;
  71.     sreg=SREG;
  72.     cli();
  73.     res = gMilliSecTick;
  74.     SREG=sreg;
  75.     return res;
  76. }
  77.  
  78.  
  79. /**
  80.  * Checks how much time has passed since a given timestamp.
  81.  *
  82.  * @param t0
  83.  *      The timestamp.
  84.  * @return
  85.  *      Number of milliseconds that have passed since t0.
  86.  */
  87. uint32_t Timebase_PassedTimeMillis(uint32_t t0) {
  88.     uint8_t sreg;
  89.     uint16_t res;
  90.     sreg=SREG;
  91.     cli();
  92.     res = (uint16_t)(gMilliSecTick-t0);
  93.     SREG=sreg;
  94.     return res;
  95. }
  96.