6 years, 1 month ago.

Questions about using external adc for sampling.

I am using ad7606 to sampling audio. The chips itself can achieve a 200kHz sampling rate and spi is used for transmitting data to mbed. I have several questions in my sampling program.

  • How to increase my sampling rate

Although I used a pwm wave of 50000Hz to control the ad7606, the actual sampling rate is about 45000Hz. Then I find that it is because of this code:

void record(){
 
          cs = 0;
        sample1[wp] = spi.write(0x0000);
        sample2[wp] = spi.write(0x0000);
        sample3[wp] = spi.write(0x0000);
        sample4[wp] = spi.write(0x0000);
        cs = 1;
        wp++;

 }

This function will be called every time when the adc conversion is done. And time for running this function is about 21ns, which limit sampling rate to about 45000Hz. I wonder how can I decrease the running time. Is DMA is usable for this program?

  • Is it wrong to use interruptin like this?

title

int main() {
    spi.format(16,2);
    spi.frequency(50000000);
    convst.period(1.0f); 
    convst.write(1.0f);
    AD7606_Reset();

    convst.period_us(INTERAL_US);
    convst.write(0.5f);
  
   while((wp < BUF_LEN)){
        button.fall(&record); 
   }
 
    convst.write(1.0f);   

}

Every time the conversion is finished, the busy signal from chips will falling from 1 to 0. Then button will call the "record " function(the one mention before). If I move the "button.fall(&record);" out from while loop(down below "convst.write(0.5f)"), it seems that no sample is recorded.

1 Answer

6 years, 1 month ago.

If wp defined as volatile?

If it's not volatile then moving the button.fall out of the loop leaves an empty loop. With nothing to force the compiler to re-check memory for the current value of wp it won't see the number change. Making it volatile forces the compiler to re-check the value stored in memory every time. The reason you can get away without it being volatile with the button.fall in the loop is that the function is complicated enough that wp is no longer being held in a register and so the compiler is checking memory anyway.

Make wp volatile and put the button.fall before the while loop and you should get a speed up. You may also need to add a safety check in the record function to prevent overrunning the buffer if you get one more reading before the PWM shuts off.

One other thing to look at for extra speed, the mbed libraries are designed to be able to cope with different devices using different speeds and formats on the same bus, if you only have one device you don't need this ability. There is a library called BurstSPI, it only supports certain mbed boards but removes these checks and so speeds up SPI transfers if you can use it.