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();
29 namespace MozziPrivate {
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 + (P));
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);
92 adcDisconnectAllDigitalIns();
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);
148 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
154 #elif defined(TCCR4A)
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)
173 #elif defined(TCCR4A)
179 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
200 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
202 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
206 # if (MOZZI_AUDIO_CHANNELS > 1
)
215 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM) && (MOZZI_PWM_RATE < 32768
)
231 # if (MOZZI_AUDIO_CHANNELS > 1
)
244 # if (MOZZI_AUDIO_RATE < MOZZI_PWM_RATE)
255 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
313 #elif defined(TCCR4A)
336 #if defined(TIMER2_COMPA_vect)
338 #elif defined(TIMER2_COMP_vect)
340 #elif defined(TIMER4_COMPA_vect)
344 "This board does not have a hardware timer which is compatible with FrequencyTimer2"
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__)
424 static long longRandom()
427 unsigned long rv = 0;
428 for (uint8_t i=0; i< 32; i++) rv |= ((analogRead(8)+1171) & 1L) << i;