UART Tutorial: One Way Communication from Chip-to-Chip
Share
UART Tutorial: One Way Communication from Chip-to-Chip
The Transmitting Chip's Program:
#define numberOfButtons 1
include
#include"ButtonPress.h"
int main(void)
{
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)
{
}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;
}//Wait until the Transmitter is ready
while (! (UCSR0A & (1 << UDRE0)) );
//Get that data outa here!
UDR0 = 0b11110000;
You might notice a few differences from the program in the video. The button presses are put into a library and the program calls the functions in the library. If you want to see how I did this, just look at this video.
The Receiving Chip's Program:
include
int main(void)
{
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)
{
}//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);
}receiveData = UDR0;
if (receiveData == 0b11110000) PORTB ^= (1 << PINB0);