读取引脚状态函数会触发中断响应问题

使用zedboard开发板,做GPIO中断触发实验

功能:按键BTN8(MIO50)触发中断,运行中断函数。

完成:中断初始化;中断函数

实现:出现问题,在读取按键状态时直接触发中断。

出现问题位置

解决状态:NONE


#include <stdio.h>
#include "xparameters.h"
#include "xgpiops.h"
#include "xstatus.h"
#include "xplatform_info.h"
#include <xil_printf.h>
#include "sleep.h"
#include "xscugic.h"


#define GPIO_DEVICE_ID		XPAR_XGPIOPS_0_DEVICE_ID
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID
#define MIO7_LED            7    //ps端LED
#define	MIO50_KEY           50    //按键

//GPIO中段号:52
#define GPIO_INTERRUPT_ID	XPAR_XGPIOPS_0_INTR

static u32 Input_Pin; /* Switch button */
static u32 Output_Pin; /* LED button */
static u32 AllButtonsPressed; /* Intr status of the bank */

XGpioPs_Config *ConfigPtr;
XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
XGpioPs Gpio;	/* The driver instance for GPIO Device. */
static XScuGic Intc; /* The Instance of the Interrupt Controller Driver */

void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio, u16 GpioIntrId);
void IntrHandler();

int main()
{
	u32 Data;

	printf("***hello,hello,hello,hello,hello,hello***\r\n");
	//初始化gpio驱动
	//根据器件id,查找器件配置信息
	ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
	//初始化gpio参数
	XGpioPs_CfgInitialize(&Gpio, ConfigPtr, ConfigPtr->BaseAddr);

	//gpio方向设置为输出(0:输入、1:输出)
	XGpioPs_SetDirectionPin(&Gpio, MIO7_LED, 1);
	XGpioPs_SetDirectionPin(&Gpio, MIO50_KEY, 0);

	//设置输出使能(0:关闭、1:打开)
	XGpioPs_SetOutputEnablePin(&Gpio, MIO7_LED, 1);

	SetupInterruptSystem(&Intc, &Gpio, GPIO_INTERRUPT_ID);

while(1)
{
	Data = XGpioPs_ReadPin(&Gpio, MIO50_KEY);
	if(Data == 1)
	{
	//写数据到输出引脚
		XGpioPs_WritePin(&Gpio, MIO7_LED, 0x1);

	}
	else
	{

		XGpioPs_WritePin(&Gpio, MIO7_LED, 0x0);
	}
}
	return 0;
}



void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,
				u16 GpioIntrId)
{

	//查找器件配置信息,并进行初始化
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
	XScuGic_CfgInitialize(GicInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);

//	初始化ARM处理器异常句柄
	Xil_ExceptionInit();
//	给中断异常注册一个处理程序
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
				(Xil_ExceptionHandler)XScuGic_InterruptHandler,
				GicInstancePtr);

	/* Enable interrupts in the Processor.使能处理器中断 */
	Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);

//	关联   中断处理函数,对中断进行处理
	XScuGic_Connect(GicInstancePtr, GpioIntrId,	(Xil_ExceptionHandler)IntrHandler, (void *)Gpio);



	/* Enable the interrupt for the GPIO device.为GPIO器件使能中断 */
	XScuGic_Enable(GicInstancePtr, GpioIntrId);

	/* Enable falling edge interrupts for all the pins in bank 0. */
//	XGpioPs_SetIntrType(Gpio, GPIO_BANK, 0x00, 0xFFFFFFFF, 0x00);

//	设置引脚终端类型:下降沿触发
	XGpioPs_SetIntrTypePin(Gpio, MIO50_KEY, XGPIOPS_IRQ_TYPE_EDGE_RISING);

	/* Set the handler for gpio interrupts. */
//	XGpioPs_SetCallbackHandler(Gpio, (void *)Gpio, IntrHandler);

	/* Enable the GPIO interrupts of Bank 0. */
//	XGpioPs_IntrEnable(Gpio, GPIO_BANK, (1 << Input_Pin));
//  打开引脚中断使能
	XGpioPs_IntrEnablePin(Gpio, MIO50_KEY);



//	return XST_SUCCESS;
}

void IntrHandler()
{
//	XGpioPs *Gpio = (XGpioPs *)CallBackRef;
//	u32 DataRead;
//
//	/* Push the switch button */
//	DataRead = XGpioPs_ReadPin(Gpio, Input_Pin);
//	if (DataRead != 0) {
//		XGpioPs_SetDirectionPin(Gpio, Output_Pin, 1);
//		XGpioPs_SetOutputEnablePin(Gpio, Output_Pin, 1);
//		XGpioPs_WritePin(Gpio, Output_Pin, DataRead);
//		AllButtonsPressed = TRUE;
//	}
	printf("interrupt detected!\r\n");
	XGpioPs_IntrDisablePin(&Gpio, MIO50_KEY);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个示例的按键触发中断程序,以一个单一按键为例: ```c #include <reg51.h> sbit key = P1^0; // 按键连接到P1口的第0位 void delay(unsigned int ms) { unsigned int i, j; for (i = 0; i < ms; i++) { for (j = 0; j < 114; j++); } } void key_interrupt() interrupt 0 // 外部中断0的中断服务函数 { // 按键触发时执行的代码 // 可以在这里进行按键状态检测、防抖处理、响应动作等操作 // 例如,可以在按键触发时通过串口发送消息或控制其他设备 // 这里只是简单地将P2口的LED灯开关状态翻转 P2 = ~P2; } void main() { EA = 1; // 全局中断使能 EX0 = 1; // 外部中断0使能 while (1) { // 主程序循环,可以处理其他任务 // 在这里可以执行其他操作,不影响按键中断响应 // 延时一段时间,避免快速连续触发按键 delay(100); } } ``` 在这个示例中,我们使用外部中断0来处理按键触发事件。按键连接到P1口的第0位,并通过中断0(INT0)来触发中断。当按键被按下时,中断服务函数`key_interrupt()`将被调用。 在中断服务函数中,你可以编写具体的按键处理代码,例如按键状态检测、防抖处理、响应动作等。这里只是简单地将P2口的LED灯开关状态翻转来作为示例。你可以根据实际需求进行相应的处理。 在主程序循环中,你可以处理其他任务,这里只是简单地延时一段时间。请注意,在中断服务函数中执行的代码应尽量简洁,以避免中断响应时间过长影响其他任务的执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RyanLee90

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值