Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow...

21
Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In contrast Parallel interfaces have multiple data lines allowing whole words to be sent as a single operation. There are different protocols for serial communication, common examples: Serial Peripheral Interface SPI I2C (i-squared cee)

description

Department of Electronic & Electrical Engineering The SPI bus specifies four logic signals: ● SCLK: serial clock (used to synchronise serial transfer) ● MOSI: master output, slave input (used to send data to device) ● MISO: master input, slave output (used to receive data from device ) ● SS: slave select (active low, output from master).slave selectactive low ● The names are often different on the device data sheet!

Transcript of Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow...

Page 1: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Serial interfaces

Serial Interfaces allow communication between devices sending one bit at a time.

In contrast Parallel interfaces have multiple data lines allowing whole words to be sent as a single operation.

There are different protocols for serial communication, common examples:

● Serial Peripheral Interface SPI● I2C (i-squared cee)

Page 2: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Serial Peripheral Interface Bus (SPI)

- Lots of devices use SPI to communicate - There is always a Master Device (eg. PIC )- and a Slave Device(s) (eg. SPI eeprom OR an ADC)

- Many PICs have inbuilt SPI or I2C- The PIC16F84A does not! (need to implement it in software)

The SPI protocol uses 4 lines to connect the two devices All slaves have a unique line to select the device. The other lines can be shared.

Page 3: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

The SPI bus specifies four logic signals:

● SCLK: serial clock (used to synchronise serial transfer)

● MOSI: master output, slave input (used to send data to device)

● MISO: master input, slave output (used to receive data from device )

● SS: slave select (active low, output from master).

● The names are often different on the device data sheet!

Page 4: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Example with 3 slaves

Page 5: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Voltmeter: Using the MCP3001

ADCPIC

Voltage(analogue)

Serial digital 12bits (SPI)

3.142 Volts

LCD display

Parallel interface

SO

CLK

CS

Page 6: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Example Sequence for operation. ADC -- MCP3001 (see DATA SHEET)

Time

Start

Discard first 3 bits! DATA

Page 7: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Example Connections (SPI EEPROM)

Page 8: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Reading data using SPI.

● We initiate an operation by taking CS low.

loop:

● We read the next bit from the SO pin.● Send a pulse to SCK (low-high-low).

● We finish an by taking CS high.

● Writing data works the same way --- set SI high/low

Page 9: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Timing Diagrams

Tell us how long it takes the device to respond . . . .

Page 10: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Some typical. Timing values

Page 11: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Timing considerations.

● PIC instruction is 4 clock cycles● @ 4MHz 1 instruction takes 1 uS● all critical times < 1uS● No need to add delays

Page 12: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Code fragments --- 1. Define some constants

; define the bits of PORTA as constants ; so we can write stuff like:; bcf PORTA,SPI_CLK ; sets clock low.

SPI_SI EQU H'0' ; See diagram in previous slide SPI_CLK EQU H'1'SPI_SO EQU H'2'SPI_CS EQU H'3'

; spi IO registers Used to store values to be sent to/from EEPROMSPI_IN_BUF EQU H'25'SPI_OUT_BUF EQU H'26'

Page 13: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Write a byte to the SPI device

SPI_OUT_BUF contains the data. Assume we send the most significant bit first.loop:● Set SPI_SI pin according to left most (MSB) of SPI_OUT_BUF ● Strobe the SPI_CLK to make device read the data● Shift SPI_OUT_BUF left ready for writing the next bit.● Repeat 8 times for each bit.

Carry SPI_OUT_BUF 01101001

110100100

101001001TO SPI_SI

Page 14: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

RLF Rotate Left through Carry

Page 15: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Routine to write one bit to the SPI device;------------------------------------------------------------------------------; write left most bit (MSB) of SPI_OUT_BUF to device; SPI_OUT_BUF is shifted one bit to the leftspi_write_bit: rlf SPI_OUT_BUF,f ; rotate to set carry bit if MSB is 1 btfsc STATUS,C ; skip if carry is clear goto set_out_bit ; if carry bit is set bcf PORTA,SPI_SI ; if carry bit is clear (SI pin=0) goto doit_write_bit ; send bitset_out_bit: bsf PORTA,SPI_SI ; if carry (SI pin=1); now send bit to devicedoit_write_bit: call spi_strobe ; advance clock to send bit return

Page 16: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Strobe routine (read/write next bit)

; advance the clock in the serial EEPROM; no delay needed if running from a 4MHz clockspi_strobe: bsf PORTA,SPI_CLK ; set clock high bcf PORTA,SPI_CLK ; set clock low return

Page 17: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Writing one byte to the SPI device

; routine to write SPI_OUT_BUF reg to the SPI devicespi_out: call spi_write_bit ; write MSB and shift SPI_OUT_BUF right call spi_write_bit ; write next bit etc call spi_write_bit call spi_write_bit call spi_write_bit call spi_write_bit call spi_write_bit call spi_write_bit return

Page 18: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Routine to read a bit

;-------------------------------------------------------------------------------; read a single bit from ee-prom into SPI_IN_BUF (right most bit); rotate SPI_IN_BUF to the left before readingspi_read_bit rlf SPI_IN_BUF,f ; rotate buffer left btfsc PORTA,SPI_SO ; test the SO pin bsf SPI_IN_BUF,0 ; high? then set bit0 call spi_strobe ; advance the clock return

Page 19: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Serial interfaces I2C

● More complex then SPI.● Only uses 2 lines ● Slave devices have addresses

http://www.cypress.com/?rID=34486

OPEN DRAIN

Page 20: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

How I2C works

Video at:

http://www.cypress.com/?rID=34486

Tutorial and C code at:

http://www.robot-electronics.co.uk/acatalog/I2C_Tutorial.html

Page 21: Department of Electronic & Electrical Engineering Serial interfaces Serial Interfaces allow communication between devices sending one bit at a time. In.

Department of Electronic & Electrical Engineering

Example I2C devices

CMPS03 - Compass Module SRF08 ultrasonic rangefinder

Contact-less Infrared Thermopile Sensor Breakout - TMP006