music.c (1267B)
1 /* 2 * avr pomodoro timer 3 * © 2022 Anton Konyahin <me@konyahin.xyz> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 #include <avr/io.h> 20 #include <avr/interrupt.h> 21 #include <util/delay.h> 22 #include <stdarg.h> 23 24 #include "music.h" 25 26 void 27 play_melody(const melody_t *melody) 28 { 29 // set clk/8 prescaling 30 TCCR0B |= (1 << CS01); 31 32 for (int8_t i = 0; i < melody->length; i++) 33 { 34 OCR0A = melody->tones[i]; 35 _delay_ms(DELAY); 36 } 37 38 // disable timer 39 TCCR0B &= ~(1 << CS01); 40 } 41 42 void 43 init_music(void) 44 { 45 DDRB |= (1 << SOUND); 46 PORTB &= ~(1 << SOUND); 47 48 // set timer setting 49 TCCR0A |= (1 << WGM01); 50 TCCR0A |= (1 << COM0A0); 51 }