Using ADC to interrupt and write to memory buffer

09 Aug 2011

I'm trying to use the example2.h code from the MODDMA library to sample an ADC at 100Hz for 15 sec. After the first sample, I need to progress to the next on the buffer and delete the previous one. My code is below, and the interrupts work, the sampling works, but I can't progress through the buffer for the next sample. I think it's either a logic error, or a command like nextLLI.

    while (1)  {
        if (dmaTransferComplete || counter>0) {

            int i, value, channel;

            if (counter ==1500) {
                int write = 0;
                led1 = 1;
                d = opendir("/sd/data"); //open the directory for data storage. If non existant, create

                FILE *fp = fopen("/sd/Data/triax.txt", "a");//confirm can access text file for saving. If not there, create
                if (fp == NULL) {
                    error("Could not open file for write\n");
                }

                while (write<1500) {
                    write++;
                    for (i = 0; i < 3; i++) {

                        value = (adBuffer[i] >> 4) & 0xFFF;
                        channel = (adBuffer[i] >> 24) & 0x3;
                        channel--;
                        if (channel == -1) channel = 2; // Workaround ch num problem.
                        double fVal = 3.3 * (double)((double)value) / ((double)0x1000); // scale to 0v to 3.3v
         
                        if (channel != 2) {
                            fprintf(fp,"%01.3f\t", fVal);
                            fflush(stdout);
                        }
                        if (channel == 2) {
                            fprintf(fp,"%01.3f\n", fVal);
                            fflush(stdout);
                        }

                        counter--;
                        c++;

                    }
                }
            }
            fclose(fp);//close file and stop accessing SD card
            led1 = 0;
        }
        dmaTransferComplete = false;
    }
13 Aug 2011

Change the counter == 1500 to counter >= 1500.

Counter is incremented externally and so the value can be greater than.

15 Aug 2011

Andrew Harpin wrote:

Change the counter == 1500 to counter >= 1500.

Counter is incremented externally and so the value can be greater than.

Thanks Andrew, I missed that one. However I can get into the while loop, but the buffer doesn't progress to the next sample for 5 loops. I think it's because I'm not actually popping, but not 100% sure on how to do it.