javierhernandezdeniz

Interrupt with external switch

Discussion created by javierhernandezdeniz on Mar 22, 2018
Latest reply on Mar 23, 2018 by javierhernandezdeniz

Target: FRDM-K64F 

Firmware DAPLink rev0244

MCUXpresso IDE v10.1.1 [Build 606] [2018-01-02]

KSDK 2.3.0

 

Hello,

 

I'm trying to controlled a 7 Segment Led Display with a external switch (not on board switch) but this code not works. 

 

Please, What is the correct way to create the interrupt for PTC11?

 

Best regards

 

 

#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_debug_console.h"
#include "fsl_port.h"
#include "fsl_gpio.h"
#include "fsl_common.h"
/*******************************************************************************
* Definitions
******************************************************************************/

//FRDM-K64F pintout-> https://os.mbed.com/platforms/FRDM-K64F/#board-pinout
#define SW_FQ_GPIO GPIOC /* Frequency switch :: PTC11 */
#define SW_FQ_GPIO_PORT PTC
#define SW_FQ_GPIO_PIN 11U
#define SW_FQ_IRQ 61U
#define SW_FQ_IRQ_HANDLER PORTC_IRQHandler
#define SW_PWR_GPIO_PORT PTC /* Power button :: PTC3 */ /*TO-DO*/
#define SW_PWR_GPIO_PIN 3U
/**
* E10561-J
* Common Anode 3,8
*
* A B C D E F G DP
* 7 6 4 2 1 9 10 5
*
* A -- PTC7
* B -- PTC0
* C -- PTC4
* D -- PTC2
* E -- PTC1
* F -- PTC9
* G -- PTC10
* DP - PTC5
**/

#define LED_GPIO GPIOC
#define LED_A_GPIO_PORT PTC
#define LED_A_GPIO_PIN 7U
#define LED_A_GPIO_NAME "A"
#define LED_B_GPIO_PORT PTC
#define LED_B_GPIO_PIN 0U
#define LED_B_GPIO_NAME "B"
#define LED_C_GPIO_PORT PTC
#define LED_C_GPIO_PIN 4U
#define LED_C_GPIO_NAME "C"
#define LED_D_GPIO_PORT PTC
#define LED_D_GPIO_PIN 2U
#define LED_D_GPIO_NAME "D"
#define LED_E_GPIO_PORT PTC
#define LED_E_GPIO_PIN 1U
#define LED_E_GPIO_NAME "E"
#define LED_F_GPIO_PORT PTC
#define LED_F_GPIO_PIN 9U
#define LED_F_GPIO_NAME "F"
#define LED_G_GPIO_PORT PTC
#define LED_G_GPIO_PIN 10U
#define LED_G_GPIO_NAME "G"
#define LED_DP_GPIO_PORT PTC
#define LED_DP_GPIO_PIN 5U
#define LED_DP_GPIO_NAME "DP"
/*******************************************************************************
* Prototypes
******************************************************************************/

/*******************************************************************************
* Variables
******************************************************************************/

/* Whether the SW button is pressed */
volatile bool g_ButtonPress = false;
/*******************************************************************************
* Code
******************************************************************************/

/*!
* @brief Interrupt service fuction of switch.
*
* This function toggles the LED
*/

void SW_FQ_IRQ_HANDLER(void)
{
/* Clear external interrupt flag. */
GPIO_PortClearInterruptFlags(SW_FQ_GPIO, 1U << SW_FQ_GPIO_PIN);
/* Change state of button. */
g_ButtonPress = true;
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
exception return operation might vector to incorrect interrupt */

#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}
/*
* @brief Application entry point.
*/

int main(void) {
/* Define the init structure for the input frequency switch pin */
gpio_pin_config_t sw_fq_config = {
kGPIO_DigitalInput, 0,
};
/* Define the init structure for the input power switch pin */
gpio_pin_config_t sw_pwr_config = {
kGPIO_DigitalInput, 0,
};
/* Define the init structure for the output 7Segment Led Display pin */
gpio_pin_config_t led_config = {
kGPIO_DigitalOutput, 0,
};
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
/* Init FSL debug console. */
BOARD_InitDebugConsole();
/* Print a note to terminal. */
PRINTF("\r\n OpenVTX Firmware\r\n");
PRINTF("\r\n Press the Frequency switch to change the channel \r\n");
/* Init Frequency input switch GPIO */
PORT_SetPinInterruptConfig(SW_FQ_GPIO_PORT, SW_FQ_GPIO_PIN, kPORT_InterruptFallingEdge);
EnableIRQ(SW_FQ_IRQ);
GPIO_PinInit(SW_FQ_GPIO, SW_FQ_GPIO_PIN, &sw_fq_config);
/* Init output 7 Segment Led Display */
GPIO_PinInit(LED_GPIO, LED_A_GPIO_PIN, &led_config);
while(1)
{
if(g_ButtonPress)
{
/* Toggle A LED */
GPIO_PortToggle(LED_GPIO, 1U << LED_A_GPIO_PIN);
/* Reset state of button */
g_ButtonPress = false;
}
}
}

 

7 Segment Display datasheet

Outcomes