UART register is not debugging to terminal

2019-09-06 02:13发布

问题:

This is a UART transfer between two atmega328p controllers. I am trying to confirm that the data is being properly passed by printing a variable from the data struct on the receiving controller.

My terminal is not displaying any information. I can only confirm that the code is being reached from my LED calls, where I enable portB.

I am fairly sure my BAUD rate is correct (51 for 8mhz). Also confident the hardware is hooked up properly. Any recommendations on how to debug further to find the issue? I'm new to AVR development so perhaps it's obvious to someone else.

Transfer

#include <avr/io.h>
#include <util/delay.h>


typedef struct  {
  uint8_t data;
  uint8_t timestamp;
} tData;

//Waits until bits are set to grab data
char readChar()
{
  while (! (UCSR0A & (1 << RXC0)) );
  return UDR0;
}

//Waits until buffer is empty
void writeChar(char data)
{
  while(!(UCSR0A & (1<<UDRE0)));
  UDR0 = data;
}

int main(void)
{
  DDRD |= 1 << PIND1;         //pin1 of portD as OUTPUT
  DDRD &= ~(1 << PIND0);      //pin0 of portD as INPUT
  PORTD |= 1 << PIND0;

  int UBBRValue = 51;                                 //Set BAUD
  UBRR0H = (unsigned char) (UBBRValue >> 8);          //Put the upper part of the baud number here (bits 8 to 11)
  UBRR0L = (unsigned char) UBBRValue;                 //Put the remaining part of the baud number here
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);               //Enable the receiver and transmitter
  UCSR0C = (1 << USBS0) | (3 << UCSZ00);              //Set 2 stop bits and data bit length is 8-bit

  tData payload;                                      // Instance struct
  payload.data = 2;                                   // Store some data

  unsigned char * payloadPtr;
  payloadPtr = &payload;                              // Pointer to struct memory location

  while (1)
  {
    while (! (UCSR0A & (1 << UDRE0)) );
    {

      for (int n=0;n<sizeof(payload);n++)             // Send bytes contained in struct
      {
        writeChar(payloadPtr[n]);
        PORTB = 0xFF;
      }


    }
    _delay_ms(1000);
    PORTB = 0x00;
    _delay_ms(1000);
  }
}

Receive

#include <avr/io.h>
#include <util/delay.h>


typedef struct  {
  uint8_t data;
  uint8_t timestamp;
} tData;

//Waits until bits are set to grab data
char readChar()
{
  while (! (UCSR0A & (1 << RXC0)) );
  return UDR0;
}

//Waits until buffer is empty
void writeChar(char data)
{
  while(!(UCSR0A & (1<<UDRE0)));
  UDR0 = data;
}

int main(void)
{
  DDRD |= (1 << PIND0);//PORTD pin0 as INPUT
  DDRC=0xFF;//PORTC as OUTPUT

  int UBRR_Value = 51;
  UBRR0H = (unsigned char) (UBRR_Value >> 8);
  UBRR0L = (unsigned char) UBRR_Value;
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << USBS0) | (3 << UCSZ00);

  tData payload;                                      // Instance struct
  unsigned char * payloadPtr;
  payloadPtr = &payload;                              // Pointer to struct memory location

  while (1)
  {

    for (int n=0;n<sizeof(payload);n++)               // Commit recieved bytes to struct
    {
      payloadPtr[n] = readChar();
    }

    writeChar(payload.data);
    //writeChar(2); //test

    PORTB = 0xFF;
    _delay_ms(1000);
    PORTB = 0x00;
    _delay_ms(1000);

  }
}