STM32
直播中

申根换

7年用户 1567经验值
私信 关注
[问答]

如何对基于STM32F0xx系列单片机的ADC功能进行配置呢

如何对基于STM32F0xx系列单片机的ADC功能进行配置呢?其代码该如何去实现呢?

回帖(1)

王桂兰

2021-11-26 15:43:01
STM32F0xx系列单片机基于ST官方标准库V1.5.0的ADC功能的配置
  ADC.c文件

#include "ADC.h"


uint32_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;


void ADC_GPIO_Configuration(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
       
        //端口配置
//        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
//        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                //50M时钟速度
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
}


void ADC_Configuration(void)
{
        ADC_InitTypeDef ADC_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
       
        ADC_DeInit(ADC1);
        ADC_StructInit(&ADC_InitStructure);
       
        ADC_InitStructure.ADC_Resolution  = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ContinuousConvMode  = ENABLE;
        ADC_InitStructure.ADC_ExternalTrigConvEdge  = ADC_ExternalTrigConvEdge_None;
        ADC_InitStructure.ADC_DataAlign  = ADC_DataAlign_Right;
//        ADC_InitStructure.ADC_ScanDirection  = ADC_ScanDirection_Backward;
        ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;        //向上扫描
        ADC_Init(ADC1, &ADC_InitStructure);
       
        ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles);
  
        ADC_GetCalibrationFactor(ADC1);


        ADC_Cmd(ADC1, ENABLE);


        while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
       
//        ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
        //开始校准状态
        ADC_StartOfConversion(ADC1);
}


uint32_t ADC_Read(void)
{
    /* Test EOC flag */
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
   
    /* Get ADC1 converted data */
    ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
   
    /* Compute the voltage */
    ADC1ConvertedVoltage = (ADC1ConvertedValue * 3300)/0xFFF;
          
        return ADC1ConvertedVoltage;
}


ADC.h文件


#ifndef __ADC_H
#define __ADC_H
#include "stm32f0xx.h"
#include "stm32f0xx_adc.h"


//extern uint16_t  ADC1ConvertedValue , ADC1ConvertedVoltage ;
//硬件初始化
void ADC_GPIO_Configuration(void);                                                //AD相关初始化
void ADC_Configuration(void);
uint32_t ADC_Read(void);
#endif


main.c文件


#include "main.h"
#include "stm32f0xx_it.h"
#include "USART.h"
#include "ADC.h"


uint32_t ADC_value;


int main(void)
{
        ADC_GPIO_Configuration();
        ADC_Configuration();
  while (1)
  {
    ADC_value = ADC_Read();
    USART_printf( USART2, "rn %d rn", ADC_value );
  }
}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分