2020-09-21 06:03 AM
I am able to read my two ADC inputs individually when I implement the code shown in this example: http://embedded-lab.com/blog/starting-stm8-microcontrollers/13/
However, the ADC values in the buffers do not seem to be getting updated when I try to get the ADC to work in multi-input mode as per the example here: http://embedded-lab.com/blog/continuing-stm8-microcontroller-expedition/2/
I've looked at different locations in the ADC1_GetBufferValue() buffer, and none of them seem to be getting updated.
Perhaps I have modified something incorrectly for my application?
Here is my code, the specific device is the STM8S003F3.
#include "STM8S.h"
#include "STM8S_DELAY.h"
#include "MY_UART.h"
void Clock_Setup(void);
void GPIO_Setup(void);
void UART1_Setup(void);
void ADC1_Setup(void);
void TIM2_Setup(void);
void main (void)
{
char short_adc = 0x00;
char batt_adc = 0x00;
char tx_buffer[16];
int short_count = 0;
int i = 0;
// configure peripherals
GPIO_Setup();
Clock_Setup();
ADC1_Setup();
UART1_Setup();
while(1)
{
ADC1_ScanModeCmd(ENABLE);
ADC1_StartConversion();
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);
ADC1_ClearFlag(ADC1_FLAG_EOC); //clear the ADC conversion flag
batt_adc = ADC1_GetBufferValue(2) >> 2; //update the ADC value
short_adc = ADC1_GetBufferValue(3) >> 2;
//Transmit the ADC value to the terminal
my_itoa(batt_adc, tx_buffer); //convert the int to a string
UART_SendStr("ADC value: ");
UART_SendStr(tx_buffer);
UART_SendChar(10); //LF
UART_SendChar(13); //CR
}
}
void Clock_Setup(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
void GPIO_Setup(void)
{
// PC3 - Input
// PC4 - VBatt - ADC AIN2)
// PD2 - Short - ADC AIN3)
// PD3 - Low Batt LED
// PD4 - Strobe LED
// PD5 - UART1 TX
// PD6 - UART1 RX
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_IN_PU_IT);
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_FL_NO_IT);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(GPIOD, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);
}
void UART1_Setup(void)
{
UART1_DeInit();
UART1_Init(9600,
UART1_WORDLENGTH_8D,
UART1_STOPBITS_1,
UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE,
UART1_MODE_TXRX_ENABLE);
UART1_Cmd(ENABLE);
}
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_TypeDef)(ADC1_CHANNEL_2 | ADC1_CHANNEL_3)),
ADC1_ALIGN_RIGHT);
ADC1_DataBufferCmd(ENABLE);
ADC1_Cmd(ENABLE);
}