timer02.c 3.4 KB

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