/*程序中编写了 3 个函数,一个是 main 主函数,一个是中断建立函数,另一个是中断调用 函数。首先在 Main 函数中初始化定时器 timer,指定 timer 的设备号为 XPAR_XSCUTIMER_0_DEVICE_ID,此设备号在 xparameters.h 的头文件中定义了的。再设置定时器的初始设定值并启动定时器,当定时器减为 0 的时候,会重新赋值这个初始设定值。再调用定时器中断建立函数 SetupInterruptSystem。在定时器中断建立函数里,主要是初始化中断设备,注册定时器的中断号,绑定定时器中断调用函数和使能定时器中断。当定时器计数到 0 时会产生定时器中断。这时 CPU 会跳转到在定时器中断处理程序中,在处理程序中改变 sec 的值。相当于定时器发生一次中断,sec 的数值就加 1, 再从串口信息中打 印出来。*/ #include<stdio.h> #include"xadcps.h" #include"xil_types.h" #include"Xscugic.h" #include"Xil_exception.h" #include"xscutimer.h" //timerinfo #defineTIMER_DEVICE_ID XPAR_XSCUTIMER_0_DEVICE_ID #defineINTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID #defineTIMER_IRPT_INTR XPAR_SCUTIMER_INTR //#defineTIMER_LOAD_VALUE 0x0FFFFFFF #defineTIMER_LOAD_VALUE 0x13D92D3F //staticXAdcPs XADCMonInst;//XADC staticXScuGicIntc;//GIC staticXScuTimerTimer;//timer staticvoidSetupInterruptSystem(XScuGic*GicInstancePtr, XScuTimer*TimerInstancePtr,u16TimerIntrId); staticvoidTimerIntrHandler(void*CallBackRef); /*初始化定时器timer*/ intmain() { XScuTimer_Config*TMRConfigPtr; //timerconfig printf("------------START-------------\n"); //私有定时器初始化 TMRConfigPtr=XScuTimer_LookupConfig(TIMER_DEVICE_ID); XScuTimer_CfgInitialize(&Timer,TMRConfigPtr,TMRConfigPtr->BaseAddr); XScuTimer_SelfTest(&Timer); //加载计数周期,私有定时器的时钟为CPU的一般,为333MHZ,如果计数1S,加载值为 1sx(333x1000x1000)(1/s)-1=0x13D92D3F XScuTimer_LoadTimer(&Timer,TIMER_LOAD_VALUE); //自动装载 XScuTimer_EnableAutoReload(&Timer); //启动定时器 XScuTimer_Start(&Timer); //setuptheinterrupts SetupInterruptSystem(&Intc,&Timer,TIMER_IRPT_INTR); while(1){ } return0; } voidSetupInterruptSystem(XScuGic*GicInstancePtr, XScuTimer*TimerInstancePtr,u16TimerIntrId) { XScuGic_Config*IntcConfig;//GICconfig Xil_ExceptionInit(); //initialisetheGIC IntcConfig=XScuGic_LookupConfig(INTC_DEVICE_ID); XScuGic_CfgInitialize(GicInstancePtr,IntcConfig, IntcConfig->CpuBaseAddress); //connecttothehardware Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, GicInstancePtr); //setupthetimerinterrupt XScuGic_Connect(GicInstancePtr,TimerIntrId, (Xil_ExceptionHandler)TimerIntrHandler, (void*)TimerInstancePtr); //enabletheinterruptfortheTimeratGIC XScuGic_Enable(GicInstancePtr,TimerIntrId); //enableinterruptonthetimer XScuTimer_EnableInterrupt(TimerInstancePtr); //EnableinterruptsintheProcessor. Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ); } staticvoidTimerIntrHandler(void*CallBackRef) { staticintsec=0; //计数 XScuTimer*TimerInstancePtr=(XScuTimer*)CallBackRef; XScuTimer_ClearInterruptStatus(TimerInstancePtr); sec++; printf("%dSecond\n\r",sec); //每秒打印输出一次 }
文章来源: https://blog.csdn.net/GAI159/article/details/89682706