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. 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. cli();
  20. timer_count = 0;
  21. sei();
  22. }
  23. inline
  24. uint16_t timer_read(void)
  25. {
  26. uint8_t _sreg = SREG;
  27. uint16_t t;
  28. cli();
  29. t = timer_count;
  30. SREG = _sreg;
  31. return t;
  32. }
  33. inline
  34. uint16_t timer_elapsed(uint16_t last)
  35. {
  36. uint8_t _sreg = SREG;
  37. uint16_t t;
  38. cli();
  39. t = timer_count;
  40. SREG = _sreg;
  41. return TIMER_DIFF(t, last);
  42. }
  43. // This interrupt routine is run approx 61 times per second.
  44. // A very simple inactivity timeout is implemented, where we
  45. // will send a space character and print a message to the
  46. // hid_listen debug message window.
  47. ISR(TIMER0_OVF_vect)
  48. {
  49. timer_count++;
  50. }