cancel
Showing results for 
Search instead for 
Did you mean: 

Hi,I am working on STM8s0003f3.I need to read analog voltage from ADC_Channel_5 I have stuck at how to initialise channel cmd for the same as I dont find any ADC_Read examples.Can someone help me sort it out please

SRAM.11
Associate III
 
1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

I have formatted your code again and inserted it with the </> button under the text field:

ADC1_DeInit();
 
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_2, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL2, DISABLE); // OCD
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3, DISABLE); // TEMP SENSOR
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_4, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4, DISABLE); //VBAT
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_5, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL5, DISABLE); // MAINS VGE
 
ADC1_Cmd(ENABLE);
ADC1_StartConversion();
 
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);
ADC1_GetConversionValue();
ADC1_ClearFlag(ADC1_FLAG_EOC);

Well, you:

  • configured the ADC four times (line 3...6),
  • but only started it once (line 9)
  • and then read the value without using it (line 12)

For the first measurement of OCD you should only use channel 2, so delete lines 4...6 and in the old line 12 assign the return value of ADC1_GetConversionValue(); to a variable, e.g. result_OCD, which you can then use further.

Then do the same with channels 3...5, of course with a different result variable each (e.g. result_TEMP_SENSOR, etc).

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
Peter BENSCH
ST Employee

Welcome, @SRAM.1​, to the community!

You will find an example for the use of the ADC in STSW-STM8069:

Project\STM8S_StdPeriph_Examples\ADC2\ADC2_ContinuousConversion

Hope that helps?

Good luck!

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
SRAM.11
Associate III

Thanks for the reply but it doesn't helps as I need to read voltages from 3 channels as the example shows just convesrion with GPIO.As I am working with powersupplies,I need to get the ADC value .I have completed the samein STM8L15x but stucked at stm8s0003f

I don't quite understand your problem: the STM8S003 has an ADC with five multiplexed channels. The example mentioned uses the somewhat more powerful ADC2 with channel 9, but in principle it can also be used for ADC1 on one of the available channels AINx. You can therefore expand the measurement accordingly.

With the STM8S003, however, you have to bear in mind that VREF+ is internally permanently connected to VDDA. This means that you can only measure ratiometrically, i.e. relative to the supply voltage VDDA. For an absolute value measurement of a voltage at a GPIO, which must not exceed VDDA, you must therefore always perform a second comparison measurement at an external reference voltage. Then you can calculate the absolute value of the actual, first voltage.

Hope that helps?

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Show some code, it might help understand your approach and why it is failing.

It should be able to sequence between multiple channels, but will sample at different instances. ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SRAM.11
Associate III

ADC1_DeInit();

ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_2, ADC1_PRESSEL_FCPU_D2,

      ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL2,         // OCD

      DISABLE);

ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D2,

      ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3,        // TEMP SENSOR

      DISABLE);

ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_4, ADC1_PRESSEL_FCPU_D2,

      ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4,        //VBAT

      DISABLE);

ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_5, ADC1_PRESSEL_FCPU_D2,

      ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL5,        // MAINS VGE

      DISABLE);

 ADC1_Cmd(ENABLE);

ADC1_StartConversion();

while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);

 ADC1_GetConversionValue();

 ADC1_ClearFlag(ADC1_FLAG_EOC);

I am not sure what I am proceeding as a starter.

how can i get the value of these channels.It would be great if any link to learn more

Peter BENSCH
ST Employee

I have formatted your code again and inserted it with the </> button under the text field:

ADC1_DeInit();
 
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_2, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL2, DISABLE); // OCD
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3, DISABLE); // TEMP SENSOR
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_4, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4, DISABLE); //VBAT
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_5, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL5, DISABLE); // MAINS VGE
 
ADC1_Cmd(ENABLE);
ADC1_StartConversion();
 
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);
ADC1_GetConversionValue();
ADC1_ClearFlag(ADC1_FLAG_EOC);

Well, you:

  • configured the ADC four times (line 3...6),
  • but only started it once (line 9)
  • and then read the value without using it (line 12)

For the first measurement of OCD you should only use channel 2, so delete lines 4...6 and in the old line 12 assign the return value of ADC1_GetConversionValue(); to a variable, e.g. result_OCD, which you can then use further.

Then do the same with channels 3...5, of course with a different result variable each (e.g. result_TEMP_SENSOR, etc).

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
SRAM.11
Associate III

its really helping a lot Thank you so much.I will try the same.