Question: Here is my code so far, it runs fine and compiles fine, I just need it to run when any of the pushbuttons are pressed

Here is my code so far, it runs fine and compiles fine, I just need it to run when any of the pushbuttons are pressed down individually because right now it only runs either when only KEY 1 is pressed or when all 3 KEYS (KEY1-3) are pressed simultaneously. Might also need to add JTAG like the directions say.
#include
#include
#include
#include
#include "altera_avalon_pio_regs.h"
#include "sys/alt_irq.h"
#include "altera_avalon_timer_regs.h"
volatile int running =1; // Initially running
volatile int edge_capture;
char hex_table[16]={0x40,0x79,0xA4,0xB0,0x99,0x92,0x82,
0x78,0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E};
int counter =1;// Start from the first odd number
int increment_flag =1; //1 for incrementing, 0 for decrementing
static void timer_init();
static void timer_isr();
void clear_hex();
void pio_init();
void handle_key_interrupts(void* context);
void write_to_seven(int count);
int main(){
clear_hex();
timer_init();
pio_init();
while(1){
usleep(30000);
if (running){
write_to_seven(counter);
}
}
return 0;
}
void write_to_seven(int count){
// Show ones place on HEX0 and tens place on HEX1
IOWR_ALTERA_AVALON_PIO_DATA(HEX0_BASE, hex_table[count & 0xF]);
IOWR_ALTERA_AVALON_PIO_DATA(HEX1_BASE, hex_table[0]); // Only shows 0 on HEX1 as count is one-digit
}
static void timer_init(){
// Stop the timer initially
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYSTEM_TIMER_BASE, 8);
// Set a 1-second interval
int period = SYSTEM_TIMER_LOAD_VALUE *1000;
int period_h = period >>16;
int period_l = period & 0xFFFF;
IOWR_ALTERA_AVALON_TIMER_PERIODL(SYSTEM_TIMER_BASE, period_l);
IOWR_ALTERA_AVALON_TIMER_PERIODH(SYSTEM_TIMER_BASE, period_h);
// Start the timer with interrupts enabled
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYSTEM_TIMER_BASE, 7);
alt_ic_isr_register(SYSTEM_TIMER_IRQ_INTERRUPT_CONTROLLER_ID, SYSTEM_TIMER_IRQ, timer_isr, NULL, NULL);
}
static void timer_isr(){
// Reset the timer status to acknowledge the interrupt
IOWR_ALTERA_AVALON_TIMER_STATUS(SYSTEM_TIMER_BASE, 0);
if (!running) return;// If paused, exit ISR
// Update counter
if (increment_flag){
counter +=2;// Move to the next odd number in increment mode
} else {
counter -=2;// Move to the previous odd number in decrement mode
}
// Boundaries for cycle: 1 to 15, then reverse back and forth
if (counter >=15){
increment_flag =0; // Start decrementing at upper boundary
} else if (counter <=1){
increment_flag =1; // Start incrementing at lower boundary
}
// Print current counter value to JTAG UART
printf("Counter: %X
", counter);
}
void pio_init(){
void* edge_capture_ptr =(void*)&edge_capture;
// Enable interrupts for all keys
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY1_BASE, 0x1);
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY2_BASE, 0x2);
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY3_BASE, 0x4);
// Clear edge capture registers
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY1_BASE, 0x0);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY2_BASE, 0x0);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY3_BASE, 0x0);
// Register the ISR for each key
alt_ic_isr_register(KEY1_IRQ_INTERRUPT_CONTROLLER_ID, KEY1_IRQ, handle_key_interrupts, edge_capture_ptr,0x00);
alt_ic_isr_register(KEY2_IRQ_INTERRUPT_CONTROLLER_ID, KEY2_IRQ, handle_key_interrupts, edge_capture_ptr,0x00);
alt_ic_isr_register(KEY3_IRQ_INTERRUPT_CONTROLLER_ID, KEY3_IRQ, handle_key_interrupts, edge_capture_ptr,0x00);
}
void handle_key_interrupts(void* context){
volatile int* edge_capture_ptr =(volatile int*) context;
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY1_BASE)+2*(IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY2_BASE))+4*(IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY3_BASE));
// Toggle running state when any key is pressed
running =!running;
// Clear edge capture registers for each key

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!