Advertisement
Guest User

Untitled

a guest
May 28th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/sleep.h>
  4.  
  5. // global variable, # of overflows
  6. volatile uint16_t overflow_cnt;
  7. volatile uint16_t sec_cnt;
  8. volatile uint16_t min_cnt;
  9.  
  10. // TIMER0 overflow interrupt service routine
  11. ISR(TIMER0_OVF_vect)
  12. {
  13. // keep a track of number of overflows
  14. overflow_cnt++;
  15. }
  16.  
  17. void timer_init()
  18. {
  19. TCCR0 |= (1 << CS01);
  20. TCCR0 |= (1 << CS00); // set prescaler = 64
  21. TCNT0 = 132; // set TIMER0 = 132
  22. TIMSK |= (1<<TOIE0); // enable TIMER0 interrupts
  23. sei();
  24. }
  25.  
  26. ////////////////////////////////////////////////////////
  27. //
  28. // clock frequency = 8MHz
  29. //
  30. // 64 = prescaler clock source
  31. // 132 = start value (125 clocks up to 257 overflow)
  32. //
  33. // 1 / (8MHz / 64 / 125) = 1ms * 1000 = 1s
  34. //
  35. ////////////////////////////////////////////////////////
  36.  
  37. int main(void)
  38. {
  39. while(1)
  40. {
  41. timer_init();
  42. sleep_enable();
  43. sleep_cpu();
  44. sleep_disable();
  45. if (overflow_cnt >= 1000)
  46. {
  47. overflow_cnt = 0;
  48. sec_cnt++;
  49. }
  50. if (min_cnt >= 60){
  51. sec_cnt = 0;
  52. min_cnt++;
  53. }
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement