cancel
Showing results for 
Search instead for 
Did you mean: 

Multi Channels ADC of STM8L DISCOVERY (STM8L152C6T6) ADC

Ali Abdal-Kadhim
Associate II
Posted on May 31, 2018 at 08:54

Hello all,

1) I am new to ST MCUs, and I had developed a simple code based on STM8L DISCOVERY (STM8L152C6T6) to read three ADC channels (IN_16, IN_17, and IN_18) once at a time in sequence and print them to the UART as shown in the code below.

         Apparently, it works fine to read the three ADC channels. However, I would like to ask the experts, Please what is the optimal code to do it? and how to optimize my code?.

2) One more thing please, what is the correct way to make the mentioned MCU

(STM8L152C6T6) 

goes into a deep sleep mode for a certain amount of time (for example 10 seconds) directly after it finishes reading the three ADC channels and prints them to the UART? (i.e. the MCU will sleep>wakeup>sleep... continuously).

Thank you very much in advance,

#include 'stm8l15x.h'

#include 'delay.h'

#include <string.h>

///////////////////////////string send function////////////////

int uart_Swrite(const char *str) {

char x;

for(x = 0; x < strlen(str); x++) {

while(!(USART1->SR & USART_SR_TC));

USART_SendData8(USART1, str[x]);}

return(x);}

//////////////////////////////////////////////////////////////////////////

float vlt;

main()

{

CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);

/////////////////////adc init/////////////////////////////////

ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_2);

CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);

ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);

ADC_Cmd(ADC1, ENABLE);

///////////////////uart////////////////////////////////////////

CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);

USART_Init(USART1, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Tx | USART_Mode_Rx);

uart_Swrite('UART SEND WORKS FINE!\n');

delay_ms(1000);

/////////////////////////loop///////////////////////////////

while (1)

{

/////////////////////////////////1/////////////////

ADC_ChannelCmd(ADC1, ADC_Channel_18, ENABLE);//PB0

ADC_ChannelCmd(ADC1, ADC_Channel_17, DISABLE);//PB1

ADC_ChannelCmd(ADC1, ADC_Channel_16, DISABLE);//PB2

ADC_SoftwareStartConv(ADC1);

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

{

}

vlt=((ADC_GetConversionValue(ADC1)*5.00)/4095);

uart_Swrite('ADC_1 = >');

while(!(USART1->SR & USART_SR_TC));

USART_SendData8(USART1,

vlt

);

/////////////////////////////////2/////////////////////

ADC_ChannelCmd(ADC1, ADC_Channel_18, DISABLE);//PB0

ADC_ChannelCmd(ADC1, ADC_Channel_17, ENABLE);//PB1

ADC_ChannelCmd(ADC1, ADC_Channel_16, DISABLE);//PB2

ADC_SoftwareStartConv(ADC1);

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

{

}

vlt=((ADC_GetConversionValue(ADC1)*5.00)/4095);

uart_Swrite(' ADC_2 = >');

while(!(USART1->SR & USART_SR_TC));

USART_SendData8(USART1,

vlt

);

/////////////////////////////////3/////////////////////

ADC_ChannelCmd(ADC1, ADC_Channel_18, DISABLE);//PB0

ADC_ChannelCmd(ADC1, ADC_Channel_17, DISABLE);//PB1

ADC_ChannelCmd(ADC1, ADC_Channel_16, ENABLE);//PB2

ADC_SoftwareStartConv(ADC1);

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

{

}

vlt=((ADC_GetConversionValue(ADC1)*5.00)/4095);

uart_Swrite(' ADC_3 = >');

while(!(USART1->SR & USART_SR_TC));

USART_SendData8(USART1,

vlt

);

while(!(USART1->SR & USART_SR_TC));

USART_SendData8(USART1, '\n');

delay_ms(100);

}

}
4 REPLIES 4
henry.dick
Senior II
Posted on May 31, 2018 at 23:34

'

Please what is the optimal code to do it?'

What's optimal is dependent on your objective - which you didn't articulate.

Posted on June 01, 2018 at 06:49

Thank you dhenry pretty much for your response, 

      I meant by my question is, is this the correct way to read multi ADCs channels of the 

(STM8L152C6T6)

? or there is a more accurate method to do this? 

      The objective is, the readings should be done in short time so then the MCU goes into low power mode to save the energy. 

     Also, how to let the

(STM8L152C6T6)

goes into the ultra-low-power mode?. This part is very important.

Many thanks.

Posted on June 01, 2018 at 13:03

'

The objective is, the readings should be done in short time'

then check out the datasheet on the various timing requirement for the adc module.

Posted on June 05, 2018 at 11:40

thank u very much for ur kind response. according to my understandings from the datasheet I wrote the above-mentioned code to read multi-channels of the ADC.  so according to ur vast experience does the way that I have constructed my code is correct?

thanks