shift registers: converting between serial and parallel data mark neil - microprocessor course 1...

19
SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Upload: fiona-topliff

Post on 15-Dec-2015

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND

PARALLEL DATA

Mark Neil - Microprocessor Course

1

Serial versus Parallel Data Transfers

Page 2: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Serial communications

Mark Neil - Microprocessor Course

2

Most communications is carried out over serial links Fewer wires needed Less electronics needed Must run faster

Examples Ethernet USB – Universal Serial Bus USART – Universal Synchronous and Asynchronous

serial Receiver and Transmitter (RS232/COM ports) I2C – Inter Integrated Circuit SPI – Serial Peripheral Interface

All available in ATmega128 as

“on-chip peripherals”

Page 3: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Shift Registers

Mark Neil - Microprocessor Course

3

Shift Registers convert numbers expressed in terms of several bits (many signal lines) into a stream of 0s, 1s and a Clock. Advantage: A convenient way to reduce the number of

Electrical signal lines by factors of 8, 16, 32, 64. Disadvantage: For the same transfer rate the

electronics must be faster by factors of 8, 16, 32, 64.

Page 4: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

A Four Bit Shift RegisterComposed of 4 D-Flip-FlopsParallel to Serial Conversion

This Shift Register converts data on a 4 bit parallel data bus into a serial bit stream

When Parallel load is set, the S/R bits on the Flip Flops are arranged to latch in the bits on the data bus (1 bit on each Flip Flop)

On each rising edge of the clock, The Data on the Flip Flops is put on the Q (Output Line), which appears at the input of the next flip flop. The input data is latched, and the bits move from one FF to the next

It takes 4 clock cycles to move the 4 bits out to the serial ouput line

Mark Neil - Microprocessor Course

4

Page 5: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

1 0 1 0

0

Data is placed on the parallel data bus

PL: Data Loaded into Flip Flops

On Clock edge Data is shifted from output of each FF to the Input of the next

NEILNEIL - Microprocessor Course 5

Page 6: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

8 Bit Parallel to Serial Shift Register

Mark Neil - Microprocessor Course

6

Page 7: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Parallel to Serial Conversion:Parallel load a number then clock it out

Mark Neil - Microprocessor Course

7

clock D0 D1 D2 D3 D4 D5 D6 D7 Output

0 1 1 0 1 1 0 1 1

1 Tick 1 1 0 1 1 0 1 1

2 Ticks 1 1 0 1 1 0 1

3 Ticks 1 1 0 1 1 0

4 Ticks 1 1 0 1 1

5 Ticks 1 1 0 1

6 Ticks 1 1 0

7 Ticks 1 1

8 Ticks 1

Page 8: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Serial to Parallel shift register

At each clock transition, the data is shifted to the next flip-flop in the chain

We need to keep track of how many clock pulses we have sent to know when the full byte is on the outputs

Mark Neil - Microprocessor Course

8

Page 9: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Suppose you feed a ‘1’:

Mark Neil - Microprocessor Course

9

D0 D1 D2 D3 D4 D5 D6 D7

0 0 0 0 0 0 0 0 0

1T 1 0 0 0 0 0 0 0

2T 1 1 0 0 0 0 0 0

3T 1 1 1 0 0 0 0 0

4T 1 1 1 1 0 0 0 0

5T 1 1 1 1 1 0 0 0

6T 1 1 1 1 1 1 0 0

7T 1 1 1 1 1 1 1 0

8T 1 1 1 1 1 1 1 1

Page 10: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

The 74HC164 Shift Register: Serial to Parallel

Mark Neil - Microprocessor Course

10

Serial DataIn

*Reset

Parallelout

Clock

Page 11: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

74HC164 Function Table

Mark Neil - Microprocessor Course

11

Page 12: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

12

M. Neil - Microprocessor Course

Serial/parallel peripheral module on AVR

There are Several different peripheral functional units on the AVR which you can use.

The Serial Peripheral Interface (SPI) unit allows the AVR to communicate to external devices using a simple serial protocol.

Page 13: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

13

M. Neil - Microprocessor Course

SPI module on AVR

At the heart of the SPI module is a shift register.

You can write parallel 8-bit data to the SPI module which is then automatically clocked out through the shift register to external pins.

The SPI unit can also read in serial data from external pins and present it as parallel 8-bit data to your program.

Page 14: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

SPI timing diagram

Mark Neil - Microprocessor Course

14

Note that as well as selecting clock polarity, you can also select whether the MSB or the LSB is output first!

Page 15: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

SPI code example

Mark Neil - Microprocessor Course

15

SPI_MasterInit:; Set MOSI and SCK output, SS* also as output, all others

input ldi r17,(1<<DDB2)|(1<<DDB1)|(1<<DDB0)out DDRB,r17; Enable SPI, Master, set clock rate fck/16 ldi r17,(1<<SPE)|(1<<MSTR)|(1<<SPR0)out SPCR,r17ret

SPI_MasterTransmit:; Start transmission of data (held in r16) out SPDR,r16

Wait_Transmit:; Wait for transmission to complete sbis SPSR,SPIFrjmp Wait_Transmitret

Page 16: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

High Level Design

Mark Neil - Microprocessor Course

16

The Project:

8-bit Serial Data

ATmega128

Parallel To Serial

PORTB

Data

Clock

SPI

ShiftRegister74HC164

8-Bits of data (Parallel)

Serialto

Parallel

8-Bits of data (Serial)

8-Bits of data (Parallel)

LEDs

Page 17: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Low Level Design

Wire up LEDs so that you can see the data arrive on the output

Wire up a switch to the MR* line to allow you to clear the Shift Register outputs (make sure you understand how this works)

The clock and data bits should come from PORTB

17

Mark Neil - Microprocessor Course

Data: PORTB2(MOSI)

Clock: PORTB1(SCK)

Page 18: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

The Serial Link Test Program

Mark Neil - Microprocessor Course

18

Start

Delay several seconds

Send new pattern to SPI

SPI Clocks the data 8 times into 164 using PORTB

Look atthe LEDs

Page 19: SHIFT REGISTERS: CONVERTING BETWEEN SERIAL AND PARALLEL DATA Mark Neil - Microprocessor Course 1 Serial versus Parallel Data Transfers

Task Plan:

Mark Neil - Microprocessor Course

19

Construct a device that will turn 8-bit parallel data into serial data + clock using the on-chip SPI peripheral.

Construct a device that will receive serial data, convert them to parallel data and display them using LEDs.

Write a program that will send several patterns down your ‘serial link’ and demonstrate that it works.