Спойлер

Код: Выделить всё
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_adc.h>
int ReadADC1()
{
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles);
ADC_SoftwareStartConv(ADC1);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
return ADC_GetConversionValue(ADC1);
}
void main(void)
{
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
GPIO_InitTypeDef GPIO_STR;
ADC_InitTypeDef ADC_STR;
ADC_CommonInitTypeDef ADC_CSTR;
GPIO_STR.GPIO_Mode=GPIO_Mode_AN;
GPIO_STR.GPIO_Pin=GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_STR);
ADC_DeInit();
ADC_CSTR.ADC_Mode=ADC_Mode_Independent;
ADC_CSTR.ADC_Prescaler=ADC_Prescaler_Div2;
ADC_CommonInit(&ADC_CSTR);
ADC_STR.ADC_ContinuousConvMode=DISABLE;
ADC_STR.ADC_DataAlign=ADC_DataAlign_Right;
ADC_STR.ADC_ExternalTrigConv=ADC_ExternalTrigConvEdge_None;
//ADC_STR.ADC_NbrOfConversion=ADC_N
ADC_STR.ADC_Resolution=ADC_Resolution_12b;
ADC_STR.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_STR);
ADC_Cmd(ADC1,ENABLE);
int adc_temp=0,sum=0;
while(1)
{
for (time = 0; time <= 1000; time++)
{
delay(300);
adc_temp=ReadADC1();
sum+=adc_temp;
}
sum/=1000;
}
}
Код: Выделить всё
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
