The ADC is auto-triggered from a sampling timer. See
Analog-to-Digital Conversion for details about ADC sampling and acquisition. The 12-bit
conversion result from the ADC is available in the ADRESH:L registers. The ADC result
can be left-aligned so that 8-bit values stored in ADRESH register can be transferred
directly to the ping-pong buffers using DMA
(1). The DMA setup configurations are listed in
Table 1.
Table 1. DMA Setup Configuration
(DMA2)
DMA2 Setup |
SSA |
Source Address |
&ADRESH |
SSZ |
Source Size |
1 |
SMR |
Source Memory Region |
SFR |
SMODE |
Source Address Mode |
SPTR remains unchanged |
DSA |
Destination Address |
Beginning of ping buffer |
DSZ |
Destination Size |
512 (2 banks for ping and pong buffers) |
DMODE |
Destination Address Mode |
DPTR is incremented |
SIRQEN |
Start Trigger Enable |
Yes |
SIRQ |
Start Trigger |
AD (ADC conversion) |
SSTP |
Source Counter Reload Stop |
No - SIRQEN not cleared |
DSTP |
Destination Counter Reload Stop |
No - SIRQEN not cleared |
AIRQEN |
Abort Trigger Enable |
No (stops when SCNT reloads) |
AIRQ |
Abort Trigger |
N/A |
How It Works:
- 1.The DMA is triggered as soon as
an ADC conversion is complete (ADIF trigger) and the content of the ADRESH
register is transferred to the first address in the ping buffer. DPTR is
incremented and now points to the next address in the ping buffer. Because SSZ =
1
, the DMA stops after the first transfer and waits for the
next trigger.
- 2.After 256 such iterations, the
ping buffer is full and a timer interrupt sends a signal to the software to
initiate data transfer from the ping buffer to memory. Meanwhile, DPTR
increments and enters the pong buffer range(2).
- 3.For the next few iterations, while the pong buffer is filling up, the
software/hardware is busy transferring data out of the ping buffer.
- 4.After another 256 iterations (once the pong buffer is full), another timer
interrupt sends another signal to the software to transfer data from the pong
buffer to memory. Meanwhile DPTR reaches the end of the allocated buffer size
and rolls back to the starting DSA address, which is the beginning of the ping
buffer(2).
- 5.This process continues as long as
the recording is in progress.
Important:
- 1.While most of the newer PIC18
devices have a 10-bit or 12-bit ADC, only 8-bit ADC values are used to keep
the application simple. This is because the DAC used to playback the audio
signal has a resolution of 8-bits.
- 2.In this application, the ping
and pong buffers are adjacent to each other in data memory. If ping and pong
buffers are separated in the data memory, then SSA needs to be reconfigured
after every 256 iterations.
- 3.The DMA servicing the ADC has
the highest priority among all the DMAs used. This is to avoid the other
DMAs from blocking the sampling and storage of new audio samples.