0
\$\begingroup\$

This is the code. I'm using stm32f103c8t6 and I want to communicate with a Bluetooth module (Hc-05).

I'm new with STM and I'm using registers and want to send just 65 for a test but it doesn't work and nothing is received.

#include <stm32f10x.h>

int main()
{
 RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN;

 GPIOA->CRH &= ~GPIO_CRH_CNF9;
 GPIOA->CRH |= GPIO_CRH_CNF9_1;     
 GPIOA->CRH |= GPIO_CRH_MODE9_1;

 USART1->BRR =833;  
 USART1->CR1 |= USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;

 USART1->DR  = 65;  
}
\$\endgroup\$
  • \$\begingroup\$ Welcome to the site. I'm sure you can see that it's not a free design house, homework-answering service or on-line technical encyclopedia, copied out to you on demand. People will help you take the next step if your question shows you've done as much as you possibly could on your own - which yours doesn't, I'm afraid. Please edit your question and greatly improve it. Show your work and findings so far in considerable detail. The better the quality of question, the better the quality of the answers you will attract. Again, a warm welcome. \$\endgroup\$ – TonyM Mar 1 at 12:16
  • \$\begingroup\$ There are no comments in your code and there is no explanation in your question of what you expect the code to do and how you know it doesn't work. Can you fix that (and the capitalisation and spelling in your title)? \$\endgroup\$ – Transistor Mar 1 at 12:22
  • \$\begingroup\$ Have you looked with an oscilloscope to see if the serial output is correct? Tried connecting it to a computer? \$\endgroup\$ – Elliot Alderson Mar 1 at 19:06
  • \$\begingroup\$ I connected this to an application on my mobile phone and got nothing and also the DR registry is empty when I go into debug mode in keil. \$\endgroup\$ – Mojtaba Miraki Mar 1 at 19:19
  • \$\begingroup\$ What do you think is going to happen when your code gets to the end of main? Have to followed it with the debugger to see what actually does happen? And what does "an application on my mobile phone" mean? Have you looked at the waveforms of the serial line or not? \$\endgroup\$ – Elliot Alderson Mar 1 at 19:27
2
\$\begingroup\$

I had incorrectly configured the GPIO registers and also didn't calculate the baud rate formula correctly. RX,TX Baud=f_ck/(16×USARTDIV) In the code below I corrected the errors and also developed a little more:

    #include "stm32f10x.h"

    int main(void) {


    //-----------------------| UART CODE |----------------------------------- 
    -------
    //USART1 / GPIOA clock enable
    RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN; 

    //remaping if needed
   //AFIO->MAPR |= AFIO_MAPR_USART1_REMAP ; //remap RX TX to PB7 PB6

  //pin configurations: PA9- TX is set to ALternate-push-pull and 50MHz
  GPIOA->CRH |= GPIO_CRH_MODE9 | GPIO_CRH_CNF9_1;
  GPIOA->CRH &= ~(GPIO_CRH_CNF9_0);

  /*
 * PIN PA10 is the RX pin and it has to be set to input &FLOATING
 * this is the rest value of the pin so we dont do anything to it
 *
 * */

 //USART DIV value
 USART1->BRR = 52; //for 8MHZ on APB2 bus

 //----------  RX enable        TX enable     UART enable
 USART1->CR1 |= USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;

 while (1) {

 if (USART1->SR & USART_SR_RXNE) //if RX is not empty
   {
     char temp = USART1->DR; //fetch the data received
     USART1->DR = temp;  //send it back out
     while (!(USART1->SR & USART_SR_TC)) //wait for TX to be complete
   ;

   }

   }

 }
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks for your update. Please take the time to properly format the code. \$\endgroup\$ – Codo Mar 2 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.