2019-11-20 01:33 PM
I would like to use 2 ADC channel in my program (0,3) using STM8S105C6. I tried to use ADC scan mode and tried to read buffer. it seems that it reads channel 0 and channel 3 for the first run but with change in analog volatge on both channels , only channel 0 gets updated to new only where channel 3 still shows old value.
here is snippet of code:
#include "STM8S.h"
#include "stm8s_delay.h"
#include "stm8s_tim3.h"
unsigned char bl_state;
unsigned char data_value;
unsigned char SampleCount;
void clock_setup(void);
void GPIO_setup(void);
void ADC1_setup(void);
void main()
{
unsigned int A0 = 0x0000;
unsigned int A1 = 0x0000;
float mv =0x0000000;
float mv1 =0x0000000;
float b1;
float b2;
clock_setup();
GPIO_setup();
ADC1_setup();
while(TRUE)
{
SampleCount = 0;
A0=0;
A1=0;
while(SampleCount < 10)
{
ADC1_ScanModeCmd(ENABLE);
ADC1_StartConversion();
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == FALSE);
ADC1_ClearFlag(ADC1_FLAG_EOC);
A0 += ADC1_GetBufferValue(0);
A1 += ADC1_GetBufferValue(1);
SampleCount++;
}
mv= A0*0.000322;
mv1= A1*0.000322;
if((mv >=1.5)&&(mv1>=1.5))
GPIO_WriteLow(GPIOD, GPIO_PIN_0);
else
GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
delay_ms(90);
};
}
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_HSIDIV4);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
void GPIO_setup(void)
{
GPIO_DeInit(GPIOB);
GPIO_Init(GPIOB, ((GPIO_Pin_TypeDef)(GPIO_PIN_0| GPIO_PIN_1)), GPIO_MODE_IN_FL_NO_IT);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_OD_HIZ_FAST);
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, ((GPIO_Pin_TypeDef)GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4), GPIO_MODE_OUT_PP_LOW_FAST);
}
void ADC1_setup(void)
{
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, //PB0 = LOAD_V_FBK = OUTPUT VOLTAGE
ADC1_CHANNEL_0,
ADC1_PRESSEL_FCPU_D18,
ADC1_EXTTRIG_GPIO,
DISABLE,
ADC1_ALIGN_RIGHT,
ADC1_SCHMITTTRIG_CHANNEL0,
DISABLE);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, //PB1 = BTV_ADC = BATTERY TERMINAL VOLTAGE
ADC1_CHANNEL_1,
ADC1_PRESSEL_FCPU_D18,
ADC1_EXTTRIG_GPIO,
DISABLE,
ADC1_ALIGN_RIGHT,
ADC1_SCHMITTTRIG_CHANNEL1,
DISABLE);
ADC1_ConversionConfig(ADC1_CONVERSIONMODE_CONTINUOUS,
((ADC1_Channel_TypeDef)(ADC1_CHANNEL_0 | ADC1_CHANNEL_1)),
ADC1_ALIGN_RIGHT);
ADC1_DataBufferCmd(ENABLE);
ADC1_Cmd(ENABLE);
}