华大HC32F003☆ ---ADC例程分享(部分一)
本帖最后由 LED2013 于 2020-8-7 10:28 编辑版本历史
日期 版本 负责人 IAR MDK 描述
2017-06-07 0.1 虹芯侠客 7.70 5.16A first version====================================
功能描述本样例主要展示ADC 单通道连续采样示例,使用中断方式获取ADC运行状态。
说明:
配置ADC并开始采样,ADC中断发生标志连续转换完成,读取累加转换结果================================================================================
测试环境测试用板:辅助工具:示波器
电源辅助软件:无====================================
使用步骤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
**
******************************************************************************//******************************************************************************
[*]Include files
******************************************************************************/
#include “adc.h”
#include “gpio.h”
/******************************************************************************
[*]Local pre-processor symbols/macros (’#define’)
******************************************************************************/
#define DEBUG_PRINT
/******************************************************************************
[*]Global variable definitions (declared in header file with ‘extern’)
******************************************************************************/
/******************************************************************************
[*]Local type definitions (‘typedef’)
******************************************************************************/
/******************************************************************************
[*]Local function prototypes (‘static’)
******************************************************************************/
/******************************************************************************
[*]Local variable definitions (‘static’) *
******************************************************************************/
static stc_adc_irq_t stcAdcIrqFlag;
/*****************************************************************************
[*]Function implementation - global (‘extern’) and local (‘static’)
******************************************************************************/
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_PRINTDebug_UartInit();#endifif (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);}
[*]1
[*]2
[*]3
[*]4
[*]5
[*]6
[*]7
[*]8
[*]9
[*]10
[*]11
[*]12
[*]13
[*]14
[*]15
[*]16
[*]17
[*]18
[*]19
[*]20
[*]21
[*]22
[*]23
[*]24
[*]25
[*]26
[*]27
[*]28
[*]29
[*]30
[*]31
[*]32
[*]33
[*]34
[*]35
[*]36
[*]37
[*]38
[*]39
[*]40
[*]41
[*]42
[*]43
[*]44
[*]45
[*]46
[*]47
[*]48
[*]49
[*]50
[*]51
[*]52
[*]53
[*]54
[*]55
[*]56
[*]57
[*]58
[*]59
[*]60
[*]61
[*]62
[*]63
[*]64
[*]65
[*]66
[*]67
[*]68
[*]69
[*]70
[*]71
[*]72
[*]73
[*]74
[*]75
[*]76
[*]77
[*]78
[*]79
[*]80
[*]81
[*]82
[*]83
[*]84
[*]85
[*]86
[*]87
}/******************************************************************************
[*]EOF (not truncated)
******************************************************************************/
感谢楼主分享资料 支持楼主 不错! 路过 学习下 长知识了 呵呵 排版有些乱 路过 学习下 谢谢楼主分享 代码比较乱,一定要看例程了
代码比较乱 上传文件可能要好点
页:
[1]