STM32F1xx 시리즈 다중 채널 ADC DMA 채집 및 비DMA 채집

7723 단어 C 언어STM32
ADC 다중 채널 DMA 채집
#include "ADcx.h"
#include 
#include "stm32f10x.h"
#include "stm32f10x_rcc.h" 
#include "stm32f10x_adc.h"
#include "stm32f10x_gpio.h"

void ADcDMA_Configuration(void);
u16 AD_Value[8][8]; //ADC    
//   
void ADcx_Init(){
	ADC_InitTypeDef ADC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_ADC1,ENABLE);//  ADC     GPIO  
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);//  ADC  72M/6=12,      14
	
	//GPIO  
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
	GPIO_Init(GPIOC,&GPIO_InitStructure);

	ADC_DeInit(ADC1);//  ADC
	ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//      
	ADC_InitStructure.ADC_ScanConvMode=ENABLE;//       (       )
	ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;//      
	ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//    
	ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//   
	ADC_InitStructure.ADC_NbrOfChannel=8;//  8   
	ADC_Init(ADC1,&ADC_InitStructure);//       ADC1

	//  8        
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_10,5,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_11,6,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_12,7,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_13,8,ADC_SampleTime_239Cycles5);
	
	ADC_DMACmd(ADC1,ENABLE);
	ADC_Cmd(ADC1,ENABLE);//  ADC
	ADC_ResetCalibration(ADC1);//      
	while(ADC_GetResetCalibrationStatus(ADC1));//      
	ADC_StartCalibration(ADC1);//  ADC  
	while(ADC_GetCalibrationStatus(ADC1));//      
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);//    ADC  
	
	ADcDMA_Configuration();
	//  
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	DMA_Cmd(DMA1_Channel1, ENABLE); //  DMA  
}
void ADcDMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1); //  DMA  ,ADC1  DMA   DMA1-1
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&ADC1->DR; //DMA ADC1 DR   
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)&AD_Value; //   AD_Value 
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//       
DMA_InitStructure.DMA_BufferSize = 64; //      
DMA_InitStructure.DMA_PeripheralInc =DMA_PeripheralInc_Disable; //       
DMA_InitStructure.DMA_MemoryInc =DMA_MemoryInc_Enable; //       1
DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_HalfWord; //           (16 )
DMA_InitStructure.DMA_MemoryDataSize =DMA_MemoryDataSize_HalfWord; //           (16 )
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //        
DMA_InitStructure.DMA_Priority =DMA_Priority_High; //  DMA1_Channel1    
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //         
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
}

비DMA 모드
#include "ADcx.h"
#include 
#include "stm32f10x.h"
#include "stm32f10x_rcc.h" 
#include "stm32f10x_adc.h"
#include "stm32f10x_gpio.h"

void ADcDMA_Configuration(void);
u16 AD_Value[8][8]; //ADC    
//   
void ADcx_Init(){
	ADC_InitTypeDef ADC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_ADC1,ENABLE);//  ADC     GPIO  
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);//  ADC  72M/6=12,      14
	
	//GPIO  
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
	GPIO_Init(GPIOC,&GPIO_InitStructure);

	ADC_DeInit(ADC1);//  ADC
	ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//      
	ADC_InitStructure.ADC_ScanConvMode=ENABLE;//       (       )
	ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;//      
	ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//    
	ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//   
	ADC_InitStructure.ADC_NbrOfChannel=8;//  8   
	ADC_Init(ADC1,&ADC_InitStructure);//       ADC1

	//  8        
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_10,5,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_11,6,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_12,7,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_13,8,ADC_SampleTime_239Cycles5);
	
	ADC_Cmd(ADC1,ENABLE);//  ADC
	ADC_ResetCalibration(ADC1);//      
	while(ADC_GetResetCalibrationStatus(ADC1));//      
	ADC_StartCalibration(ADC1);//  ADC  
	while(ADC_GetCalibrationStatus(ADC1));//      
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);//    ADC  
}
//  ADC    
void  ADCValue(void)
{
	u16 tempADC[8];//ADC     
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[0]=ADC_GetConversionValue(ADC1);
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[1]=ADC_GetConversionValue(ADC1);

	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[2]=ADC_GetConversionValue(ADC1);
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[3]=ADC_GetConversionValue(ADC1);
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[4]=ADC_GetConversionValue(ADC1);

	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[5]=ADC_GetConversionValue(ADC1);

	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[6]=ADC_GetConversionValue(ADC1);

	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[7]=ADC_GetConversionValue(ADC1);

	ADC_SoftwareStartConvCmd(ADC1, ENABLE);//  ADC  
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//      
	tempADC[8]=ADC_GetConversionValue(ADC1);
}
       
uint16_t read_adc_value(uint8_t ch)   
{
    uint16_t adc_val = 0;
    
    ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );    
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);        
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )){};
    
    adc_val = ADC_GetConversionValue(ADC1);    
    
    return adc_val;

}

인터럽트 모드 채집 소개 ADCDiscModeChannelCountConfig(ADC1, 1);//ADC 규칙 그룹 채널에 띄엄필렛 모드 ADC 구성DiscModeCmd(ADC1, ENABLE);//ADC 규칙 그룹 채널을 지정할 수 있는 인터럽트 모드
이 두 라이브러리 함수는 ADC를 인터럽트 모드로 설정하고 인터럽트 모드로 설정한 후 매번 소프트웨어 변환이 시작된 후에 해당하는 변환된 채널 데이터를 읽는 것이다.
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//지정한 ADC의 소프트웨어 변환 시작 기능while(!ADC GetFlagStatus(ADC1, ADC FLAG EOC)));//변환 완료 대기 adc1 = ADCGetConversionValue(ADC1);//최근 ADCx 규칙 그룹의 변환 결과를 반환합니다.

좋은 웹페이지 즐겨찾기