GPIO中断的例子
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* $Revision: 7 $
* $Date: 15/09/02 10:04a $
* [url=home.php?mod=space&uid=247401]@brief[/url] Show the usage of GPIO interrupt function.
* @note
* Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "M451Series.h"
#define PLLCTL_SETTING CLK_PLLCTL_72MHz_HXT
#define PLL_CLOCK 72000000
/**
* @brief GPIO PB IRQ
*
* @param None
*
* [url=home.php?mod=space&uid=266161]@return[/url] None
*
* [url=home.php?mod=space&uid=1543424]@Details[/url] The PB default IRQ, declared in startup_M451Series.s.
*/
void GPB_IRQHandler(void)
{
/* To check if PB.2 interrupt occurred */
if(GPIO_GET_INT_FLAG(PB, BIT2))
{
GPIO_CLR_INT_FLAG(PB, BIT2);
printf("PB.2 INT occurred.\n");
}
else
{
/* Un-expected interrupt. Just clear all PB interrupts */
PB->INTSRC = PB->INTSRC;
printf("Un-expected interrupts.\n");
}
}
/**
* @brief GPIO PC IRQ
*
* @param None
*
* @return None
*
* @details The PC default IRQ, declared in startup_M451Series.s.
*/
void GPC_IRQHandler(void)
{
/* To check if PC.5 interrupt occurred */
if(GPIO_GET_INT_FLAG(PC, BIT5))
{
GPIO_CLR_INT_FLAG(PC, BIT5);
printf("PC.5 INT occurred.\n");
}
else
{
/* Un-expected interrupt. Just clear all PC interrupts */
PC->INTSRC = PC->INTSRC;
printf("Un-expected interrupts.\n");
}
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock (Internal RC 22.1184MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Select HCLK clock source as HIRC and and HCLK source divider as 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable HXT clock (external XTAL 12MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART module clock source as HXT and UART module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
printf("+------------------------------------------------+\n");
printf("| GPIO PB.2 and PC.5 Interrupt Sample Code |\n");
printf("+------------------------------------------------+\n\n");
/*-----------------------------------------------------------------------------------------------------*/
/* GPIO Interrupt Function Test */
/*-----------------------------------------------------------------------------------------------------*/
printf("PB.2 and PC.5 are used to test interrupt ......\n");
/* Configure PB.2 as Input mode and enable interrupt by rising edge trigger */
GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
GPIO_EnableInt(PB, 2, GPIO_INT_RISING);
NVIC_EnableIRQ(GPB_IRQn);
/* Configure PC.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
GPIO_SetMode(PC, BIT5, GPIO_MODE_QUASI);
GPIO_EnableInt(PC, 5, GPIO_INT_FALLING);
NVIC_EnableIRQ(GPC_IRQn);
/* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
GPIO_ENABLE_DEBOUNCE(PB, BIT2);
GPIO_ENABLE_DEBOUNCE(PC, BIT5);
/* Waiting for interrupts */
while(1);
}
/*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/
|