Question
STM32F051 -> ADC1 -> sometimes no value returned....
Posted on June 07, 2015 at 11:43
Dear community,
I investigated the ADC_BasicExample given in the STM32F0xx_StdPeriph_Lib_V1.5.0 Database. I did a few changes since I want to analyze the converted value with the PC via UART. However I noticed if the EOC flag was cleared not necessarily the ADC returned a value. Is the interrupt better suited than the polling method given in the basic example ?
Here is some code for review:
//some functions to trigger the praser located on a remote pc
int SendChar(char ch) //copied from USART example
{
// Place your implementation of fputc here
// e.g. write a character to the USART
USART_SendData(USART1, (uint8_t) ch);
// Loop until transmit data register is empty
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
void print(char * string) {
char primer[] = ''print'';
int count;
//send primer
for(count = 0; count < 5; count++) SendChar(primer[count]);
//now sen the string to print
while(!(*string == '\0')) {
SendChar(*string);
string++;
}
SendChar(10); //now send the end of the string
}
void Send_value(int * value) {
char primer[] = ''value'';
int count;
//send primer
for(count = 0; count < 5; count++) SendChar(primer[count]);
//now send the char to the pc via uart
SendChar(*value);
}
//ADC1 Init
void ADC_Config(void) //copied from ADC_BasicExample
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// GPIOC Periph clock enable
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
// ADC1 Periph clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// Configure ADC Channel11 as analog input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// ADCs DeInit
ADC_DeInit(ADC1);
// Initialize ADC structure
ADC_StructInit(&ADC_InitStructure);
// Configure the ADC1 in continuous mode with a resolution equal to 8 // 12 bits
ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;// ADC_Resolution_12b; //ADC_Resolution_8b;//ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
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);
// Convert the ADC1 Channel 11 with 5 Cycles as sampling time
ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles);
// ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles);
// ADC Calibration
ADC_GetCalibrationFactor(ADC1);
// Enable the ADC peripheral
ADC_Cmd(ADC1, ENABLE);
// Wait the ADRDY flag
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
// ADC1 regular Software Start Conv
ADC_StartOfConversion(ADC1);
}
void InitPeriphery() {
COM_USART1_Init();
ADC_Config();
}
int main(void) {
int received;
InitPeriphery();
while(1) {
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
received = ADC_GetConversionValue(ADC1);
if(received) Send_value(&received ); // somtimes ADC_GetConversionValue was passed however access to ''received'' was somehow impossible
print(''+ \0''); //waste some time to check the praser is working
print('' + \0'');
print('' + \0'');
print('' + \0'');
print('' + \0'');
print('' + \0'');
print('' + \0'');
};
}
I would be very if someone could check the code for mistakes-
Thank you very much.