timer02.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //timer02.c
  2. // use atmega timer2 as main system timer instead of timer0
  3. // timer0 is used for fast pwm (OC0B output)
  4. // original OVF handler is disabled
  5. #ifdef SYSTEM_TIMER_2
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include "Arduino.h"
  9. #include "io_atmega2560.h"
  10. #define BEEPER 84
  11. uint8_t timer02_pwm0 = 0;
  12. void timer02_set_pwm0(uint8_t pwm0)
  13. {
  14. if (timer02_pwm0 == pwm0) return;
  15. if (pwm0)
  16. {
  17. TCCR0A |= (2 << COM0B0);
  18. OCR0B = pwm0 - 1;
  19. }
  20. else
  21. {
  22. TCCR0A &= ~(2 << COM0B0);
  23. OCR0B = 0;
  24. }
  25. timer02_pwm0 = pwm0;
  26. }
  27. void timer02_init(void)
  28. {
  29. //save sreg
  30. uint8_t _sreg = SREG;
  31. //disable interrupts for sure
  32. cli();
  33. //mask timer0 interrupts - disable all
  34. TIMSK0 &= ~(1<<TOIE0);
  35. TIMSK0 &= ~(1<<OCIE0A);
  36. TIMSK0 &= ~(1<<OCIE0B);
  37. //setup timer0
  38. TCCR0A = 0x00; //COM_A-B=00, WGM_0-1=00
  39. TCCR0B = (1 << CS00); //WGM_2=0, CS_0-2=011
  40. //switch timer0 to fast pwm mode
  41. TCCR0A |= (3 << WGM00); //WGM_0-1=11
  42. //set OCR0B register to zero
  43. OCR0B = 0;
  44. //disable OCR0B output (will be enabled in timer02_set_pwm0)
  45. TCCR0A &= ~(2 << COM0B0);
  46. //setup timer2
  47. TCCR2A = 0x00; //COM_A-B=00, WGM_0-1=00
  48. TCCR2B = (4 << CS20); //WGM_2=0, CS_0-2=011
  49. //mask timer2 interrupts - enable OVF, disable others
  50. TIMSK2 |= (1<<TOIE2);
  51. TIMSK2 &= ~(1<<OCIE2A);
  52. TIMSK2 &= ~(1<<OCIE2B);
  53. //set timer2 OCR registers (OCRB interrupt generated 0.5ms after OVF interrupt)
  54. OCR2A = 0;
  55. OCR2B = 128;
  56. //restore sreg (enable interrupts)
  57. SREG = _sreg;
  58. }
  59. //following code is OVF handler for timer 2
  60. //it is copy-paste from wiring.c and modified for timer2
  61. //variables timer0_overflow_count and timer0_millis are declared in wiring.c
  62. // the prescaler is set so that timer0 ticks every 64 clock cycles, and the
  63. // the overflow handler is called every 256 ticks.
  64. #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
  65. // the whole number of milliseconds per timer0 overflow
  66. #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
  67. // the fractional number of milliseconds per timer0 overflow. we shift right
  68. // by three to fit these numbers into a byte. (for the clock speeds we care
  69. // about - 8 and 16 MHz - this doesn't lose precision.)
  70. #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
  71. #define FRACT_MAX (1000 >> 3)
  72. //extern volatile unsigned long timer0_overflow_count;
  73. //extern volatile unsigned long timer0_millis;
  74. //unsigned char timer0_fract = 0;
  75. volatile unsigned long timer2_overflow_count;
  76. volatile unsigned long timer2_millis;
  77. unsigned char timer2_fract = 0;
  78. ISR(TIMER2_OVF_vect)
  79. {
  80. // copy these to local variables so they can be stored in registers
  81. // (volatile variables must be read from memory on every access)
  82. unsigned long m = timer2_millis;
  83. unsigned char f = timer2_fract;
  84. m += MILLIS_INC;
  85. f += FRACT_INC;
  86. if (f >= FRACT_MAX)
  87. {
  88. f -= FRACT_MAX;
  89. m += 1;
  90. }
  91. timer2_fract = f;
  92. timer2_millis = m;
  93. timer2_overflow_count++;
  94. }
  95. unsigned long millis2(void)
  96. {
  97. unsigned long m;
  98. uint8_t oldSREG = SREG;
  99. // disable interrupts while we read timer0_millis or we might get an
  100. // inconsistent value (e.g. in the middle of a write to timer0_millis)
  101. cli();
  102. m = timer2_millis;
  103. SREG = oldSREG;
  104. return m;
  105. }
  106. unsigned long micros2(void)
  107. {
  108. unsigned long m;
  109. uint8_t oldSREG = SREG, t;
  110. cli();
  111. m = timer2_overflow_count;
  112. #if defined(TCNT2)
  113. t = TCNT2;
  114. #elif defined(TCNT2L)
  115. t = TCNT2L;
  116. #else
  117. #error TIMER 2 not defined
  118. #endif
  119. #ifdef TIFR2
  120. if ((TIFR2 & _BV(TOV2)) && (t < 255))
  121. m++;
  122. #else
  123. if ((TIFR & _BV(TOV2)) && (t < 255))
  124. m++;
  125. #endif
  126. SREG = oldSREG;
  127. return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
  128. }
  129. void delay2(unsigned long ms)
  130. {
  131. uint32_t start = micros2();
  132. while (ms > 0)
  133. {
  134. yield();
  135. while ( ms > 0 && (micros2() - start) >= 1000)
  136. {
  137. ms--;
  138. start += 1000;
  139. }
  140. }
  141. }
  142. void tone2(__attribute__((unused)) uint8_t _pin, __attribute__((unused)) unsigned int frequency/*, unsigned long duration*/)
  143. {
  144. PIN_SET(BEEPER);
  145. }
  146. void noTone2(__attribute__((unused)) uint8_t _pin)
  147. {
  148. PIN_CLR(BEEPER);
  149. }
  150. #endif //SYSTEM_TIMER_2