timer02.c 3.6 KB

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