stm32双adc电压采集

ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;    //ADC工做模式:ADC同步规则组模式

ADC_DMACmd(ADC1, ENABLE);  //使能ADC的DMA位

ADC_ExternalTrigConvCmd(ADC2, ENABLE);   //使能ADC2的外部触发模式,用adc1触发adc2

#include "adc.h"
 #include "delay.h"
 #include "usart.h"    

#define M 128
#define N 8
uint16_t   adcvalue[N][M];
u32 ADC_ConvertedValue;  


//初始化ADC1
void Adc1_Multi_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_0;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;        //模拟输入引脚
    GPIO_Init(GPIOA, &GPIO_InitStructure);    
 
    ADC_DeInit(ADC1);  //复位ADC1,将外设 ADC1 的所有寄存器重设为缺省值
 
    ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;    //ADC工做模式:ADC1同步规则组模式
    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_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 );
 
     // 开启ADC的DMA支持
    ADC_DMACmd(ADC1, ENABLE);  //使能ADC的DMA位
    
    /* Enable ADC1 */
    ADC_Cmd(ADC1, ENABLE); //使能ADC1

    /* Enable ADC1 reset calibaration register  使能ADC1复位校准寄存器 */   
    ADC_ResetCalibration(ADC1);
    /* Check the end of ADC1 reset calibration register   ADC1复位校准寄存器检查结束*/
    while(ADC_GetResetCalibrationStatus(ADC1));

    /* Start ADC1 calibaration    启动ADC1校准 */
    ADC_StartCalibration(ADC1);
    /* Check the end of ADC1 calibration  ADC1校准检查结束 */
    while(ADC_GetCalibrationStatus(ADC1));
        
}    


//初始化ADC2
void Adc2_Multi_Init(void)
{     
    ADC_InitTypeDef ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC2    , ENABLE );      //使能ADC2通道时钟
 
    RCC_ADCCLKConfig(RCC_PCLK2_Div6);   //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
 
    //PB0,1 做为模拟通道输入引脚
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;   
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;        //模拟输入引脚
    GPIO_Init(GPIOC, &GPIO_InitStructure);    
 
    ADC_DeInit(ADC2);  //复位ADC2,将外设 ADC2 的所有寄存器重设为缺省值
 
    ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;    //ADC工做模式:ADC1同步规则组模式
    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(ADC2, &ADC_InitStructure);    //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器   
 
    ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5 );

    ADC_ExternalTrigConvCmd(ADC2, ENABLE);   //使能ADC2的外部触发模式
 
    /* Enable ADC2 */
    ADC_Cmd(ADC2, ENABLE); //使能ADC2

    /* Enable ADC1 reset calibaration register  使能ADC2复位校准寄存器 */   
    ADC_ResetCalibration(ADC2);
    /* Check the end of ADC1 reset calibration register   ADC2复位校准寄存器检查结束*/
    while(ADC_GetResetCalibrationStatus(ADC2));

    /* Start ADC1 calibaration    启动ADC2校准 */
    ADC_StartCalibration(ADC2);
    /* Check the end of ADC1 calibration  ADC2校准检查结束 */
    while(ADC_GetCalibrationStatus(ADC2));
    
}


/*初始化ADC */
void MY_ADC_Init(void)
{
    Adc1_Multi_Init();
    Adc2_Multi_Init();
    
}


void task_adc(u16 *value1,u16 *value2)//传参获取两个值
{    
    int i;
    *value1=0;
    *value2=0;

     printf("\r\n 采样开始\r\n");
     for(i=0;i<M;i++)
        {
            /* Start ADC1 Software Conversion  启动ADC1软件转换 */         
            ADC_SoftwareStartConvCmd(ADC1, ENABLE);  //开始转换
            
            ADC_ConvertedValue=ADC1->DR;
            
            adcvalue[0][i] = (ADC_ConvertedValue&0xffff); //获取ADC的值
           adcvalue[1][i] = ((ADC_ConvertedValue>> 16)&0xffff);  //获取ADC的值
            delay_ms(1);

            *value1=adcvalue[0][2];//也可以取平均值
            *value2=adcvalue[1][2];
        }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值