2017-08-31 12:46 AM
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.
Solved! Go to Solution.
2017-08-31 09:33 AM
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
2017-08-31 09:33 AM
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
2017-09-04 01:30 AM
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).2017-09-05 12:26 AM
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
2017-09-05 07:44 AM
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
2017-09-05 11:19 PM
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
.2017-09-06 08:22 PM
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);