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.

104 lines
2.6 KiB

  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/sleep.h>
  4. #include <stdint.h>
  5. #define F_CPU 9600000UL
  6. volatile uint8_t beep_disable = 0;
  7. volatile uint8_t beep_enable = 0;
  8. static void set_wait_timer (void);
  9. void set_beep_timer (uint8_t outport);
  10. // main function
  11. int main (void)
  12. {
  13. DDRB = (1<<DDB1) | (1<<DDB0); // set PB0 & PB1 to output
  14. set_sleep_mode(SLEEP_MODE_IDLE); // set sleep mode
  15. set_beep_timer(0); // generate signal with beeper frequency
  16. while (beep_disable == 0) {} // wait for expired beep counter
  17. set_wait_timer(); // start timer to wait 600 secs
  18. // wait for expired wait counter
  19. while (beep_enable == 0) {
  20. sleep_mode(); // goto sleep until next interrupt
  21. }
  22. // endless loop until switch-off
  23. while(1) {
  24. set_beep_timer(0); // generate signal with beeper frequency
  25. while (beep_disable == 0) {}
  26. set_beep_timer(1); // generate signal with beeper frequency
  27. while (beep_disable == 0) {}
  28. }
  29. return 0;
  30. }
  31. // routine to set timer0 for waiting until beep
  32. static void set_wait_timer (void)
  33. {
  34. cli();
  35. TCCR0A = 0x00; // set timer0 to normal mode
  36. TCCR0B = (1<<CS02) | (1<<CS00); // set timer0 prescaler to 1024
  37. TIMSK0 = (1<<TOIE0); // interrupt enable for timer0 overflow
  38. sei();
  39. return;
  40. }
  41. // routine to set timer0 for to generate frequency for beeper
  42. void set_beep_timer (uint8_t outport)
  43. {
  44. cli();
  45. if (outport == 0) {
  46. TCCR0A = (1<<COM0A0) | (1<<WGM01); // toggle OC0A on compare match , ctc mode
  47. } else {
  48. TCCR0A = (1<<COM0B0) | (1<<WGM01); // toggle OC0B on compare match , ctc mode
  49. }
  50. TCCR0B = (1<<CS00); // set timer0 prescaler to 1 (no prescaling)
  51. OCR0A = 0xE7; // set output compare register a
  52. TIMSK0 = (1<<OCIE0A); // interrupt enable for timer0 compare match a
  53. sei();
  54. return;
  55. }
  56. // interrupt routine for timer0 output compare match a
  57. ISR(TIM0_COMPA_vect)
  58. {
  59. static uint16_t overflow = 0;
  60. // after count up to 1 second
  61. // deactivate beeper & disable timer0 output compare match a interrupt
  62. if (overflow <= 10920) {
  63. beep_disable = 0;
  64. overflow ++;
  65. } else {
  66. TIMSK0 &= ~(1<<OCIE0A); // interrupt disable for timer0 compare match a
  67. beep_disable = 1;
  68. overflow = 0;
  69. }
  70. }
  71. // interrupt routine for timer0 overflow
  72. ISR(TIM0_OVF_vect)
  73. {
  74. static uint16_t overflow = 0;
  75. // after count up to 1200 seconds
  76. // activate beeper & disable timer0 overflow interrupt
  77. if (overflow <= 6000) { // for test: 30 ca. 6 sek.
  78. overflow ++;
  79. } else {
  80. beep_enable = 1;
  81. TIMSK0 &= ~(1<<TOIE0); // interrupt disable for timer0 overflow
  82. }
  83. }