timer02.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "system_timer.h"
  6. #ifdef SYSTEM_TIMER_2
  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #include "macros.h"
  10. void timer0_init(void)
  11. {
  12. CRITICAL_SECTION_START;
  13. TCNT0 = 0;
  14. // Fast PWM duty (0-255).
  15. // Due to invert mode (following rows) the duty is set to 255, which means zero all the time (bed not heating)
  16. OCR0B = 255;
  17. // Set fast PWM mode and inverting mode.
  18. TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1) | (1 << COM0B0);
  19. TCCR0B = (1 << CS01); // CLK/8 prescaling
  20. TIMSK0 |= (1 << TOIE0); // enable timer overflow interrupt
  21. CRITICAL_SECTION_END;
  22. }
  23. void timer2_init(void)
  24. {
  25. CRITICAL_SECTION_START;
  26. // Everything, that used to be on timer0 was moved to timer2 (delay, beeping, millis etc.)
  27. //setup timer2
  28. TCCR2A = 0x00; //COM_A-B=00, WGM_0-1=00
  29. TCCR2B = (4 << CS20); //WGM_2=0, CS_0-2=011
  30. //mask timer2 interrupts - enable OVF, disable others
  31. TIMSK2 |= (1<<TOIE2);
  32. TIMSK2 &= ~(1<<OCIE2A);
  33. TIMSK2 &= ~(1<<OCIE2B);
  34. //set timer2 OCR registers (OCRB interrupt generated 0.5ms after OVF interrupt)
  35. OCR2A = 0;
  36. CRITICAL_SECTION_END;
  37. }
  38. // The following code is OVF handler for timer 2
  39. // it was copy-pasted from wiring.c and modified for timer2
  40. // variables timer0_overflow_count and timer0_millis are declared in wiring.c
  41. // the prescaler is set so that timer0 ticks every 64 clock cycles, and the
  42. // the overflow handler is called every 256 ticks.
  43. #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
  44. // the whole number of milliseconds per timer0 overflow
  45. #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
  46. // the fractional number of milliseconds per timer0 overflow. we shift right
  47. // by three to fit these numbers into a byte. (for the clock speeds we care
  48. // about - 8 and 16 MHz - this doesn't lose precision.)
  49. #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
  50. #define FRACT_MAX (1000 >> 3)
  51. volatile unsigned long timer2_overflow_count;
  52. volatile unsigned long timer2_millis;
  53. unsigned char timer2_fract = 0;
  54. ISR(TIMER2_OVF_vect)
  55. {
  56. // copy these to local variables so they can be stored in registers
  57. // (volatile variables must be read from memory on every access)
  58. unsigned long m = timer2_millis;
  59. unsigned char f = timer2_fract;
  60. m += MILLIS_INC;
  61. f += FRACT_INC;
  62. if (f >= FRACT_MAX)
  63. {
  64. f -= FRACT_MAX;
  65. m += 1;
  66. }
  67. timer2_fract = f;
  68. timer2_millis = m;
  69. timer2_overflow_count++;
  70. }
  71. unsigned long millis2(void)
  72. {
  73. unsigned long m;
  74. uint8_t oldSREG = SREG;
  75. // disable interrupts while we read timer0_millis or we might get an
  76. // inconsistent value (e.g. in the middle of a write to timer0_millis)
  77. cli();
  78. m = timer2_millis;
  79. SREG = oldSREG;
  80. return m;
  81. }
  82. unsigned long micros2(void)
  83. {
  84. unsigned long m;
  85. uint8_t oldSREG = SREG, t;
  86. cli();
  87. m = timer2_overflow_count;
  88. #if defined(TCNT2)
  89. t = TCNT2;
  90. #elif defined(TCNT2L)
  91. t = TCNT2L;
  92. #else
  93. #error TIMER 2 not defined
  94. #endif
  95. #ifdef TIFR2
  96. if ((TIFR2 & _BV(TOV2)) && (t < 255))
  97. m++;
  98. #else
  99. if ((TIFR & _BV(TOV2)) && (t < 255))
  100. m++;
  101. #endif
  102. SREG = oldSREG;
  103. return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
  104. }
  105. void delay2(unsigned long ms)
  106. {
  107. uint32_t start = micros2();
  108. while (ms > 0)
  109. {
  110. yield();
  111. while ( ms > 0 && (micros2() - start) >= 1000)
  112. {
  113. ms--;
  114. start += 1000;
  115. }
  116. }
  117. }
  118. #endif //SYSTEM_TIMER_2