stm8s timer 1 delay_STM8 timer achieves precise delay (blocking)

Publisher:数据梦行者Latest update time:2021-09-30 Source: eefocusKeywords:stm8s Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Timer selection:


The stm8 timer is slightly different from the stm32 timer. The stm32 timer is 24 bits - 16,777,216. According to the 72M clock divided by 8, the period is about 1.86s, while the stm8 timer is 16/8 - 65535/255, and its own clock is 16M. Even with the 16-division period, it is much less than 1s.


Currently, the TIM2 timer is selected, with a division ratio of 1 and a maximum period of 65536/16, which is approximately equal to 4096us.


initialization


void TIM_Delay_Init()


{

/* The auto-reload value range is 0~255 for TIM4 and 0~65535 for TIM2*/


TIM2_TimeBaseInit(TIM2_PRESCALER_1,65535); //65535*(1/16)≈4095us


TIM2_Cmd(DISABLE); //Disable the timer


}


Delay function


It is best to use register operations in delay functions to reduce the amount of calculations and reduce errors.


void TIM_Delay_us(uint16_t nus)


{

uint16_t value = nus*16;


TIM2->CNTRH = 0; //calculated value cleared


TIM2->CNTRL = 0;


TIM2->CR1 |= (uint8_t)TIM2_CR1_CEN; //Start timing


while(TIM2_GetCounter() < value)


{

/*if(TIM2_GetFlagStatus(TIM2_FLAG_UPDATE)) //The maximum delay time for a single us is reached


{

TIM2_ClearFlag(TIM2_FLAG_UPDATE);


break;


}*/


};


TIM2->CR1 &= (uint8_t)(~TIM2_CR1_CEN); //Stop timing


}


Conclusion


When an 8-bit timer (TIM4) is selected, the one-division period is 16us, the two-division period is 32us, and in the case of ms delay, 5/10/20us can be used as the delay period to implement functions such as Delay_10us() and Delay_N_10us().

Keywords:stm8s Reference address:stm8s timer 1 delay_STM8 timer achieves precise delay (blocking)

Previous article:stm8s timer 1 delay_STM8S_library function_timer automatically loads TIM1
Next article:STM8-TIMER implements system tick timer

Latest Microcontroller Articles
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号