【ACM32G103RCT6开发板测评】+ ADC测试
[复制链接]
测试下ADC功能,检测PA1端口输入的电压值。
一、ADC内部框图
内部一共有20路ADC输入
测试使用内部IN1,对应的PA1引脚
二、程序部分
2.1、ADC引脚初始化
acm32g103_hal_msp.c
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
uint32_t i;
GPIO_InitTypeDef GPIO_Handle;
uint32_t ADC_Pin_Map[][3] =
{
// { ADC_CHANNEL_0_EN, (uint32_t)GPIOA, GPIO_PIN_0 },
{ ADC_CHANNEL_1_EN, (uint32_t)GPIOA, GPIO_PIN_1 },
// { ADC_CHANNEL_2_EN, (uint32_t)GPIOA, GPIO_PIN_2 },
// { ADC_CHANNEL_3_EN, (uint32_t)GPIOA, GPIO_PIN_3 },
// { ADC_CHANNEL_4_EN, (uint32_t)GPIOA, GPIO_PIN_4 },
// { ADC_CHANNEL_5_EN, (uint32_t)GPIOA, GPIO_PIN_5 },
// { ADC_CHANNEL_6_EN, (uint32_t)GPIOA, GPIO_PIN_6 },
// { ADC_CHANNEL_7_EN, (uint32_t)GPIOA, GPIO_PIN_7 },
// { ADC_CHANNEL_8_EN, (uint32_t)GPIOB, GPIO_PIN_0 },
// { ADC_CHANNEL_9_EN, (uint32_t)GPIOB, GPIO_PIN_1 },
// { ADC_CHANNEL_10_EN, (uint32_t)GPIOC, GPIO_PIN_0 },
// { ADC_CHANNEL_11_EN, (uint32_t)GPIOC, GPIO_PIN_1 },
// { ADC_CHANNEL_12_EN, (uint32_t)GPIOC, GPIO_PIN_2 },
// { ADC_CHANNEL_13_EN, (uint32_t)GPIOC, GPIO_PIN_3 },
// { ADC_CHANNEL_14_EN, (uint32_t)GPIOC, GPIO_PIN_4 },
// { ADC_CHANNEL_15_EN, (uint32_t)GPIOC, GPIO_PIN_5 },
{ 0xffffffff, 0 }, //结束标志
};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
//Set gpio to analog.
for(i = 0; ADC_Pin_Map[i][0] != 0xffffffff; i++)
{
if(hadc->Init.ChannelEn & ADC_Pin_Map[i][0])
{
GPIO_Handle.Pin = ADC_Pin_Map[i][2];
GPIO_Handle.Mode = GPIO_MODE_ANALOG;
GPIO_Handle.Pull = GPIO_NOPULL;
HAL_GPIO_Init((GPIO_TypeDef *)ADC_Pin_Map[i][1], &GPIO_Handle);
}
}
/* Enable ADC Clock */
__HAL_RCC_ADC12_CLK_ENABLE();
/* Clear Pending Interrupt */
NVIC_ClearPendingIRQ(ADC12_IRQn);
/* Enable External Interrupt */
NVIC_EnableIRQ(ADC12_IRQn);
}
2.2、acm32g103_it.c
#include "main.h"
#include "acm32g103_it.h"
extern TIM_HandleTypeDef tim2_handler;
extern ADC_HandleTypeDef ADC_Handle;
void ADC1_2_IRQHandler(void)
{
HAL_ADC_IRQHandler();
}
2.3、adc.c
#include "main.h"
#define BUFFER_LENGTH (21)
uint32_t gadcBuffer[BUFFER_LENGTH]; //ADC Value
//ADC_HandleTypeDef hadc;
ADC_HandleTypeDef ADC_Handle;
void ADC_Init_Polling_Nchannels(void)
{
ADC_ChannelConfTypeDef ADC_ChannelConf;
ADC_Handle.Init.ClockPrescaler = ADC_CLOCK_DIV16; //ADC_CLK分频选择
ADC_Handle.Init.Resolution = ADC_RESOLUTION_12B; //分辨率
ADC_Handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; //数据对齐
ADC_Handle.Init.ConConvMode = DISABLE; //连续转换模式
ADC_Handle.Init.DiscontinuousConvMode = DISABLE; //间断模式
ADC_Handle.Init.NbrOfDiscConversion =1; //间断模式通道计数
ADC_Handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; //触发模式:外部触发或软件触发
ADC_Handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;//外部触发边沿选择
ADC_Handle.Init.DMAMode = ADC_DMAMODE_DISABLE; //DMA选择
ADC_Handle.Init.OverMode = ADC_OVERMODE_DISABLE; //溢出时是否保留上次采样数据
ADC_Handle.Init.OverSampMode = ADC_OVERSAMPMODE_DISABLE;//过采样使能
ADC_Handle.Init.Oversampling.Ratio =ADC_OVERSAMPLING_RATIO_2;//过采样率
ADC_Handle.Init.Oversampling.RightBitShift =ADC_RIGHTBITSHIFT_2;//过采样移位系数
ADC_Handle.Init.Oversampling.TriggeredMode =0; //过采样触发模式
ADC_Handle.Init.AnalogWDGEn = ADC_ANALOGWDGEN_DISABLE; //模拟看门狗
ADC_Handle.Init.ChannelEn = ADC_CHANNEL_1_EN;
ADC_Handle.Instance = ADC1;
HAL_ADC_Init(&ADC_Handle);
/* The total adc regular channels number */
ADC_Handle.ChannelNum = 1;
ADC_ChannelConf.Channel = ADC_CHANNEL_1;
ADC_ChannelConf.Sq = ADC_SEQUENCE_SQ1;
ADC_ChannelConf.Smp = ADC_SMP_CLOCK_320;
ADC_ChannelConf.SingleDiff = ADC_SINGLE_ENDED;
ADC_ChannelConf.OffsetNumber = ADC_OFFSET_NONE;
ADC_ChannelConf.Offset = 0;
HAL_ADC_ConfigChannel(&ADC_Handle,&ADC_ChannelConf);
}
void HAL_ADC_IRQCallback(void)
{
__IO uint32_t Status;
Status = ADC_Handle.Instance->SR;
/************ Check End of Conversion flag for injected ************/
if(__HAL_ADC_GET_IT_SOURCE(&ADC_Handle, ADC_IE_JEOCIE))
{
if((Status & ADC_SR_JEOC) == ADC_SR_JEOC)
{
/* Conversion complete callback */
if (NULL != ADC_Handle.InjectedConvCpltCallback)
{
ADC_Handle.InjectedConvCpltCallback(&ADC_Handle);
}
/* Clear injected group conversion flag */
__HAL_ADC_CLEAR_FLAG(&ADC_Handle, ADC_SR_JEOC);
}
}
/************ Check End of Conversion flag for injected group ************/
if(__HAL_ADC_GET_IT_SOURCE(&ADC_Handle, ADC_IE_JEOGIE))
{
if((Status & ADC_SR_JEOG) == ADC_SR_JEOG)
{
if(ADC_IS_SOFTWARE_START_INJECTED(&ADC_Handle) || \
(ADC_IS_SOFTWARE_START_REGULAR(&ADC_Handle) &&(ADC_Handle.Init.ConConvMode == 0)) && (HAL_IS_BIT_CLR(ADC_Handle.Instance->CR1, ADC_CR1_JAUTO)))
{
/* Disable ADC end of conversion interrupt on injected injected */
__HAL_ADC_DISABLE_IT(&ADC_Handle, ADC_IE_JEOCIE);
}
/* Conversion complete callback */
if (NULL != ADC_Handle.InjectedGroupConvCpltCallback)
ADC_Handle.InjectedGroupConvCpltCallback(&ADC_Handle);
/* Clear injected group conversion flag */
__HAL_ADC_CLEAR_FLAG(&ADC_Handle, ADC_SR_JEOG);
}
}
/************ Check Conversion flag for regular Channel ************/
if(__HAL_ADC_GET_IT_SOURCE(&ADC_Handle, ADC_IE_EOCIE))
{
if((Status & ADC_SR_EOC) == ADC_SR_EOC)
{
/* Conversion complete callback */
if (NULL != ADC_Handle.ConvCpltCallback)
ADC_Handle.ConvCpltCallback(&ADC_Handle);
/* Clear conversion flag */
__HAL_ADC_CLEAR_FLAG(&ADC_Handle, ADC_SR_EOC);
}
}
/************ Check End of Conversion flag for regular group ************/
if(__HAL_ADC_GET_IT_SOURCE(&ADC_Handle, ADC_IE_EOGIE))
{
if((Status & ADC_SR_EOG) == ADC_SR_EOG)
{
if((__HAL_ADC_CHECK_TRIG_REGULAR(&ADC_Handle, ADC_SOFTWARE_START)) &&
(ADC_Handle.Init.ConConvMode == 0))
{
/* Disable ADC end of conversion interrupt on group regular */
__HAL_ADC_DISABLE_IT(&ADC_Handle, ADC_IE_EOGIE);
}
/* Conversion complete callback */
if (NULL != ADC_Handle.GroupCpltCallback)
ADC_Handle.GroupCpltCallback(&ADC_Handle);
/* Clear regular group conversion flag */
__HAL_ADC_CLEAR_FLAG(&ADC_Handle, ADC_SR_EOG);
}
}
/************ Check Analog watchdog flags ************/
if(__HAL_ADC_GET_IT_SOURCE(&ADC_Handle, ADC_IE_AWDIE))
{
if((Status & ADC_SR_AWD) == ADC_SR_AWD)
{
/* Level out of window callback */
if (NULL != ADC_Handle.LevelOutOfWindowCallback)
ADC_Handle.LevelOutOfWindowCallback(&ADC_Handle);
/* Clear the ADC analog watchdog flag */
__HAL_ADC_CLEAR_FLAG(&ADC_Handle, ADC_SR_AWD);
}
}
}
void adc_test(void)
{
uint32_t i;
float vol=0.0;
ADC_Init_Polling_Nchannels();
while(1)
{
for (i = 0; i < BUFFER_LENGTH; i++)
{
gadcBuffer[i] = 0;
}
HAL_ADC_Polling(&ADC_Handle, gadcBuffer, ADC_Handle.ChannelNum, 0);
for (i = 0; i < ADC_Handle.ChannelNum; i++)
{
vol = (gadcBuffer[i]&0xFFF)*3.3/4095;
printf("PA1 Voltage is: %0.1f V \r\n", vol);
}
HAL_DelayMs(1000);
}
}
2.4、main.c
#include "main.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
usart_init(115200);
printf("\r\n\r\n====== ACM32G103 MCU is runing ======\r\nSysCoreClk: %dHz, HCLK:%dHz\r\nPCLK1:%dHz, PCLK2:%dHz\r\n\r\n",g_SystemCoreClock, HAL_RCC_GetHCLKFreq(),HAL_RCC_GetPCLK1Freq(),HAL_RCC_GetPCLK2Freq());
init_led();
init_tim2();
init_key();
adc_test();
while(1)
{
}
}
三、运行结果
3.1、硬件连接图
3.2、调节电位器,串口输出
|