Subversion Repositories HomeAutomation

Rev

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

  1. #ifndef TIMER_CFG_H_
  2. #define TIMER_CFG_H_
  3.  
  4. #include <config.h>
  5.  
  6. //#define TIMER_DEBUG_CONFIG
  7.  
  8. /*-----------------------------------------------------------------------------
  9.  * Configuration
  10.  *---------------------------------------------------------------------------*/
  11. #ifndef TIMER_NUM_TIMERS
  12. #   error TIMER_NUM_TIMERS is not defined! Edit config.inc.
  13. #endif
  14.  
  15. #ifndef TIMER_USE_TIMER
  16. #   error TIMER_USE_TIMER is not defined! Edit config.inc.
  17. #endif
  18.  
  19. #ifndef TIMER_TICKFREQ
  20. #   warning Using default TIMER_TICKFREQ value of 1000.
  21. #   define TIMER_TICKFREQ 1000
  22. #endif
  23.  
  24. #if defined(__AVR_ATmega8__)
  25. #define TIMER0_MAX                  255
  26. #define TIMER0_VECTOR               TIMER0_OVF_vect
  27. #define TIMER0_RELOAD(_t_)          TCNT0 = ( 256 - _t_ );
  28. #define TIMER0_INIT()               TIFR=(1<<TOV0);TIMSK|=(1<<TOIE0);
  29. #define TIMER0_SET_PRESCALER_1()    TCCR0=1;
  30. #define TIMER0_SET_PRESCALER_8()    TCCR0=2;
  31. #define TIMER0_SET_PRESCALER_64()   TCCR0=3;
  32. #define TIMER0_SET_PRESCALER_256()  TCCR0=4;
  33. #define TIMER0_SET_PRESCALER_1024() TCCR0=5;
  34. #define TIMER1_MAX                  65535
  35. #define TIMER1_VECTOR               TIMER1_COMPA_vect
  36. #define TIMER1_COMPARE_REG          OCR1A
  37. #define TIMER1_INIT()               TCCR1A=0;TIFR=(1<<OCF1A);TIMSK|=(1<<OCIE1A);
  38. #define TIMER1_SET_PRESCALER_1()    TCCR1B=(1<<WGM12)|1;
  39. #define TIMER1_SET_PRESCALER_8()    TCCR1B=(1<<WGM12)|2;
  40. #define TIMER1_SET_PRESCALER_64()   TCCR1B=(1<<WGM12)|3;
  41. #define TIMER1_SET_PRESCALER_256()  TCCR1B=(1<<WGM12)|4;
  42. #define TIMER1_SET_PRESCALER_1024() TCCR1B=(1<<WGM12)|5;
  43. #define TIMER2_MAX                  255
  44. #define TIMER2_VECTOR               TIMER2_COMP_vect
  45. #define TIMER2_COMPARE_REG          OCR2
  46. #define TIMER2_INIT()               TIFR=(1<<OCF2);TIMSK|=(1<<OCIE2);
  47. #define TIMER2_SET_PRESCALER_1()    TCCR2=(1<<WGM21)|1;
  48. #define TIMER2_SET_PRESCALER_8()    TCCR2=(1<<WGM21)|2;
  49. #define TIMER2_SET_PRESCALER_64()   TCCR2=(1<<WGM21)|4;
  50. #define TIMER2_SET_PRESCALER_256()  TCCR2=(1<<WGM21)|6;
  51. #define TIMER2_SET_PRESCALER_1024() TCCR2=(1<<WGM21)|7;
  52.  
  53. #elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
  54. #define TIMER0_MAX                  255
  55. #define TIMER0_VECTOR               TIMER0_COMPA_vect
  56. #define TIMER0_COMPARE_REG          OCR0A
  57. #define TIMER0_INIT()               TCCR0A=(1<<WGM01);TIFR0=(1<<OCF0A);TIMSK0|=(1<<OCIE0A);
  58. #define TIMER0_SET_PRESCALER_1()    TCCR0B=1;
  59. #define TIMER0_SET_PRESCALER_8()    TCCR0B=2;
  60. #define TIMER0_SET_PRESCALER_64()   TCCR0B=3;
  61. #define TIMER0_SET_PRESCALER_256()  TCCR0B=4;
  62. #define TIMER0_SET_PRESCALER_1024() TCCR0B=5;
  63. #define TIMER1_MAX                  65535
  64. #define TIMER1_VECTOR               TIMER1_COMPA_vect
  65. #define TIMER1_COMPARE_REG          OCR1A
  66. #define TIMER1_INIT()               TCCR1A=0;TIFR1=(1<<OCF1A);TIMSK1|=(1<<OCIE1A);
  67. #define TIMER1_SET_PRESCALER_1()    TCCR1B=(1<<WGM12)|1;
  68. #define TIMER1_SET_PRESCALER_8()    TCCR1B=(1<<WGM12)|2;
  69. #define TIMER1_SET_PRESCALER_64()   TCCR1B=(1<<WGM12)|3;
  70. #define TIMER1_SET_PRESCALER_256()  TCCR1B=(1<<WGM12)|4;
  71. #define TIMER1_SET_PRESCALER_1024() TCCR1B=(1<<WGM12)|5;
  72. #define TIMER2_MAX                  255
  73. #define TIMER2_VECTOR               TIMER2_COMPA_vect
  74. #define TIMER2_COMPARE_REG          OCR2A
  75. #define TIMER2_INIT()               TCCR2A=(1<<WGM21);TIFR2=(1<<OCF2A);TIMSK2|=(1<<OCIE2A);
  76. #define TIMER2_SET_PRESCALER_1()    TCCR2B=1;
  77. #define TIMER2_SET_PRESCALER_8()    TCCR2B=2;
  78. #define TIMER2_SET_PRESCALER_64()   TCCR2B=4;
  79. #define TIMER2_SET_PRESCALER_256()  TCCR2B=6;
  80. #define TIMER2_SET_PRESCALER_1024() TCCR2B=7;
  81.  
  82. #else
  83. #   error AVR not supported!
  84. #endif
  85.  
  86. #if TIMER_USE_TIMER == 0
  87. #define TIMER_MAX                   TIMER0_MAX
  88. #define TIMER_VECTOR                TIMER0_VECTOR
  89. #if defined(TIMER0_RELOAD)
  90. #   define TIMER_RELOAD             TIMER0_RELOAD
  91. #endif
  92. #if defined(TIMER0_COMPARE_REG)
  93. #   define TIMER_COMPARE_REG            TIMER0_COMPARE_REG
  94. #endif
  95. #define TIMER_INIT                  TIMER0_INIT
  96. #define TIMER_SET_PRESCALER_1       TIMER0_SET_PRESCALER_1
  97. #define TIMER_SET_PRESCALER_8       TIMER0_SET_PRESCALER_8
  98. #define TIMER_SET_PRESCALER_64      TIMER0_SET_PRESCALER_64
  99. #define TIMER_SET_PRESCALER_256     TIMER0_SET_PRESCALER_256
  100. #define TIMER_SET_PRESCALER_1024    TIMER0_SET_PRESCALER_1024
  101. #if defined(TIMER_DEBUG_CONFIG)
  102. #   warning Using Timer 0.
  103. #endif
  104.  
  105. #elif TIMER_USE_TIMER == 1
  106. #define TIMER_MAX                   TIMER1_MAX
  107. #define TIMER_VECTOR                TIMER1_VECTOR
  108. #if defined(TIMER1_RELOAD)
  109. #   define TIMER_RELOAD             TIMER1_RELOAD
  110. #endif
  111. #if defined(TIMER1_COMPARE_REG)
  112. #   define TIMER_COMPARE_REG            TIMER1_COMPARE_REG
  113. #endif
  114. #define TIMER_INIT                  TIMER1_INIT
  115. #define TIMER_SET_PRESCALER_1       TIMER1_SET_PRESCALER_1
  116. #define TIMER_SET_PRESCALER_8       TIMER1_SET_PRESCALER_8
  117. #define TIMER_SET_PRESCALER_64      TIMER1_SET_PRESCALER_64
  118. #define TIMER_SET_PRESCALER_256     TIMER1_SET_PRESCALER_256
  119. #define TIMER_SET_PRESCALER_1024    TIMER1_SET_PRESCALER_1024
  120. #if defined(TIMER_DEBUG_CONFIG)
  121. #   warning Using Timer 1.
  122. #endif
  123.  
  124. #elif TIMER_USE_TIMER == 2
  125. #define TIMER_MAX                   TIMER2_MAX
  126. #define TIMER_VECTOR                TIMER2_VECTOR
  127. #if defined(TIMER2_RELOAD)
  128. #   define TIMER_RELOAD             TIMER2_RELOAD
  129. #endif
  130. #if defined(TIMER2_COMPARE_REG)
  131. #   define TIMER_COMPARE_REG            TIMER2_COMPARE_REG
  132. #endif
  133. #define TIMER_INIT                  TIMER2_INIT
  134. #define TIMER_SET_PRESCALER_1       TIMER2_SET_PRESCALER_1
  135. #define TIMER_SET_PRESCALER_8       TIMER2_SET_PRESCALER_8
  136. #define TIMER_SET_PRESCALER_64      TIMER2_SET_PRESCALER_64
  137. #define TIMER_SET_PRESCALER_256     TIMER2_SET_PRESCALER_256
  138. #define TIMER_SET_PRESCALER_1024    TIMER2_SET_PRESCALER_1024
  139. #if defined(TIMER_DEBUG_CONFIG)
  140. #   warning Using Timer 2.
  141. #endif
  142.  
  143. #else
  144. #   error Invalid timer selected!
  145. #endif
  146.  
  147.  
  148. /* Find a suitable prescaler setting. Prescalers 1, 8, 64, 256 and 1024 are
  149.  * available in all timers, so consider only these for simplicity. */
  150. #define TIMER_PRESCALE 1
  151. #define TIMER_COUNTS_PER_TICK (F_CPU/TIMER_PRESCALE/TIMER_TICKFREQ-1)
  152. #if defined(TIMER_DEBUG_CONFIG)
  153. #   warning Trying prescaler 1...
  154. #endif
  155.  
  156. #if TIMER_COUNTS_PER_TICK > TIMER_MAX
  157. #undef TIMER_PRESCALE
  158. #undef TIMER_COUNTS_PER_TICK
  159. #define TIMER_PRESCALE 8
  160. #define TIMER_COUNTS_PER_TICK (F_CPU/TIMER_PRESCALE/TIMER_TICKFREQ-1)
  161. #if defined(TIMER_DEBUG_CONFIG)
  162. #   warning Trying prescaler 8...
  163. #endif
  164. #endif
  165.  
  166. #if TIMER_COUNTS_PER_TICK > TIMER_MAX
  167. #undef TIMER_PRESCALE
  168. #undef TIMER_COUNTS_PER_TICK
  169. #define TIMER_PRESCALE 64
  170. #define TIMER_COUNTS_PER_TICK (F_CPU/TIMER_PRESCALE/TIMER_TICKFREQ-1)
  171. #if defined(TIMER_DEBUG_CONFIG)
  172. #   warning Trying prescaler 64...
  173. #endif
  174. #endif
  175.  
  176. #if TIMER_COUNTS_PER_TICK > TIMER_MAX
  177. #undef TIMER_PRESCALE
  178. #undef TIMER_COUNTS_PER_TICK
  179. #define TIMER_PRESCALE 256
  180. #define TIMER_COUNTS_PER_TICK (F_CPU/TIMER_PRESCALE/TIMER_TICKFREQ-1)
  181. #if defined(TIMER_DEBUG_CONFIG)
  182. #   warning Trying prescaler 256...
  183. #endif
  184. #endif
  185.  
  186. #if TIMER_COUNTS_PER_TICK > TIMER_MAX
  187. #undef TIMER_PRESCALE
  188. #undef TIMER_COUNTS_PER_TICK
  189. #define TIMER_PRESCALE 1024
  190. #define TIMER_COUNTS_PER_TICK (F_CPU/TIMER_PRESCALE/TIMER_TICKFREQ-1)
  191. #if defined(TIMER_DEBUG_CONFIG)
  192. #   warning Trying prescaler 1024...
  193. #endif
  194. #endif
  195.  
  196. #if TIMER_COUNTS_PER_TICK > TIMER_MAX
  197. #   error Invalid frequency selection!
  198. #endif
  199.  
  200. #if defined(TIMER_DEBUG_CONFIG)
  201. #   warning Prescaler ok!
  202. #endif
  203.  
  204. #if TIMER_PRESCALE == 1
  205. #   define TIMER_SET_PRESCALER  TIMER_SET_PRESCALER_1
  206. #   if defined(TIMER_DEBUG_CONFIG)
  207. #       warning Using prescaler 1.
  208. #   endif
  209. #elif TIMER_PRESCALE == 8
  210. #   define TIMER_SET_PRESCALER  TIMER_SET_PRESCALER_8
  211. #   if defined(TIMER_DEBUG_CONFIG)
  212. #       warning Using prescaler 8.
  213. #   endif
  214. #elif TIMER_PRESCALE == 64
  215. #   define TIMER_SET_PRESCALER  TIMER_SET_PRESCALER_64
  216. #   if defined(TIMER_DEBUG_CONFIG)
  217. #       warning Using prescaler 64.
  218. #   endif
  219. #elif TIMER_PRESCALE == 256
  220. #   define TIMER_SET_PRESCALER  TIMER_SET_PRESCALER_256
  221. #   if defined(TIMER_DEBUG_CONFIG)
  222. #       warning Using prescaler 256.
  223. #   endif
  224. #elif TIMER_PRESCALE == 1024
  225. #   define TIMER_SET_PRESCALER  TIMER_SET_PRESCALER_1024
  226. #   if defined(TIMER_DEBUG_CONFIG)
  227. #       warning Using prescaler 1024.
  228. #   endif
  229. #endif
  230.  
  231. #endif /*TIMER_CFG_H_*/
  232.