⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10041fd49335001d1144ac3138784750

📁 XINLINX公司开发板的嵌入式源代码
💻
字号:
/********************************************************************
*   Copyright (c) 2003 Xilinx, Inc.  All rights reserved. 
*   Xilinx, Inc.
* 
*
* This program uses the timer and gpio to demonstrate interrupt handling.
* The timer is set to interrupt regularly. The frequency is set in the code.
* Every time there is an interrupt from the timer, 
* a rotating display of leds on the board is updated.
*
* The LEDs and switches are in these bit positions: 
* LSB 0: gpio_io<3> 
* LSB 1: gpio_io<2> 
* LSB 2: gpio_io<1> 
* LSB 3: gpio_io<0> 
**********************************************************************/

/* This is the list of files that must be included to access the peripherals:
 * xtmrctr.h - to access the timer
 * xgpiol.h - to access the general purpose I/O
 * xintc_l.h - access interrupt controller.
 * xparameters.h  - General purpose definitions.  Must always be included
 *           when any drivers/print routines are accessed.  This defines
 *           addresses of all peripherals, declares the interrupt service
 *           routines, declare STDIN/STDOUT devices etc.
 */

#include <xtmrctr.h>
#include <xintc_l.h>
#include <xgpio.h>
#include <xparameters.h>
#define LEDChan  1


/* Global variables: count is the count displayed using the 
 * LEDs, and timer_count is the interrupt frequency.
 */
unsigned int timer_count = 1;
unsigned int count = 1;
int one_second_flag = 0;

XGpio gpio;

/*
 * Interrupt service routine for the timer.  It has been declared as an ISR in
 * the mss file using the attribute INT_HANDLER.  libgen automatically
 * registers it as the routine to be called when an interrupt occurs.  The exception
 * handler ensures that all registers are correctly saved, and that the return from 
 * the interrupt occurs correctly.  The ISR can be written as a normal C routine.  
 * The peripheral can be accessed using XPAR_<peripheral name in the mhs file>_BASEADDR 
 * as the base address.
 */
void timer_int_handler(void * baseaddr_p) {
	/* Add variable declarations here */
	unsigned int csr;

	/* Read timer 0 CSR to see if it raised the interrupt */
	csr = XTmrCtr_mGetControlStatusReg(baseaddr_p, 0);

	/* If the interrupt occured, then increment a counter */
	if (csr & XTC_CSR_INT_OCCURED_MASK)
		count++;

	/* Display the count on the LEDS and print it using the UART */
	XGpio_DiscreteWrite(&gpio, LEDChan, count);
	xil_printf("Count value is: %x\r\n",count);

	/* Clear the timer interrupt */
	XTmrCtr_mSetControlStatusReg(baseaddr_p, 0, csr);

}

int main() {

  int count_mod_3;
  
  // Enable MicroBlaze Interrupts
  microblaze_enable_interrupts();
  
  /* Register the Timer interrupt handler in the vector table */
  XIntc_RegisterHandler(XPAR_XPS_INTC_0_BASEADDR,
                             XPAR_XPS_INTC_0_DELAY_INTERRUPT_INTR,
                             (XInterruptHandler) timer_int_handler,
                             (void *)XPAR_DELAY_BASEADDR);

  /* Initialize and set the direction of the GPIO connected to LEDs */
  XGpio_Initialize(&gpio, XPAR_LEDS_8BIT_DEVICE_ID);
  XGpio_SetDataDirection(&gpio,LEDChan, 0);

  /* Start the interrupt controller */
  XIntc_mMasterEnable(XPAR_XPS_INTC_0_BASEADDR);
  XIntc_mEnableIntr(XPAR_XPS_INTC_0_BASEADDR, 0x1);

  /* Set the gpio as output on high 8 bits (LEDs)*/
  XGpio_mSetDataReg(XPAR_LEDS_8BIT_DEVICE_ID,LEDChan, ~count);
  xil_printf("The value of count = %d\n\r", count);

  /* Set the number of cycles the timer counts before interrupting */
  XTmrCtr_mSetLoadReg(XPAR_DELAY_BASEADDR, 0, (timer_count*timer_count+1) * 80000000); 
   
  /* Reset the timers, and clear interrupts */
  XTmrCtr_mSetControlStatusReg(XPAR_DELAY_BASEADDR, 0, XTC_CSR_INT_OCCURED_MASK | XTC_CSR_LOAD_MASK );
	
  /* Enable timer interrupts in the interrupt controller */
  XIntc_mEnableIntr(XPAR_DELAY_BASEADDR, XPAR_DELAY_INTERRUPT_MASK);
  
  /* Start the timers */
  XTmrCtr_mSetControlStatusReg(XPAR_DELAY_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | 
						XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK);  
  /* Enable MB interrupts */
  //microblaze_enable_interrupts();

  /* Wait for interrupts to occur */
  while(1) {
	if(one_second_flag){
		count_mod_3 = count % 3;
		if(count_mod_3 = 0)
			xil_printf("Interrupt taken at %d seconds \n\r",count);
		one_second_flag=0;
		xil_printf(".");
		}
	}
}
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -