Monday 23 February 2015

Serial Communication with AVR


Serial Communication:





BASICS OF SERIAL COMMUNICATION:
·        serial communication uses single data line making it much cheaper
·        enables two computers in different cities to communicate over the telephone
·        byte of data must be converted to serial bits using a parallel-in-serial-out shift register and transmitted over a single data line
·        receiving end there must be a serial-in-parallel-out shift register
·        if transferred on the telephone line, it must be converted to audio tones by modem
·        for short distance the signal can be transferred using wire
·        how PC keyboards transfer data to the motherboard
·        2 methods, asynchronous and synchronous
·        synchronous method transfers a block of data (characters) at a time
·        asynchronous method transfers a single byte at a time
·        Uses special IC chips called UART (universal asynchronous receiver-transmitter) and USART (universal synchronous asynchronous receiver-transmitter)




·        Half- and full-duplex transmission
·        if the data can be transmitted and received, it is a duplex transmission
·        simplex transmissions the computer only sends data
·        duplex transmissions can be half or full duplex
·        depends on whether or not the data transfer can be simultaneous
·        If one way at a time, it is half duplex
·        If can go both ways at the same time, it is full duplex
·        full duplex requires two wire conductors for the data lines (in addition to the signal ground)
·        Asynchronous serial communication and data framing
·        data coming in 0s and 1s
·        to make sense of the data sender and receiver agree on a set of rules
·        Protocol
·        how the data is packed
·        how many bits/character
·        when the data begins and ends
·        Start and stop bits
·        asynchronous method, each character is placed between start and stop bits
·        called framing
·        start bit is always one bit
·        stop bit can be one or two bits
·        start bit is always a 0 (low)
·        stop bit(s) is 1 (high)
·        LSB is sent out first


·        in modern PCs one stop bit is standard
·        when transferring a text file of ASCII characters using 1 stop bit there is total of 10 bits for each character
·        8 bits for the ASCII code (1 parity bit), 1 bit each for the start and stop bits
·        for each 8-bit character there are an extra 2 bits, which gives 20% overhead
·        Data transfer rate
·        rate of data transfer bps (bits per second)
·        widely used terminology for bps is baud rate
·        baud and bps rates are not necessarily equal
·        baud rate is defined as the number of signal changes per second
·        RS232 standards
·        most widely used serial I/O interfacing standard
·        input and output voltage levels are not TTL compatible
·        1 bit is represented by -3 to -25 V
·        0 bit is +3 to +25 V
·        -3 to +3 is undefined
·        to connect RS232 to a microcontroller system must use voltage converters such as MAX232 to convert the TTL logic levels to the RS232 voltage levels, and vice versa
·        MAX232 IC chips are commonly referred to as line drivers


·       


Data communication classification
·        DTE (data terminal equipment)
·        DCE (data communication equipment)
·        DTE - terminals and computers that send and receive data
·        DCE - communication equipment responsible for transferring the data
·        simplest connection between a PC and microcontroller requires a minimum of three pins, TxD, RxD, and ground


·        Examining RS232 hand­shaking signals
·        many of the pins of the RS-232 connector are used for handshaking signals
·        they are not supported by the AVR UART chip
·        PC/compatible COM ports
·        PC/compatible computers (Pentium) microprocessors normally have two COM ports
·        both ports have RS232-type connectors
·        COM ports are designated as COM 1 and COM 2 (replaced by USB ports)
·        can connect the AVR serial port to the COM 2 port
AVR CONNECTION TO RS232:
·        RxD and TxD pins in the AVR
·        8051 has two pins used for transferring and receiving data serially
·        TxD and RxD are part of the port 3 group
·        pin 11 (P3.1) is assigned to TxD
·        pin 10 (P3.0) is designated as RxD
·        these pins are TTL compatible
·        require a line driver to make them RS232 compatible
·        driver is the MAX232 chip
·        MAX232
·        converts from RS232 voltage levels to TTL voltage levels
·        uses a +5 V power source
·        MAX232 has two sets of line drivers for transferring and receiving data
·        line drivers used for TxD are called T1 and T2
·        line drivers for RxD are designated as R1 and R2
·        T1 and R1 are used together for TxD and RxD of the AVR
·        second set is left unused

·        MAX233
·        MAX233 performs the same job as the MAX232
·        eliminates the need for capacitors
·        much more expensive than the MAX232

AVR SERIAL PORT PROGRAMMING IN C:
·        Baud rate in the AVR
·        serial communications of the AVR with the COM port of the PC
·        must make sure that the baud rate of the AVR system matches the baud rate of the PC's COM port
·        can use Windows HyperTerminal program

Ex1(a):

Write a program to transfer letter "A" serially at 9600 baud, continuously.
#include<avr/io.h>
#include<util/delay.h>
int main()
{
UBRRL=51;
UCSRB=0x08;
UCSRC=0x86;
while(1)
{
UDR='A';
_delay_ms(100);
while((UCSRA&0x40)==0);
}
}
Simulation:




Ex 1(b):

Write a program to transfer the message " Welcome To All " serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.
#include<avr/io.h>
#include<util/delay.h>

unsigned char arr[]={"Welcome To All "};
int main()
{ int i;
UBRRL=51;
UCSRB=0x08;
UCSRC=0x86;
while(1)
{
for(i=0;i<=15;i++)
{
UDR=arr[i];
_delay_ms(100);
while((UCSRA&0x40)==0);
}
}
}

Simulation:



Ex 1(c):
Program the AVR to receive bytes of data serially and put them on PORTB. Set the baud rate at 9600, 8-bit data, and 1 stop bit.
#include<avr/io.h>
#include<util/delay.h>
#include"lcd_header.h"
void main()
{
DDRC=0xFF;
DDRB=0xFF;
UBRRL=51;
UCSRB=0x10;
UCSRC=0x86;
init();
unsigned char x;
while(1)
{
while((UCSRA&0x80)==0);
x=UDR;
datta(x);
}
}

lcd_header.h
#include<avr/io.h>
#include<util/delay.h>
void datta(unsigned char a)
{
PORTB=a;
PORTC=0x05;
_delay_ms(30);
PORTC=0x01;
}
void command(unsigned char s)
{
PORTB=s;
PORTC=0x04;
_delay_ms(30);
PORTC=0x00;
}
void init()
{
command(0x38);
_delay_ms(100);
command(0x01);
_delay_ms(100);
command(0x0E);
_delay_ms(100);
command(0x80);
_delay_ms(100);
}
void stringg(unsigned char *p)
{
while(*p!='\0')
{
datta(*p);
p++;
}
}
void num(unsigned int i)
{
int j=1,k,l;
k=i;
while(k>=9)
{
j=j*10;
k=k/10;
}
while(j>=1)
{
l=i/j;
i=i%j;
j=j/10;
datta(l+48);
}
}

Simulation:




0 comments:

Post a Comment

if you have any doubt please let me know