这是两个atmega328p控制器之间的UART传输。我试图通过从接收控制器上的数据结构中打印变量来确认数据是否正确传递。
我的终端没有显示任何信息。我只能确认从我的LED调用中获取代码,我启用了portB。
我很确定我的BAUD率是正确的(51为8mhz)。同时确信硬件正确连接。有关如何进一步调试以找到问题的任何建议?我是AVR开发的新手,所以对其他人来说也许是显而易见的。
转移
#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);
}
}
接收
#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);
}
}