Connect with us

stm32 problem with ADC conversion

Discussion in 'Microcontrollers, Programming and IoT' started by mike wax, Mar 26, 2017.

  1. mike wax

    mike wax

    33
    1
    Oct 10, 2016
    can someone check my setup please? in line 1 the MCU, an stm32F070RB, does a conversion and in line 5 it does another one. but the second conversion fails and the program hangs up at line 7. if i suppress line 7 the program will continue uninterrupted.
    the ADC register values are shown below.

    Code:
    1 ADC1->CR |= (uint32_t)ADC_CR_ADSTART;  // start first conversion
    2 count = 0;
    3 while(((ADC1->ISR & ADC_ISR_EOC) == (uint32_t)reset) && (count < timeout)) count++; // wait for end of conversion
    4 capVL = (uint16_t)ADC1->DR;  // read value
    5 ADC1->CR |= (uint32_t)ADC_CR_ADSTART;  // start second conversion
    6 count = 0;
    7(//) while(((ADC1->ISR & ADC_ISR_EOC) == (uint32_t)reset) && (count < timeout)) count++;
    8 capVH = (uint16_t)ADC1->DR;  // read value


    ADCregs.png
     
    Last edited: Mar 26, 2017
  2. AdamSant

    AdamSant

    5
    5
    Oct 6, 2016
    Well, in normal program flow the program should not hang due to your timeout condition. Are count and timeout declared as the same kind of data type? Check that both variables (or constant) are defined as the same amount of bits; i.e. make sure that count is not wrapping around creating a condition were it can never reach the value of timeout.

    If you can confirm the above, to me it would seem like the controller is not operating in normal program flow and thus might be stuck in your Interrupt service routine (ISR). Are you using an ISR? If yes can you kindly attach that here? If you are able to use step debugging, you can easily confirm this; place a breakpoint on "count++" and keep resuming operation. If the breakpoint is not being hit consecutively, your program is stuck somewhere. I would start with this as a first step.
     
    mike wax and Arouse1973 like this.
  3. mike wax

    mike wax

    33
    1
    Oct 10, 2016
    well that's a good idea i'll try it although sometimes Eclipse doesn't work so well on breakpoints.
    there is not an interrupt routine, not for the ADC. as for the variable, count is an unsigned int, and timeout is a macro: "#define timeout 10"
    the essential code is as follows:

    Code:
    uint8_t  i, v;
    uint8_t word[8] = { 'w', 'o', 'r', 'd', 32, 32, 32 };
    int main(void)
    {
    // usb setup-----------------
      HAL_Init();
      SystemClock_Config();
      MX_GPIO_Init();
      MX_USB_DEVICE_Init();
    // ADC setup-----------------
      ADC1_Config();
      v = 1;
    
      while (1)
      {
         i = checkVcap(); // <--ADC routine where it hangs
         if(v++ % 25 == 0) VCP_write(word, strlen((const char *)word);// <- usb send word
         HAL_Delay(10);
         v++;
      }
    }
    
    i don't think the usb would be pulling an interrupt since it should be done sending by the time the ADC routine is called.
    thanx
     
    Last edited: Mar 26, 2017
  4. Arouse1973

    Arouse1973 Adam

    5,164
    1,078
    Dec 18, 2013
    Hi Mike. Not sure how big your code is but it would be good if you could post your main code in full, so we can have a look for you.
    Thanks
    Adam
     
    mike wax likes this.
  5. AdamSant

    AdamSant

    5
    5
    Oct 6, 2016
    Just to make sure that the USB is not interfering with the rest of the program, you can temporarily comment out all the USB initialisation and function calls, that way you can be sure.

    You can also try disabling global interrupts on the controller. It could be the case that the compiler assumes that an ISR should be found; when this happens, the program counter proceeds to a location where an ISR would usually be. While i never encountered this on M0s, i had this issue before on other architectures.
     
    mike wax likes this.
  6. mike wax

    mike wax

    33
    1
    Oct 10, 2016
    yeah i'm tryin to trim it down to simplify the problem but it's a big job because the Eclipse IDE has no facility to copy a project or to save it under a different name. but yeah that's what i'll do.
     
  7. mike wax

    mike wax

    33
    1
    Oct 10, 2016
    OK this is some freaky shit. the usb was the only program output i had, so i had to set up a LED for output, then i Xed out the usb function and ran it. Lo and behold, THAT WAS IT! the program stopped hanging at the ADC routine. the usb, it seems, was interfering.
    here's where it gets weird. the LED is blinking so i know it's running. then, of course, i re-included the usb function and not only does it continue to run correctly, it outputs the right #s, so i know the ADC is reading the accurate voltage. four days i been at it, and i still have no clue what was wrong.
    so the problem is resolved but never solved.
    go figure....
    and thanx guys for the assistance :)
     
    Arouse1973 likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-