使用 C++ 语言给 STM32 编写一个 Adc 类

我使用的STM32芯片:STM32F103ZET6
我们使用的STM32库版本:V3.5.0



注意:

  • 想学习本套 STM32 C++编程 的专栏是有点门槛的。你需要有一点点 STM32 基础 和 一点点 C++ 语言基础。

  • 完整的STM32 C++ Adc类 的下载地址可以在本篇博客的最下面找到。


Adc.cpp

#include "Adc.h"

using namespace stm32f10x;

Adc::Adc(ADC_TypeDef* ADCx, uint8_t ADC_Channel):adcx(ADCx), channel(ADC_Channel){
    initialize();
    start();
}

void Adc::initialize(){
    switch(channel){
        case 0:
                a = new Gpio(PA, 0, GPIO_Mode_AIN); delete a;   break;
        case 1:
                a = new Gpio(PA, 1, GPIO_Mode_AIN); delete a;   break;
        case 2:
                a = new Gpio(PA, 2, GPIO_Mode_AIN); delete a;   break;
        case 3:
                a = new Gpio(PA, 3, GPIO_Mode_AIN); delete a;   break;
        case 4:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PA, 4, GPIO_Mode_AIN); delete a;   break;  
                case (uint32_t)ADC3:
                a = new Gpio(PF, 6, GPIO_Mode_AIN); delete a;   break;                      
            }
            break;
        case 5:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PA, 5, GPIO_Mode_AIN); delete a;   break;  
                case (uint32_t)ADC3:
                a = new Gpio(PF, 7, GPIO_Mode_AIN); delete a;   break;                      
            }
            break;
        case 6:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PA, 6, GPIO_Mode_AIN); delete a;   break;  
                case (uint32_t)ADC3:
                a = new Gpio(PF, 8, GPIO_Mode_AIN); delete a;   break;                      
            }
            break;
        case 7:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PA, 7, GPIO_Mode_AIN); delete a;   break;  
                case (uint32_t)ADC3:
                a = new Gpio(PF, 9, GPIO_Mode_AIN); delete a;   break;                      
            }
            break;
        case 8:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PB, 0, GPIO_Mode_AIN); delete a;   break;  
                case (uint32_t)ADC3:
                a = new Gpio(PF, 10, GPIO_Mode_AIN); delete a;  break;                      
            }
            break;
        case 9:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PB, 1, GPIO_Mode_AIN); delete a;   break;  
            }
            break;
        case 10:
                a = new Gpio(PC, 0, GPIO_Mode_AIN); delete a;   break;
        case 11:
                a = new Gpio(PC, 1, GPIO_Mode_AIN); delete a;   break;
        case 12:
                a = new Gpio(PC, 2, GPIO_Mode_AIN); delete a;   break;
        case 13:
                a = new Gpio(PC, 3, GPIO_Mode_AIN); delete a;   break;
        case 14:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PC, 4, GPIO_Mode_AIN); delete a;   break;  
            }
            break;
        case 15:
            switch((uint32_t)adcx){
                case (uint32_t)ADC1:
                case (uint32_t)ADC2:
                a = new Gpio(PC, 5, GPIO_Mode_AIN); delete a;   break;  
            }
            break;

    }
    ADC_InitTypeDef ADC_InitStructure; 

    if((uint32_t)adcx < APB2PERIPH_BASE){
        uint32_t RCC_APB1Periph = (uint32_t)(1<< ( ((uint32_t)adcx-APB1PERIPH_BASE)>>10));
        RCC_APB1PeriphClockCmd(RCC_APB1Periph, ENABLE);
    }
    else{
        uint32_t RCC_APB2Periph = (uint32_t)(1<< ( ((uint32_t)adcx-APB2PERIPH_BASE)>>10));      
        RCC_APB2PeriphClockCmd(RCC_APB2Periph, ENABLE);
    }
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);   //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M


//  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(adcx, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器   

    ADC_Cmd(adcx, ENABLE);  //使能指定的ADC1
    ADC_ResetCalibration(adcx); //使能复位校准  
    while(ADC_GetResetCalibrationStatus(adcx)); //等待复位校准结束
    ADC_StartCalibration(adcx);  //开启AD校准
    while(ADC_GetCalibrationStatus(adcx));   //等待校准结束
//  ADC_SoftwareStartConvCmd(ADC1, ENABLE);     //使能指定的ADC1的软件转换启动功能    
}

uint16_t Adc::read(){
    //设置指定ADC的规则组通道,一个序列,采样时间
    ADC_RegularChannelConfig(adcx, channel, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期                   

    ADC_SoftwareStartConvCmd(adcx, ENABLE);     //使能指定的ADC1的软件转换启动功能    

    while(!ADC_GetFlagStatus(adcx, ADC_FLAG_EOC ));//等待转换结束

    return ADC_GetConversionValue(adcx);    //返回最近一次ADC1规则组的转换结果
}

uint16_t Adc::read(uint8_t cout){
    u32 temp_val=0;
    u8 t;
    for(t=0;t<cout;t++){
        temp_val+= read();
    }
    return temp_val/cout;
}

void Adc::start(){
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);     //使能指定的ADC1的软件转换启动功能    
}

void Adc::stop(){
    ADC_SoftwareStartConvCmd(ADC1, DISABLE);        //使能指定的ADC1的软件转换启动功能    
}

Adc.h

#ifndef __AOBO_Stm32f10x_Adc_H_
#define     __AOBO_Stm32f10x_Adc_H_

#include "stm32f10x.h"
#include "Gpio.h"

namespace stm32f10x{

class Adc{

    public:
        Adc(ADC_TypeDef* ADCx, uint8_t ADC_Channel);
        void initialize();
        uint16_t read();
        uint16_t read(uint8_t cout);
        void start();
        void stop();
    private:
        Gpio *a;
        ADC_TypeDef* adcx;
        uint8_t channel;
};

}

#endif

main.cpp

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "Gpio.h"
#include "Usart.h"
#include "Adc.h"

using namespace stm32f10x;
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */

int main(void){
    Usart serial(USART1, 115200);
    Adc adc1(ADC3, 11);
    while(true){
        serial.println("ADC1: %d", adc1.read());
    }

}

搞定


你可以到这里下载我已经做好的 STM32 C++ Adc类
百度云 链接:http://pan.baidu.com/s/1bpbZ2MV 密码:esam
也可以在CSDN里面下载:http://download.csdn.net/detail/github_35160620/9624596



小结:
下一讲,我们来使用 C++ 语言,创建一个 STM32硬件 I2c 类。