any example of adc

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
ganeshredcobra
Posts: 10
Joined: Wed Jul 25, 2012 8:40 pm

any example of adc

Post by ganeshredcobra »

Hi,

Can i get any example regarding the working of adc...the documentation is not that much good for a beginner its pretty hard to understand...and i couldnt find any sample codes...usually in pic i do adc like this
void main()
{
int x;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_us(20);
while(True)
{
x=read_adc();
output_B(x) ;
delay_us(20);
}
}

can i get an example like this...
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: any example of adc

Post by Giovanni »

Hi,

I agree that the ADC driver is a bit complex, under ./testhal there are ADC test applications (and for all other device drivers), try there.

Giovanni
ganeshredcobra
Posts: 10
Joined: Wed Jul 25, 2012 8:40 pm

Re: any example of adc

Post by ganeshredcobra »

can you give the code of a simple example ie if vary input using a pot then i must get digital out can you post code for that so that i can understand the example on testhal is also a complex didnt even understand the working of it...
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: any example of adc

Post by Giovanni »

Hi,

Sorry, it does not get much simpler that this because it is not meant for beginners:

The example reads IN11 eight times in a buffer.

Code: Select all

#define ADC_GRP1_NUM_CHANNELS   1
#define ADC_GRP1_BUF_DEPTH      8

static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];

/*
 *
 * ADC conversion group.
 * Mode:        Linear buffer, 8 samples of 1 channel, SW triggered.
 * Channels:    IN11.
 */
static const ADCConversionGroup adcgrpcfg1 = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  NULL,
  NULL,
  0,                        /* CR1 */
  ADC_CR2_SWSTART,          /* CR2 */
  ADC_SMPR1_SMP_AN11(ADC_SAMPLE_3),
  0,                        /* SMPR2 */
  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
  0,                        /* SQR2 */
  ADC_SQR3_SQ1_N(ADC_CHANNEL_IN11)
};

...

  /*
   * Activates the ADC1 driver.
   */
  adcStart(&ADCD1, NULL);

  /*
   * Linear conversion.
   */
  adcConvert(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);



Taken from the example in testhal. You have to undestrand the ADCConversionGroup structure, it is all defined there. Reading the reference manual helps.

Giovanni
Post Reply