By default, Channel 5 of the AFEC0 is configured as analog input and, as a result, does not require further configuration.
/*----------------------------------------------------------------------------
* Local definitions
*----------------------------------------------------------------------------*/
/** First AFE Channel used*/
#define TEST_CHANNEL 5/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void)
{
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
}“AFEC_SetExtModeReg( pAFEC, mode )” defined
in the afec.h file, configure the AFEC_MR as follows:In this example, we chose to set the tracking time to the maximum value (15). For more details, refer to the AFE Timings in the Electrical Characteristics section of the product datasheet.
/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void)
{
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
/*Step 3: AFEC Mode Register Configuration */
AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_OFF|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
}A local definition related to the AFE Clock value used can be added as follows:
/*----------------------------------------------------------------------------
* Local definitions
*----------------------------------------------------------------------------*/
/** IRQ priority for PIO (The lower the value, the greater the priority) */
#define IRQ_PRIOR_PIO 0
/** LED0 blink time, LED1 blink half this time, in ms */
#define BLINK_PERIOD 1000
/** First AFE Channel used*/
#define TEST_CHANNEL 5
/* AFE Clock Value */
#define AFE_CLK 2200000/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void)
{
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
/*Step 3: AFEC Mode Register Configuration */
AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
/* Step 4 Set AFEC clock AFEC_SetClock() */
AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
}/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void) {
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
/*Step 3: AFEC Mode Register Configuration */
AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
/* Step 4 Set AFEC clock AFEC_SetClock() */
AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
/* Step 5 Select the channel using the Macro definition AFEC_EnableChannel(). */
AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
}An offset value set in AFEC_COCR.AOFF defines the analog offset to be used for channel CSEL (configured in the AFEC_CSELR). This value is used as an input value for the DAC included in the AFE.
A local definition related to the CHANNEL OFFSET value used can be added as follows:
/*----------------------------------------------------------------------------
* Local definitions
*----------------------------------------------------------------------------*/
/** IRQ priority for PIO (The lower the value, the greater the priority) */
#define IRQ_PRIOR_PIO 0
/** LED0 blink time, LED1 blink half this time, in ms */
#define BLINK_PERIOD 1000
/** First AFE Channel used*/
#define TEST_CHANNEL 5
/** AFE Channel DAC Offset */
#define CHANNEL_OFFSET 0x200/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void)
{
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
/*Step 3: AFEC Mode Register Configuration */
AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
/* Step 4 Set AFEC clock AFEC_SetClock() */
AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
/* Step 5 Select the active channel using the Macro definition AFEC_EnableChannel(). */
AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
/* Step 6 Adjust the channel level offset */
AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, CHANNEL_OFFSET);
}/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void)
{
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
/*Step 3: AFEC Mode Register Configuration */
AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
/* Step 4 Set AFEC clock AFEC_SetClock() */
AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
/* Step 5 Select the active channel using the Macro definition AFEC_Enable Channel(). */
AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
/* Step 6 Adjust the channel level offset */
AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, CHANNEL_OFFSET);
/* Step 7 Enable the PGA 0 and 1 of the AFEC0 and adjust the performances by tuning the AFE Bias Current Control */
AFEC_SetAnalogControl(AFEC0, AFEC_ACR_IBCTL(1) | AFEC_ACR_PGA0_ON | AFEC_ACR_PGA1_ON );
}/**
* \brief Initialize AFE.
*
*/
static void _afe_initialization(void) {
/* Step 1: Configure the pins for AFEC. (If not already done by default) */
/* Step 2: AFEC init */
AFEC_Initialize( AFEC0, ID_AFEC0 );
/*Step 3: AFEC Mode Register Configuration */
AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
/* Step 4 Set AFEC clock AFEC_SetClock() */
AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
/* Step 5 Select the active channel using the Macro definition AFEC_EnableChannel(). */
AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
/* Step 6 Adjust the channel level offset */
AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, CHANNEL_OFFSET);
/* Step 7 Enable the PGA 0 and 1 of the AFEC0 and adjust the performances by tuning the AFE Bias Current Control */
AFEC_SetAnalogControl(AFEC0, AFEC_ACR_IBCTL(1) | AFEC_ACR_PGA0_ON | AFEC_ACR_PGA1_ON );
/* Step 8 Configure the Extended Mode Register by disabling the Averaging, enabling the channel tag and using the Single Trigger Mode */
AFEC_SetExtModeReg(AFEC0,0| AFEC_EMR_RES(256)| AFEC_EMR_TAG | AFEC_EMR_STM );
}At this point, the configuration of the AFEC is finished. The next steps in the sequence start a conversion and wait for the End Of Conversion flag. This is implemented directly inside the main function as described below.
/* Step 9: Start Conversion by software trigger */
AFEC_StartConversion(AFEC0);/* Step 10: Wait for the end of the conversion by polling status with AFEC_GetStatus() */
while (!(AFEC_GetStatus(AFEC0) & AFEC_ISR_EOC5));/* Step 11: Finally, get the converted data using AFEC_GetConvertedData() or AFEC_GetLastConvertedData() */
printf("\n\rCH Voltage(mV) \n\r");
ch = (AFEC_GetLastConvertedData(AFEC0) & AFEC_LCDR_CHNB_Msk ) >> AFEC_LCDR_CHNB_Pos;
voltage = ((AFEC_GetLastConvertedData(AFEC0) & AFEC_LCDR_LDATA_Msk)) * 3254/ 4096;
printf("%02u %04u\n\r" ,(unsigned int)ch,(unsigned int)voltage)