STM8 Tutorials #4 – ADC Interfacing

This is 4th tutorial for STM8 Microcontrollers. This post will give you a basic idea on using STM8 internal ADC.

All STM8 family devices feature 10/12 bit ADC as peripheral. ADC can be used in single or continuous conversion mode. This example code is tested on STM8S003F3P6 and STM8S105C6T6 controller but ideally it should work for every STM8 controller.

Experiment description

Schematic below shows the connections that I made in order to test the working of ADC peripheral. I wrote a simple code to change the LED blinking frequency according to the analog voltage input at Analog Channel 3 (PD2) of STM8S000F3P6. LED is connected to PD3 in source mode.

STM8 ADC Experiment setup

LED connected to PD3 will blink at a frequency determined by analog voltage input at PD2.

Code

#include "stm8s.h"

void myDelay(unsigned int value)
{
     unsigned int i,j;
     for(i=0;i<1000;i++)
     {
         for(j=0;j<value;j++);
     }
}

void GPIO_Config(void)
{ 
     GPIOD->DDR |= 1<<3; //PD.3 as output
     GPIOD->CR1 |= 1<<3; //push pull output
}

unsigned int readADC1(unsigned int channel) 
{
     unsigned int val=0;
     //using ADC in single conversion mode
     ADC1->CSR |= ((0x0F)&channel); // select channel
     ADC1->CR2 |= (1<<3); // Right Aligned Data
     ADC1->CR1 |= (1<<0); // ADC ON 
     ADC1->CR1 |= (1<<0); // ADC Start Conversion
     while(((ADC1->CSR)&(1<<7))== 0); // Wait till EOC
     val |= (unsigned int)ADC1->DRL;
     val |= (unsigned int)ADC1->DRH<<8;
     ADC1->CR1 &= ~(1<<0); // ADC Stop Conversion
     val &= 0x03ff;
     return (val);
}

int main(void)
{
      unsigned int stepNos;
      GPIO_Config();
 
      while(1)
      {
            stepNos=readADC1(3);
            GPIOD->ODR |= (1<<); // PD.0 = 1, LED ON
            myDelay(stepNos);
            GPIOD->ODR &= ~(1<<3); // PD.0 = 0, LED Off
            myDelay(stepNos);
       }
}

About author

Devesh Samaiya

Chief Tinkerer at Lonely Night Projects

Related Articles

7 Comments

  1. Scott Beasley December 10, 2016 at 10:13 pm

    Namaste Devesh,

    What compiler are you using for this example?

    regards…

    • Devesh Samaiya January 17, 2017 at 5:29 am

      Namaskar,

      I am using STVD with Cosmic C compiler for this example.

  2. Munish Kumar January 25, 2017 at 11:47 am

    Hi Devesh,

    Great to see a tut on STM8 ADC! I am using STM8L051F3 (TSSOP20 Pkg) & I am having trouble understanding the reference voltage selection & usage behavior for the ADC.

    Usually the ADC is configured to use either of AVCC, External Vref or Internal Vref. In STM8L051F3, Vref+ pin is physically AVCC pin so Vref+ is always AVCC. It also has an internal VREFINT = ~1.224V, but I see it can be used only as an ADC channel as VREFINT is only an input to the analog mux inside the ADC, so in my opinion it is not being used in the conventional sense.

    I have a signal in 50 – 1000mV range which I want to measure. How can I use the VREFINT as the analog reference voltage so that the dynamic range is utilized? Can’t use amplifiers here.

  3. Berkcan February 5, 2017 at 3:53 pm

    Hello how can i use standart lib functions.
    When i was trying use them ;

    Running Linker
    clnk -m Debug\blink.map -l”C:\Program Files (x86)\COSMIC\CXSTM8_EVAL\Lib” -o Debug\blink.sm8 Debug\blink.lkf
    #error clnk Debug\blink.lkf:1 symbol _CLK_DeInit not defined (Debug\main.o )
    #error clnk Debug\blink.lkf:1 symbol _CLK_HSIPrescalerConfig not defined (Debug\main.o )
    #error clnk Debug\blink.lkf:1 symbol _CLK_CCOConfig not defined (Debug\main.o )
    The command: “clnk -m Debug\blink.map -l”C:\Program Files (x86)\COSMIC\CXSTM8_EVAL\Lib” -o Debug\blink.sm8 Debug\blink.lkf ” has failed, the returned value is: 1
    exit code=1.

    blink.elf – 5 error(s), 0 warning(s)

  4. alex March 21, 2017 at 4:17 pm

    When I am trying to upload simple gpio code I am getting the following error

    Failed to set configuration with MCU name STM8S003F3: SWIM error [30006]: Comm init error: chip does not answer

    Can you please help me

    • alex March 21, 2017 at 4:18 pm

      When I am trying with the above still I am getting following error

      Failed to set configuration with MCU name STM8S003F3: SWIM error [30006]: Comm init error: chip does not answer

      Can you please help me

  5. Madeshwaran V June 30, 2017 at 11:16 am

    Can any one explain that the above ADC program. I cant understand the word stepnos, values, DRL,DRH and also why it is used please. Quikly.

Leave a reply

Your email address will not be published. Required fields are marked *

50 − = 40