Page 1 of 1

ADC works for buffer =1, but not more.

Posted: Thu Jan 01, 2015 3:55 pm
by rew
As a side note: Lots of "simple" applications need to "occasionally" read an analog value. The "fancy" features of the ADC with DMA autotriggering etc etc. is all not necessary, just a "what is the voltage on the pin now" function would suffice.

I've written such a function (or copied it from somewhere...)

Code: Select all

// This is STM32F0xx specific!
int read_adc_naiive(uint8_t channel)
{
  int i, t;
#define NN 1 // NN != 1 doesn't work (yet?)
  adcsample_t samples[NN];
  ADCConversionGroup grpp;

  grpp.circular = FALSE;
  grpp.num_channels = 1;
  grpp.end_cb = NULL;
  grpp.error_cb = adcerrorcallback;
  grpp.cfgr1 =  ADC_CFGR1_RES_12BIT;
  grpp.tr = ADC_TR(0, 0);
  grpp.smpr = ADC_SMPR_SMP_28P5;
  grpp.chselr = 1 << channel;

  adcSTM32SetCCR( ADC_CCR_TSEN | ADC_CCR_VREFEN);

  adcConvert (&ADCD1, &grpp, samples, NN);

  t = 0;
  for (i=0;i<NN;i++)
    t += samples[i];

  return t;
}

Why does this work for NN=1, but not for other values? What am I doing wrong? Do I need to set up a timer to trigger the ADC conversions? (As you can see from the comment: I'm currently using an STM32F030... The '072s and the new PCBs should arrive tomorrow)

P.S. In main I have a simple:

Code: Select all

  adcStart(&ADCD1, NULL);

PPS: static initialization of most of grpp would be more efficient, but this way I now have easy control over all the fields.

Re: ADC works for buffer =1, but not more.

Posted: Thu Jan 01, 2015 4:55 pm
by Giovanni
Hi,

Probably this:

Code: Select all

  grpp.num_channels = 1;

Giovanni

Re: ADC works for buffer =1, but not more.

Posted: Thu Jan 01, 2015 5:06 pm
by rew
The "num channels" is the number of different channels, right?

If I wanted to sample 12 each of, AIN2 and AIN5 (i.e. twenty four total ADC conversions), I'd set num channels to TWO, the last argument of adcConvert to 12, and chselr to (1 << 2) | ( 1 << 5), right?

Re: ADC works for buffer =1, but not more.

Posted: Thu Jan 01, 2015 5:42 pm
by Giovanni
num_channels is the number of channels in conversion group, the parameter of adcConvert() is the number of times the channel group must be converted.

Giovanni

Re: ADC works for buffer =1, but not more.

Posted: Thu Jan 01, 2015 6:12 pm
by rew
Yes, so for my "simple" conversion, I want a conversion group with just one channel, but convert that single channel multiple times. So I pass the "NN" to the adcConvert routine.

As I understand things, this routine will simply software-trigger the ADC. But is there an arrangement for the "second" trigger after the first conversion? (IIRC the hardware will run a conversion group if you software tirgger it, and then wait for a second "trigger" before it does the second run of the conversion group. In this case that should be a new software trigger).

Re: ADC works for buffer =1, but not more.

Posted: Thu Jan 01, 2015 8:00 pm
by Giovanni
The driver puts the ADC in continuous mode so there are no multiple triggers, it is started once.

Giovanni

Re: ADC works for buffer =1, but not more.

Posted: Fri Jan 02, 2015 9:45 am
by rew
I didn't know that existed, I checked the datasheet and it does.... I'll see if I can program a register dump for the ADC module.