Guest User

Untitled

a guest
May 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1.  
  2.  
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5. #include <inttypes.h>
  6. #include <util/delay.h>
  7.  
  8.  
  9. #include "notes.h"
  10. #include "buttons.h"
  11. #define F_CPU 16000000
  12.  
  13. #define DEFAULT_VOLUME 100
  14.  
  15. #define BTN_0_PORT PORTA
  16. #define BTN_0_PIN PD0
  17. #define BTN_1_PORT PORTA
  18. #define BTN_1_PIN PD1
  19. #define BTN_2_PORT PORTA
  20. #define BTN_2_PIN PD2
  21.  
  22. #define BTNS_LAST 3
  23.  
  24.  
  25. void InitMusic()
  26. {
  27. // Configure OC1B pin as output
  28.  
  29. DDRD |= _BV(DDD4); //OC1B as output
  30.  
  31. // timer1 configuration (for PWM)
  32. TCCR1A |= _BV(COM1B1); // Clear OC1A/OC1B on compare match
  33.  
  34.  
  35. TCCR1B |= _BV(WGM13) //mode 8, PWM, Phase and Frequency Correct (TOP value is ICR1)
  36. |_BV(CS11); //prescaler(8)
  37. }
  38.  
  39.  
  40. /**
  41. Plays music.
  42. */
  43.  
  44. void PlayMusic( const int* pMusicNotes /** Pointer to table containing music data */,
  45. uint8_t tempo /** paying tempo from 0 to 100. Higher value = slower playback*/ )
  46. {
  47.  
  48. int duration;
  49. int note;
  50. int i;
  51. uint16_t delay = tempo * 1000;
  52.  
  53. while( *pMusicNotes )
  54. {
  55. note = *pMusicNotes;
  56. pMusicNotes++;
  57.  
  58. duration = *pMusicNotes;
  59. pMusicNotes++;
  60.  
  61. if( p == note )
  62. {
  63. //pause, do not generate any sound
  64. OCR1B = 0;
  65. }
  66. else
  67. {
  68. //not a pause, generate tone
  69. OCR1B = DEFAULT_VOLUME;
  70.  
  71. //set frequency
  72. ICR1H = (note >> 8);
  73. ICR1L = note;
  74. }
  75.  
  76. //wait duration
  77. for(i=0;i<32-duration;i++)
  78. {
  79. _delay_loop_2( delay );
  80. }
  81.  
  82. }
  83.  
  84.  
  85. //turn off any sound
  86. OCR1B = 0;
  87. }
  88.  
  89. const int octave[] = {c4, 8, d4, 8, e4, 8, f4, 8, g4, 8, a4, 8, h4, 8, c5, 8, MUSIC_END};
  90.  
  91.  
  92. // Star Wars
  93. const int starwars[] =
  94. {
  95. Ais2,8, Ais2,8, P,16, F3,8, F3,8, P,16, Dis3,16, P,16, D3,16, P,16, C3,16, P,16, Ais3,8,
  96. Ais3,8, P,16, F3,8, P,16, Dis3,16, P,16, D3,16, P,16, C3,16, P,16, Ais3,8, Ais3,8, P,16,
  97. F3,8, P,16, Dis3,16, P,16, D3,16, P,16, Dis3,16, P,16, C3,8, C3,8,
  98. MUSIC_END
  99. };
  100.  
  101. // Fur Elise
  102. const int furelise[] =
  103. {
  104. e4, 8, d4x, 8, e4, 8, d4x, 8, e4, 8, b3, 8, d4, 8, c4, 8, a3,8, p, 8,
  105. c3, 8, e3, 8, a3, 8, b3, 4, p, 8, e3, 8, g3x, 8, b3, 8, c4, 4, p, 8, e3, 8,
  106. e3, 8, d4x, 8, e4, 8, d4x, 8, e4, 8, b3, 8, d4, 8, c4, 8, a3, 8, p, 8, c3, 8,
  107. e3, 8, a3, 8, b3, 4, p, 8, e3, 8, c4, 8, b3, 8, a3, 4,
  108. MUSIC_END
  109. };
  110.  
  111.  
  112.  
  113.  
  114. main()
  115. {
  116.  
  117. DDRA=0;
  118. switch(PINA)
  119. {
  120.  
  121. case 1:
  122. PlayMusic(octave, 40);
  123. break;
  124.  
  125. case 2:
  126. PlayMusic( furelise, 40 );
  127.  
  128. break;
  129.  
  130. case 4:
  131. PlayMusic( starwars, 17 );
  132. break;
  133.  
  134.  
  135. }
  136.  
  137. }
Add Comment
Please, Sign In to add comment