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.

52 lines
1.4 KiB

  1. /* gc_n64_usb : Gamecube or N64 controller to USB firmware
  2. Copyright (C) 2007-2016 Raphael Assenat <raph@raphnet.net>
  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 3 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 "intervaltimer.h"
  16. void intervaltimer_init(void)
  17. {
  18. TCCR1A = 0;
  19. TCCR1B = (1<<WGM12) | (1<<CS12) | (1<<CS00); // CTC, /1024 prescaler
  20. }
  21. void intervaltimer_set(int interval_ms)
  22. {
  23. static int cur_interval = 0;
  24. // We are setting TCNT back to zero, and updating
  25. // the compare match register.
  26. //
  27. // To allows for simple repeated calling of this
  28. // function from the main loop, only touch
  29. // the counter when the interval changes.
  30. if (cur_interval != interval_ms) {
  31. cur_interval = interval_ms;
  32. TCNT1 = 0;
  33. OCR1A = interval_ms * (F_CPU/1024) / 1000;
  34. }
  35. }
  36. char intervaltimer_get(void)
  37. {
  38. if (TIFR1 & (1<<OCF1A)) {
  39. TIFR1 = 1<<OCF1A;
  40. return 1;
  41. }
  42. return 0;
  43. }