新聞中心

STM32-ADC認識

作者: 時間:2016-11-26 來源:網(wǎng)絡(luò) 收藏
一、ADC配置的基本步驟:

1、打開DMA和ADC1的時鐘。

本文引用地址:http://m.butianyuan.cn/article/201611/321617.htm

在RCC_Configuration()中添加:

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

2、配置模擬IO輸入口

在GPIO_Configuration()中配置

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //ADC0 -light
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //ADC9-sound
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);

3、配置DMA初始化和ADC初始化

void DMA_Configuration(void)

void ADC1_Configuration(void)

4、啟動和讀值處理ADC數(shù)據(jù)

啟動:ADC_SoftwareStartConvCmd(ADC1, ENABLE);

等待DMA處理完成標志位:if(DMA_GetFlagStatus(DMA_FLAG_TC1)==1)

處理:

5、ADC硬件參考電壓的選擇:

void DMA_Configuration(void)
{
DMA_DeInit(DMA_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//定義DMA外設(shè)基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // 定義DMA內(nèi)存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外設(shè)作為數(shù)據(jù)傳輸?shù)膩碓矗ㄍ庠O(shè)傳到內(nèi)存) DMA_DIR_PeripheralDST——內(nèi)存?zhèn)鞯酵庠O(shè)
DMA_InitStructure.DMA_BufferSize = 3;//連續(xù)轉(zhuǎn)化3個AD通道值
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設(shè)寄存器地址不變
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//內(nèi)存寄存器地址遞增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //外設(shè)數(shù)據(jù)寬度32bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//內(nèi)存數(shù)據(jù)寬度32bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA模式是循環(huán)
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA優(yōu)先級
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//DMA沒有設(shè)置內(nèi)存到內(nèi)存?zhèn)鬏?br />DMA_Init(DMA_Channel1, &DMA_InitStructure);

DMA_Cmd(DMA_Channel1, ENABLE);
}

void ADC1_Configuration(void)
{
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;// ADC1 ADC2 在獨立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //ENABLE-ADC多通道掃描, DISABLE-ADC單通道掃描
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // ENABLE--ADC連續(xù)轉(zhuǎn)化模式 DISABLE--ADC單次轉(zhuǎn)化模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //由軟件觸發(fā)
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //數(shù)據(jù)向右對齊
ADC_InitStructure.ADC_NbrOfChannel = 3; //連續(xù)轉(zhuǎn)化3個AD通道值
ADC_Init(ADC1, &ADC_InitStructure);

//Enable Vrefint channel17
ADC_TempSensorVrefintCmd(ENABLE); //channel17

// ADC1 regular channel17 configuration
ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 1, ADC_SampleTime_28Cycles5); //Vref
// ADC1 regular channel0 configuration
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 2, ADC_SampleTime_28Cycles5); //light
// ADC1 regular channel9 configuration
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 3, ADC_SampleTime_28Cycles5); //sound

// ADC_RegularChannelConfig(ADC1, ADC_Channel_1,4, ADC_SampleTime_55Cycles5); //voltage

// 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));


}

void ADC_process_Voltage_Light_Voice(void)
{
ADC_Cmd(ADC1, ENABLE);
DMA_Cmd(DMA_Channel1, ENABLE);

ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Delay(20);
while(1)
{
if(DMA_GetFlagStatus(DMA_FLAG_TC1)==1)
{
DMA_ClearFlag(DMA_FLAG_TC1); // Clear Channel 1 DMA_FLAG_TC flag
break;
}
if((DMA_GetFlagStatus(DMA_FLAG_HT1)==1)||(DMA_GetFlagStatus(DMA_FLAG_TE1)==1))
{
DMA_ClearFlag(DMA_FLAG_HT1);
DMA_ClearFlag(DMA_FLAG_TE1);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

}
// voltage
adv[0] = (float)1.2*4096/(ADC_ConvertedValue[0]&0x0fff); //放大50倍
// light
adv[1] = (float)(ADC_ConvertedValue[1]&0x0fff)*1.2/(ADC_ConvertedValue[0]&0X0FFF); //放大100倍
// sound
adv[2] = (float)(ADC_ConvertedValue[2]&0x0fff)*1.2/(ADC_ConvertedValue[0]&0X0FFF); //放大100倍

ADC_Cmd(ADC1, DISABLE);
DMA_Cmd(DMA_Channel1, DISABLE);

Voltage = adv[0]*50;
Light = adv[1]*100;
Voice = adv[2]*100;
}



關(guān)鍵詞: STM32AD

評論


技術(shù)專區(qū)

關(guān)閉