STM32 ADC acquired voltage and current values to achieve two-channel

This experiment was to obtain data sampled by the ADC configuration by DMA.

Software to achieve the following:

  adc.c file

#include " adc.h. " 

#define ADC1_DR_Address ((U32) 0x40012400 + 0x4C)                       // define ADC1 address 

volatile uint16_t ADCConvertedValue [ 2 ];                                 // define the memory address of the array 
a float AD_Value [ 2 ]; 

static  void ADC1_GPIO_Config ( void ) 
{ 
    GPIO_InitTypeDef GPIO_InitStructure; 
    
    / * the Enable ADC1 and GPIOC Clock * / 
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); 
    
    / * the Configure PC.01 the INPUT AS Analog* / 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 
    GPIO_Init (GPIOB, & GPIO_InitStructure);                               // When PB0, PB1, without providing input rate 
} 

static  void ADC1_DMA_Config ( void ) 
{ 
    DMA_InitTypeDef DMA_InitStructure; 
    
    / * the DMA Configuration CHANnel1 * / 
    RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA1, the eNABLE);                    // enable DMA transfer 
    DMA_DeInit (DMA1_Channel1); 
    DMA_InitStructure.DMA_PeripheralBaseAddr= (U32) & (ADC1-> the DR);        // the ADC address 
    DMA_InitStructure.DMA_MemoryBaseAddr = (U32) & ADCConvertedValue;     // memory address 
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 
    DMA_InitStructure.DMA_BufferSize = 2 ; 
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;    // outer fixed addresses provided 
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;              // memory address is not fixed 
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;     // 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_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);
}

static void ADC1_Config(void)
{
    /* ADC1 configuration */
    ADC_InitTypeDef ADC_InitStructure;
    ADC_DeInit(ADC1);
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);                  //使能ADC1时钟
    = ADC_Mode_Independent ADC_InitStructure.ADC_Mode;                    // independent ADC mode 
    ADC_InitStructure.ADC_ScanConvMode the ENABLE =;                          // start the scan mode, scan mode for multi-channel acquisition 
    ADC_InitStructure.ADC_ContinuousConvMode the ENABLE =;                    // Open continuous conversion mode, i.e. kept ADC measurements 
    = ADC_ExternalTrigConv_None ADC_InitStructure.ADC_ExternalTrigConv;     // no external trigger a transition 
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;               // data collection right justified 
    ADC_InitStructure.ADC_NbrOfChannel = 2 ;                                // number of channels to convert 2 
    ADC_Init (ADC1, & ADC_InitStructure);
    
    / * Configuration ADC clock, a frequency of 6 minutes PCLK2, i.e. 12Hz * /
    RCC_ADCCLKConfig (RCC_PCLK2_Div6); 
    / * Configuration of the channels 8, 9 as ADC1 239.5 sampling period, the sequence 1,2 * /  
    ADC_RegularChannelConfig (ADC1, ADC_Channel_8, . 1 , ADC_SampleTime_239Cycles5); 
    ADC_RegularChannelConfig (ADC1, ADC_Channel_9, 2 , ADC_SampleTime_239Cycles5); 
    
    / * the Enable ADC1 the DMA * / 
    ADC_DMACmd (ADC1, the ENABLE);     
    / * the Enable ADC1 * / 
    ADC_Cmd (ADC1, the ENABLE); 
    
    / * reset calibration register * /    
    ADC_ResetCalibration (ADC1); 
    / * wait calibration register of reset * / 
    the while(ADC_GetResetCalibrationStatus (ADC1));    
     / * the ADC Calibration * /
    ADC_StartCalibration (ADC1); 
    / * wait for the calibration is completed * / 
    the while (ADC_GetCalibrationStatus (ADC1)); 
} 

void adc1_start ( void ) 
{ 
    ADC_SoftwareStartConvCmd (ADC1, the ENABLE);                          // ADC1 soft start count 
    DMA_Cmd (DMA1_Channel1, the ENABLE);                                  // make DMA channel. 1 
} 

void ADC1_Init ( void ) 
{ 
    ADC1_GPIO_Config (); 
    ADC1_DMA_Config (); 
    ADC1_Config (); 
}

adc.h file

#ifndef __ADC_H
#define __ADC_H

#include <stm32f10x.h>

void ADC1_Init(void);
void adc1_start(void);

#endif  /*ADC_H*/

main.c file

#include " adc.h. " 

extern  volatile uint16_t ADCConvertedValue [ 2 ];                   // define the memory address of the array 
extern  a float AD_Value [ 2 ]; 

int main ( void ) 
{     
    NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);              // Set the interrupt priority group 2 group, 2 preemption priority, priority 2 in response 
    USART1_Int ();                                                // serial port initialization 
    
    ADC1_Init ();                                                 // ADC1 initialization 
     adc1_start ();
    SysTick_Init ();                                              // the system clock to initialize 
    
    the while ( . 1 ) 
    {         
       AD_Value [ 0 ] = ( a float ) ADCConvertedValue [ 0 ] / 4096 * 3.3 ;      // acquires the voltage value 
       AD_Value [ . 1 ] = ( a float ) ADCConvertedValue [ . 1 ] / 4096 * 3.3 ;      // Get the current value of         
       the printf ( " \ R & lt \ n-voltage is: V F% \ R & lt \ n- " , AD_Value [ 0 ]); 
       the printf ( " \ R & lt \ n-current value:% F A \ R & lt \ n- " , (AD_Value [ 1 ] - 2.5 ) / 0.185 ); 
    } 
}

The experiment is current pickup module ACS712 module, so the need to change if the printout.

Guess you like

Origin www.cnblogs.com/lucky-3/p/11269835.html