STM32中发送中断标志位

来源:互联网 发布:网络打印机管理软件 编辑:程序博客网 时间:2024/06/11 21:27

数据手册中对此标志位的解释:

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串口发送中断,基本代码如下:

  1. <font color="rgb(85, 85, 85)"><font face="verdana,"><span style="background-color: white;">void usart_Configuration(void)
  2. {
  3.     USART_InitTypeDef USART_InitStruct;
  4.     GPIO_InitTypeDef GPIO_InitStruct;

  5.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
  6.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE);
  7.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 ,ENABLE);

  8.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  9.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
  10.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  11.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  12.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  13.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;    
  14.     GPIO_Init(GPIOA, &GPIO_InitStruct);

  15.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  16.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
  17.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  18.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  19.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  20.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;    
  21.     GPIO_Init(GPIOA, &GPIO_InitStruct);

  22.     GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_4;                     //485 dir pin
  23.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  24.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; 
  25.     GPIO_Init(GPIOA ,&GPIO_InitStruct);

  26.     USART_InitStruct.USART_BaudRate = 9600;
  27.     USART_InitStruct.USART_StopBits = USART_StopBits_1;
  28.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  29.     USART_InitStruct.USART_Parity = USART_Parity_No;
  30.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  31.     USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  32.     
  33.     USART_Init(USART2, &USART_InitStruct);
  34.     USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  35.     USART_ClearFlag(USART2,USART_IT_TC);             
  36.     USART_Cmd(USART2, ENABLE);
  37.     RS485_DIR_0; 

  38.     USART_InitStruct.USART_BaudRate = 115200;
  39.     USART_Init(USART1, &USART_InitStruct);
  40.     USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  41.     USART_ClearFlag(USART1,USART_IT_TC);        
  42.     USART_Cmd(USART1, ENABLE);
  43. }</span></font></font>
复制代码

上面配置了USART1和USART2(个人项目需要),并设置了串口1和串口2接收中断(USART_ITConfig(USART1,USART_IT_RXNE,ENABLE)。


    启动串口1数据发送的代码:

  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)
  2. {
  3.     if(len >= MAX_TX1_BUFSIZE)
  4.         len = MAX_TX1_BUFSIZE;

  5.     memcpy(uart1_tx_buf,buf,len);
  6.     uart1_tx_SendLength = len;

  7.     USART_ITConfig(USART1,USART_IT_TXE,ENABLE);    //这里立即启动了发送中断
  8. }</span></font></font>
复制代码
    串口1的中断函数:
  1. void USART1_IRQHandler(void)
  2. {
  3.     if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)           //USART1接收中断
  4.     {    
  5.         uart1_rx_buf[uart1_rx_RecvIndex] = USART1->DR;
  6.         uart1_rx_RecvIndex++;
  7.         if(uart1_rx_RecvIndex > 3)
  8.         {
  9.              uart1_rx_RecvIndex = 4;
  10.              Uart1_RS232_SendString(uart1_rx_buf,4);
  11.         }
  12.         //uart1_rx_RecvOvertimeCnt = 0;
  13.         //uart1_rx_RecvFlag = 1;
  14.     }

  15.     if(USART_GetITStatus(USART1,USART_IT_TXE) != RESET)               //USART1发送中断
  16.     {
  17.         if(uart1_tx_SendIndex < uart1_tx_SendLength)              
  18.         {
  19.             USART1->DR = uart1_tx_buf[uart1_tx_SendIndex]&0x01FF;
  20.             uart1_tx_SendIndex++;
  21.         }
  22.         else
  23.         {
  24.             uart1_tx_SendIndex = 0;            
  25.             USART_ITConfig(USART1, USART_IT_TXE, DISABLE);         //关闭发送中断           
  26.         }
  27.     }
  28. }
复制代码
结合代码和上面数据手册标志位的解释,就很容易看懂了。
0 0
原创粉丝点击