Professional Documents
Culture Documents
Interfacing (SPI)
Programming in
AVR
LECTURE# 24
MICROPROCESSOR SYSTEMS AND INTERFACING
UCSR0B
RXC0
RXCIE0
TXC0
TXCIE0
UDRE0
UDRIE0 RXEN0
FE0 DOR0
TXEN0
UPE0
UCSZ02
U2X0
RXB80
MPCM0
TXB80
UART UCSR0C UMSEL01 UMSEL00 UPM01 UPM00 USBS0 UCSZ01 UCSZ00 UCPOL0
UCSR0B
RXC0
RXCIE0
TXC0
TXCIE0
UDRE0
UDRIE0 RXEN0
FE0 DOR0
TXEN0
UPE0
UCSZ02
U2X0
RXB80
MPCM0
TXB80
UART UCSR0C UMSEL01 UMSEL00 UPM01 UPM00 USBS0 UCSZ01 UCSZ00 UCPOL0
You have an AVR ATmega328p running at 11.0592 MHz. Configure the USART
module for receiving asynchronous mode at 57600 baud rate, 6-bit data, 1-stop bit,
no parity and receive interrupts enable.
1. Write the values in binary for UCSR0A,UCSR0B, UCSR0C, UBRR0H and UBRR0L
2. Role of Flags
a) Which flag will be set when a data is received.
b) Which flag is set when invalid frame is received
c) Which flag is set when data is received repeatedly, but UDR is not read in the code.
3. Write an assembly program which receives data and stores to SRAM location 0x300
Introduction
Wire Name Alternate Pin Names
A synchronous serial
communication protocol Master Slave
MOSI (Master Out Slave In) SDO (Serial Data Out) SDI
Singe master multiple slaves
MISO (Master In Slave Out) SDI (Serial Data In) SDO
◦ Master controls the clock
SCL (Serial Clock) SCLK SCLK
Four-wire interface SS (Slave Select) CE (Chip Enable)/SS CE/SS
◦ May also be three
◦ When not using SS pin MASTER
SDO
MOSI
SDI
MISO
SDI SDO
SCL
Number of pins increase with SS3 SS2 SS1
SCLK SCLK
slaves SS SLAVE1
SLAVE2
At a time, the master SS
SS SLAVE3
SPI Protocol
Master and Slaves have shift registers
◦ Usually 8-bit shift registers
In each transaction
◦ !SS is brought to zero (active low)
◦ Master and a Slave exchange data in the
shift registers
◦ Eight clock pulses required
When Master wants to write a byte
◦ It places data it in its shift register
◦ Gives 8 clock pulses and the data goes to
slave
When Master wants the read a byte
◦ Master provides 8 clock cycles
◦ Master reads its shift register
SPI Communication
In addition to the shift register, each slave has multiple data registers
◦ The master can write or read each of these registers
◦ Each of these data registers have unique address
When Reading a data register
◦ The Master sends the address of the data register (along with R/W bit)
◦ The slave places the data in its shift register
◦ Master shift the data into its own shift register
When Master wants to write data
◦ The Master sends the address of the data register (along with R/W bit)
◦ The Master sends the data to be written
◦ The slave copies the receive data from shift register to data register
SPI allows reading or writing multiple consecutive data registers of slave
◦ Using Burst Read or write mode
32
31
30
29
28
27
26
25
PD3 1 24 PC1
ATmega328p PD4
GND
VCC
GND
2
3
4
5 32 TQFP
23
22
21
20
PC0
ADC7
GND
AREF
VCC 6 19 ADC6
Try to find the SPI pins on SPDIP package PB6 7 18 AVCC
PB7 8 17 PB5
10
11
12
13
14
15
16
9
PD7
PB2
PD5
PD6
PB0
PB3
PB4
PB1
28 SPDIP
(PCINT14/RESET) PC6 1 28 PC5 (ADC5/SCL/PCINT13)
(PCINT16/RXD) PD0 2 27 PC4 (ADC4/SDA/PCINT12)
(PCINT17/TXD) PD1 3 26 PC3 (ADC3/PCINT11)
(PCINT18/INT0) PD2 4 25 PC2 (ADC2/PCINT10)
(PCINT19/OC2B/INT1) PD3 5 24 PC1 (ADC1/PCINT9)
(PCINT20/XCK/T0) PD4 6 23 PC0 (ADC0/PCINT8)
VCC 7 22 GND
GND 8 21 AREF
(PCINT6/XTAL1/TOSC1) PB6 9 20 AVCC
(PCINT7/XTAL2/TOSC2) PB7 10 19 PB5 (SCK/PCINT5)
(PCINT21/OC0B/T1) PD5 11 18 PB4 (MISO/PCINT4)
(PCINT22/OC0A/AIN0) PD6 12 17 PB3 (MOSI/OC2A/PCINT3)
(PCINT23/AIN1) PD7 13 16 PB2 (SS/OC1B/PCINT2)
(PCINT0/CLKO/ICP1) PB0 14 15 PB1 (OC1A/PCINT1)
The slave will not initiate the transfer, it will transmit ‘G’ only when the Master wants to read.
Also the clock will be controlled by the Master, so no need to configure the clock source