Continuing the STM8 Expedition

STM8S105 Discovery

Creating Delays using Basic Timer

Rather than using loops of no operation instructions, it is wiser to use an unused or free timer to create software delays. It will be both accurate and precise. It can be done with any timer but using a basic timer will be a smart move because basic timers are usually for timebase generation purposes.

Hardware Connection

CubeMX

Code Example

 #include "STM8S.h"


void clock_setup(void);
void GPIO_setup(void);
void delay_us(signed int us);
void delay_ms(signed int ms);


void main(void)
{
       clock_setup();
       GPIO_setup();

       while(TRUE)
       {
              GPIO_WriteReverse(GPIOD, GPIO_PIN_0);
              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_HSIDIV1);
       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, 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, ENABLE);
}


void GPIO_setup(void)
{
       GPIO_DeInit(GPIOD);
       GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_OD_HIZ_SLOW);
}


void delay_us(signed int us)
{
       TIM4_DeInit();     

       if((us <= 200) && (us >= 0))
       {
              TIM4_TimeBaseInit(TIM4_PRESCALER_16, 200);
              TIM4_Cmd(ENABLE);  
       }
       else if((us <= 400) && (us > 200))
       {
              us >>= 1;
              TIM4_TimeBaseInit(TIM4_PRESCALER_32, 200);
              TIM4_Cmd(ENABLE);  
       }
       else if((us <= 800) && (us > 400))
       {
              us >>= 2;
              TIM4_TimeBaseInit(TIM4_PRESCALER_64, 200);
              TIM4_Cmd(ENABLE);  
       }
       else if((us <= 1600) && (us > 800))
       {
              us >>= 3;
              TIM4_TimeBaseInit(TIM4_PRESCALER_128, 200);
              TIM4_Cmd(ENABLE);  
       }
       while(TIM4_GetCounter() < us);
       TIM4_ClearFlag(TIM4_FLAG_UPDATE);
       TIM4_Cmd(DISABLE);    
}


void delay_ms(signed int ms)
{
       while(ms--)
       {
              delay_us(1000);
       };
}

 

Explanation

The very first important things to decide and note are the clock speeds of peripherals and CPU. Here both are set to 16MHz. Secondly, TIM4 is use to create software delays and so it should be clocked.

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
....
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, ENABLE);

The smallest unit of time that we usually use in microcontroller coding is microseconds and so it should be coded first:

void delay_us(signed int us)
{
       TIM4_DeInit();     

       if((us <= 200) && (us >= 0))
       {
              TIM4_TimeBaseInit(TIM4_PRESCALER_16, 200);
              TIM4_Cmd(ENABLE);  
       }
       else if((us <= 400) && (us > 200))
       {
              us >>= 1;
              TIM4_TimeBaseInit(TIM4_PRESCALER_32, 200);
              TIM4_Cmd(ENABLE);  
       }
       else if((us <= 800) && (us > 400))
       {
              us >>= 2;
              TIM4_TimeBaseInit(TIM4_PRESCALER_64, 200);
              TIM4_Cmd(ENABLE);  
       }
       else if((us <= 1600) && (us > 800))
       {
              us >>= 3;
              TIM4_TimeBaseInit(TIM4_PRESCALER_128, 200);
              TIM4_Cmd(ENABLE);  
       }
       while(TIM4_GetCounter() < us);
       TIM4_ClearFlag(TIM4_FLAG_UPDATE);
       TIM4_Cmd(DISABLE);    

}

TIM4 is deinitialized first. The input or argument to the delay_us function is the amount of time in microseconds we wish to delay. TIM4 is an 8-bit timer and so it can count up to 255. For simplicity, the count has been limited to 200. The first if-else conditional statement will configure the timer for periods from 0 to 200 microseconds. Input clock to the timer is prescaled by 16 and so it has a input clock frequency of 1MHz or 1 microsecond tick interval. In the while loop at the bottom of the function, TIM4’s counter count is checked against the variable us. The loop breaks when the counter exceeds the variable’s value, creating a software delay. For bigger delays, the other conditional statements are used.

For more bigger delays, delay milliseconds (delay_ms) function is used. This function loops calls of a thousand microseconds.

void delay_ms(signed int ms)
{
       while(ms--)
       {
              delay_us(1000);
       };
}

The demo here is just a simple LED blinker.

Demo

Software Delay

Related Posts

19 comments

  • Dear Shawon,
    Thank you for your useful website and articles.
    I have taken a look at ADC1_Init definition and I found it uses ADC1_ConversionConfig to set channels and conversion mode. So it seems using ADC1_Init once with all needed channels is enough. Am I right?
    Thank you very much.

  • Hello,
    I am learning stm8s, but i have a project in my mind, i am making Pong game with two encoders and PCD8544 LCD. Your tutorials are great and it’s very big source of knowledge for me, and i have a question about this article:
    Is it possible to use two encoder this way? Or can it be done in the other way? I have stm8s103f3p6, and i know it has 4 interrupt pins, so I thought about using these for two encoders, but then i saw your article about QEI

  • Hello Friend!!
    Your stuff is great !!!
    Would you have an example 433 mhz rf signal capture with flash code writing?

  • Ola amigo !!!
    Seu material é excelente!
    Você teria algum exemplo de captura de sinal rf 433 mhz com gravação na flash?

  • Hello friends, pleasant paragraph ɑnd good urging commented ɑt tһiѕ ρlace,
    I ɑm truⅼy enjoying ƅy tһese.

  • Hi,
    Thats great work
    Do you have steps for to work ST7735 TFT IN 8 BIT 8080 mode

  • I don’t even understand how I ended up right here, however
    I believed this publish used to be good. I don’t know who you are however definitely
    you’re going to a well-known blogger should you aren’t already.
    Cheers!

  • Sankalp Rai Gambhir

    Thanks Shawon, your blogs are indeed very helpful. Keep Growing.

  • why are you sending the received data back through TX pin?

  • Hi SHAWON SHAHRYIAR

    I am wondering how to get a Max31855 to talk to a STM8s via SPI.

    JP

    • What’s to wonder about it? It is a simple SPI communication and SPI for STM8 is no different from the SPI of other MCUs…. The following lines are taken from the device’s datasheet and the write up there states how to communicate with it:

      “Drive CS low to output the first bit on the SO pin. A complete serial-interface read of the cold-junction compensated thermocouple temperature requires 14 clock cycles. Thirty-two clock cycles are required to read both the thermocouple and reference junction temperatures (Table 2 and Table 3.) The first bit, D31, is the thermocouple temperature sign bit, and is presented to the SO pin within tDV of the falling edge of CS. Bits D[30:18] contain the converted temperature in the order of MSB to LSB, and are presented to the SO pin within tD0 of the falling edge of SCK. Bit D16 is normally low and goes high when the thermocouple input is open or shorted to GND or VCC. The reference junction temperature data begins with D15. CS can be taken high at any point while clocking out con-version data. If T+ and T- are unconnected, the thermocouple temperature sign bit (D31) is 0, and the remainder of the thermocouple temperature value (D[30:18]) is 1.”

Leave a Reply

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