#include "xparameters.h" // System definitions #include "stdio.h" #include "xbasic_types.h" // Low-level driver to access the GPIO peripherals #include "xgpio_l.h" // Low-level driver to configure the Interrupt Controller #include "xintc_l.h" // Low-level driver to configure interrupt on PowerPC #include "xexception_l.h" // Low-level driver to access the switch_debouncer peripheral #include "switch_debouncer.h" volatile Xuint8 interrupt_flag; volatile Xuint32 button_state; // Interrupt handler for the Switch Debouncer // This handler clears the interrupt flag and increments the 4-bit LEDs void Switch_Debouncer_Interrupt_Handler (void * baseaddr_p) { Xuint32 baseaddr = (Xuint32) baseaddr_p; Xuint32 Status; static Xuint8 led_counter = 0; // Read status from Interrupt Status Register. Status = SWITCH_DEBOUNCER_mReadReg(baseaddr, SWITCH_DEBOUNCER_INTR_IPISR_OFFSET ); // Write back status to Interrupt Status Register to clear interrupt SWITCH_DEBOUNCER_mWriteReg(baseaddr, SWITCH_DEBOUNCER_INTR_IPISR_OFFSET , Status); // Read button state and output it to the LEDs_Positions peripheral button_state = SWITCH_DEBOUNCER_ReadSwitchState(baseaddr, 0); //XGpio_mSetDataReg(XPAR_LEDS_POSITIONS_BASEADDR, 1, button_state); // Increment 4-bit LEDs XGpio_mSetDataReg(XPAR_LEDS_4BIT_BASEADDR, 1, button_state); //++led_counter); interrupt_flag = 1; // Set the interrupt flag } int main() { interrupt_flag = 0; print("Welcome to the Test_Switch_Debouncer program\r\n\r\n"); // Configure GPIO for 4-bit LEDs as output XGpio_mSetDataDirection(XPAR_LEDS_4BIT_BASEADDR, 1, 0x00); // Turn off 4-Bit LEDs XGpio_mSetDataReg(XPAR_LEDS_4BIT_BASEADDR, 1, 0x00); // Configure GPIO for 5-bit Position LEDs as output //XGpio_mSetDataDirection(XPAR_LEDS_POSITIONS_BASEADDR, 1, 0x00); // Turn off 5-Bit Position LEDs //XGpio_mSetDataReg(XPAR_LEDS_POSITIONS_BASEADDR, 1, 0x00); // Initialize Interrupts on PowerPC XExc_Init(); // Register the interrupt handler of the XPS Interrupt Controller // with the PowerPC's external interrupt. XExc_RegisterHandler(XEXC_ID_NON_CRITICAL_INT, (XExceptionHandler)XIntc_DeviceInterruptHandler, (void *)XPAR_INTC_0_DEVICE_ID); // Register the Switch Debouncer interrupt handler in // the vector table of the XPS Interrupt Controller XIntc_RegisterHandler(XPAR_INTC_0_BASEADDR, XPAR_XPS_INTC_0_SWITCH_DEBOUNCER_0_IP2INTC_IRPT_INTR, (XInterruptHandler)Switch_Debouncer_Interrupt_Handler, (void *)XPAR_SWITCH_DEBOUNCER_0_BASEADDR); // Start the XPS Interrupt Controller XIntc_mMasterEnable(XPAR_INTC_0_BASEADDR); // Enable Switch Debouncer interrupt requests in the XPS Interrupt Controller XIntc_mEnableIntr(XPAR_INTC_0_BASEADDR, XPAR_SWITCH_DEBOUNCER_0_IP2INTC_IRPT_MASK); // Local Interrupt enable for the Switch Debouncer peripheral SWITCH_DEBOUNCER_mWriteReg(XPAR_SWITCH_DEBOUNCER_0_BASEADDR, SWITCH_DEBOUNCER_INTR_IPIER_OFFSET, 1); // Global interrupt enable for the Switch Debouncer peripheral SWITCH_DEBOUNCER_mWriteReg(XPAR_SWITCH_DEBOUNCER_0_BASEADDR, SWITCH_DEBOUNCER_INTR_DGIER_OFFSET, INTR_GIE_MASK); // Enable PowerPC non-critical (external) interrupts XExc_mEnableExceptions(XEXC_NON_CRITICAL); // Wait for the state of the Buttons to change while(1) { // When the state of the buttons changes... // Print which button is pressed (one button at a time) if(interrupt_flag) { switch(button_state) { case 0x01: print("North Button Pressed\r\n"); break; case 0x02: print("East Button Pressed\r\n"); break; case 0x04: print("South Button Pressed\r\n"); break; case 0x08: print("West Button Pressed\r\n"); break; case 0x10: print("Center Button Pressed\r\n"); break; } interrupt_flag = 0; // Clear the interrupt flag } } }