Question: My code compiles fine and works properly, besides the push buttons I cannot get to work properly, and it has something to do with the

My code compiles fine and works properly, besides the push buttons I cannot get to work properly, and it has something to do with the code, but I cannot figure out why. Here is my code:
int main(){
clear_hex();
timer_init(period_multipler);
pio_init();
while (1){
IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, x);
}
return 0;
}
static void timer_init(int period_multipler){
// Setting the stop bit high
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYSTEM_TIMER_BASE, 8);
int period = SYSTEM_TIMER_LOAD_VALUE * period_multipler;
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);
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 status
IOWR_ALTERA_AVALON_TIMER_STATUS(SYSTEM_TIMER_BASE, 0);
if (incrementing_decrementing_flag){
x = pow(2, red_led);
red_led++;
if (red_led ==18) incrementing_decrementing_flag =0;
} else {
x = x /2;
red_led = red_led -1;
if (red_led ==0) incrementing_decrementing_flag =1;
}
}
void pio_init(){
void* edge_capture_key1_ptr =(void*)&edge_capture_key1;
void* edge_capture_key2_ptr =(void*)&edge_capture_key2;
void* edge_capture_key3_ptr =(void*)&edge_capture_key3;
// Initialize Key 1
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY1_BASE, 1);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY1_BASE, 0x0);
alt_ic_isr_register(KEY1_IRQ_INTERRUPT_CONTROLLER_ID, KEY1_IRQ,
handle_key1_interrupt, edge_capture_key1_ptr,0x00);
// Initialize Key 2
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY2_BASE, 1);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY2_BASE, 0x0);
alt_ic_isr_register(KEY2_IRQ_INTERRUPT_CONTROLLER_ID, KEY2_IRQ,
handle_key2_interrupt, edge_capture_key2_ptr,0x00);
// Initialize Key 3
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY3_BASE, 1);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY3_BASE, 0x0);
alt_ic_isr_register(KEY3_IRQ_INTERRUPT_CONTROLLER_ID, KEY3_IRQ,
handle_key3_interrupt, edge_capture_key3_ptr,0x00);
}
void handle_key1_interrupt(void* context){
volatile int* edge_capture_ptr =(volatile int*) context;
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY1_BASE);
// Set delay to 1 second
period_multipler =1000;
timer_init(period_multipler);
printf("Key 1 pressed. Speed set to 1 second.
");
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY1_BASE, 0x0);
IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY1_BASE);
}
void handle_key2_interrupt(void* context){
volatile int* edge_capture_ptr =(volatile int*) context;
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY2_BASE);
// Set delay to 250 ms
period_multipler =250;
timer_init(period_multipler);
printf("Key 2 pressed. Speed set to 250 milliseconds.
");
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY2_BASE, 0x0);
IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY2_BASE);
}
void handle_key3_interrupt(void* context){
volatile int* edge_capture_ptr =(volatile int*) context;
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(KEY3_BASE);
// Set delay to 125 ms
period_multipler =125;
timer_init(period_multipler);
printf("Key 3 pressed. Speed set to 125 milliseconds.
");

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!