01-10-2012, 11:01 AM
AVR335: Digital Sound Recorder with AVR® and DataFlash®
AVR335.pdf (Size: 149.29 KB / Downloads: 13)
Introduction
This application note describes how to record, store and play back sound using any
AVR microcontroller with A/D converter, the AT45DB161B DataFlash memory and a
few extra components.
This application note shows in detail the usage of the A/D Converter for sound recording,
the Serial Peripheral Interface – SPI – for accessing the external DataFlash
memory and the Pulse Width Modulation – PWM – for playback. Typical applications
that would require one or more of these blocks are temperature loggers, telephone
answering machines, or digital voice recorders.
The AT45DB161B DataFlash is a 2.7 volt only, Serial-interface Flash memory. Its 16
Mbit of memory are organized as 4096 pages of 528 bytes each. In addition to its
main memory, the DataFlash contains two SRAM data buffers of 528 bytes each. The
buffers allow a virtually continuous data stream to be written to the DataFlash.
The AT45DB161B uses an SPI serial interface to sequentially access its data. This
interface facilitates hardware layout, increases system reliability, minimizes switching
noise, and reduces package size and active pin count. Typical applications are image
storage, data storage and digital voice storage. The DataFlash operates at SPI clock
frequencies up to 20 MHz with a typical active read current consumption of 4 mA. It
operates from a single voltage power supply (from 2.7V to 3.6V) for both the write and
read operations.
Its serial interface is compatible to the Serial Peripheral Interface – SPI – Modes 0 and
3, thus it can easily be interfaced to the AVR microcontroller.
In this application note the AVR AT90S8535 is used to take analog samples from a
microphone and convert them to digital values. Its built-in SPI controls data transfers
to and from the DataFlash. The PWM feature of the AVR is used for playback. The
code size is very small, the application will therefore also fit into smaller AVR devices.
Theory of Operation
First, the analog signal (Figure 1) is converted into a time discrete signal by taking periodic
samples (Figure 2). The time interval between two samples is called the “sampling
period” and its reciprocal the “sampling frequency”. According to the sampling theorem,
the sampling frequency has to be at least double the maximum signal frequency. Otherwise
the periodic continuation of the signal in the frequency domain would result in
spectral overlap, called “aliasing”. Such an aliased signal can not be uniquely recovered
from its samples.
A speech signal contains its major information below 3000 Hz. Therefore a low-pass filter
can be used to band-limit the signal.
For an ideal low-pass filter with a cut-off frequency of 3000 Hz the sampling frequency
must be 6000 Hz. Depending on the filter, the filter slope is more or less steep. Especially
for a first order filter like the RC-filter used in this application it is necessary to
choose a much higher sampling frequency. The upper limit is set by the features of the
A/D-converter.
Determining the digital values that represent the analog samples taken at this sampling
frequency is called “quantization”. The analog signal is quantized by assigning an analog
value to the nearest “allowed” digital value (Figure 3). The number of digital values is
called “resolution” and is always limited, for example to 256 values for an 8-bit digital
signal or 10 values in this example. Therefore quantization of analog signals always
results in a loss of information. This “quantization error” is inversly proportional to the
resolution of the digital signal. It is also inversly proportional to the signal’s “dynamic
range”, the range between minimum and maximum values (3 to 8 in this example). The
A/D converter of the AT90S8535 microcontroller can be adjusted to the dynamic range
of the signal by setting AGND and AREF to the minimum and maximum signal values.
Microcontroller and Memory Circuit
The user can control the sound system with three pushbuttons, called “Erase”, “Record”
and “Playback”. If the pushbuttons are not pressed, the internal pull-up resistors provide
VCC at PD0 - PD2. Pushing a button pulls the input line to GND.
As feedback for the user, an LED indicates the status of the system.
The DataFlash is directly connected to the AVR microcontroller using the SPI bus. In
case the ISP feature is used to reprogram the AVR, the pull-up resistor on the Chip
Select line (CS) prevents the DataFlash from going active. If the ISP feature is not used,
this resistor can be omitted.
The analog voltage, AVCC, is connected to VCC by an RC low-pass filter. The reference
voltage is set to AVCC.
The oscillator crystal with two 22 pF decoupling capacitors generates the system clock.
Record
The record subroutine consists of the setup of the A/D converter and an empty loop
which is performed as long as the “Record” button is pressed. The ADC0 pin is used in
this application which requires the ADC Multiplexer Select Register (ADMUX) being set
to zero. In the ADC Control and Status Register (ADCSR) the ADC is enabled with a
clock division factor of 32, set to single conversion mode, interrupts enabled, and the
interrupt flag is cleared. The A/D conversion is also immediately started. The first conversion
takes longer than the following conversions (832 oscillator cycles instead of
448). After this time, the ADC interrupt occurs indicating that the conversion is finished
and the result can be read out of the ADC Data Register.
The analog signal from the microphone circuit is sampled at 15,686 Hz. This is the same
frequency as the output (PWM) frequency.
To achieve a sampling frequency of 15,686 Hz, a sample has to be taken every
510 cycles (15,686 Hz x 510 = 8 MHz). To get one A/D conversion result, each
510 clock cycles the ADC is run in single conversion mode with an ADC clock division
by 32. A single conversion takes 14 ADC cycles. Therefore a conversion will be ready
after 14 x 32 = 448 cycles.
When a conversion is finished an interrupt occurs. The interrupt routine then performs a
loop to fill in the missing 510 - 448 = 62 cycles, before a new A/D conversion is started.
The 10-bit conversion result represents the value at the A/D converter input pin 2 cycles
after the conversion has started. These 10 bits cover the range from AGND to AREF,
which is 0 to 5V in this application. The microphone circuit output signal, however, is limited
to the range of 2.3V to 3.5V. Therefore the 10-bit conversion result is subtracted by
a value representing the minimum input voltage. This is 0x1D5 for 2.3V. The part of the
data representing signal values above 3.5V is removed by cutting off the two MSBs.
This is done automatically when the conversion result is handed over to the “write to
flash” subroutine, as its variable “flash_data” is defined as type “char” (8-bit). The final 8-
bit data has then to be written to the DataFlash before the next A/D conversion interrupt
occurs.