按照官方帮助文档上的教程写的,调试时定时器确实溢出了,但没有执行中断处理函数。在XPS中BUS Interface界面下,microblaze_0下的INTERRUPT 为 NO Connection。
/***************************** Include Files *********************************/
#include "xparameters.h"
#include "xtmrctr.h"
#include "xil_exception.h"
#include "xgpio.h"
#include "xintc.h"
#include <stdio.h>
/************************** Constant Definitions *****************************/
#define P1_ID XPAR_GPIO_1_DEVICE_ID
#define P1_DIRECTION 0x00 /*P1八个端口的方向*/
#define P1_ID XPAR_P1_BUS_DEVICE_ID
#define LED_CHANNEL 1 //我们在用BSB添加GPIO时用的是1号通道
#define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID//中断控制器ID
#define TMRCTR_DEVICE_ID XPAR_XPS_TIMER_0_DEVICE_ID //定时器设备ID
#define TMRCTR_INTERRUPT_ID XPAR_INTC_0_TMRCTR_0_VEC_ID //定时器中断向量
#define TIMER_CNTR_0 0 //每个设备中有两个定时计数器,我们用的是定时/计数器0
#define INTC XIntc
#define INTC_HANDLER XIntc_InterruptHandler
#define RESET_VALUE 0x0001 //16位定时器,50M的时钟
/************************** Function Prototypes ******************************/
int TmrCtrIntrExample(INTC* IntcInstancePtr, XTmrCtr* InstancePtr, u16 DeviceId,
u16 IntrId, u8 TmrCtrNumber);
//把定时器连接到中断控制器上,开启中断
static int TmrCtrSetupIntrSystem(INTC* IntcInstancePtr, XTmrCtr* InstancePtr,
u16 DeviceId, u16 IntrId, u8 TmrCtrNumber);
//中断处理函数
void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber);
void TmrCtrDisableIntr(INTC* IntcInstancePtr, u16 IntrId);
/************************** Variable Definitions *****************************/
XGpio P1;
INTC InterruptController; /* 中断控制器的实例 */
XTmrCtr TimerCounterInst; /*定时计数器的实例*/
volatile int TimerExpired; //溢出次数
int main(void) {
int Status;
//初始化P1
Status = XGpio_Initialize(&P1, P1_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XGpio_SetDataDirection(&P1, LED_CHANNEL, P1_DIRECTION);
XGpio_DiscreteWrite(&P1, LED_CHANNEL, 0xaa);
Status = TmrCtrIntrExample(&InterruptController, &TimerCounterInst,
TMRCTR_DEVICE_ID, TMRCTR_INTERRUPT_ID, TIMER_CNTR_0);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
return XST_SUCCESS;
}
int TmrCtrIntrExample(INTC* IntcInstancePtr, XTmrCtr* TmrCtrInstancePtr,
u16 DeviceId, u16 IntrId, u8 TmrCtrNumber) {
int Status;
int LastTimerExpired = 0;
int temp=0;
Status = XTmrCtr_Initialize(TmrCtrInstancePtr, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = XTmrCtr_SelfTest(TmrCtrInstancePtr, TmrCtrNumber);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = TmrCtrSetupIntrSystem(IntcInstancePtr, TmrCtrInstancePtr, DeviceId,
IntrId, TmrCtrNumber);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XTmrCtr_SetHandler(TmrCtrInstancePtr, TimerCounterHandler,
TmrCtrInstancePtr);
XTmrCtr_SetOptions(TmrCtrInstancePtr, TmrCtrNumber,
XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);
XTmrCtr_SetResetValue(TmrCtrInstancePtr, TmrCtrNumber, RESET_VALUE);
XTmrCtr_Start(TmrCtrInstancePtr, TmrCtrNumber);
while (1) {
while (TimerExpired == LastTimerExpired) {
temp=XTmrCtr_IsExpired(TmrCtrInstancePtr, TmrCtrNumber);
}
LastTimerExpired = TimerExpired;
if (TimerExpired == 3) {
XTmrCtr_Stop(TmrCtrInstancePtr, TmrCtrNumber);
break;
}
}
TmrCtrDisableIntr(IntcInstancePtr, DeviceId);
return XST_SUCCESS;
}
void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber) {
XTmrCtr *InstancePtr = (XTmrCtr *) CallBackRef;
XGpio_DiscreteWrite(&P1, LED_CHANNEL, 0xff);
if (XTmrCtr_IsExpired(InstancePtr, TmrCtrNumber)) {
TimerExpired++;
if (TimerExpired == 1000) {
XTmrCtr_SetOptions(InstancePtr, TmrCtrNumber, 0);
}
}
}
static int TmrCtrSetupIntrSystem(INTC* IntcInstancePtr,
XTmrCtr* TmrCtrInstancePtr, u16 DeviceId, u16 IntrId, u8 TmrCtrNumber) {
int Status;
Status = XIntc_Initialize(IntcInstancePtr, INTC_DEVICE_ID);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = XIntc_Connect(IntcInstancePtr, IntrId,
(XInterruptHandler) XTmrCtr_InterruptHandler,
(void *) TmrCtrInstancePtr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = XIntc_Start(IntcInstancePtr, XIN_REAL_MODE);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
XIntc_Enable(IntcInstancePtr, IntrId);
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler) INTC_HANDLER, IntcInstancePtr);
Xil_ExceptionEnable();
return XST_SUCCESS;
}
void TmrCtrDisableIntr(INTC* IntcInstancePtr, u16 IntrId)
{
XIntc_Disable(IntcInstancePtr, IntrId);
return;
}
|