STM2 serial port configuration

Publisher:等风来88888Latest update time:2018-07-20 Source: eefocusKeywords:STM2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The previous few articles introduced the three parts of clock, GPIO, and interrupt. Next, we will introduce the commonly used debugging serial port configuration:

1. Hardware interface introduction: USART1 is used as the printing serial port, where PA9--------TX    

                                                                                                  PA10------RX

      


3. Set the interrupt grouping. For details on how to set the interrupt grouping, please refer to the previous article.

      http://blog.csdn.net/u014449366/article/details/52717299


      void UART_NVIC_Configuration(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;
  
   /* Set the Vector Table base location at 0x08000000 */
   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  
   /* Configure the NVIC Preemption Priority Bits */  
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
   /* Enable the USART1 Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Channel is set to serial port 1 interrupt
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Interrupt response priority 0
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable interrupt
   NVIC_Init(&NVIC_InitStructure); //Initialization
}


4. The interrupt configuration function mainly turns on the corresponding clock, configures the multiplexed pins, sets the baud rate and data format, turns on the receiving interrupt, and enables the serial port.


/****************************************************
Function name: USART1_Configuration
Input:
Output:
Function description:
Initialize serial port hardware device and enable interrupt
Configuration steps:
(1) Turn on GPIO and USART1 clock
(2) Set the GPIO mode of two pins of USART1
(3) Configure USART1 data format, baud rate and other parameters
(4) Enable USART1 receive interrupt function
(5) Finally enable USART1 function


********************************************************/
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;


/* Step 1: Turn on the clock of GPIO and USART components*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);


/* Step 2: Configure the GPIO of USART Tx to push-pull multiplexing mode*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);


/* Step 3: Configure the GPIO of USART Rx to floating input mode
Since the GPIO defaults to floating input mode after CPU reset, the following step is not necessary.
However, I still recommend adding it for easy reading and to prevent other places from modifying the setting parameters of this port line
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Step 3 has been done, so this step can be skipped
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
*/
GPIO_Init(GPIOA, &GPIO_InitStructure);




/* Step 4: Configure USART1 parameters
   - BaudRate = 115200 baud
   - Word Length = 8 Bits
   - One Stop Bit
   - No parity
   - Hardware flow control disabled (RTS and CTS signals)
   - Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);


    /* If the receive data register is full, an interrupt is generated*/
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


/* Step 5: Enable USART1, configuration completed*/
USART_Cmd(USART1, ENABLE);


    /* The following statement solves the problem that the first byte cannot be sent correctly*/
    USART_ClearFlag(USART1, USART_FLAG_TC); // Clear flag
}


5. Write a function to send bytes through the serial port

void Uart1_PutChar(u8 ch)
{
  USART_SendData(USART1, (u8) ch);
  while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}      


6. Write the serial port data receiving interrupt function

void USART1_IRQHandler(void) //In the interrupt service routine, since the host does not know which interrupt source issued the interrupt request when responding to the interrupt, the interrupt source must be identified in the interrupt service routine and then processed separately. Of course, if only one interrupt request is involved, the above identification is not necessary. But no matter what the situation, it is a good habit to make the above identification
{
  u8 dat;
   
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //If the receive data register is full
  {     
    dat = USART_ReceiveData(USART1);  
         
     if(dat == 0x00)                    
{   
 dat = 0;      
      GPIO_ResetBits(GPIOB, GPIO_Pin_0); //Light up LED0         
      Uart1_PutChar(0x00);                                
}                                             
  }


7. In the actual test, when 00 is sent in hexadecimal through the serial port tool, the LED is lit and the host computer receives 00.

       


Keywords:STM2 Reference address:STM2 serial port configuration

Previous article:Detailed explanation of STM32 system clock settings
Next article:STM32 interrupt configuration

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号