0% found this document useful (0 votes)
95 views

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

This document contains code for two programs - INTERRUPTS.C and POLL.C. INTERRUPTS.C uses interrupts to capture button presses on a PIO and store the value in an edge_capture variable. It defines an interrupt handler function that reads the edge capture register and resets it. The init_SW_pio function initializes the PIO registers, enables interrupts, and registers the interrupt handler. POLL.C continuously polls the switch PIO register and writes the value to the LED PIO without using interrupts.

Uploaded by

jkosorio
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

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

This document contains code for two programs - INTERRUPTS.C and POLL.C. INTERRUPTS.C uses interrupts to capture button presses on a PIO and store the value in an edge_capture variable. It defines an interrupt handler function that reads the edge capture register and resets it. The init_SW_pio function initializes the PIO registers, enables interrupts, and registers the interrupt handler. POLL.C continuously polls the switch PIO register and writes the value to the LED PIO without using interrupts.

Uploaded by

jkosorio
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 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;
}

You might also like