STM32中发送中断标志位USART_IT_TXE的用法
时间:10-02
整理:3721RD
点击:
数据手册中对此标志位的解释:
Bit 7 TXE : Transmit data register empty
This bit is set by hardware when the content of the TDR register has been transferred into
the shift register. An interrupt is generated if the TXEIE bit =1 in the USART_CR1 register. It
is cleared by a write to the USART_DR register.
0: Data is not transferred to the shift register
1: Data is transferred to the shift register)
仔细看看这段话。TXE在数据寄存器为空的时候就会置一,也就是说你的数据发送完成之后,如果没有数据发送了,这一位就是置一的。既然是置一的,那么就会发生中断。所以要关一下中断,要发送数据的时候可以再开启中断。
所以要使用STM32串口发送中断,基本代码如下:
- <font color="rgb(85, 85, 85)"><font face="verdana,"><span style="background-color: white;">void usart_Configuration(void)
- {
- USART_InitTypeDef USART_InitStruct;
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 ,ENABLE);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; //485 dir pin
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA ,&GPIO_InitStruct);
- USART_InitStruct.USART_BaudRate = 9600;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_Parity = USART_Parity_No;
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
-
- USART_Init(USART2, &USART_InitStruct);
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- USART_ClearFlag(USART2,USART_IT_TC);
- USART_Cmd(USART2, ENABLE);
- RS485_DIR_0;
- USART_InitStruct.USART_BaudRate = 115200;
- USART_Init(USART1, &USART_InitStruct);
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_ClearFlag(USART1,USART_IT_TC);
- USART_Cmd(USART1, ENABLE);
- }</span></font></font>
上面配置了USART1和USART2(个人项目需要),并设置了串口1和串口2接收中断(USART_ITConfig(USART1,USART_IT_RXNE,ENABLE)。
启动串口1数据发送的代码:
- <font color="rgb(85, 85, 85)"><font face="verdana,"><span style="background-color: white;">void Uart1_RS232_SendString(unsigned char *buf,unsigned char len)
- {
- if(len >= MAX_TX1_BUFSIZE)
- len = MAX_TX1_BUFSIZE;
- memcpy(uart1_tx_buf,buf,len);
- uart1_tx_SendLength = len;
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //这里立即启动了发送中断
- }</span></font></font>
- void USART1_IRQHandler(void)
- {
- if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) //USART1接收中断
- {
- uart1_rx_buf[uart1_rx_RecvIndex] = USART1->DR;
- uart1_rx_RecvIndex++;
- if(uart1_rx_RecvIndex > 3)
- {
- uart1_rx_RecvIndex = 4;
- Uart1_RS232_SendString(uart1_rx_buf,4);
- }
- //uart1_rx_RecvOvertimeCnt = 0;
- //uart1_rx_RecvFlag = 1;
- }
- if(USART_GetITStatus(USART1,USART_IT_TXE) != RESET) //USART1发送中断
- {
- if(uart1_tx_SendIndex < uart1_tx_SendLength)
- {
- USART1->DR = uart1_tx_buf[uart1_tx_SendIndex]&0x01FF;
- uart1_tx_SendIndex++;
- }
- else
- {
- uart1_tx_SendIndex = 0;
- USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //关闭发送中断
- }
- }
- }