STM8_GPIO 外部中断输入

时间:2020-12-27 17:52:33
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 实验平台: 
+ 硬件    : STM8S105S6T6C
+ 开发平台: IAR For STM8 1.10
+ 仿真器 : ST-Link
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "stm8s.h"
/*******************************************************************************
*                         ==时钟初始化函数==
* CLK_DeInit();                                   --> 复位时钟寄存器
* CLK_HSICmd(ENABLE);                             --> 使能内部高速时钟
* CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2); --> 配置内部高速时钟(时钟分频器:2分频)
* CLK_ClockSecuritySystemEnable();                --> 开启时钟安全系统
*******************************************************************************/
void CLK_INIT(void)                
{
CLK_DeInit();
CLK_HSICmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
CLK_ClockSecuritySystemEnable();
}
/*******************************************************************************
*                         ==I/O口初始化函数==
* GPIO_DeInit();                                   --> 复位I/O端口 D
* GPIO_Init(GPIOD , GPIO_PIN_7 , GPIO_MODE_IN_PU_IT);                      
                                                     --> 使能PORTD_7, 设置为 输入 ,上拉 ,带中断
* GPIO_Init(GPIOD , GPIO_PIN_0 , GPIO_MODE_OUT_PP_LOW_SLOW);                      
                                                     --> 使能PORTD_0, 设置为 输出,推挽式 ,低电平 ,低速率
*******************************************************************************/
void GPIO_INIT(void)
{
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD , GPIO_PIN_7 , GPIO_MODE_IN_FL_IT);          
GPIO_Init(GPIOD , GPIO_PIN_0 , GPIO_MODE_OUT_PP_LOW_SLOW);      // LED
}
/*******************************************************************************
*                         ==外部中断初始化函数==
* EXTI_DeInit();                                                            --> 初始化寄存器
* EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD , EXTI_SENSITIVITY_FALL_ONLY); --> 设定下降沿触发外部中断 
*******************************************************************************/
void EXTI_INIT(void)
{
EXTI_DeInit();
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD , EXTI_SENSITIVITY_FALL_ONLY);
}
/*******************************************************************************
*                           ==MAIN 函数==
* 初始化 时钟
* 初始化 端口
* 初始化 外部中断触发方式
* 使能全局中断
*******************************************************************************/
void main(void)
{
CLK_INIT();
GPIO_INIT();
EXTI_INIT();
enableInterrupts();
   while(1);
}
/*******************************************************************************
*                           ==中断服务函数 函数==
* PORTD_7是外部最高优先级的中断源(TLI)
* 按下按键LED亮,松开按键则LED灭 
*******************************************************************************/
#pragma vector=2
__interrupt void TLI_IRQHandler(void)
{
        GPIO_WriteReverse(GPIOD,GPIO_PIN_0);    //翻转 管脚电平

}

中断的形式和程序的写法