Tutorial 3 - Interrupt Types and Priority

STM32F4Discovery Tutorial 3 - Interrupt Types and Priority

In this tutorial we will go a step further into the interrupt process. Till now we were just learning about the external interrupt. But now we will see what other types of interrupts are available in the stm32f4discovery board. Then we will learn about a new concept called interrupt priority. We will understand how interrupt priority works. Then finally we will write program to understand how interrupt priority actually works in the microcontroller. How you can program interrupt priorities in the code.

#include "stm32f4xx.h"

GPIO_InitTypeDef GPIO_InitLed;
EXTI_InitTypeDef EXTI_InitSwitch;
NVIC_InitTypeDef NVIC_InitSwitch;
int i =0;
int j =0;

void Delay(__IO uint32_t nCount){
        while(nCount--){
        }
}

int main(void)
{
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

        GPIO_InitLed.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
        GPIO_InitLed.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitLed.GPIO_OType = GPIO_OType_PP;
        GPIO_InitLed.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitLed.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOD, &GPIO_InitLed);

        GPIO_InitLed.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
        GPIO_InitLed.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitLed.GPIO_OType = GPIO_OType_PP;
        GPIO_InitLed.GPIO_PuPd = GPIO_PuPd_DOWN;
        GPIO_InitLed.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitLed);

        SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource1);
        SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource2);

        EXTI_InitSwitch.EXTI_Line = EXTI_Line1;
        EXTI_InitSwitch.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitSwitch.EXTI_Trigger = EXTI_Trigger_Rising;
        EXTI_InitSwitch.EXTI_LineCmd = ENABLE;
        EXTI_Init(&EXTI_InitSwitch);

        EXTI_InitSwitch.EXTI_Line = EXTI_Line2;
        EXTI_InitSwitch.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitSwitch.EXTI_Trigger = EXTI_Trigger_Rising;
        EXTI_InitSwitch.EXTI_LineCmd = ENABLE;
        EXTI_Init(&EXTI_InitSwitch);

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

        NVIC_InitSwitch.NVIC_IRQChannel = EXTI1_IRQn;
        NVIC_InitSwitch.NVIC_IRQChannelPreemptionPriority = 1;
        NVIC_InitSwitch.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitSwitch.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitSwitch);

        NVIC_InitSwitch.NVIC_IRQChannel = EXTI2_IRQn;
        NVIC_InitSwitch.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitSwitch.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitSwitch.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitSwitch);

        while (1)
        {
                GPIO_WriteBit(GPIOD, GPIO_Pin_0, Bit_SET);
                Delay(1680000);
                GPIO_WriteBit(GPIOD, GPIO_Pin_0, Bit_RESET);
                Delay(1680000);

        }
}

void EXTI1_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line1)!=RESET)
        {
                i=0;
                do
                {
                        GPIO_WriteBit(GPIOD, GPIO_Pin_1, Bit_SET);
                        Delay(16800000);
                        GPIO_WriteBit(GPIOD, GPIO_Pin_1, Bit_RESET);
                        Delay(16800000);
                        i++;
                }while(i<5);
                EXTI_ClearITPendingBit(EXTI_Line1);
        }
}

void EXTI2_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line2)!=RESET)
        {
                j=0;
                do
                {
                        GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_SET);
                        Delay(16800000);
                        GPIO_WriteBit(GPIOD, GPIO_Pin_2, Bit_RESET);
                        Delay(16800000);
                        j++;
                }while(j<5);
                EXTI_ClearITPendingBit(EXTI_Line2);
        }
}