Вы находитесь на странице: 1из 2

2 Programas en C, lab 2 Página 1 de 2

INTERRUPS.C

#include <stdio.h>
#include "altera_avalon_pio_regs.h"
#include "system.h"
#include "sys/alt_irq.h"
volatile int edge_capture;
int test;

/* Initialize the button_pio. */

//Interrupt hadnler fucntion. This functions is defined using preprocssor so that it can cope
//with presence of enhanped api or old api which ever is present.
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
static void handle_button_interrupts(void* context)
#else
static void handle_button_interrupts(void* context, alt_u32 id)
#endif
{
/* Cast context to edge_capture's type. It is important that this
be declared volatile to avoid unwanted compiler optimization. */
volatile int* edge_capture_ptr = (volatile int*) context;
/*
* Read the edge capture register on the button PIO.
* Store value.
*/
*edge_capture_ptr =IORD_ALTERA_AVALON_PIO_DATA(Switches_BASE);

//IORD_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE); // You can use this macro to read


edge captuer register
/* Write to the edge capture register to reset it. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(Switches_BASE,0xff);
/* Read the PIO to delay ISR exit. This is done to prevent a
spurious interrupt in systems with high processor -> pio
latency and fast interrupts. */
IORD_ALTERA_AVALON_PIO_EDGE_CAP(Switches_BASE);
}

// This fucntion will intialize the PIO registers for interrupts and also register interrupt handler
static void init_SW_pio()
{
/* Recast the edge_capture pointer to match the
alt_irq_register() function prototype. */
void* edge_capture_ptr = (void*) &edge_capture;
/* Enable all interrupts assosiated to switches. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(Switches_BASE, 0xff);
/* Reset the edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_BASE, 0x0);
/* Register the ISR. */
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
test = alt_ic_isr_register(SW_IRQ_INTERRUPT_CONTROLLER_ID,
SW_IRQ,
handle_button_interrupts,
edge_capture_ptr, 0x0);
// The return value in test is 0 then interrupt is enabled and handler is registered
if(test == 0)
{
printf("The interrupt enabled and function registered");
}
else
printf("Handler Not Registered");
2 Programas en C, lab 2 Página 2 de 2
#else
alt_irq_register( SW_IRQ,
edge_capture_ptr,
handle_button_interrupts );
#endif
}

void delay(void)
{
int tt = 0;
int i ,j;
for(i = 0; i<300;i++)
{
for(j = 0; j<100;j++)
{
tt = tt + 1;
}
}
}

int main()
{
int data_in;
long time;
long cycles;
unsigned char count = 0;
// init and enable interrupts
init_SW_pio();
// Keep processor in loop in order not to get out of flow
while(1)
{
// The data read during interrupt is stored in edge_capture so write it to LEDS
// you can actully write the data to LEDS in interrupt handler an do something else here...
IOWR_ALTERA_AVALON_PIO_DATA(LEDG_BASE,edge_capture);
}
return 0;
}
*************************************************************************************+
POLL.C

#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h" // defines register address map for PIOs
int main() {

volatile int ReadData; // Using volatile keyword diables some compiler optimizations that are
Unnecessary
printf("Hello from Nios II!\n");

while (1)
// stay in this loop forever
{
ReadData = IORD_ALTERA_AVALON_PIO_DATA(Switches_BASE);
//This Macro Reads data from register that is tied to Switches
//Switches_BASE is the address of that register comming from altera_avalon_pio_regs.h
IOWR_ALTERA_AVALON_PIO_DATA(LEDG_BASE, ReadData & 0xff);
// Write data to the LEDS that was read from Switches
}
return 0;
}

Вам также может понравиться