您的位置 首页 分销

STM32-ADC知道

一、ADC配置的基本步骤:1、打开DMA和ADC1的时钟。在RCC_Configuration()中添加:RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA,ENABLE)

一、ADC装备的根本过程:

1、翻开DMA和ADC1的时钟。

在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数据

发动: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外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // 界说DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外设作为数据传输的来历(外设传到内存) DMA_DIR_PeripheralDST——内存传到外设
DMA_InitStructure.DMA_BufferSize = 3;//接连转化3个AD通道值
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设寄存器地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存寄存器地址递加
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //外设数据宽度32bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//内存数据宽度32bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA形式是循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//DMA没有设置内存到内存传输
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接连转化形式 DISABLE–ADC单次转化形式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //由软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据向右对齐
ADC_InitStructure.ADC_NbrOfChannel = 3; //接连转化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;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/fenxiao/257880.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部