cancel
Showing results for 
Search instead for 
Did you mean: 

STM8SK003 discovery. Issues with the ADC1_GetBufferValue function.

mcarv
Associate

The problem is, the channel0 works fine, and keeps reading the data as it should be, but the channel 1 just reads the data once. So I keep watching the data changing in one channel, but in the other one it keeps like it is.

There is any problem with my code? I've tested the Multi channel code example that you guys published some time ago, and this one do not works also (it kinda freezes the micro).

The full code for the main archive is shown bellow.

#include "STM8S.h"

unsigned int value = 0x00;

unsigned char n = 0x00;

unsigned char seg = 0x01;

const unsigned char num[0x0A] = 

 {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

void GPIO_setup(void);

void clock_setup(void);

void TIM4_setup(void);

void ADC1_setup(void);

void main(void)

{

unsigned int A0 = 0x0000;

unsigned int A1 = 0x0000;

   GPIO_setup();

   clock_setup();

   TIM4_setup();

ADC1_setup();

         

   while (TRUE)

   {

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

                 

if(GPIO_ReadInputPin(GPIOB, GPIO_PIN_7) == FALSE)

value=-16.145*(A1 / 4)*0.0195+61.437;

else

value=-16.145*(A0 / 4)*0.0195+61.437;

    delay_ms(999);

   };

}

void GPIO_setup(void)

{

  GPIO_DeInit(GPIOB);

GPIO_Init(GPIOB, GPIO_PIN_0, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOB, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT);

 

   GPIO_DeInit(GPIOC);

   GPIO_Init(GPIOC, ((GPIO_Pin_TypeDef)(GPIO_PIN_4 | GPIO_PIN_5 

        | GPIO_PIN_6 | GPIO_PIN_7)), GPIO_MODE_OUT_PP_HIGH_FAST);

         

   GPIO_DeInit(GPIOD);

   GPIO_Init(GPIOD, GPIO_PIN_ALL, GPIO_MODE_OUT_PP_HIGH_FAST);

}

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

   CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

         

   CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, 

   DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);

         

   CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);

   CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);

   CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, ENABLE);

   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, ENABLE);

}

void TIM4_setup(void)

{        

   TIM4_DeInit();

   TIM4_TimeBaseInit(TIM4_PRESCALER_32, 128);    

   TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);

   TIM4_Cmd(ENABLE);

   

   enableInterrupts();

}

void ADC1_setup(void)

{

  ADC1_DeInit();  

   

         

  ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, 

       ADC1_CHANNEL_0,

       ADC1_PRESSEL_FCPU_D18, 

       ADC1_EXTTRIG_GPIO, 

       DISABLE, 

       ADC1_ALIGN_RIGHT, 

       ADC1_SCHMITTTRIG_CHANNEL0, 

       DISABLE);

ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, 

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

}

0 REPLIES 0