timer02.c 3.8 KB

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