Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.c 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Time-Keeping helper
  3. */
  4. //#define MILLISECONDS
  5. //#define MICROSECONDS
  6. //#define TEN_MICROSECONDS
  7. #define HUNDRED_MICROSECONDS
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include "timer.h"
  11. volatile time_t systemTime = 0;
  12. void timerInit(void) {
  13. #if defined(__AVR_ATmega328__)
  14. TCCR2A |= (1 << WGM21); // CTC Mode
  15. #if defined(MILLISECONDS)
  16. TCCR2B |= (1 << CS22); // Prescaler: 64
  17. OCR2A = 250; // Count to 250
  18. #elif defined(MICROSECONDS)
  19. TCCR2B |= (1 << CS20); // Prescaler: 1
  20. OCR2A = 16; // Count to 16
  21. #elif defined(TEN_MICROSECONDS)
  22. TCCR2B |= (1 << CS20); // Prescaler: 1
  23. OCR2A = 160; // Count to 160
  24. #elif defined(HUNDRED_MICROSECONDS)
  25. TCCR2B |= (1 << CS22); // Prescaler: 64
  26. OCR2A = 25; // Count to 25
  27. #endif
  28. TIMSK2 |= (1 << OCIE2A); // Enable compare match interrupt
  29. #elif defined __AVR_ATtiny85__
  30. // Using 8bit Timer1
  31. TCCR1 |= (1 << CTC1); // CTC Mode
  32. #if defined(MILLISECONDS)
  33. TCCR1 |= (1 << CS12) | (1 << CS11) | (1 << CS10); // Prescaler: 64
  34. OCR1A = 250; // Count to 250
  35. #elif defined(MICROSECONDS)
  36. TCCR1 |= (1 << CS10); // Prescaler: 1
  37. OCR1A = 16; // Count to 16
  38. #elif defined(TEN_MICROSECONDS)
  39. TCCR1 |= (1 << CS10); // Prescaler: 1
  40. OCR1A = 160; // Count to 160
  41. #elif defined(HUNDRED_MICROSECONDS)
  42. TCCR1 |= (1 << CS12) | (1 << CS11) | (1 << CS10); // Prescaler: 64
  43. OCR1A = 25; // Count to 25
  44. #endif
  45. TIMSK |= (1 << OCIE1A); // Enable compare match interrupt
  46. #endif
  47. }
  48. #if defined(__AVR_ATmega328__)
  49. ISR(TIMER2_COMPA_vect) {
  50. #elif defined __AVR_ATtiny85__
  51. ISR(TIMER1_COMPA_vect) {
  52. #endif
  53. #if defined(MILLISECONDS) || defined(MICROSECONDS)
  54. systemTime++;
  55. #elif defined(TEN_MICROSECONDS)
  56. systemTime += 10;
  57. #elif defined(HUNDRED_MICROSECONDS)
  58. systemTime += 100;
  59. #endif
  60. }
  61. time_t timerGet(void) {
  62. return systemTime;
  63. }