STM8S_002_TIM precise delay (blocking)

Publisher:快乐的天使Latest update time:2020-02-16 Source: eefocusKeywords:STM8S  TIM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Ⅰ. Write in front

In some specific situations, precise delay (us level) is required, especially for the underlying driver. If software delay is used, the delay will change with the change of system clock and various factors. Therefore, TIM precise delay is required.


Blocking delay: From the start to the end of the delay, the program is blocked there and will not jump to other places (except interrupts) to execute the program. Friends who don’t understand can search for the answer online.


There are many types and functions of TIM. This article is about the basic knowledge and is relatively simple. For more powerful and practical functions of timers, please pay attention to my later articles.


For your convenience, the content of this article has been organized into a PDF file:


http://pan.baidu.com/s/1i5uWhJR


II. Basic knowledge of TIM

There are three types of timers in STM8S: basic timer, general timer and advanced timer. The basic timer is an 8-bit counting timer, and the general and advanced timers are 16-bit counting timers.


Different types of timers have different functions and complexities, and are suitable for different occasions. This article uses the most basic and simple 8-bit basic timer to explain the delay of TIM.


Emphasize one point: 8-bit counting timer, the maximum count value is 256.

TIM4 basic timer functions:


Ø 8-bit up-counting (UP-COUNTER) automatic reload counter;


Ø 3-bit programmable prescaler (modifiable during operation) providing 8 frequency division ratios: 1, 2, 4, 8, 16, 32, 64 and 128.


Ø Interrupt generation: If interrupts are enabled, an interrupt is generated when the counter is updated (counter overflows). This article does not enable interrupts.


III. Software Engineering Source Code

1. About the project

The engineering code provided in this article is based on the previous "STM8S_Demo" and modified by adding TIM timer. Beginners can refer to my previous corresponding basic articles, which are more detailed.


Software engineering source code implementation function: Change the LED on and off state through blocking delay (500ms) to observe the delay size. If you want to measure the accuracy of the delay, you can change TIMDelay_Nms(500) to TIMDelay_N10us(10) (delay 100us), and measure the frequency of the LED pin through an oscilloscope to be 5KHz (period 200us).


2. Software Overview

This article provides a simple introduction to the content included in software engineering:


System Initialization: System_Initializes


v BSP_Initializes: clock initialization CLK_Configuration and GPIO_Configuration initialization;


v TIMER_Initializes: Timer initialization, the focus of this article.


Function implementation: while(1)


3. Code Analysis Description

The contents of BSP_Initializes will not be described in detail here, please refer to the previous article: STM8S_001_GPIO Basics


This article focuses on the contents of the bsp_timer.c file:


A.TIMER_InitializesTimer initialization


void TIMER_Initializes(void)


{


  TIM4_TimeBaseInit(TIM4_PRESCALER_2, 79);


  TIM4_ClearFlag(TIM4_FLAG_UPDATE);


}


The software engineering we provide is to achieve a 10us delay, and the implementation formula is: 16MHz / 2 / (79+1) = 0.1MHz (100KHz).


The first parameter TIM4_PRESCALER_2: 2-frequency division, this parameter is as follows:


typedef enum


{


  TIM4_PRESCALER_1 = ((uint8_t)0x00),


  TIM4_PRESCALER_2 = ((uint8_t)0x01),


  TIM4_PRESCALER_4 = ((uint8_t)0x02),


  TIM4_PRESCALER_8 = ((uint8_t)0x03),


  TIM4_PRESCALER_16 = ((uint8_t)0x04),


  TIM4_PRESCALER_32 = ((uint8_t)0x05),


  TIM4_PRESCALER_64 = ((uint8_t)0x06),


  TIM4_PRESCALER_128 = ((uint8_t)0x07)


} TIM4_Prescaler_TypeDef;


The second parameter 79: The value of this parameter is actually the value of the auto-reload register. As can be seen from the formula, it is the source of the 10us delay.


Many people don’t understand why it is 79 instead of 80?


The reason is that the counting starts from 0, so 0 to 79 means counting 80, so here it is 79.


Statement TIM4_ClearFlag(TIM4_FLAG_UPDATE):


The meaning of this statement is very simple, clear the UPDATE update flag.


B. Delay N 10us: void TIMDelay_N10us(uint16_t Times)


void TIMDelay_N10us(uint16_t Times)


{


  TIM4_SetCounter(0); //Count value reset to zero


  TIM4_Cmd(ENABLE); //Start the timer


  while(Times--)


  {


    while(RESET == TIM4_GetFlagStatus(TIM4_FLAG_UPDATE));


    TIM4_ClearFlag(TIM4_FLAG_UPDATE);


  }


  TIM4_Cmd(ENABLE); //turn off the timer


}


Why is it N 10us?


From the above timer initialization, we can know that a counting process (delay) is 10us, and the parameter Times represents the number of times the delay of 10us is to be executed.


TIM4_SetCounter(0);


Before starting the timer each time, reset the count value to zero to ensure that the first count (delay) is accurate.


while(RESET == TIM4_GetFlagStatus(TIM4_FLAG_UPDATE));


This statement means that the program keeps reading the update flag TIM4_FLAG_UPDATE (blocked) until the read flag is valid (count is full), then it jumps out of the while loop.


TIM4_ClearFlag(TIM4_FLAG_UPDATE);


Clear the update flag TIM4_FLAG_UPDATE. After the above flag is valid, it needs to be cleared, and then the next counting process is carried out.


I believe that everyone can understand the start and stop timer here, which starts from the execution of the TIMDelay_N10us function to the end of the operation process. Here is a reminder: the counting process is a cyclic process, and try to avoid repeated switching timers (it will take some time). The TIMDelay_Nms I provided is actually not very accurate strictly speaking, and this function is repeated switching.


C. Specific implementation functions


The while in the main function is the specific function implemented by the source code of this article. An LED light (IO) is output alternately in high and low, and a timer is used in the middle to delay more accurately by 500ms to achieve the effect of LED turning on and off.


Code:


while(1)


{


  LED_ON; //LED is on


  TIMDelay_Nms(500);


  LED_OFF; //LED off


  TIMDelay_Nms(500);


}


Strictly speaking, there is a certain deviation in the TIMDelay_Nms function. From the above description, I believe you know how to modify it to avoid such errors.


IV. Download

STM8S information:


http://pan.baidu.com/s/1o7Tb9Yq


Software source code project (STM8S-A02_TIM precise delay (blocking)):


http://pan.baidu.com/s/1c2EcRo0

Keywords:STM8S  TIM Reference address:STM8S_002_TIM precise delay (blocking)

Previous article:The problem of delay function being optimized when debugging stm8 microcontroller with IAR
Next article:STM8L101F3P6 waveforms of different writing methods of millisecond delay function

Recommended ReadingLatest update time:2024-11-16 16:02

STM8S assembly code analysis
The .asm file is the source file of the assembly code, and the .inc file is the include file, similar to the .c file and .h file in C language. Next, let's analyze these three files. (To analyze the assembly code, it is best to have some understanding of the startup process of the STM8 microcontroller. You can read m
[Microcontroller]
STM8S assembly code analysis
STM32CubeMX (Keil5) Development Road——5 Timer Interrupt TIM
To facilitate debugging, redirect printf and set usart 1——Click USART1 to set up 2——Mode selection Asynchronous asynchronous transmission 3——You can see that Tx and Rx appear automatically on the right 4——You can set the baud rate, stop bit, check bit and other parameters by yourself 1——Click Clock Configuration
[Microcontroller]
STM32CubeMX (Keil5) Development Road——5 Timer Interrupt TIM
[STM32 Motor FOC] Record 15 - TIM input capture
Input capture principle and configuration steps   1. Input Capture Concept   STM32 input capture, in simple terms, detects the edge signal on TIMx_CHx (channel X of timer X), and when the edge signal changes (such as rising edge/falling edge), stores the current timer value (TIMx_CNT) in the capture/compare register (
[Microcontroller]
[STM32 Motor FOC] Record 15 - TIM input capture
STM32 timer TIM and PWM output
STM32 has a total of 8 16-bit timers. TIM6 and TIM7 are basic timers; TIM2, TIM3, TIM4, and TIM5 are general timers; and TIM1 and TIM8 are advanced timers. These timers enable STM32 to have functions such as timing, signal frequency measurement, signal PWM measurement, PWM output, three-phase 6-step motor control, and
[Microcontroller]
STM32 timer TIM and PWM output
One of the STM32/STM8L/STM8S series, running water lamp
1. STM32F103 lights up the LED 1. Circuit diagram: 2. Code: //FUNCTION: LED initialization //PARA: None //RETURN: None void LED_INIT(void)  {     GPIO_InitTypeDef GPIO_InitStructure;       RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); //Enable AFIO and GPIOB ports     GPIO_PinRemapCon
[Microcontroller]
One of the STM32/STM8L/STM8S series, running water lamp
STM8S---Power consumption management halt mode (halt) implementation
Main content introduction Main factors affecting power consumption Power Systems Clock Management Run mode and low power mode  Operation Mode Waiting Mode Active shutdown mode Stop Mode Power consumption and wake-up event measurements and results Power Management Key Points Summary of key points   Halt
[Microcontroller]
stm8s io configuration
The io registers of stm8s include DDR, IDR, ODR, CR1, and CR2. As for the configuration of io, you can understand it by reading the table below. The commonly used ones are push-pull output, floating input, and pull-up input. The following uses the PC1 pin as an example to explain how to configure these three modes.
[Microcontroller]
stm8s io configuration
How to use STM32's TIM as a normal timer
Take TIM2 of stm32 as an example, configure it as a normal timer, and trigger an interrupt when the timing time is up. 1: Basic configuration of the timer  First declare a structure variable TIM_TimeBaseStructure for timer configuration. For details, please refer to the TIM library provided by STM32 TIM_TimeBaseStruct
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号