【国民技术车规MCU N32A455开发板】 N32A455 ADC模块测试
[复制链接]
本帖最后由 尹小舟 于 2024-2-27 08:58 编辑
实验目的:使用软件触发采集板子的电位计的电压信号。
N32A455 ADC简介:
N32A455xxL7系列采用 32 bit ARM Cortex-M4F内核,最高工作主频144MHz,内部有4 个 12bit 4.7Msps 高速 ADC,可配置为 12/10/8/6bit 模式,6bit 模式下采样率高达 8.9Msps,多达 38 路外部单端输入通道,支持差分模式,ADC1 支持 9 个外部通道,ADC2 支持 12 个外部通道,ADC3 支持 15 个外部通道,ADC4 支持 13 个 外部通道,并且 ADC 的输入时钟的最大频率为 80MHz,还支持模拟看门狗,所有模拟接口支持 1.8~3.6V 全电压工作。
ADC特性:
ADC 使能控制:
只有在上电过程完成后,才能进行下一步,可以通过轮询 ADC_CTRL3.RDY 来检查上电是否完成。设置 ADC_CTRL2.ON 来打开 或关闭ADC,打开ADC或重新打开后要校准ADC
函数说明:启用或禁用指定的ADC外设。
参数说明:ADCx 其中x可以为1,2,3或4来选择ADC外设。
Cmd ADCx外设的新状态,取值包括:ENABLE或DISABLE。
void ADC_Enable(ADC_Module* ADCx, FunctionalState Cmd)
{
uint32_t i =0;
/* Check the parameters */
assert_param(IsAdcModule(ADCx));
assert_param(IS_FUNCTIONAL_STATE(Cmd));
if (Cmd != DISABLE)
{
/* Set the AD_ON bit to wake up the ADC from power down mode */
ADCx->CTRL2 |= CTRL2_AD_ON_SET;
}
else
{
/* Disable the selected ADC peripheral */
ADCx->CTRL2 &= CTRL2_AD_ON_RESET;
}
/*Wait for ADC to filter burr after a delay of more than 8us */
for(i=0;i<0x128;i++);
}
函数说明:启用ADC校准
void ADC_StartCalibration(ADC_Module* ADCx)
{
uint32_t i =0;
/* Check the parameters */
assert_param(IsAdcModule(ADCx));
/* Enable the selected ADC calibration process */
if(ADCx->CALFACT==0)
ADCx->CTRL2 |= CTRL2_CAL_SET;
/*Wait for ADC to filter burr after a delay of more than 8us */
for(i=0;i<0x128;i++);
}
ADC 初始化:
1.配置ADC 初始化结构体,调用初始化参数
2.使能ADC
3.校准ADC
typedef struct
{
uint32_t WorkMode; /*!< Configures the ADC to operate in independent or
dual mode.
This parameter can be a value of @ref ADC_mode */
FunctionalState MultiChEn; /*!< Specifies whether the conversion is performed in
Scan (multichannels) or Single (one channel) mode.
This parameter can be set to ENABLE or DISABLE */
FunctionalState ContinueConvEn; /*!< Specifies whether the conversion is performed in
Continuous or Single mode.
This parameter can be set to ENABLE or DISABLE. */
uint32_t ExtTrigSelect; /*!< Defines the external trigger used to start the analog
to digital conversion of regular channels. This parameter
can be a value of @ref
ADC_external_trigger_sources_for_regular_channels_conversion */
uint32_t DatAlign; /*!< Specifies whether the ADC data alignment is left or right.
This parameter can be a value of @ref ADC_data_align */
uint8_t ChsNumber; /*!< Specifies the number of ADC channels that will be converted
using the sequencer for regular channel group.
This parameter must range from 1 to 16. */
} ADC_InitType;
硬件电路:
使用的是PB15 用的是ADC4
软件代码:
#include "main.h"
#include "stdio.h"
/** @addtogroup ADC_ADC1_DMA
* @{
*/
ADC_InitType ADC_InitStructure;
DMA_InitType DMA_InitStructure;
__IO uint16_t ADC4ConvertedValue[5];
void RCC_Configuration(void);
void GPIO_Configuration(void);
uint16_t ADC_GetData(ADC_Module* ADCx, uint8_t ADC_Channel);
void ADC_Initial(ADC_Module* ADCx);
void ADC_Initial(ADC_Module* ADCx)
{
/* ADC configuration ------------------------------------------------------*/
ADC_InitStructure.WorkMode = ADC_WORKMODE_INDEPENDENT;
ADC_InitStructure.MultiChEn = DISABLE;
ADC_InitStructure.ContinueConvEn = DISABLE;
ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE;
ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
ADC_InitStructure.ChsNumber = 1;
ADC_Init(ADCx, &ADC_InitStructure);
/* Enable ADC */
ADC_Enable(ADCx, ENABLE);
/*Check ADC Ready*/
while(ADC_GetFlagStatusNew(ADCx,ADC_FLAG_RDY) == RESET)
;
/* Start ADC calibration */
ADC_StartCalibration(ADCx);
/* Check the end of ADC calibration */
while (ADC_GetCalibrationStatus(ADCx))
;
}
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] Main program
*/
int main(void)
{
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
uart_print_init();
ADC_Initial(ADC4);
while (1)
{
ADC4ConvertedValue[0]=ADC_GetData(ADC4,ADC4_Channel_05_PB15);
printf("ADC4 P15 = %d \r\n",ADC4ConvertedValue[0]);
}
}
/**
* @brief Configures the different system clocks.
*/
void RCC_Configuration(void)
{
/* Enable peripheral clocks ------------------------------------------------*/
/* Enable GPIOC clocks */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF|RCC_APB2_PERIPH_GPIOG|RCC_APB2_PERIPH_GPIOA|RCC_APB2_PERIPH_GPIOB|RCC_APB2_PERIPH_GPIOC|RCC_APB2_PERIPH_GPIOD|RCC_APB2_PERIPH_GPIOE, ENABLE);
/* Enable ADC4 clocks */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC4, ENABLE);
/* RCC_ADCHCLK_DIV16*/
ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB,RCC_ADCHCLK_DIV16);
RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSE, RCC_ADC1MCLK_DIV8); //selsect HSE as RCC ADC1M CLK Source
}
/**
* @brief Configures the different GPIO ports.
*/
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}
uint16_t ADC_GetData(ADC_Module* ADCx, uint8_t ADC_Channel)
{
uint16_t dat;
ADC_ConfigRegularChannel(ADCx, ADC_Channel, 1, ADC_SAMP_TIME_239CYCLES5);
/* Start ADC Software Conversion */
ADC_EnableSoftwareStartConv(ADCx, ENABLE);
while(ADC_GetFlagStatus(ADCx, ADC_FLAG_ENDC)==0){
}
ADC_ClearFlag(ADCx, ADC_FLAG_ENDC);
ADC_ClearFlag(ADCx, ADC_FLAG_STR);
dat=ADC_GetDat(ADCx);
return dat;
}
实验结果:
|