目录
- 硬知识
- 外中断初始化函数
- 测试
- main.c
- Exti.c
STC实验箱4
IAP15W4K58S4
Keil uVision V5.29.0.0
PK51 Prof.Developers Kit Version:9.60.0.0
硬知识
摘自《STC库函数使用参考》
外中断初始化函数
Ext_Inilize
EXTI_InitTypeDef的定义见于文件"Exti.H"。
typedef struct
{u8 EXTI_Mode; u8 EXTI_Polity; u8 EXTL_Interrupt;
} EXTI_InitTypeDef;
EXTI_Mode:设置外中断的工作模式:初始化INT0、INT1时的取值,初始化INT2,INT3.INT4时忽略,固定为下降沿中断。
EXTI_Polity:中断的优先级:初始化INT0、INT1时的取值,初始化INT2,INT3,INT4时忽略,固定为低优先级中断。
EXTI_Interrupt:中断允许或禁止:
测试
main.c
#include "./Drivers/config.h"
#include "./Drivers/delay.h"#include "./Drivers/GPIO.h"
#include "./Drivers/Exti.h"void GPIO_config(void)
{GPIO_InitTypeDef GPIO_InitStructure; //结构定义GPIO_InitStructure.Mode = GPIO_OUT_PP; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PPGPIO_InitStructure.Pin = GPIO_Pin_6 | GPIO_Pin_7; //指定要初始化的IO, 或操作GPIO_Inilize(GPIO_P1,&GPIO_InitStructure); //P1.6、P1.7初始化为推挽输出GPIO_Inilize(GPIO_P4,&GPIO_InitStructure); //P4.6、P4.7初始化为推挽输出GPIO_InitStructure.Pin = GPIO_Pin_2 | GPIO_Pin_3; //指定要初始化的IO, 或操作GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PPGPIO_Inilize(GPIO_P3,&GPIO_InitStructure); //P3.2、P3.3初始化为上拉输入P32 = 1;P33 = 1;
}void EXTI_config(void)
{EXTI_InitTypeDef EXTI_InitStructure; //结构定义EXTI_InitStructure.EXTI_Mode = EXT_MODE_Fall; //中断模式, EXT_MODE_RiseFall, EXT_MODE_FallEXTI_InitStructure.EXTI_Polity = PolityLow; //中断优先级, PolityLow,PolityHighEXTI_InitStructure.EXTI_Interrupt = ENABLE; //中断允许, ENABLE或DISABLEExt_Inilize(EXT_INT0,&EXTI_InitStructure); //初始化INT0 EXT_INT0,EXT_INT1,EXT_INT2,EXT_INT3,EXT_INT4Ext_Inilize(EXT_INT1,&EXTI_InitStructure); //初始化INT0 EXT_INT0,EXT_INT1,EXT_INT2,EXT_INT3,EXT_INT4
}bit P32_Flag = 0;
bit P33_Flag = 0;
void main(void)
{u8 Dir_Flag = 0;u8 LED_Port = 0;u8 i = 0;GPIO_config();EXTI_config();EA = 1;while(1){if (P32_Flag){delay_ms(20);if (P32 == 0){Dir_Flag = 0;}while (P32 == 0);P32_Flag = 0;}if (P33_Flag){delay_ms(20);if (P33 == 0){Dir_Flag = 1;}while (P33 == 0);P33_Flag = 0;}if (Dir_Flag){LED_Port >>= 1;if ((LED_Port & 0x0f) == 0x00)LED_Port = 0x08;}else{LED_Port <<= 1;if ((LED_Port & 0x0f) == 0x00)LED_Port = 0x01;}P17 = (((~LED_Port) & 0x01) != 0);P16 = (((~LED_Port) & 0x02) != 0);P47 = (((~LED_Port) & 0x04) != 0);P46 = (((~LED_Port) & 0x08) != 0);i = 25;while (P32 && P33 && i--)delay_ms(20);}
}
Exti.c
修改Exti.c中的中断服务函数:
/********************* INT0中断函数 *************************/
void Ext_INT0 (void) interrupt INT0_VECTOR //进中断时已经清除标志
{extern bit P32_Flag;P32_Flag = 1;
}/********************* INT1中断函数 *************************/
void Ext_INT1 (void) interrupt INT1_VECTOR //进中断时已经清除标志
{extern bit P33_Flag;P33_Flag = 1;
}
实验现象同上一个实验。