Xilinx MicroBlaze中断定时器代码
时间: 2023-12-20 19:30:21 浏览: 161
以下是Xilinx MicroBlaze中断定时器的代码示例:
```c
#include <stdio.h>
#include "xparameters.h"
#include "xil_exception.h"
#include "xtmrctr_l.h"
#define TMRCTR_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_INTC_0_DEVICE_ID
#define TMRCTR_INTERRUPT_ID XPAR_INTC_0_TMRCTR_0_VEC_ID
volatile int TimerExpired;
void TimerCounterHandler(void *CallBackRef, u8 TmrCtrNumber)
{
if (XTmrCtr_IsExpired(CallBackRef, TmrCtrNumber))
{
XTmrCtr_Reset(CallBackRef, TmrCtrNumber);
TimerExpired = 1;
}
}
int SetupInterruptSystem(XTmrCtr *InstancePtr, u16 DeviceId, u16 IntrId)
{
int Status;
XScuGic_Config *IntcConfig;
Xil_ExceptionInit();
IntcConfig = XScuGic_LookupConfig(DeviceId);
Status = XScuGic_CfgInitialize(&InterruptController, IntcConfig, IntcConfig->CpuBaseAddress);
if (Status != XST_SUCCESS)
{
return XST_FAILURE;
}
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, &InterruptController);
XScuGic_Connect(&InterruptController, IntrId, (Xil_ExceptionHandler)TimerCounterHandler, (void *)InstancePtr);
XScuGic_Enable(&InterruptController, IntrId);
XTmrCtr_SetHandler(InstancePtr, TimerCounterHandler, InstancePtr);
XTmrCtr_SetOptions(InstancePtr, 0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);
XTmrCtr_Start(InstancePtr, 0);
Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
return XST_SUCCESS;
}
int main()
{
int Status;
XTmrCtr TimerCounterInstancePtr;
u32 TicksPerSecond = XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ / 2;
u32 TimerLoadValue = TicksPerSecond / 2;
Status = XTmrCtr_Initialize(&TimerCounterInstancePtr, TMRCTR_DEVICE_ID);
if (Status != XST_SUCCESS)
{
return XST_FAILURE;
}
Status = SetupInterruptSystem(&TimerCounterInstancePtr, INTC_DEVICE_ID, TMRCTR_INTERRUPT_ID);
if (Status != XST_SUCCESS)
{
return XST_FAILURE;
}
XTmrCtr_SetResetValue(&TimerCounterInstancePtr, 0, TimerLoadValue);
XTmrCtr_Start(&TimerCounterInstancePtr, 0);
while (1)
{
if (TimerExpired)
{
printf("Timer expired!\n");
TimerExpired = 0;
}
}
return 0;
}
```
阅读全文