STM8S903K3T6C基于ST Visual Develop开发外部中断示例

STM8S903K3T6C基于ST Visual Develop开发外部中断示例


📚外部中断

  • 📑STM8 I/O 口引脚配置表

在这里插入图片描述

  • 📖外部中断端口以及引脚
    在这里插入图片描述
  • 📜外部中断向量表
    在这里插入图片描述
  • ✅对应的是stm8_interrupt_vector.c文件中的代码:
struct interrupt_vector const _vectab[] = {
	{0x82, (interrupt_handler_t)_stext}, /* reset */
	{0x82, NonHandledInterrupt}, /* trap  */
	{0x82, NonHandledInterrupt}, /* irq0  TLI引脚中断-》PD7引脚*/
	{0x82, NonHandledInterrupt}, /* irq1  */
	{0x82, NonHandledInterrupt}, /* irq2  */
	{0x82, NonHandledInterrupt}, /* irq3  PA端口*/
	{0x82, NonHandledInterrupt}, /* irq4  PB端口*/
	{0x82, PC_Out_Int}, /* irq5  PC端口*/
	{0x82, NonHandledInterrupt}, /* irq6  PD端口*/
	{0x82, NonHandledInterrupt}, /* irq7  PE端口*/
	{0x82, NonHandledInterrupt}, /* irq8  */
	{0x82, NonHandledInterrupt}, /* irq9  */
	{0x82, NonHandledInterrupt}, /* irq10 */
	{0x82, NonHandledInterrupt}, /* irq11 */
	{0x82, NonHandledInterrupt}, /* irq12 */
	{0x82, NonHandledInterrupt}, /* irq13 */
	{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 */
};
  • ITC寄存器
    在这里插入图片描述
  • 软件优先级寄存器

✨如果涉及个外设中断,那么可以使用软件优先级寄存器来配置不同中断的响应优先级。

在这里插入图片描述

🌼按键中断示例代码

📝实现的功能;当PC1按键按下时,PD2引脚上的LED灯状态翻转,当PC5按键按下时,PD7引脚上的LED灯状态翻转。

#include"stm8s903k.h"
_Bool LED1  @PD_ODR:7;
_Bool LED2  @PD_ODR:2;

_Bool but1  @PC_IDR:1;
_Bool but2  @PC_IDR:5;

void GPIO_Init(void)
{
    PD_DDR |= 0xFF;//推挽输出
    PD_CR1 |= 0xFF;
		
		PC_DDR = 0x00;
    PC_CR1 |= 0xFF;//PC1和PC5中断上拉输入
    PC_CR2 |= 0xFF;
		
}
void main()
{
	_asm("sim");    //disable all interrupt
    GPIO_Init();
		EXTI_CR1 = 0x20;    //开启PC口,0x20下降沿触发
    _asm("rim");    //enable all interrupt
		LED1 = 0;
		LED2 = 0;
    while(1);
}
//@far @interrupt void TLI_Int(void)
//{
//    LED1 = !LED1;
 //   while(1);
//}
@far @interrupt void PC_Out_Int(void)
{
	if(but1 == 0){
    LED2 = !LED2;
	}
   	if(but2 == 0){
		LED1 = !LED1;
	} 
}
  • 🌻stm8_interrupt_vector.c文件中的代码:
/*	BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
 *	Copyright (c) 2007 STMicroelectronics
 */

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 */
//@far @interrupt void TLI_Int(void);
extern @far @interrupt void PC_Out_Int(void);
//@far @interrupt void PC5_Out_Int(void);
struct interrupt_vector const _vectab[] = {
	{0x82, (interrupt_handler_t)_stext}, /* reset */
	{0x82, NonHandledInterrupt}, /* trap  */
	{0x82, NonHandledInterrupt}, /* irq0  TLI_Int*/
	{0x82, NonHandledInterrupt}, /* irq1  */
	{0x82, NonHandledInterrupt}, /* irq2  */
	{0x82, NonHandledInterrupt}, /* irq3  */
	{0x82, NonHandledInterrupt}, /* irq4  */
	{0x82, PC_Out_Int}, /* irq5  */
	{0x82, NonHandledInterrupt}, /* irq6  */
	{0x82, NonHandledInterrupt}, /* irq7  */
	{0x82, NonHandledInterrupt}, /* irq8  */
	{0x82, NonHandledInterrupt}, /* irq9  */
	{0x82, NonHandledInterrupt}, /* irq10 */
	{0x82, NonHandledInterrupt}, /* irq11 */
	{0x82, NonHandledInterrupt}, /* irq12 */
	{0x82, NonHandledInterrupt}, /* irq13 */
	{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 */
};

使用2个不同端口的外部中断引脚

📝例如使用PC1和PD1引脚作为外部中断输入引脚时,可以通过软件优先级配置PD1的优先级高于PC1。默认情况下,从硬件中断向量表可以看到:PC端口的中断向量号5比PD端口的中断向量号6,数值要大,优先级则低。

  • 配置方法:
void INT_Init(void)
{
	ITC_SPR2 = 0X04;//软件优先级: VECT5SPR < VECT6SPR
	EXTI_CR1 = 0x20;    //开启PC口,0x20下降沿触发
}

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值