ADC Pins
5/23/19 3:18 PM



Does anyone have any code to read the values ​​of the ADC PD6 and PD5 pins for version 3.6.0 of the XDK-Workbech?

0 (0 Votes)
RE: ADC Pins
5/24/19 6:15 AM as a reply to Andres Rivera.

Hello Andres,

You can use Mita for that. In the example below, once a second, both pins are read out and output via usb. Here is a little introduction for the ADC:

package main;
import platforms.xdk110;

setup adc: ADC{
	var PD5 = channel(CH5,Ref_1V25,Resolution_12Bit,Time_128);
	var PD6 = channel(CH6,Ref_1V25,Resolution_12Bit,Time_128);
every 1 second {
	println(`test1: ${}`);
	println(`test2: ${}`);


0 (0 Votes)
RE: ADC Pins
5/24/19 2:41 PM as a reply to Saeid Kajlar.


I was working with the following code in version 3.0.1 of the XDK-Workspace but as soon as I updated to version 3.6.0, the code does not work for me.


/* additional interface header files */
#include "BCDS_Basics.h"
#include "BCDS_Assert.h"
#include "BCDS_TaskConfig.h"
#include "BCDS_CmdProcessor.h"
#include "FreeRTOS.h"
#include "timers.h"

#include "em_cmu.h"
#include "em_adc.h"
#include "em_gpio.h"
#include "BSP_BoardShared.h"

#include "XdkApplicationTemplate.h"

/* system header files */
#include <stdint.h>
#include <stdio.h>

/*local variables */
xTimerHandle scanAdcTimerHandle;

/*constant definitions */

#define ONESECONDDELAY      UINT32_C(1000)       /* one second is represented by this macro */
#define TIMERBLOCKTIME      UINT32_C(0xffff)     /* Macro used to define blocktime of a timer */
#define TIMEBASE            UINT32_C(0)          /* Macro used to define the Timebase for the ADC */
#define ADCFREQUENCY        UINT32_C(9000000)    /* Macro Used to define the frequency of the ADC Clock */

static void scanAdc(xTimerHandle pxTimer)
    /* Initialize the Variables */
    (void) (pxTimer);
    uint32_t _adc0ChData = 0;

        ADCLock = BSP_LOCKED;
        ADC_Start(ADC0, adcStartScan);
        for (int channels = 0; channels < 2; channels++) {
            // Wait for Valid Data
            while (!(ADC0->STATUS & ADC_STATUS_SCANDV))
            // Read the Scanned data
               _adc0ChData = 0xFFF & ADC_DataScanGet(ADC0);
                printf("ADC Channel %ld = %ld\r\n", ((ADC0->STATUS & _ADC_STATUS_SCANDATASRC_MASK) >> _ADC_STATUS_SCANDATASRC_SHIFT), _adc0ChData);
        ADCLock = BSP_UNLOCKED;

 * @brief Configure the ADC to scan multiple channels;
 *        Initialize and Start a timer to scan the ADC
static void scanAdcInit(void)
    //ADC_Init_TypeDef     adc0_init_conf     = ADC_INIT_DEFAULT;
    ADC_InitScan_TypeDef scanInit = ADC_INITSCAN_DEFAULT;

    /* Configure ADC pins as input */
    GPIO_PinModeSet(gpioPortD, 5, gpioModeInputPull, 0);
    GPIO_PinOutClear(gpioPortD, 5);
    GPIO_PinModeSet(gpioPortD, 6, gpioModeInputPull, 0);
    GPIO_PinOutClear(gpioPortD, 6);

    /* Configure the ADC Initialization Structure */
    //ADC_Init(ADC0, &adc0_init_conf);

    /* Configure the ADC Scan Structure */
    scanInit.reference = adcRef2V5;
    scanInit.resolution = adcRes12Bit;

    ADC_InitScan(ADC0, &scanInit);

    /* Setup and Start the timer to scan the ADC Channels */
    scanAdcTimerHandle = xTimerCreate(
        (const char *) "ADC read", ONESECONDDELAY,
        TIMER_AUTORELOAD_ON, NULL, scanAdc);

    xTimerStart(scanAdcTimerHandle, TIMERBLOCKTIME);

/* global functions ********************************************************* */

 * @brief This is a template function where the user can write his custom application.
void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
    (void) ( CmdProcessorHandle);
    (void) (param2);


0 (0 Votes)
RE: ADC Pins
5/24/19 4:43 PM as a reply to Andres Rivera.

Hi Andrés,

I am sorry, but due to further development, newer versions of the XDK platform software may break older code.

Connecting and configuring ADC Pins:
Use the _ConnectAdc and _EnableAdc APIs provided in BSP_ExtensionPort.h:

#include "BSP_ExtensionPort.h"

/* ... */

/* Connect PD5 to MCU. */

/* Connect PD6 to MCU */

/* Enable PD5 and ADC0 */

/* Enable PD6 and ADC0 */

Once this is done, you can access the ADC API under common/Source/Mcu_Adc.h and BSP_Adc.c. These APIs are showcased in the VirtualXdkDemo to access an external LEM current sensor, which also used PD5 and PD6 as ADC inputs

However you can still use APIs coming from the EMLIB like em_adc.h

Pura Vida,



0 (0 Votes)