13 #include "mozzi_config.h" 14 #include "mozzi_analog.h" 28 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 30 if (speed == FAST_ADC){
31 ADCSRA |= (1 << ADPS2);
32 ADCSRA &= ~(1 << ADPS1);
33 ADCSRA &= ~(1 << ADPS0);
34 }
else if(speed == FASTER_ADC){
35 ADCSRA &= ~(1 << ADPS2);
36 ADCSRA |= (1 << ADPS1);
37 ADCSRA |= (1 << ADPS0);
38 }
else if(speed == FASTEST_ADC){
39 ADCSRA &= ~(1 << ADPS2);
40 ADCSRA |= (1 << ADPS1);
41 ADCSRA &= ~(1 << ADPS0);
54 void setupMozziADC(int8_t speed) {
55 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 57 adc->enableInterrupts(ADC_0);
59 ADCSRA |= (1 << ADIE);
67 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 69 DIDR0 |= 1<<channel_num;
75 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 77 DIDR0 &= ~(1<<channel_num);
83 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 85 for (uint8_t i = 0; i<NUM_ANALOG_INPUTS; i++){
93 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 95 for (uint8_t i = 0; i<NUM_ANALOG_INPUTS; i++){
102 uint8_t adcPinToChannelNum(uint8_t pin) {
103 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 106 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 107 if (pin >= 54) pin -= 54;
108 #elif defined(__AVR_ATmega32U4__) 109 if (pin >= 18) pin -= 18;
110 pin = analogPinToChannel(pin);
111 #elif defined(__AVR_ATmega1284__) 112 if (pin >= 24) pin -= 24;
114 if (pin >= 14) pin -= 14;
122 static void adcSetChannel(uint8_t channel) {
123 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 126 #if defined(__AVR_ATmega32U4__) 127 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((channel >> 3) & 0x01) << MUX5);
128 #elif defined(ADCSRB) && defined(MUX5) 131 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((channel >> 3) & 0x01) << MUX5);
138 ADMUX = (1 << REFS0) | (channel & 0x07);
149 void adcStartConversion(uint8_t channel) {
150 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 151 teensy_pin = channel;
152 adc->startSingleRead(teensy_pin);
154 adcSetChannel(channel);
155 #if defined(ADCSRA) && defined(ADCL) 157 ADCSRA |= (1 << ADSC);
171 static volatile int analog_readings[NUM_ANALOG_INPUTS];
173 volatile static int8_t current_channel = -1;
174 static bool first =
true;
179 void adcStartReadCycle(){
180 if (current_channel == -1)
182 #if (USE_AUDIO_INPUT == true) 185 adcReadSelectedChannels();
195 void adcReadSelectedChannels() {
196 current_channel = adc_channels_to_read.
pop();
197 if(current_channel != -1) adcStartConversion(current_channel);
203 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 206 pin = adcPinToChannelNum(pin);
208 adc_channels_to_read.
push(pin);
209 return analog_readings[pin];
220 void startSecondControlADC() {
221 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 222 adc->startSingleRead(teensy_pin);
224 ADCSRA |= (1 << ADSC);
229 void receiveSecondControlADC(){
230 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 231 analog_readings[current_channel] = adc->readSingle();
233 analog_readings[current_channel] = ADC;
244 #if(USE_AUDIO_INPUT==false) 245 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1 248 ISR(ADC_vect, ISR_BLOCK)
254 startSecondControlADC();
260 receiveSecondControlADC();
261 adcReadSelectedChannels();
A simple stack, used internally for keeping track of analog input channels as they are read...
T pop()
Get the item on top of the stack.
int mozziAnalogRead(uint8_t pin)
Reads the analog input of a chosen channel, without blocking other operations from running...
void adcReconnectAllDigitalIns()
Reconnect the digital input buffers for analog input channels which have been set for analog input wi...
void push(T item)
Put an item on the stack.
#define AUDIO_INPUT_PIN
This sets which analog input channel to use for audio input, if you have #define USE_AUDIO_INPUT true...
void reconnectDigitalIn(uint8_t channel_num)
Reconnect the digital input buffer for an analog input channel which has been set for analog input wi...
void disconnectDigitalIn(uint8_t channel_num)
Prepare an analog input channel by turning off its digital input buffer.
void setupFastAnalogRead(int8_t speed)
This is automatically called in startMozzi.
void adcDisconnectAllDigitalIns()
Prepare all analog input channels by turning off their digital input buffers.