STM32,ADC,多通道转换,是怎么样读取转换的结果的 ?
哪个大神能详细的讲解一下啊,一直没搞懂,STM32,ADC,多通道转换,但是获取ADC的值只有一个函数,怎么样读取多通道的值啊?谢谢了...
哪个大神能详细的讲解一下啊,一直没搞懂,STM32,ADC,多通道转换,但是获取ADC的值只有一个函数,怎么样读取多通道的值啊 ?谢谢了
展开
1个回答
展开全部
使用DMA传输或者使用ADC中断,建议使用DMA
/*********************************************************************************
* 文件名 :ADC.c
* 描述 :ADC模块
* 库版本 :ST3.5.0
*/
#include "system.h"
#include "ADC.h"
vu16 ADC_ConvertedValue[Sample_Num][Channel_Num];
void ADC_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = Sample_Num*Channel_Num;
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;
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);
DMA_Cmd(DMA1_Channel1,ENABLE);
}
void ADC1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
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_DMA_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADC1和ADC2工作在独立模式
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 = Channel_Num;//通道数目
ADC_Init(ADC1, &ADC_InitStructure);
// ADC_TempSensorVrefintCmd(ENABLE);//使能片内温度传感器
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //PCLK 6分频
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);//通道,转换次序,转换时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_55Cycles5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 6, ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);//开始校准
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能ADC的软件转换启动功能
}
uint16_t ReadADCAverageValue(uint16_t Channel)
{
uint8_t i;
uint32_t sum = 0;
for(i=0; i<Sample_Num; i++)
{
sum+=ADC_ConvertedValue[i][Channel];
}
return (sum/Sample_Num);
}
头文件
#ifndef __ADC_H
#define __ADC_H
/*使用示例:
*
printf("%d\r\n",ADC_ConvertedValue[0]);
printf("%d\r\n",ADC_ConvertedValue[1]);
printf("%d\r\n",ADC_ConvertedValue[2]);
printf("о?:%f\r\n",(1.43-(ADC_ConvertedValue[3]*3.3/4096))/0.0043+25);
*
sprintf(ch,"AD0 Value Is:%5d.",ADC_ConvertedValue[0]);
printf("%s",ch);
LCD_ShowString(20,20,(uint8_t*)ch,Color[i]);
sprintf(ch,"AD1 Value Is:%5d.",ADC_ConvertedValue[1]);
printf("%s",ch);
LCD_ShowString(20,40,(uint8_t*)ch,Color[i]);
sprintf(ch,"AD2 Value Is:%5d.",ADC_ConvertedValue[2]);
printf("%s",ch);
LCD_ShowString(20,60,(uint8_t*)ch,Color[i]);
sprintf(ch,"AD3 Value Is:%5d.",ADC_ConvertedValue[3]);
printf("%s",ch);
LCD_ShowString(20,80,(uint8_t*)ch,Color[i]);
*/
#define Channel_Num 4
#define Sample_Num 10
extern vu16 ADC_ConvertedValue[Sample_Num][Channel_Num];
void ADC1_Config(void);
uint16_t ReadADCAverageValue(uint16_t Channel);
#endif
追问
怎么没有主函数啊
追答
主函数调用这两个函数就行了
void ADC1_Config(void);
uint16_t ReadADCAverageValue(uint16_t Channel);
其他的没东西了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询