- 初始化及配置
#include "ACS712.h"
__IO uint16_t ADC_Value[2] = {0, 0};
void adc_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOC,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5; //通道14和15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init( GPIOC, &GPIO_InitStructure );
DMA_DeInit(DMA1_Channel1); // 复位DMA
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC1->DR)); // 外设基地址为ADC1_DR寄存器地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_Value; // 内存存储地址,即ADC_ConvertedValue数组的首地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 数据来源于外设
DMA_InitStructure.DMA_BufferSize = 2; // 缓冲区大小,这里为ADC_ConvertedValue数组的长度
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 只有ADC1一个外设,所以地址不递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存存储地址递增
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通道优先级为高,当使用一个DMA通道时,优先级设置不受影响
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // 外设到内存模式
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode=ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel=2; //用2通道AD采集
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_14,1,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_15,2,ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
这里头文件用的是之前检测电流的芯片,没有改。也涉及到电压转换公式计算,挺简单的,看懂数据手册就行,因为我的芯片有点问题,所以没有放出来。
POINT_COLOR=BLUE;
LCD_ShowString(30,120,200,16,16,"AD_Value:");
LCD_ShowString(30,136,200,16,16,"Voltage:");
LCD_ShowString(25,184,200,16,16,"CH2 :");
POINT_COLOR=RED;
ADC_Temp[0]=(3.3/4096)*ADC_Value[0]*1000; //取小数点后3位