STM32 透過ADC_GetFlagStatus 知道 EOC狀態並讀值

其實網路隨意google一下就很多資料

uint16_t readADC1(void) 
{ 
 uint16_t value;
 
 //ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5);

 // Start the conversion 
 ADC_SoftwareStartConvCmd(ADC1, ENABLE); 
 

 // Wait until conversion completion 
 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 
 ADC_ClearFlag(ADC1, ADC_FLAG_EOC);

 // Get the conversion value 
 value = ADC_GetConversionValue(ADC1); 
 ADC_SoftwareStartConvCmd(ADC1, DISABLE);

 return value;
}
void AdcPro(void)
{
 static uint16_t buf[SAMP_COUNT];
 static uint8_t write;
 uint32_t sum;
 uint8_t i;


 //buf[write] = ADC_GetConversionValue(ADC1); 原本直接讀取資料  
 buf[write] = readADC1();
 
 if (++write >= SAMP_COUNT)
 {
  write = 0;
 }

 sum = 0;
 for (i = 0; i < SAMP_COUNT; i++)
 {
  sum += buf[i];
 }
 g_usAdcValue = sum / SAMP_COUNT; /* 取平均 */

 //ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* 原本先為下次使用而enable */
}

發表迴響