Professional Documents
Culture Documents
Parallel communication requires More lines ,costly, generally used for short distance
Serial communication requires single line, so suitable for long distances, and cheap. Data passes through a parallel to
serial converter at transmitter, and receiver has s/p converter.
With help of modem converts data to signal and transmitted through communication line.
UART stands for Universal Asynchronous Receiver/Transmitter. The data bits are not synchronized with the clock pulses,
transfers a byte of data at a time
USART stands for Universal Synchronous Asynchronous Receiver/Transmitter. The data bits are synchronized with the clock
pulses, transfers a block of data at a time
– AVR has built in USART
The rate of data transfer is measured in Bits per second or Baud rate. In wired communication these two are the same.In
USART speed is generally limited to 100000b/s
In practice, serial communication with no handshaking uses 3 wires (TX, RX and GND). Serial communications with basic
hardware handshaking uses 5 wires (TX, RX, RTS, CTS and GND).
Modes of Operation
In this mode of communication, the data is transmitted/received asynchronously, i.e. we do not need (and use) the clock pulses,
as well as the XCK pin. The data is transferred at the BAUD rate we set in the UBBR register. This is similar to the UART
operation.
Asynchronous Double Speed Mode
This is higher speed mode for asynchronous communication. The difference is that data is transferred at double the baud we set
in the UBBR Register. Setting the U2X bit in UCSRA register can double the transfer rate.
Synchronous Mode
This is the USART operation of AVR. When Synchronous Mode is used (UMSEL = 1 in UCSRC register), the XCK pin will
be used as either clock input (Slave) or clock output (Master).
Register Description
The USART of AVR has five registers, namely UDR, UCSRA, UCSRB, UCSRC and UBBR.
The USART Transmit Data Buffer Register and USART Receive Data Buffer Registers share the same I/O address referred to
as USART Data Register or UDR. The Transmit Data Buffer Register (TXB) will be the destination for data written to the
UDR Register location. Reading the UDR Register location will return the contents of the Receive Data Buffer Register
(RXB).
The baud rate of UART/USART is set using the 16-bit wide UBRR register. The register is as follows:
16-bit UBRR register is comprised of two 8-bit registers – UBRRH (high) and UBRRL (low).
The UCSRC register can be used as either UCSRC, or as UBRRH register. This is done using the URSEL bit.
Initializing UART
Procedure to transmit
Program to transmit ‘G’ serially at 9600 baud rate continuously. System clock 8MHz.
.INCLUDE "M32DEF.INC"
LDI R16,(1<<TXE ; enable transmit
OUT UCSRB, R16
LDI R16,(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL) ; 8 bit, no parity, 1 stop bit
OUT UCSRC, R16
LDI R16,0x33 ; @8 MHz for 9600
OUT UBRRL,R16
AGAIN:
SBIS UCSRA,UDRE
RJMP AGAIN
LDI R16,'G'
OUT UDR,R16
RJMP AGAIN
Program to transmit “YES” continuously , serially at 9600baud, 8 bit data, and one stop bit.
.INCLUDE "M32DEF.INC"
LDI R21,HIGH(RAMEND)
OUT SPH,R21
LDI R21,LOW(RAMEND)
OUT SPL,R21
LDI R16,(1<<TXEN)
OUT UCSRB, R16
LDI R16,(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL)
OUT UCSRC, R16
LDI R16,0x33
OUT UBRRL,R16
AGAIN:
LDI R17,'Y'
CALL TRNSMT
LDI R17,'E'
CALL TRNSMT
LDI R17,'S'
CALL TRNSMT
LDI R17,' '
CALL TRNSMT
RJMP AGAIN
TRNSMT:
SBIS UCSRA,UDRE
RJMP TRNSMT
OUT UDR,R17
RET
Procedure to receive
Program to receive data serially, @9600 baud rate, 8 bit data, one stop bit, and out received data on port b.
.INCLUDE "M32DEF.INC"
LDI R21,HIGH(RAMEND)
OUT SPH,R21
LDI R21,LOW(RAMEND)
OUT SPL,R21
LDI R16,(1<<RXEN)
OUT UCSRB, R16
LDI R16,(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL)
OUT UCSRC, R16
LDI R16,0x33
OUT UBRRL,R16
LDI R16,0xFF
OUT DDRB,R16
RCVE: SBIS UCSRA,RXC
RJMP RCVE
IN R17,UDR
OUT PORTB,R17
RJMP RCVE
RS-232
RS-232 (Recommended Standard – 232) is a standard, describes the physical interface and protocol for relatively low-speed
serial data communication between computers and related devices
When we want to interconnect two devices, one of which works over TTL and the other over RS232, we need to convert the
HIGH of TTL (which is 3.3v~5v) into the HIGH of RS232 (which is -3v ~ -25v) and similarly, the LOW of TTL (0v~0.8v)
into the low of RS232 (which is +3v ~ +25v).
IC MAX232
MAX232 ICs were invented by Maxim. Used to convert TTL/CMOS logics to RS232 logic . Need some passive components
only.
Limitations
The two devices must have same rate, for proper asynchronous transfer.
Complex h/w.
Minimum one start and stop pulse for every byte of data transfer.
Speed limit.
Serial Peripheral Interface (SPI) is a series interface protocol, commonly used to send data between microcontrollers and small
peripherals such as shift registers, sensors, and SD cards. It is a synchronous mode, uses separate clock and data lines, along
with a select line to choose the device. The side that generates the clock is called the “master”, and the other side is called the
“slave”. There is always only one master, but multiple slaves. The master sends a bit on the line and the slave reads it, while
the slave sends a bit on its line and the master reads it. Data transmission is full duplex.
Master places data byte(A) in its shift register, and the Slave places its data (B) in its shift register. Master generates 8 clock
pulses, and the contents of the Master's shift register are transferred to the Slave's shift register and vice versa. So, at the end of
the clock pulses, the Master has completely received B, and the Slave has received A.
The interface can send data with the most-significant bit (MSB) first, or least-significant bit (LSB) first..
The slave will read the data on either the rising edge or the falling edge of the clock pulse
SPI can operate at extremely high speeds (millions of bytes per second
In an AVR, four signals (pins) are used for the SPI: MISO, MOSI, SCK and SS'
MISO (Master In Slave Out): the input of the Master's shift register, and the output of the Slave's shift register.
MOSI (Master Out Slave In): the output of the Master's shift register, and the input of the Slave's shift register.
SCK (Serial Clock): In the Master, this is the output of the clock generator. In the Slave, it is the input clock signal.
SS' (Slave Select): This tells the slave that it should wake up and receive / send data and is also used when multiple slaves are
present to select one.
SPI Registers
Control bits to select spi, enable spi interrupt, data flow order, master or slave mode selection, clock edge selection, idle clock
status, and frequency pre-scalar.
SPIE (SPI Interrupt Enable) bit: Set SPIE to one if you want the SPI interrupt to be executed when a serial transfer is
completed.
SPE (SPI Enable) bit: If you want to use the SPI, you must set this bit.
DORD (Data Order) bit: You can choose in which order the data will be transmitted. Set DORD to one to send the least
significant bit (LSB) first. Set DORD to zero to send the most significant bit (MSB) first.
MSTR (Master/Slave Select) bit: Set MSTR to configure the AVR as a Master SPI device. Clear MSTR to configure it as a
Slave.
The following image shows figure 76 and 77 from the mega128 datasheet:
SPR1 and SPR2 (SPI Clock Rate Select) bits: The SPR bits configure the frequency of the clock signal.
0 0 fosc/4
0 1 fosc/16
1 0 fosc/64
1 1 fosc/128
SPIF (SPI Interrupt Flag) bit: It is set by hardware when a serial transfer is complete
WCOL (Write Colision Flag) bit: The WCOL bit is set if the SPDR register is written to during a data transfer.
SPI2x (Double SPI Speed) bit: When this bit is set to one, the SPI speed will be doubled when the SPI is in Master mode.
The SPI Data Register used for data transfer between the Register File and the SPI Shift Register. Writing to the register
initiates data transmission. Reading the register causes the Shift Register receive buffer to be read.
Advantages of SPI:
Disadvantages of SPI:
I²C (Inter-Integrated Circuit), is a multi-master, multi-slave, packet switched, single-ended, serial protocol, used for
attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
Basics of I2C
The I²C (Inter-Integrated Circuit) protocol, referred to as I-squared-C, I-two-C, or IIC) is two wire serial communication
protocol for connecting low speed peripherals to a micrcontroller or computer motherboard.
I²C require two wires for communication.,Serial Data (SDA) and Serial Clock (SCL) .The Master generates the clock for
serial communication(SCL). A stream of data bits(B1 to BN) is transferred between the start and the stop bits. I2C bus drivers
are open drain, each line has a pull up resister, so no bus over loading and damage. Up to 120 devices (nodes) can be
connected.
Data transfer is initiated with the Start(S) ,ieSDA goes from high to low when SCL is high .
A data bit is transmitted at every low level of the clock (SCL) after the start condition.
To stop the data transfer, the clock(SCL) is held high, while data(SDA) goes from low to high.
Message has two frames ; one address frame and one or more data frames.
Information ( address/data) is transferred as a packet of 9 bits. 7-bit address, the address is clocked out most significant bit
(MSB) first, followed by a R/W bit indicating whether this is a read (1) or write (0) operation. The 9th bit is the NACK/ACK
bit, from the slave to acknowledge-low/ not acknowledge –high, the receipt of data. Address is used to select a slave, and if it
is ready ACK is generated in 9th clock, else NACK. In this case master may send a stop signal to abrt operation or a repeated
start to initiate a new transmission,
After the address, any number of frames of data transfer initiated, from master or slave depends on the R/W bit.
Clock stretching
Some slow slaves keeps the SCL line low until it is ready and refrain master from generating further clocks. It is called clock
stretching.
In AVR,I2C is referred as TWI- two wire serial interface. TWI module of atmega 32 consists of rate generator-control clock
frequency when operating as master , address match unit- receives address when operating as slave, interface unit-generating
start, stop and receive ACK and related signals, and control unit.
In Transmit mode, contains the next byte to be transmitted, last byte received, in receiving mode.