pentabug/old_firmware/lib/music.c

44 lines
911 B
C
Raw Normal View History

2012-10-04 13:57:18 +02:00
#include <stdint.h>
#include <math.h>
#include <avr/interrupt.h>
#include <avr/common.h>
#include <avr/io.h>
#include "util.h"
#include "bughal.h"
#include "music.h"
//static volatile uint16_t currentNote;
2012-10-07 02:04:16 +02:00
void music_init(void) {
2012-10-04 13:57:18 +02:00
2012-10-07 02:04:16 +02:00
TCCR1A = 0; //TIMER1, normal, no PWM
TCCR1B = (1 << WGM12) | (1 << CS11); //CTC Mode, Clear Timer on Compare, Prescaler = 8
TIMSK1 &= ~(1 << OCIE1A); //disable Output Compare Interrupt
2012-10-04 13:57:18 +02:00
return;
2012-10-07 02:04:16 +02:00
}
;
2012-10-04 13:57:18 +02:00
2012-10-07 02:04:16 +02:00
void music_setNote(uint16_t note, uint8_t octave) {
2012-10-04 13:57:18 +02:00
2012-10-07 02:04:16 +02:00
cli();
if (note != NOTE_PAUSE) {
//Play a Note
buzzr_up();
TIMSK1 |= (1 << OCIE1A); //enable Output Compare Interrupt
OCR1A = octave == 0 ? note : note / (1 << octave);
2012-10-04 13:57:18 +02:00
} else { // Pause (silence)
2012-10-07 02:04:16 +02:00
buzzr_off();
TIMSK1 &= ~(1 << OCIE1A); //disable Output Compare Interrupt
2012-10-04 13:57:18 +02:00
}
sei();
return;
}
2012-10-07 02:04:16 +02:00
/* timer interrupt function */ISR(TIMER1_COMPA_vect, ISR_NOBLOCK) {
2012-10-04 13:57:18 +02:00
2012-10-07 02:04:16 +02:00
// invert buzzer polarity
buzzr_inv();
2012-10-04 13:57:18 +02:00
}