STM8S903K3基于ST Visual Develop开发定时器5中断示例

STM8S903K3基于ST Visual Develop开发定时器5中断示例


📖TIM5基本功能

  • 🌿16位向上计数和自动装载计数器
    在这里插入图片描述

  • 🍂4位可编程(可以实时修改的)预分频器。

在这里插入图片描述

🚩TIM5和高级定时器TIM1差异

📓TIM5_PSCR 可配置的位只有0-3位:

在这里插入图片描述

  • 🌴定时器溢出时间计算公式:Tout(溢出时间)=(ARR+1)((PSC+1)/Tclk)
ARR(预重装载值) = 124;
Tclk(内部时钟频率) = 16 000 000
T = 125* (128)/16 000 000 = 0.001S = 1MS

  • ⚡定时器5在STVD环境下,中断向量表中的中断号:13

🌼示例

✨利用定时器5,1ms中断一次,0.5S让led翻转一次。

/* MAIN.C file
 * 
 * Copyright (c) 2002-2005 STMicroelectronics
 */


#include"stm8s903k3.h"
volatile unsigned int i=0;

void Init_Timer5(void)
{
//	CLK_ECKR = 0x01; //开启外部时钟寄存器
//  CLK_SWR = 0xb4; //HSE外部时钟源作为主时钟源
//  CLK_CKDIVR = 0x00;//不分频
  CLK_ICKR |= 0X01; //使能内部高速时钟 HSI
     CLK_CKDIVR = 0x00; // 16M
    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 main()
{
	_asm("sim");    //disable all interrupt
	Init_GPIO();
	Init_Timer5();
	_asm("rim");

	while (1);
}

@far @interrupt void TIM5_UPD_OVF_IRQHandler (void)
{
		i++;
      TIM5_SR1 =0x00;// 清除更新中断标记,这步不能漏掉
if(i ==498)
{
			PD_ODR ^= 0x80;
			i =0;
}
}


  • 📈通过逻辑分析仪采集的波形
    在这里插入图片描述
  • 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 */
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值