I am using the Martin L code (Setting up ISR for ADC on Zero - #5 by MartinL, Post 5). However, I have a problem. The sketch repeats a lot the output values. Below are two outputs: real values first (A4) and then the values generated by the sketch (A4). As can it be seen the values displayed by the sketch repeat too much.
Real Data:
3880,3952,260,66,64,3894,3944,1076,86,76,3894,3946,3912,242,80,3904,3952,3944,264,57,3885,3939,3939,257,61,65,3893,3945,259,57,73,3897,3953,2059,129,65,3888,3946,3938,259,57,3887,3945,3945,259,57,3891,3951,3949,265,59,73,3891,3944,788,74,72,3882,3938,3281,201,71,3893,3953,3945,261,59,3891,3945,3945,264,58,68,3891,3945,251,88,76,3896,3953,1691,109,77,3893,3945,3945,257,58,3882,3930,3936,250,90,3892,3948,3952,258,66,72,3891,3945,523,69,67,3896,3944,2762,175,71,3893,3949,3949,259,65,3891,3945,3944,256,58,3209,3929,3936,248,88,68,3896,3946,1331,97,77,3897,3947,3947,258,58,3884,3944,3944,265,56,3888,3937,3945,259,56,64,3896,3953,329,73,74,3888,3944,2309,131,58,3881,3945,3945,258,64,3892,3952,3946,264,56,3896,3944,3944,258,52,72,3893,3947,955,90,78,3890,3954,3755,217,73,3889,3945,3945,257,59,3881,3937,3936,260,56,66,3892,3954,245,86,74,3892,3947,1845,119,83,3891,3945,3945,256,58,3890,3947,3947,259,64,3893,3939,3938,248,82,68,3889,3937,649,69,72,3892,3948,3032,192,74,3888,3945,3945,261,64,3884,3945,3944,258,52,297,3890,3946,266,59,73,3905,3947,1474,104,76,3884,3944,3937,259,61,3888,3944,3947,259,56,3896,3946,3944,251,88,70,3892,3945,427,68,74,3892,3944,3938,251,86,3897,3952,3946,265,56,3891,3945,3944,256,58,71,3888,3944,715,89,74,3891,3948,3120,183,65,3889,3938,3945,257,58,3888,3946,3945,261,65,66,3889,3953,274,65,73,3892,3946,1546,95,79,3885,3936,3944,264,56,3892,3944,3947,259,65,3889,3945,3945,264,56,64,3888,3944,472,88,76,3891,3947,2665,180,66,3892,3945,3945,259,56,3882,3928,3937,249,89,3889,3945,3946,260,60,3944,3952,3944...,264,56,66,3892,3944,1152,78,76,3890,3947,3945,256,56,3896,3949,3949,265,65,3885,3945,3937,249,89,71,3895,3945,274,64,73,3904,3946,2168,158,66,3883,3945,3945,265,59,3891,3945,3945,257,56,3904,3953,3947,261,56,75,3889,3944,896,74,73,67,73,73,3897,3949,3954,270,65,66,3896,3944,1545,93,75,3890,3944,3944,256,58,3885,3945,3945,256,64,3905,3949,3947,260,56,64,3888,3936,452,76,77,3889,3948
The values do not repeat
Data displayed by the sketch:
118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3940,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3939,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,3943,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948,3948
Values repeat a lot.
The code (based in the Martin L code):
/*
Notes:
Source: https://forum.arduino.cc/t/setting-up-isr-for-adc-on-zero/655967/5
*/
#include <SPI.h>
#include <Wire.h>
#include <math.h>
//#define DEBUG
#define SAMPLES_TO_READ 20000L
char getGoing = 'N';
unsigned long timeStartReading,timeEndReading;
int sampleValue[2*SAMPLES_TO_READ];
uint16_t adcResult[2]; // A4 and A5 result array
uint32_t inputCtrl[] = { ADC_INPUTCTRL_MUXPOS_AIN4, // ADC0 INPUTCTRL register MUXPOS settings AIN0 = A0, AIN5 = A1
ADC_INPUTCTRL_MUXPOS_AIN6 };
typedef struct // DMAC descriptor structure
{
uint16_t btctrl;
uint16_t btcnt;
uint32_t srcaddr;
uint32_t dstaddr;
uint32_t descaddr;
} dmacdescriptor ;
volatile dmacdescriptor wrb[DMAC_CH_NUM] __attribute__ ((aligned (16))); // Write-back DMAC descriptors
dmacdescriptor descriptor_section[DMAC_CH_NUM] __attribute__ ((aligned (16))); // DMAC channel descriptors
dmacdescriptor descriptor __attribute__ ((aligned (16))); // Place holder descriptor
void ADC_Init()
{
DMAC->BASEADDR.reg = (uint32_t)descriptor_section; // Specify the location of the descriptors
DMAC->WRBADDR.reg = (uint32_t)wrb; // Specify the location of the write back descriptors
DMAC->CTRL.reg = DMAC_CTRL_DMAENABLE | DMAC_CTRL_LVLEN(0xf); // Enable the DMAC peripheral
DMAC->Channel[0].CHCTRLA.reg = DMAC_CHCTRLA_TRIGSRC(ADC0_DMAC_ID_SEQ) | // Set DMAC to trigger on ADC0 DMAC sequence
DMAC_CHCTRLA_TRIGACT_BURST; // DMAC burst transfer
descriptor.descaddr = (uint32_t)&descriptor_section[0]; // Set up a circular descriptor
descriptor.srcaddr = (uint32_t)inputCtrl + sizeof(uint32_t) * 2; // Configure the DMAC to set the
descriptor.dstaddr = (uint32_t)&ADC0->DSEQDATA.reg; // Write the INPUT CTRL
descriptor.btcnt = 2; // Beat count is 2
descriptor.btctrl = DMAC_BTCTRL_BEATSIZE_WORD | // Beat size is WORD (32-bits)
DMAC_BTCTRL_SRCINC | // Increment the source address
DMAC_BTCTRL_VALID; // Descriptor is valid
memcpy(&descriptor_section[0], &descriptor, sizeof(descriptor)); // Copy the descriptor to the descriptor section
DMAC->Channel[1].CHCTRLA.reg = DMAC_CHCTRLA_TRIGSRC(ADC0_DMAC_ID_RESRDY) | // Set DMAC to trigger when ADC0 result is ready
DMAC_CHCTRLA_TRIGACT_BURST; // DMAC burst transfer
descriptor.descaddr = (uint32_t)&descriptor_section[1]; // Set up a circular descriptor
descriptor.srcaddr = (uint32_t)&ADC0->RESULT.reg; // Take the result from the ADC0 RESULT register
descriptor.dstaddr = (uint32_t)adcResult + sizeof(uint16_t) * 2; // Place it in the adcResult array
descriptor.btcnt = 2; // Beat count is 1
descriptor.btctrl = DMAC_BTCTRL_BEATSIZE_HWORD | // Beat size is HWORD (16-bits)
DMAC_BTCTRL_DSTINC | // Increment the destination address
DMAC_BTCTRL_VALID; // Descriptor is valid
memcpy(&descriptor_section[1], &descriptor, sizeof(descriptor)); // Copy the descriptor to the descriptor section
ADC0->INPUTCTRL.bit.MUXPOS = 0x0; // Set the analog input to A0
while(ADC0->SYNCBUSY.bit.INPUTCTRL); // Wait for synchronization
ADC0->SAMPCTRL.bit.SAMPLEN = 0x00; // Set max Sampling Time Length to half divided ADC clock pulse (2.66us)
while(ADC0->SYNCBUSY.bit.SAMPCTRL); // Wait for synchronization
ADC0->DSEQCTRL.reg = ADC_DSEQCTRL_AUTOSTART | // Auto start a DMAC conversion upon ADC0 DMAC sequence completion
ADC_DSEQCTRL_INPUTCTRL; // Change the ADC0 INPUTCTRL register on DMAC sequence
ADC0->CTRLA.reg = ADC_CTRLA_PRESCALER_DIV256; // Divide Clock ADC GCLK by 256 (48MHz/256 = 187.5kHz)
ADC0->CTRLB.reg = ADC_CTRLB_RESSEL_12BIT; // Set ADC resolution to 12 bits
while(ADC0->SYNCBUSY.bit.CTRLB); // Wait for synchronization
ADC0->CTRLA.bit.ENABLE = 1; // Enable the ADC
while(ADC0->SYNCBUSY.bit.ENABLE); // Wait for synchronization
ADC0->SWTRIG.bit.START = 1; // Initiate a software trigger to start an ADC conversion
while(ADC0->SYNCBUSY.bit.SWTRIG); // Wait for synchronization
DMAC->Channel[0].CHCTRLA.bit.ENABLE = 1; // Enable DMAC Sequencing on channel 0
DMAC->Channel[1].CHCTRLA.bit.ENABLE = 1; // Enable DMAC ADC on channel1
delay(1); // Wait a millisecond
}
void setup()
{
Serial.begin(115200);
while(!Serial);
ADC_Init();
Serial.println("Begin ...");
}
void loop()
{
if (Serial.available() > 0 )
{
getGoing = Serial.read();
if (getGoing=='x')
{
readNOAnalogRead();
#if defined DEBUG
print100Samples();
#endif
Serial.write('K');
}
else if(getGoing=='z')
{
for(long i=0;i<SAMPLES_TO_READ;i++)
{
Serial.print(sampleValue[i]);
if(i<SAMPLES_TO_READ-1)
Serial.print(",");
}
}
while(Serial.available()> 0)
Serial.read();
}
}
void readNOAnalogRead()
{
timeStartReading = micros();
for(long i=0;i<SAMPLES_TO_READ;i++)
{
sampleValue[i] = adcResult[0]; // Save the A4 result
sampleValue[SAMPLES_TO_READ+i] = adcResult[1]; // Save the A5 result
delayMicroseconds(1);
}
timeEndReading = micros();
Serial.print("Samples per second (channel): "); Serial.println(1000000./(float((timeEndReading-timeStartReading))/SAMPLES_TO_READ),0);
}
#if defined DEBUG
char iStr[6];
void print100Samples()
{
Serial.println("\n i A4 A5");
for(long i=0;i<1000;i++)
{
sprintf(iStr,"%4d",i+1);
Serial.print(iStr); Serial.print(" "); Serial.print(sampleValue[i]); Serial.print(" "); Serial.println(sampleValue[SAMPLES_TO_READ+i]);
}
Serial.println("");
}
#endif