STM8 timer 4 block diagramSTM8S Timer 4 block diagram

Today we are going to see stm8 timer tutorial for creating delay_ms and delay_us function in Cosmic C compiler. This STM8 Timer tutorial will help you to understand the stm8 basic timers. This STM8 TIMER 4 example will not use any interrupt and help you to learn how to configure TIMER 4 of STM8 which is the 8-bit basic timer to create delay functions.

Related Background

For creating a COSMIC C project you can check our previous tutorial about interfacing 16×2 LCD with STM8 in Cosmic C from scratch code.

STM8 TIMER 4 Structure

As I mentioned above in STM8 TIMER 4 is 8-bit basic timer which do not have any external pin attached to this timer is basically a clocked counter which start counting from a preloaded value upto 255 (because of its 8-bit nature) and After the 255th count it goes overflow and again start counting from 0. If we are using Interrupt with the time an Interrupt will occure at every overflow and we can deside either this will only run for one timer or keep runing always. Here is a block diagram for the STM8 TIMER 4.

STM8 timer 4 block diagram
STM8S Timer 4 block diagram

AS you can see from this block diagram that TIME Base Unit is clocked via a Clock Controller. This clock controller is fed with system clock source which is the clock source of main microcontroller.

Registers for TIMER 4 in STM8

According to the reference manual for the STM8S microcontroller series Here is the list of available registers for the TIMER 4 in STM8S based microcontrollers.

Timer 4 Prescaler

Default Clock frequency in our case of STM8S103F3P6 microcontroller is 2 MHz internal clock. But we can change this up to 16MHz. For today’s STM8 TIMER 4 Example, we will keep this to 2MHz. To further downgrade this frequency we are using a Prescaler to provide the TIME Base Unit a 1Mhz frequency. So we are actually dividing the system clock with 2 to get half of 2Mhz. This 1Mhz frequency will help us to generate a 1us delay. So now because of 1MHz frequency, every tick of the timer will take exactly 1 microsecond. So all we have to do is to put a proper value into the Prescaler. The Prescaler register for TIMER 4 in STM8 is called TIM4_PSCR and only the last 3 bits are user writeable and the upper remaining 5 bits are reserved for the microcontroller.

We are writing 0x01 to this register and to reinitialize this value we are using the only available bit of TIM4_EGR register least significant bit (0th bit)

STM8S TIMER 4 Initialization

Now we are creating a function to initialize TIMER 4. The initialization process is pretty simple because we are not going to use any interrupt for this timer, so all we need to do is to provide one Auto reload value in TIM4_ARR register and Prescaler value in TIM4_PSCR register. So a TIMER 4 initialization function without interrupt will look something like as follows

void tim4_init(){
   TIM4_PSCR = 0x01;
   TIM4_ARR  = 0xFF;
   TIM4_CR1  = 0;
   TIM4_CR1  |= (1<<2);
   TIM4_EGR  = 1;
   TIM4_CNTR = 0x00;
   TIM4_IER &= ~(1<<0);  //Disable interrupt 
}Code language: JavaScript (javascript)

STM8 delay_us function

Now that we had created a function to initialize our timer we can easily create a function which will enable timer by setting 0th bit of TIM4_CR1 register and wait for TIM4_CNTR to reach a count of 0-255 (what ever required) and then finally disable Timer 4 by clearing 0th bit of TIM4_CR1 register again. So here is delay_us function which could generate microsecond delay from 0 to 255 microseconds.

void delay_us(unsigned char time){
   TIM4_EGR |= (1<<0);
   TIM4_CNTR = 0;
   TIM4_CR1 |= (1<<0);
   while(TIM4_CNTR<time);
   TIM4_CR1 &= ~(1<<0);
   TIM4_SR &= ~(1<<0); //clear flag

}Code language: JavaScript (javascript)

STM8 delay_ms function

Now if we want to create millisecond delay all we need to do is to take 100-microsecond delay 10th time the required millisecond delay time value. So here is a quick code for STM8 delay_ms function.

void delay_ms(unsigned int timer){
   time *= 10;
   while(time--);
   delay_us(100);
}Code language: JavaScript (javascript)

References:

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

One thought on “STM8 TIMER 4 Example in Cosmic C”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.