程序地带

STM32G031 HAL库adc+DMA采集电压


硬件原理图



程序:


int main(void)
{
//Flash_EnableReadProtection();//RDP设置 使能读保护
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_ADC1_Init();
MX_TIM2_Init();
MX_NVIC_Init();
HAL_TIM_Base_Start_IT(&htim2);
HAL_ADC_Start_DMA(&hadc1,(uint32_t *)&adcbuf,2);
while(1)
{
}
}

 


void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}

 


uint16_t adcbuf[2];//0,电池电压, 1,温度ADC
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
void MX_ADC1_Init(void)
{
ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
//hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;//STM32CubeMX生成,只能使用通道 0-14,不能使用通道 15,16,17,18;
hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 2;
//hadc1.Init.DiscontinuousConvMode = DISABLE;//STM32CubeMX生成,只能使用通道 0-14,不能使用通道 15,16,17,18;
hadc1.Init.DiscontinuousConvMode = ENABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_160CYCLES_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;
hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analog WatchDog 2
*/
AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
if (HAL_ADC_AnalogWDGConfig(&hadc1, &AnalogWDGConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = ADC_REGULAR_RANK_2;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/* ADC1 clock enable */
__HAL_RCC_ADC_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC1 GPIO Configuration
PB0 ------> ADC1_IN8
PA11 [PA9] ------> ADC1_IN15
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* ADC1 DMA Init */
/* ADC1 Init */
hdma_adc1.Instance = DMA1_Channel1;
hdma_adc1.Init.Request = DMA_REQUEST_ADC1;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
}

分压公式:U1/U2=R1/R2


#define R0 220//分压电阻阻值
#define R1 100//分压电阻阻值
#define ref_vol 2800//参考电压
static uint16_t get_volt(uint16_t advalue)
{
return advalue * ref_vol / 0xfff * (R0 + R1) / R0;
}

 


 


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dailin2012/article/details/111246359

随机推荐

Mac终端安装pip后pycharm无法调用的问题

环境:MacOsMojave10.14.6pip是Python包管理工具,该工具提供了对Python包的查找、下载、安装、卸载的功能。电脑中已经安装了anaconda和py...

zsc19990913 阅读(399)

多任务知识点总结

多任务一、线程​线程是操作系统调度的单位,在Python中使用threading类中的Thread来创建函数,并且利用target指定线程需要执行的函数名称,...

胖大xian 阅读(118)

Oracle 给字段增加注释

听起来如此常见而又简单的需求,结果我百度半天,既然决定以后建表、修改表等数据库操作,不再用可视化操作,而是用SQL实现,所以&#x...

懒得懒 阅读(979)

Java程序运行时的内存分析

JAVA程序运行的内存分析栈stack:每个线程私有,不能实现线程间的共享!局部变量放置于栈中。栈是由系统自动分配,速度快!栈是一...

博客正在整理 阅读(534)

JAVA基础 练习-15-HashMap集合练习(一)

JAVA基础练习-15-HashMap集合练习(一)📑需求:创建一个HashMap集合,键是学号(String),值是学生对象(Student)。存储三个键值对元素&#x...

WTIHK 阅读(140)

python Django自定义错误处理机制

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理想要获取更多Python学习资料了解更多关于Python的知识可以加Q群630390733踊跃发言大家...

pythonputao 阅读(995)

Linux设备模型(五) Bus

参考文章:http://www.wowotech.net/linux_kenrel/bus.html目录1.前言2.Bus基础知识2.1.structbus_type介绍2.2.stru...

_Modest_ 阅读(152)

记录一下 DC1靶机渗透

记录一下 DC1靶机渗透

一.准备首先kaili攻击机和DC1靶机都是桥接网络,为了nmap扫描(桥接网络在设置里)。二.namp扫描1.首先扫描一下这个网段很容易可以看见DC1的靶机...

吃醋不爱蘸饺子 阅读(368)