简体   繁体   中英

How to implement UART1 Interrupts correctly on STM32f103xx while running a main programme?

I am trying to program my bluePill to blink an LED at the PB11 pin while echoing whats being send over the serial port UART1.

as far as my knowledge, Interrupts allows us to run the programme we want and while running it if any flag triggered an interrupt signal the program the control will be diverted to run the interrupt service routine while the original program is running, am I correct here?

So, I'm trying to keep the Green LED blinking and when I type anything on the serial port the red LED blinks while the green is blinking and the blue led must be always turned on unless data is being sent.

Problem 1: The green led never blinks!

Problem 2: Whenever sending letters or numbers through Arduino serial monitor the received echo is always 2 letters out of the entire send letters, also the echo is always on the same line, I mean not not on a newline so typing Hello and pressing enter yields He and when typing hi there and pressing enter the it just adds the first two letters like so Hehi , why?

my code is



/* ********************** Project Description ***********************
    STM32f103xx BluePill Board
    PB10 = Green LED
    PB11 = Blue LED (To be toggled)
    PB0 = Red LED (PWM OUTPUT Controlling the Brightness)
    PB9 = Push Button for toggling the state of the blue LED
    PA0 = Potentiomater Pin (Analog Input)
    USART1 Activated and Sends the Voltage of PA0 to the user1



*/


#include "stm32f1xx.h"  // Include the MCU header
#include <stdbool.h>    // Boolean Library header
#include <stdint.h>


// Prototypes:
void UART1_Init(void);              //Enable UART1 on PA9(Tx) & PA10(Rx).
void portsEnable(void);             //Enable Clock for desired MCU Ports.
void delay(uint32_t delay);         //Intuduce Delays.
void pinsConfig(void);              //Configure the used pins.


// Defines and Macros



int main(void)
{
  // Inintialization:
  portsEnable();
  pinsConfig();
  UART1_Init(); // Enable USART1 & interrupts @ 8Mhz clock Speed only @ 9600Bps

  while(1)
  {
    // ********  Blink The green LED ***********************
    GPIOB->BSRR = GPIO_BSRR_BS11;
    delay(100000);
    GPIOB->BSRR = GPIO_BSRR_BR11;
    delay(100000);

  } // End of while loop
} // End main() loop

// ************** Function Implimentation *****************************
void UART1_Init(void)                         // Initiallize USART1
{
  // Reset Setting (8bit, one stop bit, no parity)
  // Enable clock for UART1 First after already enabling the PortA clock
  RCC->APB2ENR |= RCC_APB2ENR_USART1EN;       // Enable Clock to the USART1 Module

  // Pin Configuration for the USART1 Peripheral where Tx(PA9) = AF Push-Pull and Rx = Input Floating
  
  // Setting Tx (PA9) Pin
  GPIOA->CRH |= ((1<<4) | (1<<5));      // Set PA9 to Output 50Mhz Mode
  GPIOA->CRH &= ~(1<<6);                // Configure it to be an AF push-pull
  GPIOA->CRH |= (1<<7);                 // Same as above^
  
  // Setting Rx (PA10) pin
  // Nothing to be set as the reset value makes it an input pin with floating configuration
  
  // Set the Baud-Rate
  USART1->BRR = 0x341;    //@ 8Mhz, USARTDIV = 8Mhz/(16*9600) = 52.083 -> 52=0x34 and 0.083 * 16 = 1.328 = 1 which is 0x1  

  // Enable Interrupts for the USART1 Module:
  // A peripheral can generates a flag at a certain event, this flag can trigger an interrupt signal but first the certain event interrupt must be enabled and the peripheral interrupt as well and the global interrupts.
  USART1->CR1 |= USART_CR1_TXEIE | USART_CR1_RXNEIE;    // Enable the Transmit Data Register Empty Interrupt register and data received interrupt

  // Enable the Tx, Rx, USART1 as a whole block
  USART1->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);

  // Enable the USART1 Global interrupt on NVIC "Nested Vectored Interrupt controller" side. The NVIC is the interrupt processing unit in the MCU.
  NVIC_EnableIRQ(USART1_IRQn);  // This function's name can't be changed!
}
// This is a global interrupt service routine, any flag from the USART1 will lead to the same ISR, to distinguesh which is which we check the the flags and if one is set then this what caused the interrupt
void USART1_IRQHandler(void)                  // This function's name must be the same as it's defined in the main libraries
{
    // Check if we are here because we Received Data or simply the "RXNE flag is set".
    if(USART1->SR & USART_SR_RXNE)  // If Rx is Not Empty, or if we received Data, The USART1->SR register is going to change as it is controlled by the hardwart and we compare it to the value of the register USART_SR_RXNE which indicates a 1 at bit5
    {
      char temp = USART1->DR;  // Read the 8bit data received fron the data register into a char called temp
      USART1->DR = temp;       // Put the same data in the data register to be resent again, here the data registers are clled shadow registers they are not the same registers from the hardware prospective but from programming prospective we use the same registers
      
      while(!(USART1->SR & USART_SR_TC))
      {
        // Wait while the transmission completes and indicate the waiting process by flashing the RED led indicating data being sent.
        GPIOB->BSRR = GPIO_BSRR_BR10;      // When not ready to accept more data (e.g. while transmitting data process) turn of the Blue LED
        GPIOB->BSRR = GPIO_BSRR_BS1;
        delay(10000);
        GPIOB->BSRR = GPIO_BSRR_BR1;
      }

    } 

      // Check if we are here because the TXEIE is set "OR the Transmit complete" meaning we are ready to accept more data to transmit
      if(USART1->SR & USART_SR_TXE)
      {
        // Handle transmit complete here: (Blink an LED)
        GPIOB->BSRR = GPIO_BSRR_BS10;       // When Ready to accept more data
      }
      else
      {
        GPIOB->BSRR = GPIO_BSRR_BR10;      // When not ready to accept more data (e.g. while transmitting data process) 
      }
      
    
}

void portsEnable(void)                        /* Enable PortA and PortB */
{
  // Enable clock for Ports (A & B) on "APB2" Bus.
  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;       // Enable PortA Clock
  RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;       // Enable PortB Clock
}

void pinsConfig(void)                         //Configure the used pins.
{  
  //Set pin "PB1" as output push-pull (Red LED)
  GPIOB->CRL |= ((1<<4) | (1<<5));          //Set Pin to Output 50Mhz max speed
  GPIOB->CRL &= ~((1<<6) | (1<<7));         //Configure Pin as Push-Pull


  //Set Pin "PB9" as Input Pulled-Up (Push Button Pin)
  GPIOB->CRH &= ~(1<<6);                   //Set PB9 to input "pullup|pulldown"
  GPIOB->CRH |= (1<<7);
  GPIOB->ODR |= (1<<9);                    //Set PB9 input pin as Pull-up pin.


  //Set pin "PB10" as output push-pull (Blue LED)
  GPIOB->CRH |= ((1<<9) | (1<<8));          //Set Pin to Output 50Mhz max speed
  GPIOB->CRH &= ~((1<<11) | (1<<10));       //Configure Pin as Push-Pull

  //Set pin "PB11" as output push-pull (Green LED)
  GPIOB->CRH |= ((1<<12) | (1<<13));        //Set Pin to Output 50Mhz max speed
  GPIOB->CRH &= ~((1<<14) | (1<<15));       //Configure Pin as Push-Pull


  //Set pin PA0 as Analog input
  GPIOA->CRL &= ~((1<<0) | (1<<1));          // Make sure the Mode registers are 00 for input
  GPIOA->CRL &= ~((1<<2) | (1<<3));          // Set the CNF registers to 00 for input analog
  
} // End PinsConfig()

void delay(uint32_t delay)                    /* Psudo-delay in Milliseconds */
{
    for(uint32_t i = 0; i <= delay; i++)
    {
      // Looping to delay!
    }
}



Maybe below link is helpful for you: stm32 dead lock

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM