12 #include "utility/FrequencyTimer2.h"
13 #include "utility/TimerOne.h"
15 #if (F_CPU != 16000000)
17 "Mozzi has been tested with a cpu clock speed of 16MHz on Arduino! Results may vary with other speeds."
21 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
22 extern uint8_t analog_reference;
24 ISR(ADC_vect, ISR_BLOCK)
26 MozziPrivate::advanceADCStep();
30 #define getADCReading() ADC
31 #define channelNumToIndex(channel) channel
32 uint8_t adcPinToChannelNum(uint8_t pin) {
33 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
34 if (pin >= 54) pin -= 54;
35 #elif defined(__AVR_ATmega32U4__)
36 if (pin >= 18) pin -= 18;
37 # if defined(CORE_TEENSY)
38 static const uint8_t PROGMEM adc_mapping[] = {
40 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8, 10, 11, 12, 13, 7, 6, 5, 4, 1, 0, 8
42 pin = pgm_read_byte(adc_mapping + (pin));
44 pin = analogPinToChannel(pin);
46 #elif defined(__AVR_ATmega1284__)
47 if (pin >= 24) pin -= 24;
49 if (pin >= 14) pin -= 14;
54 void adcStartConversion(uint8_t channel) {
55 #if defined(__AVR_ATmega32U4__)
56 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((channel >> 3) & 0x01) << MUX5);
57 #elif defined(ADCSRB) && defined(MUX5)
60 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((channel >> 3) & 0x01) << MUX5);
68 # if defined(TEENSYDUINO)
69 ADMUX = (1 << REFS0) | (channel & 0x07);
71 ADMUX = (analog_reference << 6) | (channel & 0x07);
74 #if defined(ADCSRA) && defined(ADCL)
76 ADCSRA |= (1 << ADSC);
80 static void startSecondADCReadOnCurrentChannel() {
81 ADCSRA |= (1 << ADSC);
90 void setupMozziADC(int8_t speed) {
91 ADCSRA |= (1 << ADIE);
93 setupFastAnalogRead(speed);
96 void setupFastAnalogRead(int8_t speed) {
97 if (speed == FAST_ADC){
98 ADCSRA |= (1 << ADPS2);
99 ADCSRA &= ~(1 << ADPS1);
100 ADCSRA &= ~(1 << ADPS0);
101 }
else if(speed == FASTER_ADC){
102 ADCSRA &= ~(1 << ADPS2);
103 ADCSRA |= (1 << ADPS1);
104 ADCSRA |= (1 << ADPS0);
105 }
else if(speed == FASTEST_ADC){
106 ADCSRA &= ~(1 << ADPS2);
107 ADCSRA |= (1 << ADPS1);
108 ADCSRA &= ~(1 << ADPS0);
145 static uint8_t pre_mozzi_TCCR1A, pre_mozzi_TCCR1B, pre_mozzi_OCR1A,
148 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
150 static uint8_t pre_mozzi_TCCR2A, pre_mozzi_TCCR2B, pre_mozzi_OCR2A,
153 static uint8_t pre_mozzi_TCCR2, pre_mozzi_OCR2, pre_mozzi_TIMSK;
154 #elif defined(TCCR4A)
155 static uint8_t pre_mozzi_TCCR4A, pre_mozzi_TCCR4B, pre_mozzi_TCCR4C,
156 pre_mozzi_TCCR4D, pre_mozzi_TCCR4E, pre_mozzi_OCR4C, pre_mozzi_TIMSK4;
160 static void backupPreMozziTimer1() {
162 pre_mozzi_TCCR1A = TCCR1A;
163 pre_mozzi_TCCR1B = TCCR1B;
164 pre_mozzi_OCR1A = OCR1A;
165 pre_mozzi_TIMSK1 = TIMSK1;
168 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
170 static uint8_t mozzi_TCCR2A, mozzi_TCCR2B, mozzi_OCR2A, mozzi_TIMSK2;
172 static uint8_t mozzi_TCCR2, mozzi_OCR2, mozzi_TIMSK;
173 #elif defined(TCCR4A)
174 static uint8_t mozzi_TCCR4A, mozzi_TCCR4B, mozzi_TCCR4C, mozzi_TCCR4D,
175 mozzi_TCCR4E, mozzi_OCR4C, mozzi_TIMSK4;
179 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
180 static void startAudio() {
181 backupPreMozziTimer1();
182 Timer1.initializeCPUCycles(
195 ISR(TIMER1_OVF_vect, ISR_BLOCK) {
196 MozziPrivate::defaultAudioOutput();
200 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
201 static void startAudio() {}
202 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
205 MOZZI_AUDIO_PIN_1_REGISTER = f.l()+MOZZI_AUDIO_BIAS;
206 # if (MOZZI_AUDIO_CHANNELS > 1)
207 MOZZI_AUDIO_PIN_2_REGISTER = f.r()+MOZZI_AUDIO_BIAS;
211 static void startAudio() {
212 backupPreMozziTimer1();
215 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM) && (MOZZI_PWM_RATE < 32768)
216 Timer1.initializeCPUCycles(
231 # if (MOZZI_AUDIO_CHANNELS > 1)
232 pinMode(MOZZI_AUDIO_PIN_2, OUTPUT);
233 Timer1.pwm(MOZZI_AUDIO_PIN_2, MOZZI_AUDIO_BIAS);
243 ISR(TIMER1_OVF_vect, ISR_BLOCK) {
244 # if (MOZZI_AUDIO_RATE < MOZZI_PWM_RATE)
246 static boolean alternate;
247 alternate = !alternate;
248 if (alternate)
return;
251 MozziPrivate::defaultAudioOutput();
255 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
284 static void setupTimer2();
285 static void startAudio() {
286 backupPreMozziTimer1();
289 pinMode(MOZZI_AUDIO_PIN_1_LOW, OUTPUT);
290 Timer1.initializeCPUCycles(
294 Timer1.pwm(MOZZI_AUDIO_PIN_1_LOW, 0);
302 static void backupPreMozziTimer2() {
305 pre_mozzi_TCCR2A = TCCR2A;
306 pre_mozzi_TCCR2B = TCCR2B;
307 pre_mozzi_OCR2A = OCR2A;
308 pre_mozzi_TIMSK2 = TIMSK2;
310 pre_mozzi_TCCR2 = TCCR2;
311 pre_mozzi_OCR2 = OCR2;
312 pre_mozzi_TIMSK = TIMSK;
313 #elif defined(TCCR4A)
314 pre_mozzi_TCCR4B = TCCR4A;
315 pre_mozzi_TCCR4B = TCCR4B;
316 pre_mozzi_TCCR4B = TCCR4C;
317 pre_mozzi_TCCR4B = TCCR4D;
318 pre_mozzi_TCCR4B = TCCR4E;
319 pre_mozzi_OCR4C = OCR4C;
320 pre_mozzi_TIMSK4 = TIMSK4;
326 static void setupTimer2() {
327 backupPreMozziTimer2();
329 FrequencyTimer2::setPeriodCPUCycles(period);
330 FrequencyTimer2::setOnOverflow(dummy);
331 FrequencyTimer2::enable();
336 #if defined(TIMER2_COMPA_vect)
337 ISR(TIMER2_COMPA_vect)
338 #elif defined(TIMER2_COMP_vect)
339 ISR(TIMER2_COMP_vect)
340 #elif defined(TIMER4_COMPA_vect)
341 ISR(TIMER4_COMPA_vect)
344 "This board does not have a hardware timer which is compatible with FrequencyTimer2"
345 void dummy_function(
void)
348 MozziPrivate::defaultAudioOutput();
364 TCCR1A = pre_mozzi_TCCR1A;
365 TCCR1B = pre_mozzi_TCCR1B;
366 OCR1A = pre_mozzi_OCR1A;
368 TIMSK1 = pre_mozzi_TIMSK1;
370 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
372 TCCR2A = pre_mozzi_TCCR2A;
373 TCCR2B = pre_mozzi_TCCR2B;
374 OCR2A = pre_mozzi_OCR2A;
375 TIMSK2 = pre_mozzi_TIMSK2;
377 TCCR2 = pre_mozzi_TCCR2;
378 OCR2 = pre_mozzi_OCR2;
379 TIMSK = pre_mozzi_TIMSK;
380 #elif defined(TCCR4A)
381 TCCR4B = pre_mozzi_TCCR4A;
382 TCCR4B = pre_mozzi_TCCR4B;
383 TCCR4B = pre_mozzi_TCCR4C;
384 TCCR4B = pre_mozzi_TCCR4D;
385 TCCR4B = pre_mozzi_TCCR4E;
386 OCR4C = pre_mozzi_OCR4C;
387 TIMSK4 = pre_mozzi_TIMSK4;
403 #if defined (__AVR_ATmega644P__)
406 static long longRandom()
408 return ((
long)analogRead(0)+63)*(analogRead(1)+97);
424 static long longRandom()
427 unsigned long rv = 0;
428 for (uint8_t i=0; i< 32; i++) rv |= ((analogRead(8)+1171) & 1L) << i;
433 void MozziRandPrivate::autoSeed() {
434 ADCSRA &= ~ (1 << ADIE);
443 ADCSRA |= (1 << ADIE);
void adcDisconnectAllDigitalIns()
Prepare all analog input channels by turning off their digital input buffers.
#define MOZZI_PWM_RATE
Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM.
#define MOZZI_AUDIO_PIN_1
Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM: The IO pin to use as (first) ...
#define MOZZI_AUDIO_BITS_PER_CHANNEL
Only for MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM.
#define MOZZI_AUDIO_RATE
Defines the audio rate, i.e.
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
This struct encapsulates one frame of mono audio output.