Page 1 of 1

Simple ADC configuration

Posted: Sun Nov 25, 2012 6:46 pm
by Tectu
Hello folks,

I've a four wire resistive touchscreen foil connected to GPIOC 0 to 3 (ADC channel 10 to 14) on an STM32F103.
I have it working using the normal stm standard lib without ChibiOS/RT. Here's how it works to read out both coordinates:

Code: Select all

/*
 * YD=0 YU=1, measure XL and XR
 * YD=1 YU=0, measure XL and XR
 * The average of the previous four samples is the X value.
 *
 * XL=0 XR=1, measure YD and YU
 * XL=1 XR=0, measure YD and YU
 * The average of the previous four samples is the Y value.
 *
 * A total of 8 ADC measurements are needed!
 */

u32 val1, val2, y_coordinate, x_coordinate

GPIO_SetAnalog(TS_PIN_XL | TS_PIN_XR);
GPIO_SetOutput(TS_PIN_YD, 0);
GPIO_SetOutput(TS_PIN_YU, 1);
delay(100);
val1 = (ADC_Measure(TS_ADCCH_XL) + ADC_Measure(TS_ADCCH_XR))/2;

GPIO_SetOutput(TS_PIN_YD, 1);
GPIO_SetOutput(TS_PIN_YU, 0);
delay(100);
val2 = (ADC_Measure(TS_ADCCH_XL) + ADC_Measure(TS_ADCCH_XR))/2;

y_coordinate = (val1+((1<<12)-val2))/4;

GPIO_SetAnalog(TS_PIN_YD | TS_PIN_YU);
GPIO_SetOutput(TS_PIN_XL, 0);
GPIO_SetOutput(TS_PIN_XR, 1);
delay(100);
val1 = (ADC_Measure(TS_ADCCH_YD) + ADC_Measure(TS_ADCCH_YU))/2;

GPIO_SetOutput(TS_PIN_XL, 1);
GPIO_SetOutput(TS_PIN_XR, 0);
delay(100);
val2 = (ADC_Measure(TS_ADCCH_YD) + ADC_Measure(TS_ADCCH_YU))/2;

y_coordinate = (val1+((1<<12)-val2))/4;


What I would like to do now is doing the same thing but using the ADC driver of ChibiOS/RT. The problem is that I kinda fail at figuring out how to make only one measurement, change the two GPIO states and make another measurement in another buffer. Then, do the same but with the two other ADC pins. Can anyone help here?

Thanks in advance.


~ Tectu

Re: Simple ADC configuration

Posted: Sun Nov 25, 2012 6:59 pm
by Giovanni
Hi,

You can find examples under ./testhal. In you case you need to create two conversion groups like this:

Code: Select all

#define ADC_GRP1_NUM_CHANNELS   1

/*
 * ADC conversion group.
 * Mode:        Linear buffer, 8 samples of 1 channel, SW triggered.
 * Channels:    IN10.
 */
static const ADCConversionGroup adcgrpcfg1 = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  NULL,
  adcerrorcallback,
  0, 0,                         /* CR1, CR2 */
  ADC_SMPR1_SMP_AN10(ADC_SAMPLE_1P5),
  0,                            /* SMPR2 */
  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
  0,                            /* SQR2 */
  ADC_SQR3_SQ1_N(ADC_CHANNEL_IN10)
};


The use adcConvert() in order to make conversions.

Giovanni

Re: Simple ADC configuration

Posted: Sun Nov 25, 2012 7:29 pm
by Tectu
Aaah! The trick is it to use the two configs :D
I saw the example in ./testhal and I also saw the two config groups, but I wasn't sure what's exactly the meaning of them.

Thank you very much for your help, Giovanni!


~ Tectu