Passing ADC Data

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
daviddawe1982
Posts: 94
Joined: Thu Apr 11, 2013 10:35 am

Passing ADC Data

Post by daviddawe1982 »

hello,
I am trying to measure voltage on pc1 and pass the reading on to my display but all i get displayed is what appear to be random characters i am using a CONST INT to pass the data threw but i may be passing the incorrect data could some one have a look at my code and tell me there i am going wrong..Please

Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ch.h"
#include "hal.h"
#include "gfx.h"
#include "dawetecsml.h"
static gdispImage myImage;
const int   *msg;



static void pwmpcb(PWMDriver *pwmp);
static void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n);


/* Total number of channels to be sampled by a single ADC operation.*/
#define ADC_GRP1_NUM_CHANNELS   2

/* Depth of the conversion buffer, channels are sampled four times each.*/
#define ADC_GRP1_BUF_DEPTH      4

/*
 * ADC samples buffer.
 */
static adcsample_t samples[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];

/*
 * ADC conversion group.
 * Mode:        Linear buffer, 4 samples of 2 channels, SW triggered.
 * Channels:    IN11   (48 cycles sample time)
 *              Sensor (192 cycles sample time)
 */
static const ADCConversionGroup adcgrpcfg = {
  FALSE,
  ADC_GRP1_NUM_CHANNELS,
  adccb,
  NULL,
  /* HW dependent part.*/
  0,
  ADC_CR2_SWSTART,
  ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) | ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_144),
  0,
  ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
  0,
  ADC_SQR3_SQ2_N(ADC_CHANNEL_IN11) | ADC_SQR3_SQ1_N(ADC_CHANNEL_SENSOR)
};

/*
 * PWM configuration structure.
 * Cyclic callback enabled, channels 1 and 4 enabled without callbacks,
 * the active state is a logic one.
 */
static PWMConfig pwmcfg = {
  10000,                                    /* 10KHz PWM clock frequency.   */
  10000,                                    /* PWM period 1S (in ticks).    */
  pwmpcb,
  {
    {PWM_OUTPUT_ACTIVE_HIGH, NULL},
    {PWM_OUTPUT_DISABLED, NULL},
    {PWM_OUTPUT_DISABLED, NULL},
    {PWM_OUTPUT_ACTIVE_HIGH, NULL}
  },
  /* HW dependent part.*/
  0
};



/*
 * PWM cyclic callback.
 * A new ADC conversion is started.
 */
static void pwmpcb(PWMDriver *pwmp) {

  (void)pwmp;

  /* Starts an asynchronous ADC conversion operation, the conversion
     will be executed in parallel to the current PWM cycle and will
     terminate before the next PWM cycle.*/
  chSysLockFromIsr();
  adcStartConversionI(&ADCD1, &adcgrpcfg, samples, ADC_GRP1_BUF_DEPTH);
  chSysUnlockFromIsr();
}

/*
 * ADC end conversion callback.
 * The PWM channels are reprogrammed using the latest ADC samples.
 * The latest samples are transmitted into a single SPI transaction.
 */
void adccb(ADCDriver *adcp, adcsample_t *buffer, size_t n) {

  (void) buffer; (void) n;
  /* Note, only in the ADC_COMPLETE state because the ADC driver fires an
     intermediate callback when the buffer is half full.*/
  if (adcp->state == ADC_COMPLETE) {
    adcsample_t avg_ch1, avg_ch2;

    /* Calculates the average values from the ADC samples.*/
    avg_ch1 = (samples[0] + samples[2] + samples[4] + samples[6]) / 4;
    avg_ch2 = (samples[1] + samples[3] + samples[5] + samples[7]) / 4;

    chSysLockFromIsr();
    msg = avg_ch1;

    /* Changes the channels pulse width, the change will be effective
       starting from the next cycle.*/
    pwmEnableChannelI(&PWMD4, 0, PWM_FRACTION_TO_WIDTH(&PWMD4, 4096, avg_ch1));
    pwmEnableChannelI(&PWMD4, 3, PWM_FRACTION_TO_WIDTH(&PWMD4, 4096, avg_ch2));

    /* SPI slave selection and transmission start.*/


    chSysUnlockFromIsr();
  }
}


/*
 * Application entry point.
 */
int main(void) {
  halInit();
  chSysInit();

  //font Setup

  font_t font1 = gdispOpenFont("Larger");
  font_t font2 = gdispOpenFont("UI1");

  //reset lcd

  palClearPad(GPIOC, GPIOC_PIN6);
  chThdSleepMilliseconds(100);
  palSetPad(GPIOC, GPIOC_PIN6);

  //SET PINS

  palSetPadMode(GPIOD, 13, PAL_STM32_PUDR_PULLDOWN);

  /* Initialize and clear the display */http://www.google.com.au/#output=search&sclient=psy-ab&q=send+data+to+main+from+void&oq=send+data+to+main+from+void&gs_l=hp.3...1408.8700.1.8855.27.26.0.1.1.0.351.6357.2-23j2.25.0...0.0...1c.1.12.hp.8UokiyYaJkU&psj=1&bav=on.2,or.r_qf.&bvm=bv.45960087,d.dGI&fp=f4974ed63c241413&biw=1027&bih=423
  gdispInit();
  gdispClear(Black);

  // Splash Screen

  gdispImageSetMemoryReader(&myImage, dawetecsml);
  gdispImageOpen(&myImage);
  gdispImageDraw(&myImage, 120, 150, myImage.width, myImage.height, 0, 0);
  gdispImageClose(&myImage);
  chThdSleepMilliseconds(5000);
  gdispClear(Black);

  //main menu

  gdispDrawString(10, 1, "DaweTec Titan200", font1, White);
  gdispDrawString(10, 470, "Version 1.1", font1, White);
  gdispDrawString(680, 1, "Battery Level 100%", font1 , White);
  gdispDrawString(10, 235, "Detection Level:",font2 , White);

  /*
   * Initializes the ADC driver 1 and enable the thermal sensor.
   * The pin PC0 on the port GPIOC is programmed as analog input.
   */
  adcStart(&ADCD1, NULL);
  palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG);

  /*
   * Initializes the PWM driver 4, routes the TIM4 outputs to the board LEDs.
   */
  pwmStart(&PWMD4, &pwmcfg);
  //PLUSE START

  while ( TRUE ) {

          int gpioState = palReadPad(GPIOA, GPIOA_BUTTON);

           if (!gpioState) {
              gdispDrawBox(10, 150, 400, 10, Black);
              gdispFillArea(10, 150, 400, 10, Black);
             gdispDrawString(10, 150, msg,font2, White);
              gdispDrawBox(10, 220, 90, 10, Black);
              gdispFillArea(10, 220, 90, 10, Black);
             gdispDrawString(10, 220, "Detection OFF",font2 , White);
             chThdSleepMilliseconds(500);

           }
           else if ( gpioState ) {

               gdispDrawBox(10, 220, 90, 10, Black);
               gdispFillArea(10, 220, 90, 10, Black);
               gdispDrawString(10, 220, "Detection ON",font2 , White);
               palSetPad(GPIOD,13);
               chThdSleepMilliseconds(2000);
               palClearPad(GPIOD,13);
               chThdSleepMilliseconds(2000);
           }

         }
  while(TRUE) {
                 chThdSleepMilliseconds(500);
             }

}

User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Passing ADC Data

Post by Giovanni »

Hi,

You kept into the code the whole Discovery demo, probably you should start removing all things you don't need. You could also try starting from a less complex demo, try ./testhal/STM32F4xx/ADC, a much simpler way to use the ADC is shown there.

Giovanni
daviddawe1982
Posts: 94
Joined: Thu Apr 11, 2013 10:35 am

Re: Passing ADC Data

Post by daviddawe1982 »

I have dropped back to the simple haltest ADC and have added it to my code but i still cannot get a value to display i have tried to write sample 1 and 2 but no result . And i have connected the pins as per the example read me

Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ch.h"
#include "hal.h"
#include "gfx.h"
#include "dawetecsml.h"
static gdispImage myImage;
const int   *msg;

#define ADC_GRP1_NUM_CHANNELS   1
#define ADC_GRP1_BUF_DEPTH      8

#define ADC_GRP2_NUM_CHANNELS   8
#define ADC_GRP2_BUF_DEPTH      16

static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
static adcsample_t samples2[ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH];

/*
 * ADC streaming callback.
 */
size_t nx = 0, ny = 0;
static void adccallback(ADCDriver *adcp, adcsample_t *buffer, size_t n) {

  (void)adcp;
  if (samples2 == buffer) {
    nx += n;
  }
  else {
    ny += n;
  }
}

static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {

  (void)adcp;
  (void)err;
}

/*
 * 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,
  adcerrorcallback,
  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)
};

/*
 * ADC conversion group.
 * Mode:        Continuous, 16 samples of 8 channels, SW triggered.
 * Channels:    IN11, IN12, IN11, IN12, IN11, IN12, Sensor, VRef.
 */
static const ADCConversionGroup adcgrpcfg2 = {
  TRUE,
  ADC_GRP2_NUM_CHANNELS,
  adccallback,
  adcerrorcallback,
  0,                        /* CR1 */
  ADC_CR2_SWSTART,          /* CR2 */
  ADC_SMPR1_SMP_AN12(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) |
  ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_144) | ADC_SMPR1_SMP_VREF(ADC_SAMPLE_144),
  0,                        /* SMPR2 */
  ADC_SQR1_NUM_CH(ADC_GRP2_NUM_CHANNELS),
  ADC_SQR2_SQ8_N(ADC_CHANNEL_SENSOR) | ADC_SQR2_SQ7_N(ADC_CHANNEL_VREFINT),
  ADC_SQR3_SQ6_N(ADC_CHANNEL_IN12)   | ADC_SQR3_SQ5_N(ADC_CHANNEL_IN11) |
  ADC_SQR3_SQ4_N(ADC_CHANNEL_IN12)   | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN11) |
  ADC_SQR3_SQ2_N(ADC_CHANNEL_IN12)   | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN11)
};

/*
 * Red LEDs blinker thread, times are in milliseconds.
 */
static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (TRUE) {
    palSetPad(GPIOD, GPIOD_LED3);
    chThdSleepMilliseconds(500);
    palClearPad(GPIOD, GPIOD_LED3);
    chThdSleepMilliseconds(500);
  }
}

/*
 * Application entry point.
 */
int main(void) {
  halInit();
  chSysInit();

  //font Setup

  font_t font1 = gdispOpenFont("Larger");
  font_t font2 = gdispOpenFont("UI1");

  //reset lcd

  palClearPad(GPIOC, GPIOC_PIN6);
  chThdSleepMilliseconds(100);
  palSetPad(GPIOC, GPIOC_PIN6);

  //SET PINS

  //palSetPadMode(GPIOD, 13, PAL_STM32_PUDR_PULLDOWN);

  /* Initialize and clear the display */
  gdispInit();
  gdispClear(Black);
  /*
       * Setting up analog inputs used by the demo.
       */
      palSetGroupMode(GPIOC, PAL_PORT_BIT(1) | PAL_PORT_BIT(2),
                      0, PAL_MODE_INPUT_ANALOG);

      /*
       * Creates the blinker thread.
       */
      chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

      /*
       * Activates the ADC1 driver and the thermal sensor.
       */
      adcStart(&ADCD1, NULL);
      adcSTM32EnableTSVREFE();

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

      /*
       * Starts an ADC continuous conversion.
       */
      adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);

  // Splash Screen

  gdispImageSetMemoryReader(&myImage, dawetecsml);
  gdispImageOpen(&myImage);
  gdispImageDraw(&myImage, 120, 150, myImage.width, myImage.height, 0, 0);
  gdispImageClose(&myImage);
  chThdSleepMilliseconds(5000);
  gdispClear(Black);

  //main menu

  gdispDrawString(10, 1, "DaweTec Titan200", font1, White);
  gdispDrawString(10, 470, "Version 1.1", font1, White);
  gdispDrawString(680, 1, "Battery Level 100%", font1 , White);
  gdispDrawString(10, 235, "Detection Level:",font2 , White);


  //PLUSE START

  while ( TRUE ) {

          int gpioState = palReadPad(GPIOA, GPIOA_BUTTON);

           if (!gpioState) {
              gdispDrawBox(10, 150, 400, 10, Black);
              gdispFillArea(10, 150, 400, 10, Black);
             gdispDrawString(10, 150, samples1, font1, White);
              gdispDrawBox(10, 220, 90, 10, Black);
              gdispFillArea(10, 220, 90, 10, Black);
             gdispDrawString(10, 220, "Detection OFF",font2 , White);
             chThdSleepMilliseconds(500);

           }
           else if ( gpioState ) {

               gdispDrawBox(10, 220, 90, 10, Black);
               gdispFillArea(10, 220, 90, 10, Black);
               gdispDrawString(10, 220, "Detection ON",font2 , White);
               palSetPad(GPIOD,13);
               chThdSleepMilliseconds(2000);
               palClearPad(GPIOD,13);
               chThdSleepMilliseconds(2000);
           }

         }
  while(TRUE) {
                 chThdSleepMilliseconds(500);
             }

}

User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Passing ADC Data

Post by Giovanni »

Hi,

Here: gdispDrawString(10, 150, samples1, font1, White);

"samples1" is not a string.

Giovanni
Post Reply