can somebody help with programing arduino due

I have this code which samples analog input 1Msps and I want to put the data into declared array instead of sending to serial port

int myArray[2000];

here is the code which samples the analog ADC input on pin A0

#undef HID_ENABLED

// Arduino Due ADC->DMA->USB 1MSPS
// by stimmer
// from http://forum.arduino.cc/index.php?topic=137635.msg1136315#msg1136315
// Input: Analog in A0
// Output: Raw stream of uint16_t in range 0-4095 on Native USB Serial/ACM

// on linux, to stop the OS cooking your data:
// stty -F /dev/ttyACM0 raw -iexten -echo -echoe -echok -echoctl -echoke -onlcr

volatile int bufn,obufn;
uint16_t buf[4][256]; // 4 buffers of 256 readings

void ADC_Handler(){ // move DMA pointers to next buffer
int f=ADC->ADC_ISR;
if (f&(1<<27)){
bufn=(bufn+1)&3;
ADC->ADC_RNPR=(uint32_t)buf[bufn];
ADC->ADC_RNCR=256;
}
}

void setup(){
SerialUSB.begin(0);
while(!SerialUSB);
pmc_enable_periph_clk(ID_ADC);
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST);
ADC->ADC_MR |=0x80; // free running

ADC->ADC_CHER=0x80;

NVIC_EnableIRQ(ADC_IRQn);
ADC->ADC_IDR=~(1<<27);
ADC->ADC_IER=1<<27;
ADC->ADC_RPR=(uint32_t)buf[0]; // DMA buffer
ADC->ADC_RCR=256;
ADC->ADC_RNPR=(uint32_t)buf[1]; // next DMA buffer
ADC->ADC_RNCR=256;
bufn=obufn=1;
ADC->ADC_PTCR=1;
ADC->ADC_CR=2;
}

void loop(){
while(obufn==bufn); // wait for buffer to be full
SerialUSB.write((uint8_t *)buf[obufn],512); // send it - 512 bytes = 256 uint16_t
obufn=(obufn+1)&3;
}
You can use memcpy in order to copy from one array to another. See:

http://www.cplusplus.com/reference/cstring/memcpy/?kw=memcpy

It will be similar to SerialUSB.write(...). The difference will be the the first parameter is the destination array. You need an offset:
1
2
3
4
5
6
7
int offs = 0;
void loop(){
while(obufn==bufn); // wait for buffer to be full
memcpy(((uint8_t *)myArray) + offs, ((uint8_t *)buf) + obufn, 512); // send it - 512 bytes = 256 uint16_t
offs += 512;
obufn=(obufn+1)&3;
}
Note that you need to stop before the array overflows.
this is what I though I would do something like writing to array

I cannot get the coding complied error after error after I try to play with the code and array,

I want to take reading every 1 us, looks like it is the sampling limit for arduino due.

by looking at the code I am not sure what data will be assigned to myArray[1], myArray[2] etc

I want to take 2000 reading in 2000 x 1 us, so in the period of 2 ms.
Topic archived. No new replies allowed.