Files
microchip-application-pec93…/Examples/pga/pga_src_internal/src/main.c
T

189 lines
6.0 KiB
C

/**
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file main.c
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2024/08/29
* @license
* @description
*/
#include "main.h"
#include "isr.h"
//=============================================================================
// Constant Definition
//=============================================================================
/**
* The reference mVoltage of ADC on board
*/
#define CONFIG_ADC_REF_mVOLTAGE 3300 // 5000
/**
* The amplify gain, @ref OPAMP_PGAGainTypeDef
* ps. OPAMP_PGAGain_1 ~ OPAMP_PGAGain_16
*/
#define CONFIG_AMP_GAIN OPAMP_PGAGain_1
#define OPAMPx OPAMP0
#define CONFIG_ADC_TARGET_CHANNEL ADC_Channel_OPA0_O
typedef enum internal_src
{
INTERNAL_SRC_TEMPERATURE,
INTERNAL_SRC_DAC0,
INTERNAL_SRC_DAC1,
INTERNAL_SRC_VBUF_1P5V,
INTERNAL_SRC_VDDL,
INTERNAL_SRC_VSS,
INTERNAL_SRC_VDD,
} internal_src_t;
//=============================================================================
// Macro Definition
//=============================================================================
//=============================================================================
// Structure Definition
//=============================================================================
//=============================================================================
// Global Data Definition
//=============================================================================
static AMISC_InternalSrcTypeDef g_VinP_Src = AMISC_InternalSrc_NONE;
static AMISC_InternalSrcTypeDef g_InternalSrc[] =
{
AMISC_InternalSrc_TEMP,
AMISC_InternalSrc_DAC0,
AMISC_InternalSrc_DAC1,
AMISC_InternalSrc_VBuf_1P5V,
AMISC_InternalSrc_VDDL,
AMISC_InternalSrc_VSS,
AMISC_InternalSrc_VDD,
};
//=============================================================================
// Private Function Definition
//=============================================================================
//=============================================================================
// Public Function Definition
//=============================================================================
int main(void)
{
{ /* Configure system clock */
SYSCFG_ClkInitTypeDef SysClkInit = {0};
SysClkInit.ClkSource = SYSCFG_ClkSrc_HSI;
SYSCFG_SysClkConfig(&SysClkInit);
}
sys_config_systick(SYS_TICK_1_MS);
syslog_init();
info("%s %s\nRun PGA-%d (Programmable Gain Amplifier) internal source\n",
__DATE__, __TIME__, (OPAMPx == OPAMP0) ? 0 : 1);
msg(" The gain %d\n",
(CONFIG_AMP_GAIN == OPAMP_PGAGain_2 ) ? 2 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_3 ) ? 3 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_4 ) ? 4 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_5 ) ? 5 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_6 ) ? 6 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_7 ) ? 7 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_8 ) ? 8 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_9 ) ? 9 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_10) ? 10 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_11) ? 11 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_12) ? 12 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_13) ? 13 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_14) ? 14 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_15) ? 15 :
(CONFIG_AMP_GAIN == OPAMP_PGAGain_16) ? 16 :
1);
msg(" VinP= Internal signal\n");
msg(" VinM= GND\n");
msg(" Vout= ADC AIN%d\n", 31ul - HAL_CLZ(CONFIG_ADC_TARGET_CHANNEL));
{ /* Configure ADC */
ADC_InitTypeDef init = {0};
ADC_StructInit(&init);
init.SelChannels = CONFIG_ADC_TARGET_CHANNEL;
init.IsSoftTrig = true;
ADC_Init(ADC0, &init);
}
while(1)
{
static int cnt = 0;
OPAMP_InitTypeDef init = {0};
uint32_t ConvValue = 0;
/* Configure OPAMP module */
init.OPAMP_VinP = OPAMP_VinP_Internal;
init.OPAMP_VinM = OPAMP_VinM_GND;
init.OPAMP_Vout = OPAMP_Vout_None;
init.OPAMP_Gain = CONFIG_AMP_GAIN;
if( init.OPAMP_VinP == OPAMP_VinP_Internal )
{
uint16_t dac_step = 512; // 0 ~ 1023 (10-bits)
g_VinP_Src = g_InternalSrc[cnt % (sizeof(g_InternalSrc)/sizeof(g_InternalSrc[0]))];
log_color(SLOG_GREEN, "\nSet internal signal: %s\n",
(g_VinP_Src == AMISC_InternalSrc_TEMP ) ? "Temperature" :
(g_VinP_Src == AMISC_InternalSrc_DAC0 ) ? "DAC 0" :
(g_VinP_Src == AMISC_InternalSrc_DAC1 ) ? "DAC 1" :
(g_VinP_Src == AMISC_InternalSrc_VBuf_1P5V) ? "VBuf 1.5" :
(g_VinP_Src == AMISC_InternalSrc_VDDL ) ? "VDDL" :
(g_VinP_Src == AMISC_InternalSrc_VSS ) ? "VSS" :
(g_VinP_Src == AMISC_InternalSrc_VDD ) ? "VDD" :
"None");
/* Select internal source as VinP */
AMISC_Sel_PGA_VinP_Src(g_VinP_Src);
AMISC_OutInternalSignalToIO(g_VinP_Src, true);
if( g_VinP_Src == AMISC_InternalSrc_DAC0 )
{
AMISC_DAC0_Config(dac_step);
AMISC_DAC0_Enable();
}
else if( g_VinP_Src == AMISC_InternalSrc_DAC1 )
{
AMISC_DAC1_Config(dac_step + 10);
AMISC_DAC1_Enable();
}
}
OPAMP_Init(OPAMPx, &init);
OPAMP_Enable(OPAMPx);
sys_busy_wait(60*1000);
ADC_Start(ADC0);
ConvValue = ADC_GetChannelConvValue(ADC0, CONFIG_ADC_TARGET_CHANNEL);
msg(" ADC samples the PGA Vout= 0x%08X (%d mV)\n",
ConvValue, ((ConvValue * CONFIG_ADC_REF_mVOLTAGE) >> 12));
cnt++;
sys_delay(1000);
}
return 0;
}