stm32f0, adc, basic single conversion, multiple channel - problem

tomas mainzer t.mainzer na gmail.com
Čtvrtek Prosinec 8 01:08:15 CET 2016


DD,
mam potize ze zprovoznenim ADC na STM32f042. Postupne ctu 4 vstupy
(2*Externi vstup, 2*Interni). Zrejme je nejaka potiz v casovani nebo
kontroluji spatne bity pro konec konverze(?) nebot pri cteni kanaly
vzajemne obcas "preskakuji" (tj. co mam na jednom vstupu ctu najednou na
jinem).
Nize prikladam kod, jestli by si nekdo nevsiml kde je chyba.
(pozn: vsechny priklady pro single conversion od ST (pro std lib) ctou
opakovane jen jeden vstup)
Dekuji, T.M.

//*** ADC initialisaion ****************
static void ADC_Config(void)
{
  ADC_InitTypeDef     ADC_InitStructure;
  GPIO_InitTypeDef    GPIO_InitStructure;

  /* GPIOC Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  /* Configure ADC Channels as analog input */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_InitStructure.GPIO_Pin = SIGN_EA;  //PA3, GPIO_Pin_3, ADC_Channel_3
  GPIO_Init(SIGP_EA, &GPIO_InitStructure);
  /* Configure ADC Channels as analog input */
  GPIO_InitStructure.GPIO_Pin = SIGN_EB;  //PA2, GPIO_Pin_2, ADC_Channel_2
  GPIO_Init(SIGP_EB, &GPIO_InitStructure);
  /* Configure ADC Channels as analog input */
  GPIO_InitStructure.GPIO_Pin = SIGN_AI0;  //PB0, Nastavo jako AI, neni
cteno
  GPIO_Init(SIGP_AI0, &GPIO_InitStructure);
  /* Configure ADC Channels as analog input */
  GPIO_InitStructure.GPIO_Pin = SIGN_AI1;  //PB1, Nastavo jako AI, neni
cteno
  GPIO_Init(SIGP_AI1, &GPIO_InitStructure);

  /* ADCs DeInit */
  ADC_DeInit(ADC1);

  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStructure);
  ADC_ClockModeConfig(ADC1,ADC_ClockMode_AsynClk);
  ADC_AutoPowerOffCmd(ADC1,DISABLE);
  /* Configure the ADC1 in continuous mode with a resolution equal to 12
bits  */
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge =
ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC Calibration */
  ADC_GetCalibrationFactor(ADC1);

  /* Enable the ADC peripheral */
  ADC_Cmd(ADC1, ENABLE);

  /* Wait the ADRDY flag */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));


  /* Enable VREF_INT & Temperature channel */
  ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor ,
ADC_SampleTime_55_5Cycles);
  ADC_TempSensorCmd(ENABLE);
  ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles);
  ADC_VrefintCmd(ENABLE);
}

//*** read ADCinput <channel>  ***********
uint16_t adc_read(ADC_TypeDef* ADCx, uint32_t channel, uint32_t
ADC_SampleTime) {

 /* Configure Channel */
 ADC_ChannelConfig(ADCx, channel, ADC_SampleTime);

 /* check if conversion was started, if not start */
 ADC_StartOfConversion(ADCx);

 /* wait for end of conversion */
 while((ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET));

 return ADC_GetConversionValue(ADCx);

}


#define ADCSAMPLETIME ADC_SampleTime_28_5Cycles

//****** ....main loop:  ****************************
....
a = adc_read(ADC1, ADC_Channel_17, ADCSAMPLETIME); //vref voltage
b = adc_read(ADC1,ADC_Channel_16,ADCSAMPLETIME); //internal temperature
c = adc_read(ADC1,SIGA_EB,ADCSAMPLETIME); //analog in, ADC_Channel_2
d = adc_read(ADC1,SIGA_EA,ADCSAMPLETIME); //analog in, ADC_Channel_3
.....
------------- další část ---------------
HTML příloha byla odstraněna...
URL: <http://list.hw.cz/pipermail/hw-list/attachments/20161208/719881d6/attachment.html>


Další informace o konferenci Hw-list