반응형

// ADC


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Elevator (ARM)
* ADC 
1. H/W 연결
 - PB0에 가변저항이 연결되어 있음
 /**
  ******************************************************************************
  * @file    main.c
  * @author  Ac6
  * @version V1.0
  * @date    01-December-2013
  * @brief   Default main function.
  ******************************************************************************
*/
 
 
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
void ADC_init(){
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 
    ADC_InitTypeDef ADC_InitStruct;
    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStruct.ADC_ExternalTrigConv = DISABLE;
    ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    ADC_InitStruct.ADC_NbrOfConversion =1;
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStruct.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADC1,&ADC_InitStruct);
 
    ADC_Cmd(ADC1, ENABLE);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_8,1,ADC_SampleTime_84Cycles);
}
int ADC_Read(){
    ADC_SoftwareStartConv(ADC1);
    while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
    return ADC_GetConversionValue(ADC1);
}
int main(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
    ADC_init();
    int j = 0;
    while(1){
         j = ADC_Read();
    }
}
 
cs


반응형

'Study > 32-bit MCUs' 카테고리의 다른 글

[Elevator(ARM)] Flash memory  (0) 2018.10.26
[Elevator(ARM)] BLE USART  (0) 2018.10.25
[Elevator(ARM)] key Matrix  (0) 2018.10.25
[Elevator(ARM)] DoorLock  (0) 2018.10.25
[Elevator(ARM)] Calculator  (0) 2018.10.25

+ Recent posts