STM8S903K3基于STVD开发,利用定时器5中断实现毫秒时基延时
✨本次功能实现基于相关篇1进行功能修改和补充,没有设计新增外设功能的使用,只讲功能实现,不再介绍相关寄存器。
🛠功能说明
📝通过定时器5,产生1ms定时中断,来实现以毫秒为单位的延时实现的方法。
⛳注意事项
⚡时钟分频寄存器(
CLK_CKDIVR)一定要使能,并赋值,如果没有使能,或者赋值的化,定时器默认输出的时钟频率为系统时钟频率的1/8,CLK_CKDIVR = 0x18;.

🌼毫秒时基延时延时示例
#include"stm8s903k3.h"
volatile unsigned int cnt=0;
void Init_Timer5(void)
{
// CLK_ECKR = 0x01; //开启外部时钟寄存器
// CLK_SWR = 0xb4; //HSE外部时钟源作为主时钟源
// CLK_CKDIVR = 0x00;//不分频
CLK_ICKR |= 0X01; //使能内部高速时钟 HSI
CLK_CKDIVR = 0x00; // 16M,2分频后8MHz
while(!(CLK_ICKR&0x02)); //HSI准备就绪
CLK_SWR =0xE1;//HSI内部时钟源作为主时钟源(复位值)
//TIM5_IER=0x00;//disable TIMER
CLK_PCKENR1 |=0x80; //外设时钟门控寄存器
TIM5_EGR=0x01;//计数器更新,
TIM5_PSCR =0x07;//3:0可配置, 2^7=128div /8 M=8us,8us*124=1ms
TIM5_ARRH = 0x00; // 自动重载寄存器ARR=0x007c
TIM5_ARRL = 0x7c; // 每记数500次产生一次中断,即1ms
// TIM5_RCR = 0x00; //重复计数器值
TIM5_SR1 = ( ~0x01 ); //清除更新中断标志
TIM5_CR1=0x01;//enable 使能计数器
TIM5_IER=0x01;//更新中断使能
}
void Init_GPIO(void)
{
PD_DDR |=0x80;// 配置PD端口的方向寄存器PD7输出
PD_CR1 |=0x80;// 设置PD7为推挽输出
PD_CR2 |=0x00;
}
void TIM_Delay_ms( unsigned int ms)
{
cnt = ms - 1;
while(cnt !=0);
}
void main()
{
_asm("sim"); //disable all interrupt
Init_GPIO();
Init_Timer5();
_asm("rim");
while (1)
{
PD_ODR ^= 0x80;
TIM_Delay_ms( 500 );
}
}
@far @interrupt void TIM5_UPD_OVF_IRQHandler (void)
{
if(cnt != 0) cnt--;
TIM5_SR1 =0x00;// 清除更新中断标记,这步不能漏掉
}
stm8_interrupt_vector.c代码
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void _stext(); /* startup routine */
extern @far @interrupt void TIM5_UPD_OVF_IRQHandler (void);
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, NonHandledInterrupt}, /* irq11 */
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, TIM5_UPD_OVF_IRQHandler}, /* irq13 TIM5*/
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
- 📈通过逻辑分析仪采集的波形

480

被折叠的 条评论
为什么被折叠?



