USART RXNE not being set in STM32F0

deiviz

I have the STM32F0DISCOVERY Board with the STM32F051R8T6 microcontroller. I am trying to communicate to the PC using USART.

Sending data from the board to the PC works fine with my putChar() function.

Receiving data sent from the PC to the board with my getChar() function is not working properly. The problem is that the RXNE flag (Read Data Register Not Empty) is not being set. However I have seen using the debugger in Keil uVision4 that the data is indeed in RDR (Receive Data Register).

I have tried running it without debugging, just in case the debugger was reading the RDR register before my program could read the RXNE flag and it's still not working.

This is my code:

#include <STM32F0xx.h>
uint8_t data;

int main (void) {

    RCC->AHBENR |= (1UL<<17);   // PortA clock enable
    GPIOA->MODER &= ~((3UL<<2*2)|(3UL<<2*3));
    GPIOA->MODER |=  ((2UL<<2*2)|(2UL<<2*3));       // Alt func on PA2, PA3
    GPIOA->AFR[0] &= ~((15UL<<4*2)|(15UL<<4*2));
    GPIOA->AFR[0] |=  (( 1UL<<4*2)|( 1UL<<4*3));    // Alt func 1 on PA2, PA3
    RCC->APB1ENR |= (1UL<<17);  // USART clock enable
    USART2->BRR = 5000;         // Baud rate 48 000 000 / BRR -> 9600
    USART2->CR1 |= (1UL<<0);    // Enable USART

    while (1) {
        data = getChar();
        if (data!=0xFF) {
            putChar(data);
        }
    }
}

Here is the putChar() function that works properly.

void putChar (uint8_t data) {
    USART2->CR1 |= (1UL<<3); // Transmitter enable
    USART2->TDR = data;
    while ((USART2->ISR|(1UL<<7))==(1UL<<7));   // Wait until sent
}

Here is the getChar() function that is not working and keeps returning 0xFF.

uint8_t getChar (void) {
    USART2->CR1 |= (1UL<<2); // Receiver enable
    if ((USART2->ISR|(1UL<<5))==(1UL<<5)) { // Check RXNE to see if there is data
        return USART2->RDR;
    } else {
        return 0xFF;
    }
}
deiviz

You are not correctly checking the RXNE bit. This line of your code is wrong:

if ((USART2->ISR|(1UL<<5))==(1UL<<5)) {

You need to use an AND instead of an OR. This will allow you to mask correctly and check if the bit is set. Use this instead:

if ((USART2->ISR&(1UL<<5))==(1UL<<5)) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive