Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

c - STM32F103C8 not receiving uart data from PC?

Right Now I am working with the STM32F103C8 blue pill board, I am working with UART protocol here I am able to send data to putty it's working but send data from putty to microcontroller (unable to receive data from the putty). what is the problem I can't understand everything is okay but I can't receive data?

Code:

#include "stm32f10x.h"                  // Device header
volatile char data;
int main(void){
    //configure HSI SYSTEM clock .
    RCC->CR = RCC_CR_HSION;
    RCC->CFGR = RCC_CFGR_SW_HSI;
    //Enable Alternate function clock, PORTA and USART1.
    RCC->APB2ENR = RCC_APB2ENR_AFIOEN|RCC_APB2ENR_USART1EN|RCC_APB2ENR_IOPAEN;
    //Alternate function mode PA9
    GPIOA->CRH = GPIO_CRH_MODE9_1|GPIO_CRH_CNF9_1;
   //ALternate function mode PA10.
    GPIOA->CRH |= GPIO_CRH_MODE10_1|GPIO_CRH_CNF10_1;
   //USART1 Enable.
    USART1->CR1 = USART_CR1_UE;
   //8-bit word length.
    USART1->CR1 |= ~(USART_CR1_M);
   //baudrate 8MHz,9600 baudrate.
    USART1->BRR = 0x341;
   //Enable TE,RE bits
    USART1->CR1 |= USART_CR1_RE|USART_CR1_TE;
    
    while(1){
        //wait for receive data
        while((USART1->SR &USART_SR_RXNE )==0){}
            data = USART1->DR;
        while((USART1->SR & USART_SR_TXE)== 0){}
            USART1->DR = data;
        while((USART1->SR &USART_SR_TC) == 0){}
        }
this code receives data from the PC and sends data to the PC.
question from:https://stackoverflow.com/questions/66061575/stm32f103c8-not-receiving-uart-data-from-pc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the solution for the above problem.

  1. don't make GPIO pins as alternate function mode when you receiving data. //ALternate function mode PA10. GPIOA->CRH |= GPIO_CRH_MODE10_1|GPIO_CRH_CNF10_1; here RX pin as output push-pull mode in alternate function --- it is wrong.
  2. make this pin as input floating mode or input push-pull/pull-down mode -- it is correct if you receiving data from the PC.(according to user manual). 3.dont use the HSI clock for this, if you use the HSI clock on this board you will get the wrong data.HSI clock, not a good idea to use.
  3. Make sure to enable the HSE clock as a system clock you will get proper output when you receive data from the PC.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...