Erxterner-INT in C für das MM32 Board Light

//----------------------------------------------------------------------
//  MM32 example extern INT
//  MM32L073
//  LED = B0
//----------------------------------------------------------------------
#include <stddef.h>
#include <stdlib.h>
#include "hardware.h"

GPIO_InitTypeDef   GPIO_InitStructure;
EXTI_InitTypeDef   EXTI_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;
volatile bool mustToggle;

void initApplication()
{
    SysTick_Config(SystemCoreClock/100);
    mustToggle = false;

    /* Enable GPIOA clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    
    /* Configure Button pin as input */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // Input with pull-down
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Enable SYSCFG clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

    /* Connect EXTI0 Line to PA0 pin */
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
    
    /* Configure EXTI Line0 */
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    /* Configure NVIC for EXTI0_1 */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Enable GPIOB clock for LED */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    
    /* Configure LED pin as output */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

int main(void)
{
    SystemInit();
    initApplication();
    
    do
    {
        if (mustToggle)
        {
            GPIO_SetBits(GPIOB, GPIO_Pin_0);
            waitMs(100);
            GPIO_ResetBits(GPIOB, GPIO_Pin_0);
            waitMs(100);
        }
        else
        {
            GPIO_ResetBits(GPIOB, GPIO_Pin_0);
        }
    } while (1);
    
    return 0;
}

void SysTick_Handler(void)
{
    // Empty SysTick handler
}

void EXTI0_1_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line0) != RESET)
    {
        if (mustToggle)
            mustToggle = false;
        else
            mustToggle = true;
            
        EXTI_ClearITPendingBit(EXTI_Line0);
    }
}
//-----------------------------------------------------------------------------

Test

Nutzen sie die Schaltflächen Kompilieren, Linken und Brennen. Stellen Sie die nötigen Verbindungen auf dem Board mit den dafür vorgesehenen Patchkabeln her. Testen Sie die Anwendung.

Weiter mit