2023-05-21 09:41 AM
Hi, everyone. I have been working with STM8S, i am did not work with this microcontroller before, i have some questions about ADC (i am using STM8S103F3).
1) The first problem that i had was about the values read, i am initialize the peripheral for work with multiple channel using scan mode. So, the value is read one time, after i need deinitialize and initialize the adc peripheral in the while main for get a new value. I was reading the manual and i found about "Buffer overrun flag" , how can i resolve that problem? I am using stm standard library.
2) The second is about the channels that i need read. Below is my code, i get one channel value, but i dont get multiple values because all adc buffer has the same value. I need change the register during the program for change the channel?
main() {
SystemClockConfig();
TIM4_Setup();
GPIO_Setup();
UART1_Setup();
ADC1_Setup();
while(1){
if(millis() - timeoutLed > 1000){
ADC1_ScanModeCmd(ENABLE);
ADC1_StartConversion();
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);
ADC1_ClearFlag(ADC1_FLAG_EOC);
a2Value = ADC1_GetBufferValue(2);
a3Value = ADC1_GetBufferValue(3);
vH = a2Value >> 8;
vL = a2Value & 0xFF;
UART1_SendData8(vL);
UART1_ClearFlag(UART1_FLAG_TXE);
delay(5);
UART1_SendData8(vH);
UART1_ClearFlag(UART1_FLAG_TXE);
delay(5);
vH = a3Value >> 8;
vL = a3Value & 0xFF;
UART1_SendData8(vL);
UART1_ClearFlag(UART1_FLAG_TXE);
delay(5);
UART1_SendData8(vH);
UART1_ClearFlag(UART1_FLAG_TXE);
delay(5);
ADC1_Setup();
timeoutLed = millis();
GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
}
}
}
void ADC1_Setup(void){
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
ADC1_CHANNEL_2,
ADC1_PRESSEL_FCPU_D18,
ADC1_EXTTRIG_GPIO,
DISABLE,
ADC1_ALIGN_RIGHT,
ADC1_SCHMITTTRIG_CHANNEL2,
DISABLE);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
ADC1_CHANNEL_3,
ADC1_PRESSEL_FCPU_D18,
ADC1_EXTTRIG_GPIO,
DISABLE,
ADC1_ALIGN_RIGHT,
ADC1_SCHMITTTRIG_CHANNEL3,
DISABLE);
ADC1_ConversionConfig(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_2 | ADC1_CHANNEL_3 , ADC1_ALIGN_RIGHT);
ADC1_DataBufferCmd(ENABLE);
ADC1_Cmd(ENABLE);
}