Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.1 kB
0
Indexable
Never
#include "stm32f4xx.h"
#include "main.h"

void USART_init()
{
	//PA2-USART2->Tx
	  //PA3-USART->Rx
	  //ENABLE CLOCK TO GPIOA i.e, AHB1
	  RCC->AHB1ENR |= (1<<0);

	  //ENABLE CLOCK TO USART2
	  RCC->APB1ENR |= (1<<17);

	  //Select the Alternate function Mode for PA2 (USART2_TxD) pin using the GPIO_MODER
	    //MODER2[5:4]=[1 0]
		GPIOA->MODER |= (1<<5);
		GPIOA->MODER &= ~(1<<4);

	  //Select Alternate function mode for PA3 i.e, using MODER
	  //Set [1 0] for consecutive bits MODER3[7:6]=[1 0]
		GPIOA->MODER |= (1<<7);
		GPIOA->MODER &= ~(1<<6);

	  //SELECT The peripheral function AF7 for PA2 (USART2_TxD) pin using GPIO AFRL registers.
	   //AF7-0111
	   //AFRL2[11:8]=[0 1 1 1]
	   GPIOA->AFR[0] |= (1<<8);
	   GPIOA->AFR[0] |=(1<<9);
	   GPIOA->AFR[0] |=(1<<10);
	   GPIOA->AFR[0] &= ~(1<<11);

	  //Select the peripheral function AF7 for PA3 using GPIO_AFRL
	  //AFRL3[15:12]=[0 1 1 1]
	  GPIOA->AFR[0] |= (1<<12);
	  GPIOA->AFR[0] |= (1<<13);
	  GPIOA->AFR[0] |= (1<<14);
	  GPIOA->AFR[0] &= ~(1<<15);

	  //Set the baud rate for USART2 using USART2_BRR register.
	  USART2->BRR=0x0680;

	  //Configure the CR1 (Control 1) register for oversampling rate, character size (8-bit or 9bit)
	  USART2->CR1 |= (1<<15);

	  //enabling receive (RE).
	  USART2->CR1 |= (1<<3);
	  USART2->CR1 |= (1<<2);
	  USART2->CR1 &= ~(1<<12);

	  //Configure the CR2 (Control 2) register for number of stop bit(s)
	  //USART->CR2 STOP[13:12]=[0 0] FOR 1 STOP BIT
	  USART2->CR2 &= ~(1<<13);
	  USART2->CR2 &= ~(1<<12);

	  //Configure the CR3 (Control 3) register for no hardware flow control
	  USART2->CR3 &= ~(1<<8);

	  //Enable USART2 after configuration complete.
	  USART2->CR1 |=(1<<13);

}
void USART_write(int x)
{
	  //Wait until the TXE (Transmit Empty) bit of the USART_SR register is set.
	  //while((USART2->SR) && (1<<5)==0);//Not the standard way
	  while(!(USART2->SR) && (1<<5));
	  //Read a byte from USART2_DR register that was transmitted.
	  USART2->DR=(x & 0xFF);
}
char USART_read()
{
	//Wait until the RXNE (Receive Not Empty) bit of the USART_SR register is set.
	while(!(USART2->SR&(1<<5)));
	//Read a byte from USART2_DR register that was received.
	return USART2->DR;
}
int main(void)
{
	char c;
	USART_init();
	//Getting the Received Data
	//char c=USART_read();
	while (1)
	{
	  //Reading the transmitted data
      c=USART_read();
	  USART_write(c);
	}

}



RCC->AHB4ENR |= (1<<0);
  RCC->APB1LENR |= (1<<17);

  //PA2-Tx
  //PA3-Rx

  GPIOA->MODER |=(1<<5);
  GPIOA->MODER &=~(1<<4);

  GPIOA->MODER |=(1<<7);
  GPIOA->MODER &=~(1<<6);

  //[11 10 9 8]=[0111]
  GPIOA->AFR[0] |= (1<<8);
  GPIOA->AFR[0] |= (1<<9);
  GPIOA->AFR[0] |= (1<<10);
  GPIOA->AFR[0] &= ~(1<<11);

  //[15 14 13 12]=[0111]
  GPIOA->AFR[0] |= (1<<12);
  GPIOA->AFR[0] |= (1<<13);
  GPIOA->AFR[0] |= (1<<14);
  GPIOA->AFR[0] &= ~(1<<15);

  //USART->BRR

  //OVER8-0
  //TE-1,RE-1

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
Leave a Comment