Subversion Repositories HomeAutomation

Rev

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

  1. /******************************************************************************
  2.  * Copyright (C) 2005 Martin THOMAS, Kaiserslautern, Germany
  3.  * <eversmith@heizung-thomas.de>
  4.  * http://www.siwawi.arubi.uni-kl.de/avr_projects
  5.  *****************************************************************************
  6.  *
  7.  * File    : timebase.c
  8.  * Version : 0.9
  9.  *
  10.  * Summary : AVR Timebase using Hardware-Timer 0
  11.  *
  12.  *****************************************************************************/
  13.  
  14. #include <inttypes.h>
  15. #include <avr/io.h>
  16. #include <avr/interrupt.h>
  17.  
  18. #include "timebase.h"
  19.  
  20. ISR(SIG_OVERFLOW0)
  21. {
  22.     gMilliSecTick++;
  23.     TCNT0 = TIMEBASE_RELOAD;
  24. }
  25.  
  26. void timebase_init(void)
  27. {
  28.     TCCR0 = (1<<CS01) | (1<<CS00); // prescaler: 64
  29.     TCNT0 = TIMEBASE_RELOAD; // set initial reload-value
  30.     TIFR  |= (1<<TOV0);  // clear overflow int.
  31.     TIMSK |= (1<<TOIE0); // enable overflow-interrupt
  32. }
  33.  
  34. uint16_t timebase_actTime(void)
  35. {
  36.     uint8_t sreg;
  37.     uint16_t res;
  38.    
  39.     sreg=SREG;
  40.     cli();
  41.  
  42.     res = gMilliSecTick;
  43.    
  44.     SREG=sreg;
  45.    
  46.     return res;
  47. }
  48.    
  49. uint16_t timebase_passedTimeMS(uint16_t t0)
  50. {
  51.     uint8_t sreg;
  52.     uint16_t res;
  53.    
  54.     sreg=SREG;
  55.     cli();
  56.  
  57.     res = (uint16_t)(gMilliSecTick-t0);
  58.    
  59.     SREG=sreg;
  60.    
  61.     return res;
  62. }
  63.  
  64.  
  65.