您好,在示例代码中我们有nvic_clearpendingirq(usart0_tx_irqn);
从手册第220页开始启用USART_TX中断。
但在之后的代码中
这意味着启用启用TXBL标志。
看来我们两次都能做同样的事,
两个启用之间的区别是什么,为什么我们需要启用两次?
https://www.silabs.com/content/user...earmcortexm4manualih-jz8g/cortex m4 guide.pdf.
从手册第220页开始启用USART_TX中断。
但在之后的代码中
代码:
//启用发送缓冲区级别中断usart_intable (USART_IEN_TXBL);
看来我们两次都能做同样的事,
两个启用之间的区别是什么,为什么我们需要启用两次?
https://www.silabs.com/content/user...earmcortexm4manualih-jz8g/cortex m4 guide.pdf.
代码:
#include“em_device.h”#include“em_chip.h”#include“em_mu.h”#include“em_cpio.h”#include“em_usart.h”#include“bsp.h”//接收数据的缓冲区的大小#define buflen 80 //接收数据缓冲区UINT8_T缓冲器[BUFLEN];//当前位置INS BUFFER UINT32_T INPOS = 0;UINT32_T OUTPOS = 0;//在接收数据时(等待Cr或Buflen字符)BOOL接收= TRUE;/ ************************************************************************** // ** * @brief * gpio初始化******************************************************************************* / void initgpio(void){cmu_clockenable(cmuclock_gpio,true);//将VCOM发送引脚配置为板控制器作为输出GPIO_PINMODEET(BSP_BCC_TXPORT,BSP_BCC_TXPIN,GPIOMODEPUSHPULL,1);//从电路板控制器配置VCOM再次引脚作为输入GPIO_PINMODEET(BSP_BCC_RXPORT,BSP_BCC_RXPIN,GPIOMODEINPUT,0);//启用VCOM连接到板控制器GPIO_PINMODEET(BSP_BCC_ENABLE_PORT,BSP_BCC_ENABLE_PIN,GPIOMODEPUSHPULL,1); } /**************************************************************************//** * @brief * USART0 initialization (VCOM on xG1/xG12/xG13 boards) *****************************************************************************/ void initUsart0(void) { CMU_ClockEnable(cmuClock_USART0, true); // Default asynchronous initializer (115.2 Kbps, 8N1, no flow control) USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT; // Configure and enable USART0 USART_InitAsync(USART0, &init); // Enable NVIC USART sources NVIC_ClearPendingIRQ(USART0_RX_IRQn); NVIC_EnableIRQ(USART0_RX_IRQn); NVIC_ClearPendingIRQ(USART0_TX_IRQn); NVIC_EnableIRQ(USART0_TX_IRQn); USART0->ROUTEPEN |= USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN; } void USART0_RX_IRQHandler(void) { // Get the character just received buffer[inpos] = USART0->RXDATA; // Exit loop on new line or buffer full if ((buffer[inpos] != '\r') && (inpos < BUFLEN)) inpos++; else receive = false; // Stop receiving on CR // Clear the requesting interrupt before exiting the handler USART_IntClear(USART0, USART_IF_RXDATAV); } /**************************************************************************//** * @brief * The USART0 transmit interrupt outputs characters. *****************************************************************************/ void USART0_TX_IRQHandler(void) { // Send a previously received character if (outpos < inpos) USART0->TXDATA = buffer[outpos++]; else /* * Need to disable the transmit buffer level interrupt in this IRQ * handler when done or it will immediately trigger again upon exit * even though there is no data left to send. */ { receive = true; // Go back into receive when all is sent USART_IntDisable(USART0, USART_IEN_TXBL); } // Clear the requesting interrupt before exiting the handler USART_IntClear(USART0, USART_IF_TXBL); } /**************************************************************************//** * @brief * Main function *****************************************************************************/ int main(void) { uint32_t i; // Chip errata CHIP_Init(); // Initialize GPIO and USART0 initGpio(); initUsart0(); while (1) { // Zero out buffer for (i = 0; i < BUFLEN; i++) buffer[i] = 0; // Enable receive data valid interrupt USART_IntEnable(USART0, USART_IEN_RXDATAV); // Wait in EM1 while receiving to reduce current draw while (receive) EMU_EnterEM1(); // Disable receive data valid interrupt USART_IntDisable(USART0, USART_IEN_RXDATAV); // Enable transmit buffer level interrupt USART_IntEnable(USART0, USART_IEN_TXBL); // Wait in EM1 while transmitting to reduce current draw while (!receive) EMU_EnterEM1(); // Reset buffer indices inpos = outpos = 0; } }
最后的编辑: