cancel
Showing results for 
Search instead for 
Did you mean: 

ADC initialization error. USART conflict?

jesperhede
Associate II
Posted on December 10, 2014 at 23:04

Hello,

I'm trying to initialize my ADC to continuously sample, but I must have overlooked something since I'm stuck when waiting for ADC_FLAG_ADONS. I'm programming to the STM32L152C Discovery board, but want to move my code to the

STM32L151CBT6

when I've confirmed the functionality. I'm also using port A as a USART port, does this conflict in setting up the ADC? I cannot find anything suggesting it, but as I said, I might have overlooked some detail. The last thing on the serial port to come out is ''a1''. Code snippets:


void
Init_RCC()

{

/* Enable MSI and wait for it to be ready.

* The Cmd call is implicty since it restarts after every board reset.

*/

RCC_MSICmd(ENABLE);

while
(RCC_GetFlagStatus(RCC_FLAG_MSIRDY) == RESET);


/* Allow access to the RTC */

PWR_RTCAccessCmd(ENABLE);


/* LSE Enable */

RCC_LSEConfig(RCC_LSE_ON);

/* Wait until LSE is ready */

while
(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);


/* Reset RCC */

RCC_DeInit();


/* Set MSI as SYSCLOCK */

RCC_SYSCLKConfig(RCC_SYSCLKSource_MSI); 
//MSI


/* Setting the SYSCLOCK frequency */

RCC_MSIRangeConfig(RCC_MSIRange_6); 
// Range_6 = 4.194MHz


/* Enable GPIO port B (contains LEDs) */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);


/* Allow access to the RTC */

PWR_RTCAccessCmd(ENABLE);


/* Reset RTC Backup Domain */

RCC_RTCResetCmd(ENABLE);

RCC_RTCResetCmd(DISABLE);


/* RTC Clock Source Selection */

RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); 


/* Enable the RTC */

RCC_RTCCLKCmd(ENABLE); 

RCC_MSICmd(ENABLE); 

}


void
Init_ADC()

{

GPIO_InitTypeDef GPIO_InitStruct;

ADC_InitTypeDef ADC_InitStructure;


/* GPIO Init. Set PA5 as analog input*/

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStruct);


/* ADC Init. */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);


ADC_DeInit(ADC1);

ADC_StructInit(&ADC_InitStructure);

ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

ADC_InitStructure.ADC_ScanConvMode = ENABLE;

ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

// ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

//ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T9_TRGO; // Trigger on timer 9 update event

//ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

ADC_InitStructure.ADC_NbrOfConversion = 1;

ADC_Init(ADC1, &ADC_InitStructure);

ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);

/* Enable ADC1 */

ADC_Cmd(ADC1, ENABLE);


/* Wait until ADC1 ON status */

printText(
''a1''
);

/* Wait until ADC1 ON status */

while
(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)

{

}

printText(
''a222''
);

}


void
Init_USART1(
void
)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;


/* Enable bus clocks */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);


/* Set USART1 Tx (PA.09) as AF push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);


/* Set USART1 Rx (PA.10) as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);


/* Attach Pin 9 and 10 to USART port */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); 


/* Initialize USART parameters */

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No ;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure); 

USART_Cmd(USART1, ENABLE);

}


void
printText(
char
* t)

{

uint8_t i = 0;

for
( i = 0; i<strlen(t); i++ )

{

Usart1Put(t[i]);

}

}


int
main(
void
)

{ 

Init_RCC();

Init_USART1();

Init_LEDs();


printText(
''main1''
);

Init_Timer10();


printText(
''main2''
);

Init_Timer11(); 


printText(
''main3''
);

Init_ADC(); 


printText(
''main4''
);

Init_TimerInterrupt(); 


printText(
''main6''
);


while
(1)

{

// Do it forever

}

}

#stm32-adc-timer-initialization
1 REPLY 1
Posted on December 11, 2014 at 01:35

I'm trying to initialize my ADC to continuously sample, but I must have overlooked something since I'm stuck when waiting for ADC_FLAG_ADONS.

Most probably that HSI isn't clocking, and the ADC is reliant on that?

...
/* Enable the HSI oscillator */
RCC_HSICmd(ENABLE);
/* Check that HSI oscillator is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..