AnsweredAssumed Answered

Multi Channels ADC of STM8L DISCOVERY (STM8L152C6T6) ADC

Question asked by Ali Abdal-Kadhim on May 31, 2018
Latest reply on Jun 5, 2018 by Ali Abdal-Kadhim

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

Outcomes