AnsweredAssumed Answered

ADC initialization error. USART conflict?

Question asked by hede.jesper on Dec 10, 2014
Latest reply on Dec 11, 2014 by Clive One
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:
001.void Init_RCC()
002.{
003.    /* Enable MSI and wait for it to be ready.
004.     * The Cmd call is implicty since it restarts after every board reset.
005.    */
006.    RCC_MSICmd(ENABLE);
007.    while(RCC_GetFlagStatus(RCC_FLAG_MSIRDY) == RESET);
008.     
009.    /* Allow access to the RTC */
010.    PWR_RTCAccessCmd(ENABLE);
011.     
012.    /* LSE Enable */
013.    RCC_LSEConfig(RCC_LSE_ON);
014.    /* Wait until LSE is ready */
015.    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
016. 
017.    /* Reset RCC */
018.    RCC_DeInit();
019. 
020.    /* Set MSI as SYSCLOCK */
021.    RCC_SYSCLKConfig(RCC_SYSCLKSource_MSI);         //MSI
022.         
023.    /* Setting the SYSCLOCK frequency */
024.    RCC_MSIRangeConfig(RCC_MSIRange_6);         // Range_6 = 4.194MHz
025.     
026.    /* Enable GPIO port B (contains LEDs) */
027.    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
028. 
029.    /* Allow access to the RTC */
030.  PWR_RTCAccessCmd(ENABLE);
031.     
032.    /* Reset RTC Backup Domain */
033.    RCC_RTCResetCmd(ENABLE);
034.    RCC_RTCResetCmd(DISABLE);
035. 
036.    /* RTC Clock Source Selection */
037.    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
038. 
039.    /* Enable the RTC */
040.    RCC_RTCCLKCmd(ENABLE);
041.    RCC_MSICmd(ENABLE);
042.}
043. 
044.void Init_ADC()
045.{
046.    GPIO_InitTypeDef GPIO_InitStruct;
047.    ADC_InitTypeDef ADC_InitStructure;
048.     
049.    /* GPIO Init. Set PA5 as analog input*/
050.    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
051.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
052.    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
053.    GPIO_Init(GPIOA, &GPIO_InitStruct);
054. 
055.    /* ADC Init. */
056.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
057. 
058.    ADC_DeInit(ADC1);
059.    ADC_StructInit(&ADC_InitStructure);
060.    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
061.  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
062.  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
063. // ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
064.  //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T9_TRGO;              // Trigger on timer 9 update event
065.  //ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
066.  ADC_InitStructure.ADC_NbrOfConversion = 1;
067.  ADC_Init(ADC1, &ADC_InitStructure);
068.  ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);
069.    /* Enable ADC1 */
070.  ADC_Cmd(ADC1, ENABLE);
071.     
072.  /* Wait until ADC1 ON status */
073.    printText("a1");
074.    /* Wait until ADC1 ON status */
075.  while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
076.  {
077.  }
078.    printText("a222");
079.}
080. 
081.void Init_USART1(void)
082.{
083.    GPIO_InitTypeDef GPIO_InitStructure;
084.    USART_InitTypeDef USART_InitStructure;
085.      
086.    /* Enable bus clocks */
087.    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
088.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
089.      
090.    /* Set USART1 Tx (PA.09) as AF push-pull */
091.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
092.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
093.    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
094.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
095.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
096.    GPIO_Init(GPIOA, &GPIO_InitStructure);
097.      
098.    /* Set USART1 Rx (PA.10) as input floating */
099.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
100.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
101.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
102.    GPIO_Init(GPIOA, &GPIO_InitStructure);
103.     
104.    /* Attach Pin 9 and 10 to USART port */
105.    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
106.    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);  
107.     
108.    /* Initialize USART parameters */
109.    USART_InitStructure.USART_BaudRate = 9600;
110.    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
111.    USART_InitStructure.USART_StopBits = USART_StopBits_1;
112.    USART_InitStructure.USART_Parity = USART_Parity_No ;
113.    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
114.    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
115.    USART_Init(USART1, &USART_InitStructure);
116.    USART_Cmd(USART1, ENABLE);
117.}
118. 
119.void printText(char* t)
120.{
121.    uint8_t i = 0;
122.    for( i = 0; i<strlen(t); i++ )
123.    {
124.        Usart1Put(t[i]);
125.    }
126.}
127. 
128.int main(void)
129.{  
130.    Init_RCC();
131.    Init_USART1();
132.    Init_LEDs();
133. 
134.    printText("main1");
135.    Init_Timer10();
136.     
137.    printText("main2");
138.    Init_Timer11();
139. 
140.    printText("main3");
141.    Init_ADC();
142. 
143.    printText("main4");
144.    Init_TimerInterrupt(); 
145. 
146.    printText("main6");
147. 
148.    while(1)
149.    {
150.            // Do it forever
151.    }
152.}

Outcomes