Subversion Repositories HomeAutomation

Rev

Rev 150 | Go to most recent revision | 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.     #if TIMEBASE_PRESCALE = 64
  48.     TCCR0 = (1<<CS01) | (1<<CS00);  // prescaler: 64
  49.     #elif TIMEBASE_PRESCALE = 256
  50.     TCCR0 = (1<<CS02);              // prescaler: 256
  51.     #endif
  52.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  53.     TIFR  |= (1<<TOV0);  // clear overflow int.
  54.     TIMSK |= (1<<TOIE0); // enable overflow-interrupt
  55.     #endif
  56.    
  57.     #if defined(__AVR_ATmega88__)
  58.     #if TIMEBASE_PRESCALE == 64
  59.     TCCR0B = (1<<CS01) | (1<<CS00); // prescaler: 64
  60.     #elif TIMEBASE_PRESCALE == 256
  61.     TCCR0B = (1<<CS02);             // prescaler: 256
  62.     #endif
  63.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  64.     TIFR0  |= (1<<TOV0);  // clear overflow int.
  65.     TIMSK0 |= (1<<TOIE0); // enable overflow-interrupt
  66.     #endif
  67. }
  68.  
  69.  
  70. /**
  71.  * Reads the current time.
  72.  *
  73.  * @return
  74.  *      The current time in milliseconds (32bit value).
  75.  */
  76. uint32_t Timebase_CurrentTime(void) {
  77.     uint8_t sreg;
  78.     uint16_t res;
  79.     sreg=SREG;
  80.     cli();
  81.     res = gMilliSecTick;
  82.     SREG=sreg;
  83.     return res;
  84. }
  85.  
  86.  
  87. /**
  88.  * Checks how much time has passed since a given timestamp.
  89.  *
  90.  * @param t0
  91.  *      The timestamp.
  92.  * @return
  93.  *      Number of milliseconds that have passed since t0.
  94.  */
  95. uint32_t Timebase_PassedTimeMillis(uint32_t t0) {
  96.     uint8_t sreg;
  97.     uint16_t res;
  98.     sreg=SREG;
  99.     cli();
  100.     res = (uint16_t)(gMilliSecTick-t0);
  101.     SREG=sreg;
  102.     return res;
  103. }
  104.