STM32 ADC多通道 關(guān)鍵代碼
#==========================================================
define ADC1_DR_Address ((u32)0x4001244C)
vu16 AD_Value[2];
==============================================================
關(guān)鍵代碼
void ADC1_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //連續(xù)轉(zhuǎn)換開啟
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2; //設(shè)置轉(zhuǎn)換序列長度為2
ADC_Init(ADC1, &ADC_InitStructure);
//ADC內(nèi)置溫度傳感器使能(要使用片內(nèi)溫度傳感器,切忌要開啟它)
ADC_TempSensorVrefintCmd(ENABLE);
//常規(guī)轉(zhuǎn)換序列1:通道10
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_13Cycles5);
//常規(guī)轉(zhuǎn)換序列2:通道16(內(nèi)部溫度傳感器),采樣時(shí)間>2.2us,(239cycles)
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 2, ADC_SampleTime_239Cycles5);
// Enable ADC1
ADC_Cmd(ADC1, ENABLE);
// 開啟ADC的DMA支持(要實(shí)現(xiàn)DMA功能,還需獨(dú)立配置DMA通道等參數(shù))
ADC_DMACmd(ADC1, ENABLE);
// 下面是ADC自動(dòng)校準(zhǔn),開機(jī)后需執(zhí)行一次,保證精度
// 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));
// ADC自動(dòng)校準(zhǔn)結(jié)束---------------
}
/*******************************************************************************
* Function Name : DMA_Configuration
* Description : DMA設(shè)置:從ADC模塊自動(dòng)讀轉(zhuǎn)換結(jié)果至內(nèi)存
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)&AD_Value;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//BufferSize=2,因?yàn)锳DC轉(zhuǎn)換序列有2個(gè)通道
//如此設(shè)置,使序列1結(jié)果放在AD_Value[0],序列2結(jié)果放在AD_Value[1]
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
//循環(huán)模式開啟,Buffer寫滿后,自動(dòng)回到初始地址開始傳輸
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);
//配置完成后,啟動(dòng)DMA通道
DMA_Cmd(DMA1_Channel1, ENABLE);
}
評(píng)論