UART-Tutorial: Einwegkommunikation von Chip zu Chip

UART-Tutorial: Einwegkommunikation von Chip zu Chip

Das Programm des sendenden Chips:

#define numberOfButtons 1

include
#include"ButtonPress.h"

int main(void)
{
DDRB |= 1 << PINB1;
DDRB &= ~(1 << PINB0);
PORTB |= 1 << PINB0;

int UBBRValue = 25;

//Put the upper part of the baud number here (bits 8 to 11)
UBRR0H = (unsigned char) (UBBRValue >> 8);

//Put the remaining part of the baud number here
UBRR0L = (unsigned char) UBBRValue;

//Enable the receiver and transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);

//Set 2 stop bits and data bit length is 8-bit
UCSR0C = (1 << USBS0) | (3 << UCSZ00);

while (1)
{
if (ButtonPressed(0, PINB, 0, 100))
{
PORTB ^= 1 << PINB1;

//Wait until the Transmitter is ready
while (! (UCSR0A & (1 << UDRE0)) );

//Get that data outa here!
UDR0 = 0b11110000;
}
}
}

Möglicherweise bemerken Sie einige Unterschiede zum Programm im Video. Die Tastendrücke werden in eine Bibliothek aufgenommen und das Programm ruft die Funktionen in der Bibliothek auf. Wenn Sie sehen möchten, wie ich das gemacht habe, sehen Sie sich einfach dieses Video an.

Das Programm des empfangenden Chips:

include
int main(void)
{
DDRB |= (1 << PINB0);

//Communication UART specifications (Parity, stop bits, data bit length)
int UBRR_Value = 25; //This is for 2400 baud
UBRR0H = (unsigned char) (UBRR_Value >> 8);
UBRR0L = (unsigned char) UBRR_Value;
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
UCSR0C = (1 << USBS0) | (3 << UCSZ00);

unsigned char receiveData;
while (1)
{
while (! (UCSR0A & (1 << RXC0)) );

receiveData = UDR0;

if (receiveData == 0b11110000) PORTB ^= (1 << PINB0);
}
}
Zurück zum Blog

Hinterlasse einen Kommentar

Bitte beachte, dass Kommentare vor der Veröffentlichung freigegeben werden müssen.