STM8S延时函数

本文介绍了一个基于STM8S103F3P单片机的延时函数实现方法。通过不同的延时函数(40us、1ms、1s等),实现了精确的时间延迟,并应用于LED闪烁实例中。文章提供了完整的C语言源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* MAIN.C file
 *
 * Copyright (c) 2002-2005 STMicroelectronics
 */
 //延时函数
#include "stm8s103f3p.h"

void delay_40us(void);
void delay_1ms(void);
void delay_ms(int);
void delay_1s(void);
void delay_s(int);
main()
{
 PD_DDR=0x0f;
 PD_CR1=0x0f;
 PD_CR2=0x00;
 PD_ODR=0x00;
 while (1)
 {
   PD_ODR^=0x0f;
   delay_s(50);
 }
}
void delay_40us(void)//40us延时函数 大约是41us
{
 int i=2;
 while(--i);
 /*
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop"); 
 _asm("nop");
 _asm("nop");
 _asm("nop");
 _asm("nop"); 
 _asm("nop");
 _asm("nop");*/
}
void delay_1ms(void)//1ms延时函数
{
 int i=108;
 while(--i);
}
void delay_ms(int ms)//ms延时函数
{
 int i;
 while(--ms)
 {
   delay_1ms();
 }
}
void delay_1s(void)//1s延时函数、没有误差
{
 int i=5;
 while(--i)
 {
   delay_ms(250);
 }
}
void delay_s(int s)//s延时函数、没有误差
{
 while(s--)
 {
   delay_1s();
 }
}

### STM8S003F3 单片机延时函数实现 对于STM8S003F3单片机而言,可以通过多种方式实现延时功能。一种常见的做法是利用定时器(如TIM5)来创建精确的延时函数[^1]。 #### 使用TIM5实现毫秒级延时 为了实现更精准的时间延迟,可以采用定时器中断的方式。具体来说,通过配置定时器5(TIM5),使其每经过1毫秒触发一次中断,在每次进入该中断服务程序(ISR)时增加全局变量`millis_count`的计数值。这样就可以根据这个计数器来进行任意长度的延时操作了。 下面是具体的代码示例: ```c #include "stm8s.h" volatile unsigned long millis_count = 0; void TIM5_Configuration(void){ /* Enable peripheral clock */ CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIM5, ENABLE); /* Time base configuration */ TIM_TimeBaseInit(TIM5, &TIM5_TimeBaseStructure); /* Prescaler value computation to achieve desired frequency */ uint16_t prescaler_value = (uint16_t)((SystemCoreClock / 1000000) - 1); // Assuming SystemCoreClock is defined TIM_PrescalerConfig(TIM5, prescaler_value, TIM_PSCRELOAD_UPDATE); /* Period value set for a period of 1 ms at the timer output frequency */ TIM_SetAutoreload(TIM5, 999); /* Clear the update flag and enable interrupt on update event */ TIM_ClearFlag(TIM5, TIM_FLAG_UPDATE); TIM_ITConfig(TIM5, TIM_IT_UPDATE, ENABLE); } // 中断服务程序 interrupt [TIM5_UPD_OVF_TRG_BRK_IRQHandler] void TIM5_UPD_OVF_TRG_BRK_IRQHandler(void){ if(TIM_GetITStatus(TIM5, TIM_IT_UPDATE) != RESET){ ++millis_count; TIM_ClearITPendingBit(TIM5, TIM_IT_UPDATE); } } ``` 此部分负责初始化TIM5以及设定相应的中断处理逻辑。接下来就是编写实际使用的延时函数: ```c unsigned long current_millis(){ return millis_count; } void delay_ms(unsigned int ms){ unsigned long start_time = current_millis(); while((current_millis() - start_time) < ms); } ``` 上述代码片段定义了一个名为`delay_ms()`的新函数,它接受一个整型参数作为输入表示要等待多少毫秒,并且会阻塞当前线程直到指定时间过去为止。 需要注意的是,在多任务环境中直接使用这种轮询式的延时可能会导致其他任务得不到及时响应;因此在这种情况下应该考虑使用操作系统提供的调度机制或者其他非阻塞性的方法替代简单循环等待。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值