AnsweredAssumed Answered

stm8s003f3 adc

Question asked by postema.nick on May 11, 2015
Latest reply on Nov 16, 2017 by Rita Jonkute
Hello everyone

I am new to this forum, so hello. I am also new to STM en C. I am trying to learn everything step by step but get the stuck on the adc.

I am trying to get the adc working on stm8s003f3 with the following code in STVD with Cosmic, and send the result to the pc using UART. Can anybode tell my what i am doing wrong? adc_value is 1010 from ADXL335. Same setup on arduino gives a reading somewhere around 350.

Any suggestion is welkom... Already thanks

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stdio.h"
 
#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */
 
        int i;
    int nCount = 0;
    int adc_value = 0;
    int adc_h = 0;
    int adc_l = 0;
    int nAdc_Buffer[10] = {0,0,0,0,0,0,0,0,0,0};
    int nAdc_Hold = 0;
 
void main(void)
{
  char ans;
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  UART1_DeInit();
  UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
              UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
    
 
  /* ADC 1 Configuration ----------------------------------------*/
GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT );
    ADC1_DeInit();
    ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_0, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM,DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, DISABLE );
   
 
    ADC1_StartConversion();
         
 
  while (1)
  {
    for(i=0;i<20000;i++) // A delay loop
        {
            nop();
        }
                 
                for ( nCount = 0; nCount< 10; nCount++){
            nAdc_Buffer[nCount] = ADC1_GetConversionValue();
            nAdc_Hold += nAdc_Buffer[nCount];
        }
        nAdc_Buffer[0] = nAdc_Hold / 10;
        adc_value = nAdc_Buffer[0];
                 
                 for ( nCount = 0; nCount< 10; nCount++){
            nAdc_Buffer[nCount] = 0;
        }
        nAdc_Hold = 0;
                 
 
         printf(" %d\n",adc_value);
         printf("adc value\n\r");
                     
  }
}
 
 
PUTCHAR_PROTOTYPE
{
  /* Write a character to the UART1 */
  UART1_SendData8(c);
  /* Loop until the end of transmission */
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
 
  return (c);
}
 
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
  char c = 0;
#else
  int c = 0;
#endif
  /* Loop until the Read data register flag is SET */
  while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET);
    c = UART1_ReceiveData8();
  return (c);
}
 
#ifdef USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif


Outcomes