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.

timer.c 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <stdint.h>
  4. #include "timer.h"
  5. volatile uint16_t timer_count = 0;
  6. // Configure timer 0 to generate a timer overflow interrupt every
  7. // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
  8. // This demonstrates how to use interrupts to implement a simple
  9. // inactivity timeout.
  10. void timer_init(void)
  11. {
  12. TCCR0A = 0x00;
  13. TCCR0B = 0x05;
  14. TIMSK0 = (1<<TOIE0);
  15. }
  16. inline
  17. void timer_clear(void)
  18. {
  19. uint8_t sreg = SREG;
  20. cli();
  21. timer_count = 0;
  22. SREG = sreg;
  23. }
  24. inline
  25. uint16_t timer_read(void)
  26. {
  27. uint16_t t;
  28. uint8_t sreg = SREG;
  29. cli();
  30. t = timer_count;
  31. SREG = sreg;
  32. return t;
  33. }
  34. inline
  35. uint16_t timer_elapsed(uint16_t last)
  36. {
  37. uint16_t t;
  38. uint8_t sreg = SREG;
  39. cli();
  40. t = timer_count;
  41. SREG = sreg;
  42. return TIMER_DIFF(t, last);
  43. }
  44. // This interrupt routine is run approx 61 times per second.
  45. // A very simple inactivity timeout is implemented, where we
  46. // will send a space character and print a message to the
  47. // hid_listen debug message window.
  48. ISR(TIMER0_OVF_vect)
  49. {
  50. timer_count++;
  51. }