MicroBlaze定时器(Timer)的使用

来源:互联网 发布:安卓 安装 linux 编辑:程序博客网 时间:2024/05/24 03:01

MicroBlaze的Timer IP比较简单易用,一个XPS_TC IP包含两个32bit定时/计数器。工作方式也比较灵活,可以设置中断、自动加载、Count UP/DOWN等模式,每当timer溢出的时候产生中断,进入中断程序后通过写1 clear中断标志位,通常的做法是csr = = XTmrCtr_mGetControlStatusReg(XPAR_MY_TIMER_BASEADDR, 0);然后再“XTmrCtr_mSetControlStatusReg(XPAR_MY_TIMER_BASEADDR, 0, timer_csr);”,这样可以在不影响别的控制位的前提下clear中断标志位。

同样值得注意的是用了XIntc模块,要将timer中断服务程序连接(注册)到XIntc模块上。

使用Timer IP的流程是:

(1)设置Load Regisiter初始值;

(2)设置控制/状态寄存器,包括中断时能、timer使能、COUNT方向、Relaod使能等;

(3)注册中断服务函数;

(4)开MB全局中断和XIntc中断;

(5)等待中断发生。

(6)在中断服务程序中clear中断标志位。

例程(100MHz下,每一秒产生一个中断)

#include "xparameters.h"#include "xutil.h"#include "xintc.h"#include "xtmrctr.h"void timer_int_handler(void);unsigned int push_button_times, timer_cnt;int main(){ timer_cnt = 0;  xil_printf("--start the program test---\r\n");  XTmrCtr_mSetLoadReg(XPAR_MY_TIMER_BASEADDR, 0, 100000000); XTmrCtr_mSetControlStatusReg(XPAR_MY_TIMER_BASEADDR, 0,        XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK |        XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK); //must enable mb_enable bit microblaze_enable_interrupts();  //must enable xgpio_interruptglobalenable bit; //register the isr XIntc_RegisterHandler(XPAR_INTC_0_BASEADDR, \        XPAR_XPS_INTC_0_MY_TIMER_INTERRUPT_INTR,\        (XInterruptHandler)timer_int_handler, \        (void *)0);         //must enable XIntc_mMasterEnable XIntc_mMasterEnable(XPAR_INTC_0_BASEADDR); //must enable specific interrupt(s) in the interrupt controller. XIntc_mEnableIntr(XPAR_INTC_0_BASEADDR, \      XPAR_PUSH_BUTTONS_POSITION_IP2INTC_IRPT_MASK\      |XPAR_DIP_SWITCHES_8BIT_IP2INTC_IRPT_MASK\      |XPAR_RS232_UART_INTERRUPT_MASK\      |XPAR_MY_TIMER_INTERRUPT_MASK);  while(1); return 0;} void timer_int_handler(void){ unsigned int timer_csr; timer_cnt++; timer_csr = XTmrCtr_mGetControlStatusReg(XPAR_MY_TIMER_BASEADDR, 0); if(timer_csr & XTC_CSR_INT_OCCURED_MASK) {  xil_printf("--timer interrupt happened times = %d!--\r\n", timer_cnt);  XTmrCtr_mSetControlStatusReg(XPAR_MY_TIMER_BASEADDR, 0, timer_csr); }}


原创粉丝点击