STM32 ADC1_DMA
ADC規(guī)則數(shù)據(jù)寄存器(ADC_DR) 0x4001244C
本文引用地址:http://m.butianyuan.cn/article/201612/325212.htm/**************************************************************
ADC PB1_ADC9
***************************************************************/
#include "STM32Lib\stm32f10x.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
extern u16 adc;
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* PB1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/***********************************************************************************************************************
// ADC1
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//獨立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//連續(xù)多通道模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//連續(xù)轉(zhuǎn)換
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//轉(zhuǎn)換不受外界決定
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右對齊
ADC_InitStructure.ADC_NbrOfChannel = 1;//掃描通道數(shù)
ADC_Init(ADC1, &ADC_InitStructure);
//ADC1 Regular Channel1 Configuration
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_1Cycles5);//通道X,采樣時間為1.5周期,1代表規(guī)則通道第1個
ADC_Cmd (ADC1, ENABLE); // Enable ADC1 //使能或者失能指定的ADC
ADC_SoftwareStartConvCmd(ADC1,ENABLE); // Start ADC1 Software Conversion //使能或者失能指定的ADC的軟件轉(zhuǎn)換啟動功能
}
u16 TestAdc(void)
{
u16 adc;
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)==RESET); //檢查制定ADC標志位置1與否 ADC_FLAG_EOC 轉(zhuǎn)換結(jié)束標志位
adc=ADC_GetConversionValue(ADC1);
return adc;//返回最近一次ADCx規(guī)則組的轉(zhuǎn)換結(jié)果
}
*****************************************************************************************************************/
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&adc;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_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);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
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 = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel9 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_1Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
這個是抄意法半導體官方例程 注釋部分是以前寫的例程相較而言,感覺官方的就是做的細 比如這個calibaration之前我是從不注意這個問題的,以后我也會多多參考 官方的例程。
送圖一張。
評論