//Audio out with 38.5kHz sampling rate and interrupts
//by Amanda Ghassaei
//http://www.instructables.com/id/Arduino-Audio-Input/
//Sept 2012
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
int incomingAudio;
void setup(){
cli();//disable interrupts
//set up continuous sampling of analog pin 0
//clear ADCSRA and ADCSRB registers
ADCSRA = 0;
ADCSRB = 0;
ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
ADCSRA |= (1 << ADATE); //enabble auto trigger
ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADSC); //start ADC measurements
sei();//enable interrupts
//if you want to add other things to setup(), do it here
}
ISR(ADC_vect) {//when new ADC value ready
incomingAudio = ADCH;//update the variable incomingAudio with new value from A0 (between 0 and 255)
}
void loop(){
//do other stuff here
}
I'm using the above setup() and ISR() functions within my own sketch, but have the problem of running out of data pins! So, with only one data pin, A0, used in the above, I need to use the other analog pins as digital pins for other purposes, such as controlling my realtime clock (which needs three data pins of its own).
My problem is that I don't see how the above singles out "A0" as THE pin to use for audio input. And in addition, the above setup() function seems to disable using the other analog pins for normal read and write.
Is there away to tell this sketch to JUST affect A0, and let the other analog pins work normally?
If you want to use them as digital pins then just use them. None of that code affects their use as digital pins. If you want to use any as analog inputs then you've got some homework to do.
Delta_G was right. The above setup code does not interfere with using the other analog pins as digital pins.
And then,
PaulS:
... you defined which pin ... You'll need to look at the data sheet to understand exactly how you did that.
I did? Well, happy to know I can change the analog input pin, I searched the datasheet for answers (three hours). Finally, I found the answer by using these two tables together:
My not setting any of bits 4..0 in ADMUX was actually setting it for pin A0, since the required bit-setting for A0 is "00000".
But I want my analog input pin to be A7, not A0; so, guided by the above charts, I added three lines to the ADMUX setting:
ADMUX |= (1 << MUX0); //setting bit 1 of 3 for pin A7
ADMUX |= (1 << MUX1); //setting bit 2 of 3 for pin A7
ADMUX |= (1 << MUX2); //setting bit 3 of 3 for pin A7
Making the full setting:
ADMUX |= (1 << MUX0); //setting bit 1 of 3 for pin A7
ADMUX |= (1 << MUX1); //setting bit 2 of 3 for pin A7
ADMUX |= (1 << MUX2); //setting bit 3 of 3 for pin A7
ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value
im sure you already know, but all the analog pins on the board can be used as GPIO (General Use Input/Output) so if you need more digital pins and your analog pins are empty, use them. A little example: