Keyboard firmwares for Atmel AVR and Cortex-M
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.

преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <stdint.h>
  17. #include "timer.h"
  18. // counter resolution 1ms
  19. // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }}
  20. volatile uint32_t timer_count = 0;
  21. void timer_init(void)
  22. {
  23. // Timer0 CTC mode
  24. TCCR0A = 0x02;
  25. #if TIMER_PRESCALER == 1
  26. TCCR0B = 0x01;
  27. #elif TIMER_PRESCALER == 8
  28. TCCR0B = 0x02;
  29. #elif TIMER_PRESCALER == 64
  30. TCCR0B = 0x03;
  31. #elif TIMER_PRESCALER == 256
  32. TCCR0B = 0x04;
  33. #elif TIMER_PRESCALER == 1024
  34. TCCR0B = 0x05;
  35. #else
  36. # error "Timer prescaler value is NOT vaild."
  37. #endif
  38. OCR0A = TIMER_RAW_TOP;
  39. TIMSK0 = (1<<OCIE0A);
  40. }
  41. inline
  42. void timer_clear(void)
  43. {
  44. uint8_t sreg = SREG;
  45. cli();
  46. timer_count = 0;
  47. SREG = sreg;
  48. }
  49. inline
  50. uint16_t timer_read(void)
  51. {
  52. uint32_t t;
  53. uint8_t sreg = SREG;
  54. cli();
  55. t = timer_count;
  56. SREG = sreg;
  57. return (t & 0xFFFF);
  58. }
  59. inline
  60. uint32_t timer_read32(void)
  61. {
  62. uint32_t t;
  63. uint8_t sreg = SREG;
  64. cli();
  65. t = timer_count;
  66. SREG = sreg;
  67. return t;
  68. }
  69. inline
  70. uint16_t timer_elapsed(uint16_t last)
  71. {
  72. uint32_t t;
  73. uint8_t sreg = SREG;
  74. cli();
  75. t = timer_count;
  76. SREG = sreg;
  77. return TIMER_DIFF_16((t & 0xFFFF), last);
  78. }
  79. inline
  80. uint32_t timer_elapsed32(uint32_t last)
  81. {
  82. uint32_t t;
  83. uint8_t sreg = SREG;
  84. cli();
  85. t = timer_count;
  86. SREG = sreg;
  87. return TIMER_DIFF_32(t, last);
  88. }
  89. // excecuted once per 1ms.(excess for just timer count?)
  90. ISR(TIMER0_COMPA_vect)
  91. {
  92. timer_count++;
  93. }