PreLAB: External Interrupt

Name:

ID:

I. Introduction

In this tutorial, we will learn how to use External Interrupt. We will create functions that capture the falling edge trigger by pushing a button using an external interrupt.

The objectives of this tutorial are how to

  • Configure External input (EXTI) interrupt with NVIC

  • Create your own functions for configuration of interrupts

Hardware

  • NUCLEO -F411RE

Software

  • VS code, CMSIS, EC_HAL

Documentation

II.Basics of External Interrupt (EXTI)

A. Register List

List of external interrupt (EXTI) registers used in this tutorial [Reference Manual ch7, ch10.2]

Register List

B. Register Setting

(Digital Input Setting)

  • Enable GPIO peripheral clock RCC->AHB1ENR

  • Configure DigitalIn pin

(EXTI Setting)

  • Enable SYSCFG peripheral clock. RCC->APB2ENR

  • Connect the corresponding external line to GPIO SYSCFG->EXTICR

  • Configure the trigger edge. EXTI->FTSR/RTSR

  • Configure Interrupt mask EXTI->IMR

  • Enable EXTI. EXTI->IMR

(NVIC Setting)

  • Configure the priority of EXTI interrupt request. NVIC_SetPriority()

  • Enable EXTI interrupt request. NVIC_EnableIRQ()

(EXTI Use)

  • Create user codes in handler EXTIx_IRQHandler()

  • Clear pending bit after interrupt call

III. Tutorial

A. Register Configuration

Fill in the blanks below

  1. Pin Initialization & Set LED and Push-button

  • LED Pin : Port B Pin 12 / Output / Push-Pull / No Pull-Up & No Pull-Down

  • Push-Button: Port A Pin 4 / Input / No Pull-Up & No Pull-Down

// Use your library  GPIO

  1. Enable Peripheral Clock: SYSCFGEN

  • RCC_APB2ENR: Enable SYSCFG

RCC_APB2ENR
  1. EXTI Initialization & Connect Push-button to EXTI line

  • SYSCFG_EXTICR2: Connect PA_4(push-button) to EXTI4 line

EXTICR
  • EXTI_FTSR: Enable Falling Trigger

FTSR
  • EXTI_IMR: Interrupt NOT masked (Enable)

IMR

B. Programming

This is an example code for toggling LED on/off with the button input trigger (EXTI)

Fill in the empty spaces in the code.

Procedure

  • Name the project as TU_EXTI by creating a new folder as tutorial\TU_EXTI

  • Download the template code

  • Fill in the empty spaces in the code.

  • Run the program and check your result.

  • Your tutorial report must be submitted to LMS

DO NOT use ecEXTI2_student.h for this tutorial.

You MUST write your name on the source file inside the comment section

//#include "ecSTM32F4v2.h"
#include "ecRCC2.h"
#include "ecGPIO2.h"

#define LED_PIN   PB_12 		//EVAL board JKIT
#define BUTTON_PIN PA_4			//EVAL board JKIT

void LED_toggle(PinName_t pinName);

// Initialiization 
void setup(void)
{
	RCC_PLL_init();                         // System Clock = 84MHz
	// Initialize GPIOB_12 for Output
	GPIO_init(LED_PIN, OUTPUT);    // LED for EVAL board	
	// Initialize GPIOA_4 for Input Button
	GPIO_init(BUTTON_PIN, INPUT);  // OUTPUT for EVAL borad
	EXTI_init_tutorial(PA_4);
		
}

// MAIN  ----------------------------------------
int main(void) {
	setup();
	while (1);
}


// EXTI Initialiization ------------------------------------------------------
// YOUR CODE GOES HERE	
void EXTI_init_tutorial(PinName_t pinName){
	GPIO_Typedef *Port;
	unsigned int pin;
	ecPinmap(pinName,&Port,&pin);
	

	// SYSCFG peripheral clock enable
	RCC->APB2ENR |= __________________

	// Connect External Line to the GPIO
	// Button: PA_4 -> EXTICR2(EXTI4)
	SYSCFG->EXTICR[____] &= ~SYSCFG_EXTICR2_EXTI4;
	SYSCFG->EXTICR[____] |= ______________________;

	// Falling trigger enable (Button: pull-up)
	EXTI->FTSR |= 1UL << __________;

	// Unmask (Enable) EXT interrupt
	EXTI->IMR |= 1UL << ___________;

	// Interrupt IRQn, Priority
	NVIC_SetPriority(EXTI4_IRQn, 0);  		// Set EXTI priority as 0	
	NVIC_EnableIRQ(EXTI4_IRQn); 			// Enable EXTI 
	
}

// YOUR CODE GOES HERE
void EXTI4_IRQHandler(void) {
	if ((EXTI->PR & EXTI_PR_PR4) == _________) {
		LED_toggle(LED_PIN);
		EXTI->PR |= EXTI_PR_PR4; // cleared by writing '1'
	}
}


void LED_toggle(PinName_t pinName){
	GPIO_Typedef *Port;
	unsigned int pin;
	ecPinmap(pinName,&Port,&pin);
	// YOUR CODE GOES HERE
}

Last updated

Was this helpful?