Subversion Repositories HomeAutomation

Rev

Rev 97 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  Ticker service
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/branches/modules/EmbeddedSoftware/PIC/canToSerial/Source/Tick.c $
  7.  * Last changed:    $LastChangedDate: 2006-11-14 21:22:17 +0100 (Tue, 14 Nov 2006) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 97 $
  10.  ********************************************************************/
  11.  
  12. #define TICK_INCLUDE
  13.  
  14. #include <Tick.h>
  15. #include <Compiler.h>
  16.  
  17. #define TICK_TEMP_VALUE_1 ((INSTR_FREQ) / (TICKS_PER_SECOND * TICK_PRESCALE_VALUE))
  18.  
  19. #if TICK_TEMP_VALUE_1 > 60000
  20. #error TICK_PER_SECOND value cannot be programmed with current CLOCK_FREQ
  21. #error Either lower TICK_PER_SECOND or manually configure the Timer
  22. #endif
  23.  
  24. #define TICK_TEMP_VALUE         (65535 - TICK_TEMP_VALUE_1)
  25.  
  26. #define TICK_COUNTER_HIGH       ((TICK_TEMP_VALUE >> 8) & 0xff)
  27. #define TICK_COUNTER_LOW        (TICK_TEMP_VALUE & 0xff)
  28.  
  29. #if (TICK_PRESCALE_VALUE == 2)
  30.     #define TIMER_PRESCALE  (0)
  31. #elif ( TICK_PRESCALE_VALUE == 4 )
  32.     #define TIMER_PRESCALE  (1)
  33. #elif ( TICK_PRESCALE_VALUE == 8 )
  34.     #define TIMER_PRESCALE  (2)
  35. #elif ( TICK_PRESCALE_VALUE == 16 )
  36.     #define TIMER_PRESCALE  (3)
  37. #elif ( TICK_PRESCALE_VALUE == 32 )
  38.     #define TIMER_PRESCALE  (4)
  39. #elif ( TICK_PRESCALE_VALUE == 64 )
  40.     #define TIMER_PRESCALE  (5)
  41. #elif ( TICK_PRESCALE_VALUE == 128 )
  42.     #define TIMER_PRESCALE  (6)
  43. #elif ( TICK_PRESCALE_VALUE == 256 )
  44.     #define TIMER_PRESCALE  (7)
  45. #else
  46.     #error Invalid TICK_PRESCALE_VALUE specified.
  47. #endif
  48.  
  49.  
  50. TICK TickCount = 0; // 10ms/unit
  51.  
  52.  
  53.  
  54. /*
  55. *   Function: TickInit
  56. *
  57. *   Input:  none
  58. *   Output: Tick manager is initialized.
  59. *   Pre-conditions: TickInit
  60. *   Affects: none
  61. *   Depends: none
  62. */
  63. #define PERIOD (INSTR_FREQ/256/1000)*TICK_PERIOD_MS
  64. void tickInit(void)
  65. {
  66.     // Start the timer.
  67.     TMR0L = TICK_COUNTER_LOW;
  68.     TMR0H = TICK_COUNTER_HIGH;
  69.  
  70.     // 16-BIT, internal timer, PSA set to 1:256
  71.     T0CON = 0b00000000 | TIMER_PRESCALE;
  72.  
  73.     // Start the timer.
  74.     T0CONbits.TMR0ON = 1;
  75.  
  76.     INTCONbits.TMR0IF = 1;
  77.     INTCONbits.TMR0IE = 1;
  78. }
  79.  
  80. /*
  81. *   Function: TickGet
  82. *
  83. *   Input:  none
  84. *   Output: Current tick value is given, 1 tick represents approximately 10ms
  85. *   Pre-conditions: TickInit
  86. *   Affects: none
  87. *   Depends: none
  88. */
  89. TICK tickGet(void)
  90. {
  91.     return TickCount;
  92. }
  93.  
  94.  
  95. /*
  96. *   Function: tickUpdate
  97. *
  98. *   Input:  none
  99. *   Output: none
  100. *   Pre-conditions: TickInit
  101. *   Affects: none
  102. *   Depends: none
  103. */
  104. void tickUpdate(void)
  105. {
  106.     if(INTCONbits.TMR0IF)
  107.     {
  108.         TMR0H = TICK_COUNTER_HIGH;
  109.         TMR0L = TICK_COUNTER_LOW;
  110.  
  111.         TickCount++;
  112.  
  113.         INTCONbits.TMR0IF = 0;
  114.     }
  115. }
  116.