Skip to main content
Visitor II
August 31, 2017
Solved

stm8l adc

  • August 31, 2017
  • 2 replies
  • 2871 views
Posted on August 31, 2017 at 09:46

Hi guys, may some expert please help me? I am new to stm8l. I am currently using the chip stm8l151k4t6. I have connected battery voltage to pin D7, purpose of it is that i wanna monitor the status of the battery while the device is ON. However, my Lcd display keep giving me same value(4095). Where did my code goes wrong ? below are my codes. 

ADC_DeInit(ADC1); 

ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);

ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);

ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);

ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1); 

ADC_Cmd(ADC1, ENABLE); 

ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);

ADC_SoftwareStartConv(ADC1); 

while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); 

battery = ADC_GetConversionValue(ADC1); 

My lcd keep giving me 4095, which is (2^12) -1. 

    This topic has been closed for replies.
    Best answer by Szymon PANECKI
    Posted on August 31, 2017 at 18:33

    Hello Dion Tan,

    Looking at your code, potentially missing part can be enabling of a clock for ADC. You can enable this clock by calling below function:

    CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

    In addition I can see that you trigger ADC by software and in your case conversion takes place only once. So you need to put part of your code in the loop. Below I put a complete example code, which shows how to use ADC:

    #include 'stm8l15x.h'

    uint16_t ADC_value = 0;

    main()

    {

       CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

       ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_2);

       ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);

       ADC_Cmd(ADC1, ENABLE);

       ADC_ChannelCmd(ADC1, ADC_Channel_5, ENABLE);

       

       while (1)

       {

          ADC_SoftwareStartConv(ADC1);

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

          {

          }

          ADC_value = ADC_GetConversionValue(ADC1);

       }

    }

    Regards

    Szymon

    2 replies

    Visitor II
    August 31, 2017
    Posted on August 31, 2017 at 18:33

    Hello Dion Tan,

    Looking at your code, potentially missing part can be enabling of a clock for ADC. You can enable this clock by calling below function:

    CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

    In addition I can see that you trigger ADC by software and in your case conversion takes place only once. So you need to put part of your code in the loop. Below I put a complete example code, which shows how to use ADC:

    #include 'stm8l15x.h'

    uint16_t ADC_value = 0;

    main()

    {

       CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

       ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_2);

       ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);

       ADC_Cmd(ADC1, ENABLE);

       ADC_ChannelCmd(ADC1, ADC_Channel_5, ENABLE);

       

       while (1)

       {

          ADC_SoftwareStartConv(ADC1);

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

          {

          }

          ADC_value = ADC_GetConversionValue(ADC1);

       }

    }

    Regards

    Szymon

    dion tanAuthor
    Visitor II
    September 4, 2017
    Posted on September 04, 2017 at 08:30

    Hi 

    Szymon Panecki

    . Single mode indeed works, at least it is no longer stuck at 1023 (10  bits) or 4095 (12 bits), It is now between 660 to 670 until my mcu is off. However, there is one think i do not understand. I tried to use power supply as my battery source to calibrate, i wonder why when the voltage is increased, the number is displayed is dropping. When the voltage is decreasing, the converted adc value increased ? Should not it be linear ( like PIC chip). 
    ST Employee
    September 5, 2017
    Posted on September 05, 2017 at 07:26

    The ideal ADC result is the value rounded down of Vin.2^n/V

    DDA

     

    with   - Vin: ADC input voltage

             - V

    DDA

    : power supply voltage (often the same as V

    DD

    )

             - n number of bits of the ADC -> 2^n=4096 for a 12-bit ADC

    If you decrease the supply voltage, it is normal that the conversion result increases 

    ST Employee
    September 6, 2017
    Posted on September 06, 2017 at 08:19

    To operate correctly the ADC need a low impedance source.

    If you let the input floating you will get a random value depending on the previous conversion made and the parasitics in the device and on your board.

    For more information regarding the ADC, I recommend reading

    http://www.st.com/content/ccc/resource/technical/document/application_note/cc/b7/e9/1c/89/93/44/2f/CD00261960.pdf/files/CD00261960.pdf/jcr:content/translations/en.CD00261960.pdf

    .
    dion tanAuthor
    Visitor II
    September 7, 2017
    Posted on September 07, 2017 at 03:22

    Thanks for being helpful and responsive. I think I had solved my problem. It happen because of me putting these lines in loops (in function and keep calling it). My programming skills is really amateur. Really appreciate your helps. 

      CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

       ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_2);

       ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);

       ADC_Cmd(ADC1, ENABLE);

       ADC_ChannelCmd(ADC1, ADC_Channel_5, ENABLE);