完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
有人问,串口中断需要清理标志位吗?
这里找个例子给大家看看 /**************************************************************************** * @file main.c * @version V3.00 * $Revision: 3 $ * $Date: 15/08/07 1:31p $ * @brief Show how to wake up system form Power-down mode by UART interrupt. * * @note * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #include #include "M051Series.h" #define PLL_CLOCK 50000000 /*---------------------------------------------------------------------------------------------------------*/ /* Define functions prototype */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void); void UART_CTSWakeUpTest(void); /*---------------------------------------------------------------------------------------------------------*/ /* ISR to handle UART Channel 1 interrupt event */ /*---------------------------------------------------------------------------------------------------------*/ void UART1_IRQHandler(void) { if(UART_GET_INT_FLAG(UART1, UART_ISR_MODEM_INT_Msk)) /* UART Modem Status interrupt flag */ { printf("UART Modem Status interrupt happen."); UART_ClearIntFlag(UART1, UART_ISR_MODEM_INT_Msk); /* Clear UART Modem Status interrupt flag */ } } /*---------------------------------------------------------------------------------------------------------*/ /* Function for System Entry to Power Down Mode */ /*---------------------------------------------------------------------------------------------------------*/ void PowerDownFunction(void) { /* Check if all the debug messages are finished */ UART_WAIT_TX_EMPTY(UART0); /* Enter to Power-down mode */ CLK_PowerDown(); } /*---------------------------------------------------------------------------------------------------------*/ /* UART nCTS Wake-up Function */ /*---------------------------------------------------------------------------------------------------------*/ void UART_CTSWakeUpTest(void) { printf("+----------------------------------------------------------+\n"); printf("| Power-Down and Wake-up by UART interrupt Sample Code |\n"); printf("+----------------------------------------------------------+\n\n"); /* Clear Modem Status interrupt before entering Power-down mode */ UART_ClearIntFlag(UART1, UART_ISR_MODEM_INT_Msk); /* Enable UART Wake-up function and Modem Status interrupt */ UART_EnableInt(UART1, UART_IER_WAKE_EN_Msk|UART_IER_MODEM_IEN_Msk); printf("System enter to Power-down mode.\n"); printf("Toggle nCTS of UART1 to wake-up system.\n\n"); /* Unlock protected registers before entering Power-down mode */ SYS_UnlockReg(); /* Enter to Power-down mode */ PowerDownFunction(); /* Lock protected registers after entering Power-down mode */ SYS_LockReg(); /* Disable UART Wake-up function and Modem Status interrupt */ UART_DisableInt(UART1, UART_IER_WAKE_EN_Msk|UART_IER_MODEM_IEN_Msk); printf("\nSystem waken-up done.\n"); printf("\nUART Sample Program End.\n"); } void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); /* Waiting for Internal RC clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); /* Enable external XTAL 12MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); /* Waiting for external XTAL clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); /* Set core clock as PLL_CLOCK from PLL */ CLK_SetCoreClock(PLL_CLOCK); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); CLK_EnableModuleClock(UART1_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set P3 multi-function pins for UART0 RXD and TXD */ SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk); SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0); /* Set P1 multi-function pins for UART1 RXD and TXD */ SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk); SYS->P1_MFP |= (SYS_MFP_P12_RXD1 | SYS_MFP_P13_TXD1); /* Set P0 multi-function pins for UART1 CTS */ SYS->P0_MFP = SYS->P0_MFP & (~SYS_MFP_P00_Msk) | SYS_MFP_P00_CTS1; } void UART0_Init() { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset UART0 */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } void UART1_Init() { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset UART1 */ SYS_ResetModule(UART1_RST); /* Configure UART1 and set UART1 Baudrate */ UART_Open(UART1, 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(); /* Init UART0 for testing */ UART1_Init(); /*---------------------------------------------------------------------------------------------------------*/ /* SAMPLE CODE */ /*---------------------------------------------------------------------------------------------------------*/ printf("\n\nCPU @ %dHz\n", SystemCoreClock); printf("\n\nUART Sample Program.\n"); /* UART wake-up sample function */ UART_CTSWakeUpTest(); while(1); } |
|
相关推荐
1个回答
|
|
在这个例子中,我们可以看到在处理串口中断时,会清理中断标志位。具体的代码如下:
void UART0_IRQHandler(void) { uint8_t u8InChar=0xFF; /* Rx Ready or Time-out */ if(UART_GET_INT_FLAG(UART0, UART_INTSTS_RDAIF_Msk) || UART_GET_INT_FLAG(UART0, UART_INTSTS_RXTOIF_Msk)) { /* Read data until RX FIFO is empty */ while(UART_GET_RX_EMPTY(UART0) == 0) { /* Get UART char */ u8InChar = (uint8_t)UART_READ(UART0); /* Debug info */ if(g_u32comRbytes < RXBUFSIZE) { g_u8RecData[g_u32comRtail++] = u8InChar; g_u32comRtail &= RXBUFSIZE - 1; g_u32comRbytes++; } else { /* FIFO over run */ } } } /* Tx Ready */ if(UART_GET_INT_FLAG(UART0, UART_INTSTS_THREIF_Msk)) { uint16_t tmp; tmp = g_u32comTbytes; if(tmp) { /* Fill the Tx FIFO */ while(UART_GET_TX_FULL(UART0)==0 && tmp) { UART_WRITE(UART0, g_u8RecData[g_u32comThead++]); g_u32comThead &= RXBUFSIZE - 1; tmp--; } g_u32comTbytes = tmp; } else { /* Disable Tx Ready Interrupt */ UART_DISABLE_INT(UART0, UART_INTEN_THREIEN_Msk); } } /* Wake Up Interrupt Flag */ if(UART_GET_INT_FLAG(UART0, UART_INTSTS_WKIF_Msk)) { UART_ClearIntFlag(UART0, UART_INTSTS_WKIF_Msk); printf("UART wake-up.n"); } } 在处理完中断后,会调用UART_ClearIntFlag函数来清理中断标志位。所以在这个例子中,不需要手动清理中断标志位。 |
|
|
|
只有小组成员才能发言,加入小组>>
818 浏览 0 评论
1162 浏览 1 评论
2537 浏览 5 评论
2872 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2720 浏览 6 评论
keil5中manage run-time environment怎么是灰色,不可以操作吗?
1115浏览 3评论
198浏览 2评论
465浏览 2评论
382浏览 2评论
M0518 PWM的电压输出只有2V左右,没有3.3V是怎么回事?
463浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-29 13:55 , Processed in 1.027949 second(s), Total 76, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号