首页 > 代码库 > STM32 ADC 测电压

STM32 ADC 测电压

1. STM32F103 ADC

本例使用STM32F103芯片的PA1引脚测试模拟输入的电压值。

查看文档《STM32F103X.pdf》第31页,引脚定义图:

clip_image002

得知PA1使用ADC1的通道1。

查看文档《STM32F103X.pdf》第13页,时钟树图:

clip_image004

得知ADC1可2,4,6,8分频,又ADC输入时钟不得超过14MHZ(参见STM32参考手册RM0008第11章ADC)。

//初始化ADC//这里我们仅以规则通道为例//我们默认将开启通道0~3                                                   void  Adc_Init(void){         ADC_InitTypeDef ADC_InitStructure;     GPIO_InitTypeDef GPIO_InitStructure;    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1, ENABLE );      //使能ADC1通道时钟     RCC_ADCCLKConfig(RCC_PCLK2_Div6);   //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M    //PA1 作为模拟通道输入引脚                             GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;        //模拟输入引脚    GPIO_Init(GPIOA, &GPIO_InitStructure);        ADC_DeInit(ADC1);  //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;    //ADC工作模式:ADC1和ADC2工作在独立模式    ADC_InitStructure.ADC_ScanConvMode = DISABLE;    //模数转换工作在单通道模式    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;    //模数转换工作在单次转换模式    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;    //转换由软件而不是外部触发启动    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;    //ADC数据右对齐    ADC_InitStructure.ADC_NbrOfChannel = 1;    //顺序进行规则转换的ADC通道的数目    ADC_Init(ADC1, &ADC_InitStructure);    //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器      ADC_Cmd(ADC1, ENABLE);    //使能指定的ADC1    ADC_ResetCalibration(ADC1);    //使能复位校准      while(ADC_GetResetCalibrationStatus(ADC1));    //等待复位校准结束    ADC_StartCalibration(ADC1);     //开启AD校准     while(ADC_GetCalibrationStatus(ADC1));     //等待校准结束    ADC_SoftwareStartConvCmd(ADC1, ENABLE);        //使能指定的ADC1的软件转换启动功能}
//获得ADC值//ch:通道值 0~3u16 Get_Adc(u8 ch)   {      //设置指定ADC的规则组通道,一个序列,采样时间    ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );    //ADC1,ADC通道,采样时间为239.5周期                          ADC_SoftwareStartConvCmd(ADC1, ENABLE);        //使能指定的ADC1的软件转换启动功能        while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束    return ADC_GetConversionValue(ADC1);           //返回最近一次ADC1规则组的转换结果}u16 Get_Adc_Average(u8 ch,u8 times){    u32 temp_val=0;    u8 t;    for(t=0;t<times;t++)    {        temp_val+=Get_Adc(ch);        delay_ms(5);    }    return temp_val/times;}void ADCTask(void){    float vol, adcx;    while(1)    {        adcx=Get_Adc_Average(ADC_Channel_1,10);    //ADC的值         vol=(float)adcx*(3300/4096);               //电压值(mv)        delay_ms(200);    }}

2、 STM32F407 ADC

本例使用STM32F407芯片的PF9引脚测试模拟输入的电压值。

clip_image002[1]

查看文档《STM32F4x7-Datasheet.pdf》第43页,引脚定义图:

clip_image004[1]

查看文档《STM32F4x7-Reference manual.pdf》第51页,内存映射:

clip_image006

点击链接查看ADC寄存器映射:

clip_image008

又查看数据寄存器偏移量:

clip_image010

得知数据寄存器地址:#define ADC3_DR_ADDRESS ((uint32_t)0x4001224C)

查看《STM32F4x7-Reference manual.pdf》第165页DMA2 request mapping:

clip_image012

得知使用DMA2 Stream0 通道2映射ADC3。

#define ADC3_DR_ADDRESS    ((uint32_t)0x4001224C)__IO uint16_t ADC3ConvertedValue = 0;__IO uint32_t ADC3ConvertedVoltage = 0;//ADC3 channel07 with DMA configurationvoid ADC3_CH7_DMA_Config(void){  ADC_InitTypeDef       ADC_InitStructure;  ADC_CommonInitTypeDef ADC_CommonInitStructure;  DMA_InitTypeDef       DMA_InitStructure;  GPIO_InitTypeDef      GPIO_InitStructure;  /* Enable ADC3, DMA2 and GPIO clocks ****************************************/  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOF, ENABLE);  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);  /* DMA2 Stream0 channel2 configuration **************************************/  DMA_InitStructure.DMA_Channel = DMA_Channel_2;    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;  DMA_InitStructure.DMA_BufferSize = 1;  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_FIFOMode = DMA_FIFOMode_Disable;           DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;  DMA_Init(DMA2_Stream0, &DMA_InitStructure);  DMA_Cmd(DMA2_Stream0, ENABLE);  /* Configure ADC3 Channel7 pin as analog input ******************************/  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;  GPIO_Init(GPIOF, &GPIO_InitStructure);  /* ADC Common Init **********************************************************/  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;  ADC_CommonInit(&ADC_CommonInitStructure);  /* ADC3 Init ****************************************************************/  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;  ADC_InitStructure.ADC_ScanConvMode = DISABLE;  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  ADC_InitStructure.ADC_NbrOfConversion = 1;  ADC_Init(ADC3, &ADC_InitStructure);  /* ADC3 regular channel7 configuration *************************************/  ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 1, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);  /* Enable ADC3 DMA */  ADC_DMACmd(ADC3, ENABLE);  /* Enable ADC3 */  ADC_Cmd(ADC3, ENABLE);}int main(void){  /* ADC3 configuration *******************************************************/  /*  - Enable peripheral clocks                                              */  /*  - DMA2_Stream0 channel2 configuration                                   */  /*  - Configure ADC Channel7 pin as analog input                             */  /*  - Configure ADC3 Channel7                                               */  ADC3_CH7_DMA_Config();  /* Start ADC3 Software Conversion */   ADC_SoftwareStartConv(ADC3);  while (1)  {    ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
    delay_ms(200);  }}