Share Pastebin
Guest
Public paste!

Nerdful_com

By: a guest | Aug 17th, 2009 | Syntax: C | Size: 1.01 KB | Hits: 391 | Expires: Never
Copy text to clipboard
  1. /*
  2. Make music with piezo (piezoelectric buzzer).
  3. Put the buzzer on MCU pin 15 (PB1)
  4. Does not use libnerdkits in makefile use # to comment out makefile lines related
  5. Hacked by www.Nerdful.com
  6. */
  7. #define F_CPU 8000000UL // 8MHz
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10. void play_tone(uint16_t delay, uint8_t duration) {
  11.   uint16_t tmp = 100 * duration;
  12.   uint16_t delaysm = delay / 50;
  13.   uint16_t cycles = tmp / delaysm;
  14.  
  15.   while(cycles > 0) {
  16.     PORTB |= (1<<PB1);
  17.     _delay_us(delay);
  18.     PORTB &= ~(1<<PB1);
  19.     _delay_us(delay);
  20.     cycles--;
  21.   }
  22. }
  23. int main() {
  24. // oscillator @ 8MHz.
  25. OSCCAL = 176;
  26. DDRB |= (1<<PB1);
  27. while(1) {
  28. play_tone(1000, 50);
  29. play_tone(950, 50);
  30. play_tone(900, 50);
  31. play_tone(850, 50);
  32. play_tone(800, 50);
  33. play_tone(750, 50);
  34. play_tone(700, 50);
  35. play_tone(650, 50);
  36. play_tone(600, 50);
  37. play_tone(550, 50);
  38. play_tone(500, 50);
  39. play_tone(450, 50);
  40. play_tone(400, 50);
  41. play_tone(350, 50);
  42. play_tone(300, 50);
  43. play_tone(250, 50);
  44. _delay_ms(2000);
  45. }
  46. return 0;
  47. }