cancel
Showing results for 
Search instead for 
Did you mean: 

stm8l adc

dion  tan
Associate II
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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Szymon PANECKI
Senior III
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

View solution in original post

6 REPLIES 6
Szymon PANECKI
Senior III
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

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). 
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 

Posted on September 05, 2017 at 14:44

Hi max, thanks for that info. However, I tried to remove and connect nothing to pin d7. Suppose the MCU should convert and get nothing right? Yet, I still get fluctuated value around 1000. Something must be wrong with the coding right? Thanks in advance 

Max
ST Employee
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

.
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);