You are on page 1of 36

SERIAL COMMUNICATION

INTERFACES OF MICROCONTROLLER
Lecturer: DSc, dosent Utkir Khamdamov
E-mail: utkir.hamdamov@mail.ru
Resources
• https://www.mikroe.com/ebooks/architecture-and-program
ming-of-8051-mcus

• https
://learn.mikroe.com/ebooks/8051programming/chapter/2-7
-uart-universal-asynchronous-receiver-and-transmitter
/

• KRISHNA KANT
• MICROPROCESSORS AND MICROCONTROLLERS:
ARCHITECTURE, PROGRAMMING AND SYSTEM
DESIGN
Microcontroller Architecture
Serial Interfaces of Microcontroller

UART
Universal SPI
Asynchronous Serial
Receiver and Peripheral Interface
Transmitter
Application of UART
UART pins of Microcontroller

• AVR ATmega32
• UART (RxD) – прием данных
контакт PDO
• UART (TxD) –передача
данных контакт PD1
UART serial port
• UART
•  serial port
•  full-duplex port
•  transmit and receive data simultaneously
•  different baud rates
• Programmer has to do select
•  serial port mode
•  baud rate
• SBUF
• serial data transmit is nothing but writing to the SBUF register,
while data receive represents reading the same register
UART Data Format

SPI UART
Transfer rate 1-20 MHz 9 – 56 kHz
Usage field High speed Keyboard,
devices Character LCD / Monitor
UART SCON Register
• Serial port must be configured
• determine how many bits is contained in one serial “word”
• baud rate and
• synchronization of transmission

• Process is controlled by the bits of the SCON register


(Serial Control)
UART SCON Register
• SM0 – Serial port mode bit 0 is used for serial port mode selection.
• SM1 – Serial port mode bit 1.
• SM2 – Serial port mode 2 bit, also known as multiprocessor communication enable bit.
• REN – Reception Enable bit enables serial reception when set. When cleared, serial
reception is disabled.
• TB8 – Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem of
transmitting the 9th bit in modes 2 and 3. It is set to transmit a logic 1.
• RB8 – Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if
9th bit received is a logic 0.
• TI – Transmit Interrupt flag is automatically set at the moment the last bit of one byte is
sent. It’s a signal to the processor that the line is available for a new byte transmit.
• RI – Receive Interrupt flag is automatically set upon one byte receive. It signals that
byte is received and should be read quickly prior to being replaced by a new data..
UART Serial Port mode
SM0 SM1 MODE DESCRIPTION BAUD RATE

0 0 0 8-bit Shift Register 1/12 the quartz frequency

0 1 1 8-bit UART Determined by the timer 1

1/32 the quartz frequency


1 0 2 9-bit UART
(1/64 the quartz frequency)

1 1 3 9-bit UART Determined by the timer 1


UART Serial Port Mode 0
• Serial data are transmitted
and received through the
RXD pin, while the TXD pin
output clocks.

• The bout rate is fixed


at 1/12 the oscillator
frequency
UART Serial Port Mode 0
• TRANSMIT – Data transmit is initiated by writing data to the SBUF register. When
all 8 bits have been sent, the TI bit of the SCON register is automatically set.

• RECEIVE – Data receive through the RXD pin starts when: bit REN=1 and RI=0.
When all 8 bits have been received, the RI bit of the SCON register is automatically
set indicating that one byte receive is complete.
UART Serial Port Mode 1
• In mode 1, 10 bits are
transmitted through the TXD pin
or received through the RXD pin
• a START bit (always 0),

• a STOP bit (always 1).

• The START bit is only used to


initiate data receive,
• The STOP bit is automatically
written to the RB8 bit of the
SCON register.
UART Serial Port Mode 1
• TRANSMIT – Data transmit is initiated by writing data to the SBUF register.
End of data transmission is indicated by setting the TI bit of the SCON register.

• RECEIVE – The START bit (logic zero (0)) on the RXD pin initiates data receive, when: bit
REN=1 and bit RI=0. The RI bit is automatically set upon data reception is complete.

• The Baud rate in this mode is determined by the timer 1 overflow


Serial Port Mode 2
• In mode 2, 11 bits are
transmitted through the TXD pin
or received through the RXD pin
• a START bit (always 0),
• a Programmable 9th data bit
• a STOP bit (always 1).

• This bit usually has a function of


parity bit
• 10111101 - six '1' bits. Parity bit = 0,
with parity bit 101111010.
• 01110011 - five '1' bits. Parity bit = 1,
with parity bit 011100111.
• On receive, the 9th data bit goes
into the RB8 bit of the same
register (SCON).
• The baud rate is either 1/32 or
1/64 the oscillator frequency.
Serial Port Mode 2
• TRANSMIT – Data transmit is initiated by writing data to the SBUF register.
End of data transmission is indicated by setting the TI bit of the SCON register.

• RECEIVE – The START bit (logic zero (0)) on the RXD pin initiates data receive, when:
bit REN=1 and bit RI=0. The RI bit is automatically set upon data reception is complete.
Multiprocessor Communication
SERIAL PERIPHERAL INTERFACE
(SPI)
Application of SPI
ATmega16/32 SPI Pins
SPI - Serial Peripheral Interface
• SPI (Serial Peripheral
Interface) - serial
synchronous four-
wire data interface
SPI - Serial Peripheral Interface
• MISO (Master In Slave Out)
• Master receives data, and the Slave transmits data

• MOSI (Master Out Slave In)


• Master transmits data, and the Slave receives data

• SCK (Shift Clock)
• Only the Master generates synchronization pulses, which are used by the slaves..

• SS (Slave Select)
• The master can select the slave through this contact.
SPI Data transfer:
Normal SPI mode (buffer out of use)
• Data written to the SPI data register SPDR is automatically transferred to
an 8-bit Shift register.
• SPI clock generator is enabled and serial data appears on the MOSI pin.
• An initial delay may occur for the sake of synchronization with the main
oscillator.
SPI Data transfer:
Enhanced SPI mode (buffer in use)
• 1.Data written to the SPI data register SPDR is automatically transferred to
the buffer, which causes the WCOL bit to be set
• 2.Data transferred to shift register
• 3. While one byte transmit is in progress, the next byte to transmit may be
written to the SPDR register.
SPI Special Function Registers
• The SPI system is under control of 3 special function
registers
• SPDR – data register

• SPSR – status register

• SPCR – control register


SPDR (SPI Data Register)
• SPDR - The SPDR register is used for:
• storing data to be transferred via SPI.
• storing data to be received via SPI.
SPSR (SPI Status Register)
• SPSR (SPI Status Register) is designed to monitor the status of the
SPI module:
• SPIF – Interrupt flag of SPI. This bit is automatically set at the end of data
transmission /reception
• WCOL – Write Collision of SPI. This bit is set (WCOL=1) in normal mode if the
SPDR register is written during data transfer is in progress.
SPCR (SPI Control Register).
• Bit 7 – SPIE: SPI interrupt enable bit
• 1 = SPI interrupt enable. 0 = SPI interrupt disable.

• Bit 6 – SPE: SPI communication enable bit (between MC pin and Port pin)
• 1 = SPI communication enable. 0 = SPI communication disable.
• Bit 5 – DORD:  SPI Data ordination bit
• 1 = LSB first. 0 = MSB first
• Bit 4 – MSTR: mode selection bit (Master/Slave)
• 1 = Master. 0 = Slave.

• Bit 3 – CPOL:  SCK pin logic state control bit (positive/negative)


• 1 = Pin SCK is set «1». 0 = Pin SCK is cleared «0».
• Bit 2 – CPHA:  Controls relation between clock and data in serial format
• 1 = bit selection by trailing edge. 0 = bit selection by leading edge.
• Bit 1:0 – SPR1: SPR0  Baud rate or clock cycle frequency selection bit
 SPI Modes (SPCR)
• CPOL and CPHA registers
determines 4 SPI modes

• CPOL: Logic state control bit


• 1 = Pin SCK is set «1»
• 0 = Pin SCK is cleared «0»

• CPHA: Phase selection bit


• 1 = bit selection by trailing edge
• 0 = bit selection by leading edge
 SPI Modes (SPCR)

CPHA=0

CPHA=1
Baud rate of SPI (SPCR)
• SPR1: SPR0 – Baud rate or clock cycle frequency selection bit.
• where Fosc MCU clock cycle frequency.

SPR1 SPR0 SCK


0 0 Fosc/4
0 1 Fosc/16
1 0 Fosc/64
1 1 Fosc/128
Data transmission process with SPI
Programming in C UART AVR Atmega8
Result of Programming UART AVR Atmega8
Thank you for Attention !

You might also like