/*****************************************************************************
File name: bsp_driver_adc.c
Description: ADC采集函数
Version: v0.1
Date: 20220608
History: 20220608:v1.0:初版
*******************************************************************************/
#include "bsp_adc.h"
#include "stdio.h"
#include "Delay.h"
__IO uint16_t ADC_ConvertedValue;
/*****************************************************************************
Function: Driver_adc_Init
Description: ADC初始化
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_GPIO_APBxClock_FUN ( ADC_GPIO_CLK, ENABLE );
GPIO_InitStructure.GPIO_Pin = ADC_PIN | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_PORT, &GPIO_InitStructure);
}
/*****************************************************************************
Function: Driver_ADC_Config
Description: ADC模式配置,包括DMA与ADC的结构体配置
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStruct;
ADC_APBxClock_FUN ( ADC_CLK, ENABLE );
RCC_AHBPeriphClockCmd(ADC_DMA_CLK, ENABLE);
DMA_DeInit(ADC_DMA_CHANNEL);
DMA_InitStructure.DMA_PeripheralBaseAddr = ( uint32_t)(&(ADC_x->DR ));//ADC地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//数据传输从外设到内存
DMA_InitStructure.DMA_BufferSize = 2;//16位配置为2
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //外设地址不变
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环工作模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//no 内存到内存
DMA_Init(ADC_DMA_CHANNEL, &DMA_InitStructure);
DMA_Cmd(ADC_DMA_CHANNEL , ENABLE);
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
ADC_InitStruct.ADC_ScanConvMode = DISABLE;
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;//连续转换
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//软件触发
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//对齐方式
ADC_Init(ADC_x,&ADC_InitStruct);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL, 1, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC_x, ENABLE);
}
/*****************************************************************************
Function: Driver_ADC_Cmd
Description: 使能ADC
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Cmd(void)
{
ADC_Cmd(ADC_x,ENABLE);
}
/*****************************************************************************
Function: Driver_ADC_Calibration
Description: 进行ADC校准四个步骤,减小误差
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Calibration(void)
{
ADC_ResetCalibration(ADC_x);
while(ADC_GetResetCalibrationStatus(ADC_x));//等待复位校准完成
ADC_StartCalibration(ADC_x);
while(ADC_GetCalibrationStatus(ADC_x)); // 等待校准完成
}
/*****************************************************************************
Function: Driver_ADC_Getconvertedvalue
Description: ADC转换数据获取
Input: 无
Output: 无
Return: ADC_ConvertedValueLocal
*******************************************************************************/
float Driver_ADC_Getconvertedvalue(void)
{
float Vdd_Value = 3.3;//基准电压
float ADC_ConvertedValueLocal;
ADC_SoftwareStartConvCmd(ADC_x, ENABLE);
ADC_ConvertedValueLocal = (float)ADC_ConvertedValue/4096*Vdd_Value;
// printf("\r\n The current AD value = 0x%04X \r\n",ADC_ConvertedValue);
printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal);
printf("\r\n\r\n");
Delay_ms(1000);
return ADC_ConvertedValueLocal;
}
/*****************************************************************************
File name: bsp_driver_adc.h
Description: ADC采集函数
Version: v0.1
Date: 20220608
History: 20220608:v1.0:初版
*******************************************************************************/
#ifndef __BSP_ADC_H
#define __BSP_ADC_H
/****************** Device header ********************************************/
#include "stm32f10x.h"
// ADC GPIO宏定义
// 注意:用作ADC采集的IO必须没有复用,否则采集电压会有影响
#define ADC_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADC_GPIO_CLK RCC_APB2Periph_GPIOC
#define ADC_PORT GPIOC
#define ADC_PIN GPIO_Pin_1
// ADC 编号选择
// 可以是 ADC1/2,如果使用ADC3,中断相关的要改成ADC3的
#define ADC_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADC_x ADC1
#define ADC_CLK RCC_APB2Periph_ADC1
// ADC 通道宏定义
#define ADC_CHANNEL ADC_Channel_11
#define ADC_DMA_CLK RCC_AHBPeriph_DMA1
#define ADC_DMA_CHANNEL DMA1_Channel1
/*****************************************************************************
Function: Driver_adc_Init
Description: ADC初始化
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Init(void);
/*****************************************************************************
Function: Driver_ADC_Config
Description: ADC模式配置,包括DMA与ADC的结构体配置
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Config(void);
/*****************************************************************************
Function: Driver_ADC_Cmd
Description: 使能ADC
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Cmd(void);
/*****************************************************************************
Function: Driver_ADC_Calibration
Description: 进行ADC校准四个步骤,减小误差
Input: 无
Output: 无
Return: 无
*******************************************************************************/
void Driver_ADC_Calibration(void);
/*****************************************************************************
Function: Driver_ADC_Getconvertedvalue
Description: ADC转换数据获取
Input: 无
Output: 无
Return: ADC_ConvertedValueLocal
*******************************************************************************/
float Driver_ADC_Getconvertedvalue(void);
#endif /* __BSP_ADC_H */