Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <inttypes.h>
- #include <util/delay.h>
- #include "notes.h"
- #include "buttons.h"
- #define F_CPU 16000000
- #define DEFAULT_VOLUME 100
- #define BTN_0_PORT PORTA
- #define BTN_0_PIN PD0
- #define BTN_1_PORT PORTA
- #define BTN_1_PIN PD1
- #define BTN_2_PORT PORTA
- #define BTN_2_PIN PD2
- #define BTNS_LAST 3
- void InitMusic()
- {
- // Configure OC1B pin as output
- DDRD |= _BV(DDD4); //OC1B as output
- // timer1 configuration (for PWM)
- TCCR1A |= _BV(COM1B1); // Clear OC1A/OC1B on compare match
- TCCR1B |= _BV(WGM13) //mode 8, PWM, Phase and Frequency Correct (TOP value is ICR1)
- |_BV(CS11); //prescaler(8)
- }
- /**
- Plays music.
- */
- void PlayMusic( const int* pMusicNotes /** Pointer to table containing music data */,
- uint8_t tempo /** paying tempo from 0 to 100. Higher value = slower playback*/ )
- {
- int duration;
- int note;
- int i;
- uint16_t delay = tempo * 1000;
- while( *pMusicNotes )
- {
- note = *pMusicNotes;
- pMusicNotes++;
- duration = *pMusicNotes;
- pMusicNotes++;
- if( p == note )
- {
- //pause, do not generate any sound
- OCR1B = 0;
- }
- else
- {
- //not a pause, generate tone
- OCR1B = DEFAULT_VOLUME;
- //set frequency
- ICR1H = (note >> 8);
- ICR1L = note;
- }
- //wait duration
- for(i=0;i<32-duration;i++)
- {
- _delay_loop_2( delay );
- }
- }
- //turn off any sound
- OCR1B = 0;
- }
- const int octave[] = {c4, 8, d4, 8, e4, 8, f4, 8, g4, 8, a4, 8, h4, 8, c5, 8, MUSIC_END};
- // Star Wars
- const int starwars[] =
- {
- 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,
- 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,
- F3,8, P,16, Dis3,16, P,16, D3,16, P,16, Dis3,16, P,16, C3,8, C3,8,
- MUSIC_END
- };
- // Fur Elise
- const int furelise[] =
- {
- e4, 8, d4x, 8, e4, 8, d4x, 8, e4, 8, b3, 8, d4, 8, c4, 8, a3,8, p, 8,
- c3, 8, e3, 8, a3, 8, b3, 4, p, 8, e3, 8, g3x, 8, b3, 8, c4, 4, p, 8, e3, 8,
- e3, 8, d4x, 8, e4, 8, d4x, 8, e4, 8, b3, 8, d4, 8, c4, 8, a3, 8, p, 8, c3, 8,
- e3, 8, a3, 8, b3, 4, p, 8, e3, 8, c4, 8, b3, 8, a3, 4,
- MUSIC_END
- };
- main()
- {
- DDRA=0;
- switch(PINA)
- {
- case 1:
- PlayMusic(octave, 40);
- break;
- case 2:
- PlayMusic( furelise, 40 );
- break;
- case 4:
- PlayMusic( starwars, 17 );
- break;
- }
- }
Add Comment
Please, Sign In to add comment