Brise

STM32F401RE-nucleo Standard Peripheral ADC 본문

MCU

STM32F401RE-nucleo Standard Peripheral ADC

naudhizb 2017. 12. 7. 10:35
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stm32f4xx.h"
 
__IO uint16_t verti;
__IO float v = 0.0f; 
 
int main(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    
    RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE);    
 
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1 ;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
    ADC_CommonInit(&ADC_CommonInitStructure); 
        
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1, ENABLE);
    ADC_StructInit(&ADC_InitStructure);
    
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;    
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 1;
    ADC_Init(ADC1, &ADC_InitStructure);
 
    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles);
 
    ADC_Cmd (ADC1,ENABLE);    //enable ADC1
    while(1) {      
      ADC_SoftwareStartConv(ADC1); // start ONE conversion
      while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) {} 
      verti=ADC_GetConversionValue(ADC1);
      v = (float)verti/4096.0f*3.3f;
    }
 
cs


반응형
Comments