目录
1.adc.c
#include "adc.h"
void ADC_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
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);
}
//冒泡排序,取中间两个数的平均数
int ReadAdcValue(void)
{
int i=0;
int j=0;
int AdcBuff[50];
for(i=0;i<50;i++)
{
AdcBuff[i]=ADC_GetConversionValue(ADC1);
}
for(i=0;i<50-1;i++)
{
for(j=0;j<50-i-1;j++)
{
if(AdcBuff[j]>AdcBuff[j+1])
{
AdcBuff[j]=AdcBuff[j]^AdcBuff[j+1];
AdcBuff[j+1]=AdcBuff[j]^AdcBuff[j+1];
AdcBuff[j]=AdcBuff[j]^AdcBuff[j+1];
}
}
}
return (AdcBuff[25]+AdcBuff[26])/2;
}
2.adc.h
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
void ADC_GPIO_Init(void);
int ReadAdcValue(void);
#endif /* __ADC_H */
3.采集电位器R37电压
ADC_Value=ReadAdcValue()/4095.*3.3;
sprintf((char*)DisBuff,"DisBuff:%.2f",ADC_Value);
LCD_DisplayStringLine(Line1,DisBuff);