ADC Polling Mode 사용

CPU/STM32F103 2014. 4. 15. 13:19

이번엔adc를 폴링 모드로 사용해보자.

32F103C8T6 CPU의 PA0( ADC12_IN0 ) 포트로 전압을 읽어 보자

VREF는 핀수가 많은 칩은 VREF핀이 따로 나오지만 C8칩은 VDDA , VSSA와 VREF가 내부적으로 연결 되어 있따.


설정은 다음과 같다.

1. GPIO 설정

2. CLOCK설정

3. ADC 설정


void ADC2_volt_Configuration(void)

{

  ADC_InitTypeDef ADC_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;

  

  //====================================

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  /* Enable ADC2 and GPIOC clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2 , ENABLE);

  

  /* ADC2 configuration ------------------------------------------------------*/

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

  ADC_InitStructure.ADC_ScanConvMode = DISABLE;

  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_NbrOfChannel = 1;

  ADC_Init(ADC2, &ADC_InitStructure);


  /* ADC2 regular channel12 configuration */ 

  ADC_RegularChannelConfig(ADC2, ADC_Channel_0, 1, ADC_SampleTime_41Cycles5);


  /* Enable ADC2 */

  ADC_Cmd(ADC2, ENABLE);

  

  /* Enable ADC2 reset calibaration register */   

  ADC_ResetCalibration(ADC2);

  /* Check the end of ADC21 reset calibration register */

  while(ADC_GetResetCalibrationStatus(ADC2));


  /* Start ADC2 calibaration */

  ADC_StartCalibration(ADC2);

  /* Check the end of ADC1 calibration */

  while(ADC_GetCalibrationStatus(ADC2));


}


사용법은 요렇게. VDDA가 3.3V


uint16_t ADC2_vold_Read(void)

{

  uint16_t AD_value;


  ADC_SoftwareStartConvCmd(ADC2, ENABLE);

  //wait for conversion complete

  while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC)){}

  //read ADC value

  AD_value=ADC_GetConversionValue(ADC2);

  //clear EOC flag

  ADC_ClearFlag(ADC2, ADC_FLAG_EOC);


  return AD_value;

}