timer02.c 3.6 KB

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