Starting STM8 Microcontrollers

STM8S003K3 Discovery

Timer Input Capture (TIM1 & TIM2)

Input capture is needed for measurements of pulses, pulse widths, frequencies, phase detection and similar stuffs. With external interrupts these measurements can be done with some limitations. However, using timer capture has some serious benefits. First of all, accuracy of measurements and secondly timer capture simplifies many tasks as timers themselves time stuffs properly. Dedicated hardware make stuffs like PWM measurement less complex and resource-friendly too.

STM8 timers have several capture channels just like output compare channels (PWM). The number of input capture channels is same as the number of PWM channels. Except basic timers all timers have input capture option.

Hardware Connection

TIM CAP

 

Code Example

In this demo, TIM2 is configured to generate PWM on its CH1 output. TIM1 is configured to capture every rising edge of incoming waveform at its input capture channel CH1. When a capture event occurs, the current time count of TIM1 is saved. By deducting the recent capture count from the previous capture count, we can measure time period of the incoming PWM signal and hence its frequency. If the frequency is too high, TIM1 may overflow and so we need to take care of it too. We, thus, need to check TIM1 overflow event too.

main.c

#include "STM8S.h"
#include "lcd.h"
 
 
unsigned int overflow_count = 0;
unsigned long pulse_ticks = 0;
unsigned long start_time = 0;
unsigned long end_time = 0;
 
 
void clock_setup(void);
void GPIO_setup(void);
void TIM1_setup(void);
void TIM2_setup(void);
void lcd_print(unsigned char x_pos, unsigned char y_pos, unsigned long value);
 
 
void main()
{               
    unsigned long time_period = 0;
                
    clock_setup();
    GPIO_setup();
    TIM1_setup();
    TIM2_setup();
    LCD_init();  
                
    LCD_clear_home(); 
    LCD_goto(0, 0);
    LCD_putstr("T/ms:");
    delay_ms(10);
                
    while(TRUE)
    {
          time_period = pulse_ticks;
          lcd_print(0, 1, time_period);
          delay_ms(400);
    };
}
 
 
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_CPUDIV2);
                
    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, DISABLE);
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
 
 
void GPIO_setup(void)
{               
     GPIO_DeInit(GPIOC);
     GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);
                
     GPIO_DeInit(GPIOD);
     GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
}
 
 
void TIM1_setup(void)
{
     TIM1_DeInit();
     TIM1_TimeBaseInit(2000, TIM1_COUNTERMODE_UP, 55535, 1);
     TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, 
                 TIM1_ICSELECTION_DIRECTTI, 1, 1);
     TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
     TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
     TIM1_Cmd(ENABLE);
     
     enableInterrupts();
}
 
 
void TIM2_setup(void)
{
     TIM2_DeInit();
     TIM2_TimeBaseInit(TIM2_PRESCALER_32, 1250);
     TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, 
                  TIM2_OCPOLARITY_LOW);
     TIM2_SetCompare1(625);
     TIM2_Cmd(ENABLE);
}
 
 
void lcd_print(unsigned char x_pos, unsigned char y_pos, unsigned long value)
{
     char tmp[6] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20} ;
 
     tmp[0] = (((value / 100000) % 10) + 0x30);
     tmp[1] = (((value / 10000) % 10) + 0x30);
     tmp[2] = (((value / 1000) % 10) + 0x30);
     tmp[3] = (((value / 100) % 10) + 0x30);
     tmp[4] = (((value / 10) % 10) + 0x30);
     tmp[5] = ((value % 10) + 0x30);
                
     LCD_goto(x_pos, y_pos);
     LCD_putstr(tmp);   
}

 

stm8_interrupt_vector.c (Interrupt vector address part only)

….
{0x82, (interrupt_handler_t)TIM1_UPD_IRQHandler}, /* irq11 */
{0x82, (interrupt_handler_t)TIM1_CH1_CCP_IRQHandler}, /* irq12 */
….

 

stm8s_it.h (Top part only)

#ifndef __STM8S_IT_H
#define __STM8S_IT_H
 
 
@far @interrupt void TIM1_UPD_IRQHandler(void);
@far @interrupt void TIM1_CH1_CCP_IRQHandler(void);
 
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
….

 

stm8s_it.c (Top part only)

#include "stm8s.h"
#include "stm8s_it.h"
 
 
extern unsigned int overflow_count;
extern unsigned long pulse_ticks;
extern unsigned long start_time;
extern unsigned long end_time;
 
 
void TIM1_UPD_IRQHandler(void) 
{
     overflow_count++;
     TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
     TIM1_ClearFlag(TIM1_FLAG_UPDATE);
}
 
 
void TIM1_CH1_CCP_IRQHandler(void) 
{
     end_time = TIM1_GetCapture1();
     pulse_ticks = ((overflow_count << 16) - start_time + end_time);
     start_time = end_time;
     overflow_count = 0;
     TIM1_ClearITPendingBit(TIM1_IT_CC1);
     TIM1_ClearFlag(TIM1_FLAG_CC1);
}

 

Explanation

The clocks and peripherals are set first. We are using 2 MHz peripheral clock and the CPU is running at 500 kHz.

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV2);
….
….            
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);

GPIOs must be set too. Since TIM2 is to output PWM, its CH1 must be set output. Likewise, TIM1’s CH1 GPIO must be set as an input.

GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);
                
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);

TIM1 need to be configured for input capture. We need to set time base for TIM1 first. It is set as such that TIM1 will overflow every second. Then we set input capture channel by specifying the edge sensitivity, channel, mode and scalars. Since we will be using interrupts, we must enable relevant interrupts.

void TIM1_setup(void)
{
     TIM1_DeInit();
     TIM1_TimeBaseInit(2000, TIM1_COUNTERMODE_UP, 55535, 1);
     TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, 
                 TIM1_ICSELECTION_DIRECTTI, 1, 1);
     TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
     TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
     TIM1_Cmd(ENABLE);
     
     enableInterrupts();
}

TIM2 is set for PWM generation on its CH1. The generated PWM will have a frequency of 50Hz and 50% duty cycle. The setup of TIM2 should be by now self-explanatory:

void TIM2_setup(void)
{
     TIM2_DeInit();
     TIM2_TimeBaseInit(TIM2_PRESCALER_32, 1250);
     TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 1000, 
                  TIM2_OCPOLARITY_LOW);
     TIM2_SetCompare1(625);
     TIM2_Cmd(ENABLE);
}

In the vector table of stm8_interrupt_vector.c file, we need to specify the interrupts we will be using:

{0x82, (interrupt_handler_t)TIM1_UPD_IRQHandler}, /* irq11 */
{0x82, (interrupt_handler_t)TIM1_CH1_CCP_IRQHandler}, /* irq12 */

We have to specify the interrupt subroutine (ISR) prototype functions in the stm8s_it.h file. These functions are the places where the code will jump when respective interrupt occurs:

@far @interrupt void TIM1_UPD_IRQHandler(void);
@far @interrupt void TIM1_CH1_CCP_IRQHandler(void);

The ISR functions are coded in the stm8s_it.c file:

void TIM1_UPD_IRQHandler(void) 
{
     overflow_count++;
     TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
     TIM1_ClearFlag(TIM1_FLAG_UPDATE);
}

The first part is dealing with TIM1 overflow. If a capture occurs when TIM1 count is near to reset value we need to take this account. This part does so and an overflow counter is incremented.

void TIM1_CH1_CCP_IRQHandler(void) 
{
     end_time = TIM1_GetCapture1();
     pulse_ticks = ((overflow_count << 16) - start_time + end_time);
     start_time = end_time;
     overflow_count = 0;
     TIM1_ClearITPendingBit(TIM1_IT_CC1);
     TIM1_ClearFlag(TIM1_FLAG_CC1);
}

The second part is where TIM1 capture is recorded. Once a rising edge is captured, an interrupt is issued. In the interrupt, we must first save the current TIM1 counter count in the variable named end_time. The formula for pulse tick is then computed. Note how the TIM1 overflow is addressed in the formula. The new start time should be the previous capture time because we need to deduct old capture count from new capture count. Lastly, overflow counter, capture flag and pending interrupts are cleared.

In the main loop, we are just displaying the time period of capture in a LCD while everything is being processed in the background by interrupts:

while(TRUE)
{
     time_period = pulse_ticks;
     lcd_print(0, 1, time_period);
     delay_ms(400);
};

 

Demo

Capture capture

Related Posts

100 comments

  • Pingback: آموزش میکروکنترلر STM8 قسمت سوم: کامپایلر و پروگرامر - سیسوگ - Sisoog

  • Pingback: قسمت دوم:معرفی بردهای Discovery میکروکنترلر STM8 - سیسوگ - Sisoog

  • Pingback: میکروکنترلر STM8 چیست و از کجا آمده است؟ - سیسوگ - Sisoog

  • Hi SHAWON SHAHRYIAR,

    I have been working through your articles and trying to understand as much as possible. I am currently looking at the AWU,

    I have brought in the project, compiled and built with no errors. :-)

    However when running the program the CPU goes into the halt mode, and never Auto wakes up! :-(

    Is there some more work that has to be done in the interrupt side of things? Does the project need the ITC.c and ITC.h files and some adjustments in there?

    Could I also have the delay.c and delay .h files please.

    Andy.

    • By the way, I am using the STM8SVL Discovery with the 003K processor on it also so all hardware is identical.

    • No, nothing needs to be done in the interrupt “itc” files but you need to add some lines in the “it” interrupt files:

      in the “it” header file add the following line:

      @far @interrupt void AWU_trigger_IRQHandler(void);

      and in the “it” source file add the following lines:

      void AWU_trigger_IRQHandler(void)
      {
      AWU_GetFlagStatus();
      }

      Change the interrupt vector file as shown below:

      #include "stm8s_it.h"

      typedef void @far (*interrupt_handler_t)(void);

      struct interrupt_vector {
      unsigned char interrupt_instruction;
      interrupt_handler_t interrupt_handler;
      };

      //@far @interrupt void NonHandledInterrupt (void)
      //{
      /* in order to detect unexpected events during development,
      it is recommended to set a breakpoint on the following instruction
      */
      // return;
      //}

      extern void _stext(); /* startup routine */

      struct interrupt_vector const _vectab[] = {
      {0x82, (interrupt_handler_t)_stext}, /* reset */
      {0x82, NonHandledInterrupt}, /* trap */
      {0x82, NonHandledInterrupt}, /* irq0 */
      {0x82, AWU_trigger_IRQHandler}, /* irq1 */
      {0x82, NonHandledInterrupt}, /* irq2 */
      {0x82, NonHandledInterrupt}, /* irq3 */
      {0x82, NonHandledInterrupt}, /* irq4 */
      {0x82, NonHandledInterrupt}, /* irq5 */
      {0x82, NonHandledInterrupt}, /* irq6 */
      {0x82, NonHandledInterrupt}, /* irq7 */
      {0x82, NonHandledInterrupt}, /* irq8 */
      {0x82, NonHandledInterrupt}, /* irq9 */
      {0x82, NonHandledInterrupt}, /* irq10 */
      {0x82, NonHandledInterrupt}, /* irq11 */
      {0x82, NonHandledInterrupt}, /* irq12 */
      {0x82, NonHandledInterrupt}, /* irq13 */
      {0x82, NonHandledInterrupt}, /* irq14 */
      {0x82, NonHandledInterrupt}, /* irq15 */
      {0x82, NonHandledInterrupt}, /* irq16 */
      {0x82, NonHandledInterrupt}, /* irq17 */
      {0x82, NonHandledInterrupt}, /* irq18 */
      {0x82, NonHandledInterrupt}, /* irq19 */
      {0x82, NonHandledInterrupt}, /* irq20 */
      {0x82, NonHandledInterrupt}, /* irq21 */
      {0x82, NonHandledInterrupt}, /* irq22 */
      {0x82, NonHandledInterrupt}, /* irq23 */
      {0x82, NonHandledInterrupt}, /* irq24 */
      {0x82, NonHandledInterrupt}, /* irq25 */
      {0x82, NonHandledInterrupt}, /* irq26 */
      {0x82, NonHandledInterrupt}, /* irq27 */
      {0x82, NonHandledInterrupt}, /* irq28 */
      {0x82, NonHandledInterrupt}, /* irq29 */
      };

      Sorry for the trouble…. I probably forgot to state these in the code and explanation segments…. Will be updating the post…. Thanks for this catch…. :)

  • Hey,
    Grateful for the tutorial! Nice work.
    I tried this section of your tutorial and got 33 errors after building:
    it was due to commenting of :

    @far @interrupt_handler_t void NonHandledInterrupt (void)
    {
    return;
    }

    because these were used in the interrupt vector struct again. I uncommented those lines and got one error:
    “unknown space: interrupt_handler_t”

    what should i do now?
    please help me!
    Thanks alot.

  • Hi

    Thank you for the post,
    I ve used your code for another sensor and noticed that in the i2c read routine you’ve forgot to send a stop to release the clock line. Modification is as follows

    ..

    Regards

    unsigned int BH1750_read_word(void)
    {
    unsigned long value = 0x0000;
    unsigned char num_of_bytes = 0x02;
    unsigned char bytes[2] = {0x00, 0x00};

    while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(BH1750_addr, I2C_DIRECTION_RX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    while(num_of_bytes)
    {
    if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
    {
    if(num_of_bytes == 0)
    {
    I2C_AcknowledgeConfig(I2C_ACK_NONE);
    I2C_GenerateSTOP(ENABLE);
    }

    bytes[(num_of_bytes – 1)] = I2C_ReceiveData();
    num_of_bytes–;
    }
    };

    /// the following line should be added
    I2C_GenerateSTOP(ENABLE);

    ….

    }

  • Hello Sir,
    I am trying to compile the code of led blink. It gives me error like this

    Compiling main.c…
    cxstm8 -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\project\stm8s_stdperiph_examples\adc2\adc2_continuousconversion” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\Hstm8″ -clDebug\ -coDebug\ main.c
    #error cpstm8 ..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc\stm8s_uart1.h:106(13) missing enumeration member
    #error cpstm8 ..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc\stm8s_uart1.h:113(13) missing enumeration member
    main.c:
    The command: “cxstm8 -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\project\stm8s_stdperiph_examples\adc2\adc2_continuousconversion” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    • Can you help me?

      • Why are you adding ADC2 header and source files? Are you not using STM8S003?

        • stm8s_conf.h this file i am added from ADC2

          • Add only the hardware files that your target chips supports…. For example, you cannot add ADC2 for STM8S003…. This is so as STM8S003 doesn’t have these hardware blocks…. Likewise if you are using STM8S207, you cannot add UART2 files for the same reason…. If you don’t add required files then you will get errors…. You’ll also get errors if you add files that are not applicable for your target MCU….

  • Hi
    I am using stm8s003f3 pin out board and with this code I am just testing output at micro controller pin 2 i.e. TX
    I am testing this using an oscilloscope.
    but not able to get any output
    Can you please help?

    Thanks

  • SSI_Electronics

    Hello
    I have followed the code as per the explanation.
    but I’m getting garbage values on serial port.
    Can you suggest any debugging steps??
    Thanks in advanced.

    PS: Great tutorial. It’s helping me a lot.

    • 1. Check wiring for loose connections….
      2. Use a stable and noise-free power source like a battery-bank to power up the board…. Noise from PC port may cause unwanted issues….
      3. Use CP2102 or FT232-based USB-TTL converter…. Don’t use PL2303-based ones because most are clones or fakes….
      4. Use a short USB cable….
      5. Use external pull-up resistors for both TX and RX pins….
      6. Try with another board if these don’t solve the issue…. It should not behave as you are saying because the code has no issues….

  • santosh kharade

    Serial receiving program is not working

  • Hello sir
    Do you tell me how generate pwm in pin no. B5 & B5
    in Stm8 board

    • Which STM8 chip? Because different chips have different pin mapping and sometimes there are alternative pins for such purposes…. Check with STM8CubeMX if that pin is available for that purpose….

  • Santosh Kharade

    i have done code for multi channel adc but its not working ????
    when i use single channel adc its working properly…………

  • Hello guys
    here i’m again for ask your help. I wrote a very easy program but when i try to send to stm8 the light programmer give me this error :

    Programming…
    PROGRAM MEMORY:
    >Erase not feasible
    >Programming
    Error: Device not recognized !

    some idea ?
    Thank you
    Sergio

  • First thank you so much for this wonderful tutorial, i’m new with STM8 i follow all you steps but i still have a problem and here i’m please can you help me this is the error message.
    Thank you
    Sergio

    ———– Project test_prov – STM8 Cosmic – Configuration Debug ————-

    Compiling main.c…
    cxstm8 -i..\..\en.stsw-stm8069\stm8s_stdperiph_lib\project\stm8s_stdperiph_template -igpio -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c
    #error cpstm8 gpio\inc\stm8s.h:74 “Please select first the target STM8S/A device used in your application (in stm8s.h file)”
    main.c:
    The command: “cxstm8 -i..\..\en.stsw-stm8069\stm8s_stdperiph_lib\project\stm8s_stdperiph_template -igpio -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    test_prov.elf – 2 error(s), 0 warning(s)

  • Hi Shawon ,
    Excellent article on stms8.
    within 3 days i was able to understand STMS8 very clearly.
    can you send me delay library files please.
    i tried to create my own without any success.
    warm regards

  • hi sir now also getting problem in compilation time …..’

    so plz can u send me the soft copy of software wat properly working at u…??

    my emain id…

    imran.vectorindia@gmail.com

    • I’m using the same software you are using – the latest one out there…. and so someone sending it has no meaning…. I suggest that you uninstall it and give it fresh retry, following all the steps carefully…. No one can help you with licensing issue unless to do it correctly….

  • hi geting poroblem while click on programmer ten my softare automatically close tab

  • hi sir…

    i getting problem for license configured……………. at compile time getting error like that…………. plz help out

    #error cpstm8 C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\HSTM8\mods0.h:1 FlexLM Cannot find license file.
    main.c:
    The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    alpha_lcd.elf – 3 error(s), 0 warning(s)

  • helo…we hav stm8svl discovery..i need embedded c program for the glowing of led,connecting ir receiver and also powersupply…for connecting these things i need code can u please guide me

  • Thanks for this article..
    I am looking for microsd card interface with STM8s discovery board. can you please provide the tutorial for microsd card?

  • In relation to disabling unused peripheral clocks as suggested by you:

    I found the following list of peripherals in the STM8S105 Discovery Board: Advanced control timer (TIM1), General-purpose timers (TIM2 and TIM3), Basic timer (TIM4), SPI, I2C, UART2, Window WDG, Independent WDG, ADC1,
    AWU timer, Beeper.

    Is this all?

    I could not find the clock peripheral configuration function for Window WDG, Independent WDG, Beeper in the Standard Peripheral Library.
    Is it not required?

    P.S. I am new to MCU programming with a little experience on Arduino. So please forgive me if the question is foolish.

    • 1. STM8S003K3 Discovery – the one I use for the blog is different from STM8S105…. They have different peripherals and so there will be differences in peripheral clocks…. Please check the device datasheet/STM8CubeMX for available hardware….

      2. WWDG and IWDG are set in option bytes and they don’t use peripheral/CPU clock…. Their oscillator is separate…. Due to these they don’t have clock configurations like other peripherals….

      P.S: No question is a noob question…. every question is allowed no matter how silly it may look….

  • Hi please someone tel me where can i find stm8s_delay.h and stm8s_delay.c
    Thanks

  • Hi , please someone tel me . Where can i find stm8s_delay.h and stm8s_delay.c
    Thanks ..

  • i am using stms003f3p6 controller..i have done as u told exactly but tim1 input capture is not displaying any value …according to your article it should display 10…i dont have any errors everything is fine…pulse on tim2 is ok ..it is coming as u said…but capture not working..please help me …thank you…

  • Hi Sir,
    i have just follwed your instruction provied in yout tutorial in main.c

    #include “stm8s.h”

    main()
    {
    while (1);
    }

    This error comes, can you please check why this error come, i am new and learn the controller programming.

    main.c:
    The command: “cxstm8 -i”d:\other datasheet\new folder\lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

    main.o – 2 error(s), 0 warning(s)

    Thanks,

  • Hi there, i solve the previous ADC problem. Anyway, u make some good tutorial on stm8 chips. Nice work

    • What was causing that issue? How did you solve it?

      • because i put all these

        ADC_DeInit(ADC1);
        ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);
        ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);
        ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);
        ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1);
        ADC_Cmd(ADC1, ENABLE);
        ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);

        in ‘while(1)’. It keeps looping.

        Once i put it before ‘while (1)’, it becomes normal.

  • Hi there, thanks for sharing. I learn alot thru your example, but I have encounter a problem with adc. Im currently using stm8l151k4t6. I wanna use the pin D7 as to read the voltage from my battery and monitor it (display on LCD). Can u please check where it goes wrong on my code?

    ADC_DeInit(ADC1);
    ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_4Cycles);
    ADC_SchmittTriggerConfig(ADC1, ADC_Channel_7, DISABLE);
    ADC_ChannelCmd(ADC1, ADC_Channel_7, ENABLE);
    ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_1);
    ADC_Cmd(ADC1, ENABLE);
    ADC_ExternalTrigConfig(ADC1, ADC_ExtEventSelection_None, ADC_ExtTRGSensitivity_All);
    ADC_SoftwareStartConv(ADC1);
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
    battery = ADC_GetConversionValue(ADC1);

  • hi shawon great tutorials it would be nice too if you covered the coide compiler for arm it’s a good free compiler

  • hi shawon thanks for your response and support it turns out that my problem was a license issue I stupidly thought i use one license on multiple pc’s I used the license for a particular computer for mine that’s why I’ve been having compilation issues you have to use the license file stm8 sends you that is generated by your own pc else it won’t compile it’ll jus kip throwing you errors

  • It seems that you didn’t follow my instructions completely…. I found some issues:

    1. You have included header and source files of ADC2, CAN, UART2, etc which are unavailable in STM8S103…. Add only files for the peripherals available in your target MCU…. Exclude the rest….

    2. You have not included stm8s_delay header and source files…. Either disable it from stm8s_conf.h header file if you are not going to use it or add both the header and the source files for it in your project….

    Resolve these and you are good to go….

    Thanks….

  • I haven’t compiled any of the examples yet I just tried to compile the main source file just for test purpose n it just keeps showing me the same errors

  • yes the comic compiler was installed properly i even received a license file from my mail with the instructions to copy the license file to the license folder in the install directory if you fail to do this it will keep popping up for you to put in the license file. iincluded all the source files and header files as instructed i am working with the cheap stm8s103f i uncommented the stm8s103f in the stm8.h header file as you instructed and just to compile the main file it just throws me errors

  • no I just followed your instructions I just compiled it after setup i did not write any code yet just compiled the default main file

    • Need more details….

      1. The version of STVD and Cosmic
      2. Which example is giving this issue?
      3. The chip you are using if it other than STM8S003
      4. Have you tried to compile something else other than my examples?
      5. Are the paths to libraries and other folders properly added?
      6. Is the Cosmic compiler registered properly?

  • it throws this line The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
    exit code=1.

  • hi I’m having compilation issues when I compile it just throws me error about comic compiler located in the program file ialso excluded unwanted header files but my problems still weren’t solved

  • hi thanks for the tutorials but i am having compiler error issues even when I remove unnecessary header files it say comic compiler error

  • I am new to STM8S003k3 Discovery board.I have done UART communication using STM standard peripheral library example. Now I want to define any other GPIO pin for UART communication. Is it possible to use d0 and d1 pin for the UART communication? If yes, then please let me know how?

  • thank you very much for this article!

  • Thank you for your time and effort.

  • Good job bro

  • Thank you for this exceptionally comprehensive article, much appreciated

    Rando!

Leave a Reply

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