How is the 38Khz sample rate calculated, this code is not making much sense to me?
//Audio out with 38.5kHz sampling rate and DAC output
//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.
*
*/
void setup()
{
for (byte i=0;i<8;i++)
{
pinMode(i,OUTPUT);
}
//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 << ADEN); //enable
ADCSRA |= (1 << ADSC); //start ADC measurements
}
void loop()
{
PORTD = ADCH;//send 8 bit value from analog pin 0 to DAC
}
Sets the clock rate of the A/D converter by setting the prescaller value. That is the amount by which the main system clock is divided before it clocks the A/D converter.
ADCSRA is the name of a register inside the processor, and ADPS2 and ADPS0 are simply bit numbers.
You can find out more details by looking at the A/D section of the ATmega 328 data sheet. As these lines are directly manipulating the internal resources of the chip they will only work on a small number of types of Arduino.
P.S. Never try and learn anything new from instructables as many of the authors there often have limited understand about what they are doing. That one is better than most but still has a few mistakes like leaving an unused op amp in a package not connected to anything. The unused amp should have the output connected to the - input and the + input connected to the bias voltage on the Arduino.
In fact he could have made better use of that spare amplifier by using it to generate the bias in the first place. Also he has no supply decoupling on the op-amp's power rails.
Also I am supprised given his claimed background that he writes:-
the path of the incoming 360hz signal
That should be 360Hz, that might sound petty but someone who claims such a background should not me making such a mistake.
The ADC takes 13.5 ADC clocks in single-shot mode, presumably from these figures it takes 13 ADC clocks
per reading in continuously triggered mode. 500kHz/13 = 38461Hz
And yes to further Mike's comment, its important to get units right.
k = kilo prefix
K = kelvin, SI unit of temperature
Hz = hertz
hz = hecto-zepto, a nonsense combination of prefixes.
Learn your SI, its always case-sensitive. And yes the unit is hertz, the man was Hertz.