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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

buzzer.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  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 2 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 <util/delay.h>
  16. #include "buzzer.h"
  17. #include "debug.h"
  18. void buzzer_init(void)
  19. {
  20. DDRD |= (1<<PD1);
  21. PORTD &= ~(1<<PD1);
  22. }
  23. inline
  24. void buzzer_on(void)
  25. {
  26. #ifndef EXPERIMENTAL
  27. PORTD |= (1<<PD1);
  28. #else
  29. PORTD |= (1<<PD5);
  30. #endif
  31. }
  32. inline
  33. void buzzer_off(void)
  34. {
  35. #ifndef EXPERIMENTAL
  36. PORTD &= ~(1<<PD1);
  37. #else
  38. PORTD &= ~(1<<PD5);
  39. #endif
  40. }
  41. void buzzer(uint16_t frequency, uint16_t duration)
  42. {
  43. uint16_t delay = 100000L / (2 * frequency);
  44. uint16_t period = (uint32_t)frequency * duration / 1000L;
  45. uint16_t i;
  46. //dprintf("delay: %d\nperiod: %d\n", delay, period);
  47. do {
  48. buzzer_on();
  49. i = delay;
  50. do {
  51. _delay_us(10);
  52. } while (--i);
  53. buzzer_off();
  54. i = delay;
  55. do {
  56. _delay_us(10);
  57. } while (--i);
  58. } while(--period);
  59. }