Subversion Repositories HomeAutomation

Rev

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

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