AVR- Serial Communication: Page 2 of 2

USART TRANSMITTER

The USART transmitter consists of a transmitter register. The data to be transmitted are loaded into the transmit shift register via the USART I/O data register (UDRx). The start and stop framing bits are automatically appended to the data within the transmit shift register. The parity is automatically calculated and appended to the transmit shift register. data are then shifted out of the transmit shift register via the TxD pin a single bit at a time at the established baud rate. the USART transmitter is eqipped with two status flag: the USART Data Register Empty (UDRE) and the transmit complete (TXC) flags.

The UDRE flag sets when the transmit buffer is empty, indicating it is ready to receive new data. this bit should be written to a zero when writing the USART Control and Status Register A (UCSRxA). The UDRE bit is cleared by writing to the UDRx register. The TXC flag bit is set to logic 1 when the entire frame in the Transmit Shift Register has been shifted out and there are no new data currently present in the transmit buffer. The TXC bit may be reset writing a logic 1 to it.

SAMPLE PROGRAM: 

code1:

/* transmitting data without using interrupt*/ 

#define F_CPU 1000000UL

#include<avr/io.h>

void usart_int();

void usart_send(unsigned char *);

void usart_display(unsigned char);

unsigned char str[20]="projectsguru.net";

int main()

{

usart_int();

while(1)

{

usart_send(str);

}

}

void usart_int()

{

UCSRB=(1<<TXEN);

UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);

UBRRL=0x33;

void usart_send(unsigned char *p)

{

while(*p!='\0')

{

usart_display(*p);

p++;

}

}

void usart_display(unsigned char ch)

{

while(!(UCSRA&(1<<UDRE)));

UDR=ch;

}

code2:

/*transmit byte by using interrupt*/

#define F_CPU 1000000UL

#include<avr/io.h>

#include<avr/interrupt.h>

void usart_int();

int main()

{

usart_int();

sei();

while(1)

while(1);

}

 void usart_int()

{

UCSRB=(1<<TXEN)|(1<<UDRIE);

UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);

UBRRL=0x33;

ISR(USART_UDRE_vect)

{

UDR='P';

}

USART RECEIVER

The USART Receiver is virtually identified to the USART Transmitter except for the direction of the data flow, which is reversed. Data are received a single bit at a time via the RxD pin ata the established baud rate, the USART receiver is equipped with the receiver complete (RXC) flag. The RXC flag logic 1 when unread data exist int he receive buffer.

SAMPLE PROGRAM:

code1:

/* receiving data without using interrupt*/

#define F_CPU 1000000UL

#include<avr/io.h>

void usart_int();

void usart_send(unsigned char);

void usart_receive();

unsigned char value;

int main()

{

usart_int();

while(1)

{

usart_receive();

usart_send(value);

}

}

void usart_int()

{

UCSRB=(1<<TXEN)|(1<<RXEN);

UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);

UBRRL=0x33;

}

void usart_send(unsigned char ch)

{

while(!(UCSRA&(1<<UDRE)));

UDR=ch;

}

void usart_receive()

{

while(!(UCSRA&(1<<RXC)));

value=UDR;

}

code2:

/*receive byte by using interrupt*/

#define F_CPU 1000000UL

#include<avr/io.h>

#include<avr/interrupt.h>

void usart_int();

int main()

{

DDRB=0XFF;

usart_int();

sei();

while(1);

}

void usart_int()

{

UCSRB=(1<<RXEN)|(1<<RXCIE);

UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);

UBRRL=0x33;

}

ISR(USART_RXC_vect)

{

PORTB=UDR;

}

Proteus Design

 

code

 /*

 * receive.c

 *

 * Created: 6/12/2014 8:10:47 PM

 *  Author: gargi

 */ 

#include <avr/io.h>

void usart_init();

void usart_receive();

void usart_send(unsigned char);

unsigned char value;

void usart_init()

{

UCSRB=(1<<TXEN)|(1<<RXEN); // transmit enable

UCSRC=(1<<UMSEL)|(1<<UCSZ1)|(1<<UCSZ1); //synchronous mode,8-bit character size

UBRRL=0x33; //9600 baud rate

}

 

int main(void)

{

usart_init();

    while(1)

{

        usart_receive();

 }

}

void usart_receive()

{

while(!(UCSRA & (1<<RXC)));

value=UDR;

usart_send(value);

}

void usart_send(unsigned char ch)

{

while(!(UCSRA & (1<<UDRE))); //wait until UDR is empty

UDR=ch;

}

Proteus Design

 

Code

/*

 * serial.c

 *

 * Created: 6/12/2014 7:47:15 PM

 *  Author: gargi

 */ 

#include<avr/io.h>

void usart_init();

void transmit(unsigned char *);

void usart_send(unsigned char);

void usart_init()

{

UCSRB=(1<<TXEN); // transmit enable

UCSRC=(1<<UMSEL)|(1<<UCSZ1)|(1<<UCSZ1); //synchronous mode,8-bit character size

UBRRL=0x33; //9600 baud rate

}

void main(void)

{

usart_init();

while(1)

{

transmit("hello");

usart_send('\r');

}

}

void transmit(unsigned char *p)

{

while(*p!='\0')

{

usart_send(*p);

p++;

}

}

void usart_send(unsigned char ch)

{

while(!(UCSRA & (1<<UDRE))); //wait until UDR is empty

UDR=ch;

}

Proteus Design

Code

#include<avr/io.h>

#include<avr/interrupt.h>

void usart_init();

void transmit(unsigned char *);

void usart_init()

{

UCSRB=(1<<TXEN)|(1<<UDRIE); // transmit enable,TXC interrupt

UCSRC=(1<<UMSEL)|(1<<UCSZ1)|(1<<UCSZ1); //synchronous mode,8-bit character size

UBRRL=0x33; //9600 baud rate

}

void main()

{

usart_init();

while(1)

sei(); //enable interrupt

}

ISR(USART_UDRE_vect)

{

UDR='H';

}

 

Category: 

Share

Who's new

  • ravirajpatil871...
  • shubhambajoria
  • yassir
  • demiholyman890954
  • scottgillum51169040

Get Notified

 

Share

We are Social

Syndicate

Subscribe to Syndicate