Serial Communication programming using Microcontroller 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Serial Communication Tutorial: Programming Microcontroller


►AVR Programming for UART

In AVR, following set of registers are used to communicate over USART
  • UCSRA
  • UCSRB
  • UCSRC
  • UBRRH
  • UBRRL

UCSRA: in this register there are flags for various errors that might occure during data transmission, e.g. parity error, frame error etc.

UCSRB: in this register we have a lot of enable bits. For example different interrupt enable bits but also the reciving and transmitting enable bits.

UCSRC: in this register we set the parity mode, stop bits and so on.

UBRRH & UBRRL: in UBRRH register, the higher byte and in UBRRL, lower byte is stored for generating a required Baud rate.

More information on the above registers can be found in the datasheet of the AVR you are using.
►Initialising USART in AVR
CODE:
.include "m8515def.inc"
.def reg1 = r16
.def reg2 = r17
Serial_Init:
        ;Load UBRRH with 0 and UBRRL with 25
        ;to set a baud rate of 9600 at 4MHz
        ldi reg1,00
        out UBRRH,reg1
        ldi reg2,25
        out UBRRL,reg1
        ;Clear all error flags
        ldi reg1,00
        out UCSRA,reg1
        ;Enable Transmission and Reception
        ldi reg1,(1<<RXEN)+(1<<TXEN)
        out UCSRB,reg1
        ;Set Frame format
        ;8,N,1
        ldi reg1,(1<<URSEL)|(3<<UCSZ0)
        out UCSRC,reg1
        ret
 


in C we can do this as..

CODE:
#include <avr/io.h>.
void serial_init(){
        UCSRA=0x00;
        UCSRB=0x18;
        UCSRC=0x86;
        UBRRH=0x00;
        UBRRL=0x19;
}
 



To transmit data serial we need to put the data to send in 8-bit UDR (UART Data Register) and poll the empty transmit buffer to set. While receiving data we wait for the receive flag, when its set the data recieved can be read from the UDR register. This can be done as follows...

CODE:
Serial_Send:
        ;wait for empty transmit buffer flag
        sbis UCSRA, UDRE
        rjmp Serial_Send
        ;If the flag is set
        ;Then move the data to send in UDR
        out UDR,reg2
        ret
       
Serial_Read:
        ;Wait for Receive flag
        sbis UCSRA,RXC
        rjmp Serial_Read
        ;If falg is set
        ;Then read data from UDR
        in reg2,UDR
        ret
 


in C we can do this as..

CODE:
void serial_send(unsigned char dat){
        while(!(UCSRA & (1<<UDRE)));
        UDR = dat;
}
unsigned char serial_read(){
        while(!(UCSRA & (1<<RXC)));
        return UDR;
}
 



►8051 Programming for UART

In 8051, we make use of Timer 1 to generate the required baud rate. Following are the registers that are need to be configured to commnunicate over UART.
  • TMOD
  • SCON
  • TH1
  • TL1
  • TCON

TMOD: This register is used to set the mode of Timer0 and Timer1. It is also used to select whether the timers are used as Timer or Counter.

SCON: Serial Control register has various functions like.. it has flags for Framing error, Transmit interrup and receive interrupt. Its used to select the serial port mode, to enable or disable the reception etc.

TCON: This register has varios flag and control bits e.g. Timer overflow flags, interrupt edge flags, timer control bits to start/stop the timer.

TH1 & TL1: Timer registers for Timer 1 determines the baudrate of UART.

More information on the above registers can be found in the 8051 Hardware manual.
►Initializing USART in 8051

CODE:
Serial_Init:
        ;Set timer 1 mode to 8-bit Auto-Reload
        mov TMOD,#20H
        ;Enable reception
        ;Set Serial port mode to 8-bit UART
        mov SCON,#50H
        ;Set baudrate to 9600 at 11.0592MHz
        mov TH1,#0FDH
        mov TL1,#0FDH
        ;Start Timer
        setb TR1
        ret
 


in C we can do this as..

CODE:
#include <reg51.h>.
void serial_init(){
        TMOD = 0x20;
        SCON = 0x50;
        TH1  = 0xFD;
        TL1  = 0xFD;
        TR1  = 1;
}
 



To Send data to the serial port we just have to move the data in SBUF (serial buffer register) and wait for the Transmit Interrupt flag to be set. While receiving we wait for the Receive interrupt flag to be set and read the data from SBUF register. This can be done as shown below...

CODE:
Serial_Send:
        ;wait for last data to be
        ;sent completely
        jnb TI,Serial_Send
        ;clear the transmit interrupt flag
        clr TI
        ;Then move the data to send in SBUF
        mov SBUF,A
        ret
       
Serial_Read:
        ;Wait for Receive interrupt flag
        jnb RI,Serial_Read
        ;If falg is set then clear it
        clr RI
        ;Then read data from SBUF
        mov A,SBUF
        ret
 


in C we can do this as..

CODE:
void serial_send(unsigned char dat){
        while(!TI);
        TI = 0;
        SBUF = dat;
}
unsigned char serial_read(){
        while(!RI);
        RI = 0;
        return SBUF;
}
 



◄ Introduction to Serial Communication  |  Software UART Implementation ►

Google Search for Microcontrollers!

Google
 

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems