Testing USART Function

Hello,
What im trying to do is to create the USART functions rather than using audrino libraries. I created these two functions but i dont know how to test it. For transmit, i have an occiscope conntected to the tx pin of audrino and i send in 0x01 which should be one pulse 8 times however it not the case. Also my receiver, I put the tx pin into rx and put it in the variable and output to serial monitor to see the value however no go on that. Im fairly new and any help is greatly appreciated.

/#define BAUDRATE 51 //According to datasheet, this value gives 9600 baudrate @ 8Mhz

void USART_init( void ) {
  //USART initialization
  //Communication parameters: 8 data, 1 stop, no parity
  //USART Transmitter: ON
  //USART Receiver: ON
  //USART Mode: Asynchronous
  //USART Baud Rate: 9600 
  UBRR0 = BAUDRATE; 
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);  //Enable transmitter and receiver
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  //8data, 1stop bit
}

void USART_transmit( unsigned char data ) {
  //Wait for empty transmit buffer
  while( !( UCSR0A & (1 << UDRE0)) );
  //Put data into buffer, sends the data
  UDR0 = data;
}

unsigned char USART_Receive(void)
{
  
  /* Wait for data to be received */
  while ( !(UCSR0A & (1<<RXC0)) ) {};
  /* Get and return received data from buffer */
  return UDR0;
}

void setup()
{
    USART_init();
}
void loop()
{
 unsigned char start_value = 0x00; 
 
    for(unsigned char i = start_value; i <= 0xFF; i++) {
      USART_transmit(i); 
     // unsigned char temp1 = UDR0;
      //Serial.print(temp1);
      unsigned char temp = USART_Receive();
      Serial.print(temp);
    }
}

I have brought few changes in your program; now, the program works and interacts well with the Serial Monitor of UNO at Bd = 9600. Character(s) being sent from the InputBox of the Serial Monitor; it goes around the UNO; it appears back in the OutputBox of the Same Serial Monitor.

//#define BAUDRATE 51 //According to datasheet, this value gives 9600 baudrate @ 8Mhz
void setup()
{
  USART_init();  //Bd, TX/RX Enable, No-parity; Frame=10
}

void loop()
{
  byte charByte = USART_Receive();
  USART_Transmit(charByte);
  USART_Transmit(0x0A);      //New line character
}
/*
  void loop()
  {
  unsigned char start_value = 0x00;

  for (unsigned char i = start_value; i <= 0xFF; i++) {
    USART_transmit(i);
    // unsigned char temp1 = UDR0;
    //Serial.print(temp1);
    unsigned char temp = USART_Receive();
    Serial.print(temp);
  }
  }
*/
void USART_init( void )
{
  //USART initialization
  //Communication parameters: 8 data, 1 stop, no parity
  //USART Transmitter: ON
  //USART Receiver: ON
  //USART Mode: Asynchronous
  //USART Baud Rate: 9600
  UBRR0L = 0x67; //BAUDRATE;
  UBRR0H = 0x00; //Bd = 9600; U2X0-bit = 0 at 16 MHz clkSYS
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);  //Enable transmitter and receiver
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  //8data, 1stop bit
}

void USART_Transmit(byte data)//( unsigned char data )
{
  //Wait for empty transmit buffer
  while ( !( UCSR0A & (1 << UDRE0)) )
  {
    ;
  }
  //Put data into buffer, sends the data
  UDR0 = data;
}

//unsigned char USART_Receive(void)
byte USART_Receive(void)
{
  /* Wait for data to be received */
  while ( !(UCSR0A & (1 << RXC0)) )
  {
    ;
  }//;
  /* Get and return received data from buffer */
  return UDR0;
}

sm44.png

sm44.png

zkhan:
What im trying to do is to create the USART functions rather than using audrino libraries. I created these two functions but i dont know how to test it.

Why not send the data to another Arduino that is using the regular Serial library. A Mega would be best because it has 3 spare Hardware Serial ports as you can keep Serial free for communication with the PC.

Indeed a Mega would be good for the experimental code also - for the same reason

And now that I think about it you can probably do the experiment and the testing on a single Mega by sending data from (E,g.) Serial1 to Serial2

IIRC the Atmega 328 and Atmega 2560 datasheets have examples of C code for the UART.

...R

Thank you for the quick replies. I definitely will try with the mega and thank you for helping me work around the serial monitor.