Lanqiao Cup Embedded-Serial Communication Related

basis

The CT117E development board leads to two serial ports, but we will only use the circuit of serial port 2, because serial port 1 is connected to a nine-pin port, which is generally not used.
Insert picture description here
Serial port configuration steps:

  1. Serial port clock enable, GPIO clock enable
  2. Serial port reset
  3. GPIO initialization
  4. Serial port parameter initialization
  5. Turn on the serial port interrupt and initialize the interrupt
  6. Enable serial port
  7. Write serial port interrupt service function for serial port to receive data
  8. Serial data sending function

Code:

void  usart2_init()
{
    
    
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 NVIC_InitTypeDef  NVIC_InitStructure;
 
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
 
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOA,&GPIO_InitStructure);//PA2为发送引脚
 
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//PA3为接收引脚
 GPIO_Init(GPIOA,&GPIO_InitStructure);

 USART_InitStructure.USART_BaudRate=9600;//波特率
 USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//不适用硬件流控制
 USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//发送和接受模式
 USART_InitStructure.USART_WordLength=USART_WordLength_8b;//8位数据为
 USART_InitStructure.USART_StopBits=USART_StopBits_1;//1位停止位
 USART_InitStructure.USART_Parity=USART_Parity_No;//不进行奇偶校验
 USART_Init(USART2,&USART_InitStructure);
 
 NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
 NVIC_Init(&NVIC_InitStructure);
 
 USART_Cmd(USART2,ENABLE);
 
 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//串口接收中断
}

The serial port sending function needs to be written by yourself. It should be noted that after each serial port sending data, you have to wait until the sending is completed before sending the next time. The embodiment in the code is to wait until the sending buffer is empty before sending.

void USART2_SendString(u8 *str)//中断标志状态和一般状态是不一样的。
{
    
      
 u8 index = 0; 
 do  
 {
    
        
  USART_SendData(USART2,str[index]); //发送数据    
  while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == 0);  //发送缓冲区为空就是发送完成 //USART_FLAG_TC 表示传输完毕;USART_FLAG_TXE表示发送缓冲区空   
  index++;  
 }while(str[index] != 0);
}

The reception of the serial port is placed in the interrupt function:

u8 RXbuf[20];//初始化就是0
u8 RXover=0;
u8 RXcont=0;
void USART2_IRQHandler(void)
{
    
     
  u8 temp;
  if(USART_GetITStatus(USART2,USART_IT_RXNE) == SET)//接收到中断
  {
    
    
      USART_ClearITPendingBit(USART2,USART_IT_RXNE);
    temp =  USART_ReceiveData(USART2);
    if(temp == '\n')
    {
    
    

      RXcont = 0;
      RXover = 1;//接收到的标志
      USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);//先把串口给关掉,处理接收到的数据,等需要的时候再打开。。
    }else
     {
    
    
      RXbuf[RXcont] = temp;
      RXcont++;
     }
  }
}   

Configuration in the main function:

  if(RXover)
  {
    
    
   USART2_SendString(RXbuf);
   LCD_ClearLine(Line3);
   LCD_DisplayStringLine(Line3,RXbuf);
   for(i=0;i<20;i++)
   {
    
    
     RXbuf[i]=0;
   }
   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
   RXover=0;
  }

Guess you like

Origin blog.csdn.net/qq_43690936/article/details/105403665