STM32 learning: USART interrupt mode

Publisher:美丽花朵Latest update time:2018-10-21 Source: eefocusKeywords:STM32  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 Earlier we have learned about the query method of serial communication. Now let's introduce the interrupt method.

   Step 1: Initialize GPIO

GPIO_InitTypeDef GPIO_InitStructure;

 /* Configure USART1 Tx (PA.09) as alternate function push-pull */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 GPIO_Init(GPIOA, &GPIO_InitStructure);

 /* Configure USART1 Rx (PA.10) as input floating */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

 

Step 2: Turn on the clock

/* Enable USART1, GPIOA, GPIOD and AFIO clocks */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD

                         | RCC_APB2Periph_AFIO, ENABLE);

It is noted here that it is also possible not to set RCC_APB2Periph_AFIO, that is, the multiplexing function is not used here.

These two steps are the same as the query method.

 

Step 3: Initialize USART1

USART_InitStructure.USART_BaudRate = 115200;

 USART_InitStructure.USART_WordLength = USART_WordLength_8b;

 USART_InitStructure.USART_StopBits = USART_StopBits_2;

 USART_InitStructure.USART_Parity = USART_Parity_No; //Communication error occurs when setting odd parity

 USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;

 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 /* Configure the USART1 */

 USART_Init(USART1, &USART_InitStructure);

 /* Enable the USART Transmoit interrupt: this interrupt is generated when the

     USART1 transmit data register is empty */ 

 USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

 /* Enable the USART Receive interrupt: this interrupt is generated when the

     USART1 receive data register is not empty */

 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

 /* Enable USART1 */

 USART_Cmd(USART1, ENABLE);

Here we need to enable the peripheral interrupt of USART1, such as USART_ITConfig(USART1, USART_IT_TXE, ENABLE); this is to enable the transmit interrupt, but an interrupt can be generated when the transmit register is empty.

 

Step 4: Write the interrupt function

uint8_t TxBuffer[] = "\n\rUSART Hyperterminal Interrupts Example: USART-Hyperterminal\

 communication using Interrupt\n\r";

uint8_t RxBuffer[RxBufferSize];

uint8_t NbrOfDataToTransfer = TxBufferSize;

uint8_t NbrOfDataToRead = RxBufferSize;

uint8_t TxCounter = 0;

uint16_t RxCounter = 0;

 

void USART1_IRQHandler(void)

{

 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

 {

    /* Read one byte from the receive data register */

    RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);

    if(RxCounter == NbrOfDataToRead)

    {

      /* Disable the USART Receive interrupt */

      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

    }

 }

 

 if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

 {  

    /* Write one byte to the transmit data register */

      USART_SendData(USART1, TxBuffer[TxCounter++]);

    if(TxCounter == NbrOfDataToTransfer)

    {

      /* Disable the USART1 Transmit interrupt */

      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

    }

 }

}

 

This is the end of the procedure.

 

We will have a question, main() only includes the initialization of the first three steps and an infinite loop, so how is the interrupt triggered? The structure of main() is as follows:

int main(void)

{

 /* System Clocks Configuration */

 RCC_Configuration();

 /* NVIC configuration */

 NVIC_Configuration();

 /* Configure the GPIO ports */

 GPIO_Configuration();

 USART_Configuration();

 

 while (1)

 {

 }

}

It turns out that the reset value of the status register USART_SR is 0x00C0H, that is, the reset value of the seventh bit TXE and the sixth bit TC is 1, and TXE=1 indicates that the transmit data register is empty, and TC=1 indicates that the transmission is completed. In the USART settings, there is

USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

These two sentences enable interrupts, that is, when TXE=1, an interrupt will be entered, so the program can enter the interrupt after initialization and execute

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)

 {  

    /* Write one byte to the transmit data register */

      USART_SendData(USART1, TxBuffer[TxCounter++]);

    if(TxCounter == NbrOfDataToTransfer)

    {

      /* Disable the USART1 Transmit interrupt */

      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

    }

 }



Keywords:STM32  USART Reference address:STM32 learning: USART interrupt mode

Previous article:STM32 learning: the simplest GPIO operation steps
Next article:STM32 learning: preliminary understanding of NVIC

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号