ADC Pins
答复
19-5-23 下午3:18

 

Hello

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 票)
RE: ADC Pins
答复
19-5-24 上午6:15 回复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: https://www.eclipse.org/mita/platforms/xdk110/#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: ${adc.PD5.read()}`);
	println(`test2: ${adc.PD6.read()}`);
} 
 

 

0 (0 票)
RE: ADC Pins
答复
19-5-24 下午2:41 回复Saeid Kajlar。

Hello,

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.

Code:

/* 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 TIMER_AUTORELOAD_ON pdTRUE

#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;

    while ((ADC0->STATUS & (ADC_STATUS_SCANACT)) && (BSP_UNLOCKED == ADCLock))
            ;
        __disable_irq();
        ADCLock = BSP_LOCKED;
        __enable_irq();
        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);
        }
        __disable_irq();
        ADCLock = BSP_UNLOCKED;
        __enable_irq();
}

/**
 * @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;
    scanInit.input = ADC_SCANCTRL_INPUTMASK_CH5 | ADC_SCANCTRL_INPUTMASK_CH6;

    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);
    scanAdcInit();
}

 

0 (0 票)
RE: ADC Pins
答复
19-5-24 下午4:43 回复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. */
BSP_ExtensionPort_ConnectAdc(BSP_EXTENSIONPORT_GPIO_PD5);

/* Connect PD6 to MCU */
BSP_ExtensionPort_ConnectAdc(BSP_EXTENSIONPORT_GPIO_PD6);

/* Enable PD5 and ADC0 */
BSP_ExtensionPort_EnableAdc(BSP_EXTENSIONPORT_GPIO_PD5);

/* Enable PD6 and ADC0 */
BSP_ExtensionPort_EnableAdc(BSP_EXTENSIONPORT_GPIO_PD6);

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,

Francisco


 

0 (0 票)