样例使用说明
================================================================
版本历史
日期 版本 负责人 IAR MDK 描述
2017-06-07 0.1 虹芯侠客 7.70 5.16A first version
本样例主要展示ADC 单通道连续采样示例,使用中断方式获取ADC运行状态。
说明:
配置ADC并开始采样,ADC中断发生标志连续转换完成,读取累加转换结果
☆ 本文旨在抛砖引玉,其余具体使用欢迎加Q:3230875137或加群164973950交流。
☆ 华大MCU单片机资料分享连接
HC32F003 HC32F005 HC32L110 HC32L136 HC32F030 HC32M140 HC32F146 HC32L150 HC32L156
示波器
电源
无
1)打开工程编译并运行
2)查看串口输出
调试串口的使用:
调试串口在系统时钟使用高速晶振时可用,PIN为P35. 波特率为19200, 8N1.
================================================================
/****************************************************************************/
/ \file main.c
**
** A detailed description is available at
** @link Sample Group Some description @endlink
**
** - 2017-05-28 LiuHL First Version
**
******************************************************************************/
/******************************************************************************
/******************************************************************************
/******************************************************************************
/******************************************************************************
/******************************************************************************
/******************************************************************************
/*****************************************************************************
void AdcContIrqCallback(void)
{
// Adc_DisableIrq();
// Adc_ClrContIrqState();
stcAdcIrqFlag.bAdcIrq = TRUE;
}
void AdcRegIrqCallback(void)
{
// Adc_DisableIrq();
// Adc_ClrRegIrqState();
stcAdcIrqFlag.bAdcRegCmp = TRUE;
}
void AdcHhtIrqCallback(void)
{
// Adc_DisableIrq();
// Adc_ClrHhtIrqState();
stcAdcIrqFlag.bAdcHhtCmp = TRUE;
}
void AdcLltIrqCallback(void)
{
// Adc_DisableIrq();
// Adc_ClrLltIrqState();
stcAdcIrqFlag.bAdcLltCmp = TRUE;
}
/**
** \brief Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/
int32_t main(void)
{
stc_adc_cfg_t stcAdcCfg;
stc_adc_cont_cfg_t stcAdcContCfg;
stc_adc_irq_t stcAdcIrq;
stc_adc_irq_calbakfn_pt_t stcAdcIrqCalbaks;
uint32_t u32AdcResultAcc;
DDL_ZERO_STRUCT(stcAdcCfg);
DDL_ZERO_STRUCT(stcAdcContCfg);
DDL_ZERO_STRUCT(stcAdcIrq);
DDL_ZERO_STRUCT(stcAdcIrqCalbaks);
DDL_ZERO_STRUCT(stcAdcIrqFlag);
Clk_SwitchTo(ClkXTH);
Clk_SetPeripheralGate(ClkPeripheralUart0, TRUE);
Clk_SetPeripheralGate(ClkPeripheralBt, TRUE);
Clk_SetPeripheralGate(ClkPeripheralGpio, TRUE);
#ifdef DEBUG_PRINT
Debug_UartInit();
#endif
if (Ok != Clk_SetPeripheralGate(ClkPeripheralAdcBgr, TRUE))
{
return 1;
}
printf("ADC continuous sampling INTR test start.\n");
Gpio_SetAnalog(2, 4, TRUE);
Gpio_SetAnalog(2, 6, TRUE);
// Gpio_SetAnalog(3, 5, TRUE);
//ADC配置
Adc_Enable();
M0P_BGR->CR_f.BGR_EN = 0x1u;//BGR必须使能
M0P_BGR->CR_f.TS_EN = 0x1u;
delay100us(1);
stcAdcCfg.enAdcOpMode = AdcContMode;//连续采样模式
stcAdcCfg.enAdcClkSel = AdcClkSysTDiv1;
stcAdcCfg.enAdcSampTimeSel = AdcSampTime4Clk;
// stcAdcCfg.enAdcRefVolSel = RefVolSelInBgr2p5;
stcAdcCfg.enAdcRefVolSel = RefVolSelAVDD;
// stcAdcCfg.bAdcInBufEn = TRUE;
stcAdcCfg.bAdcInBufEn = FALSE;
stcAdcCfg.enAdcTrig0Sel = AdcTrigDisable;
stcAdcCfg.enAdcTrig1Sel = AdcTrigDisable;
Adc_Init(&stcAdcCfg);
stcAdcIrq.bAdcIrq = TRUE;
stcAdcIrq.bAdcRegCmp = FALSE;
stcAdcIrq.bAdcHhtCmp = FALSE;
stcAdcIrq.bAdcLltCmp = FALSE;
stcAdcIrqCalbaks.pfnAdcContIrq = AdcContIrqCallback;
stcAdcIrqCalbaks.pfnAdcRegIrq = AdcRegIrqCallback;
stcAdcIrqCalbaks.pfnAdcHhtIrq = AdcHhtIrqCallback;
stcAdcIrqCalbaks.pfnAdcLltIrq = AdcLltIrqCallback;
Adc_ConfigIrq(&stcAdcIrq, &stcAdcIrqCalbaks);
Adc_EnableIrq();
Adc_CmpCfg(&stcAdcIrq);
//stcAdcContCfg.enAdcContModeCh = AdcAiTsInput;
stcAdcContCfg.enAdcContModeCh = AdcExInputCH0;
stcAdcContCfg.u8AdcSampCnt = 0xFFu;
stcAdcContCfg.bAdcResultAccEn = TRUE;
Adc_ConfigContMode(&stcAdcCfg, &stcAdcContCfg);
while(1)
{
Adc_Start();//ADC开始转换
while(FALSE == stcAdcIrqFlag.bAdcIrq);
Adc_DisableIrq();
#ifdef DEBUG_PRINT
if (FALSE != stcAdcIrqFlag.bAdcIrq)
{
printf("Cont IRQ Fired!\n");
}
#endif
Adc_GetAccResult(&u32AdcResultAcc);
Adc_ClrAccResult();
#ifdef DEBUG_PRINT
printf("CH: %d, SampCnt: 0x%X, AdcVal_acc: 0x%X, AdcVal_avg: 0x%X\n",
stcAdcContCfg.enAdcContModeCh,
stcAdcContCfg.u8AdcSampCnt,
u32AdcResultAcc,
u32AdcResultAcc/(stcAdcContCfg.u8AdcSampCnt+1));
#endif
delay1ms(1000);
Adc_Init(&stcAdcCfg);
Adc_ConfigIrq(&stcAdcIrq, &stcAdcIrqCalbaks);
Adc_EnableIrq();
Adc_ConfigContMode(&stcAdcCfg, &stcAdcContCfg);
}
}
/******************************************************************************