Presentation is loading. Please wait.

Presentation is loading. Please wait.

Serial Peripheral Interface SPI I2C (i-squared cee)

Similar presentations


Presentation on theme: "Serial Peripheral Interface SPI I2C (i-squared cee)"— Presentation transcript:

1 Serial Peripheral Interface SPI I2C (i-squared cee)
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)

2 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.

3 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!

4 Example with 3 slaves

5 Voltmeter: Using the MCP3001
Serial digital 12bits (SPI) Parallel interface LCD display PIC ADC SO 3.142 Volts CLK CS Voltage(analogue)

6 Example Sequence for operation. ADC -- MCP3001 (see DATA SHEET)
Start Discard first 3 bits! DATA Time

7 Example Connections (SPI EEPROM)

8 We read the next bit from the SO pin.
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

9 Timing Diagrams Tell us how long it takes the device to respond

10 Some typical. Timing values

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

12 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 EEPROM SPI_IN_BUF EQU H'25' SPI_OUT_BUF EQU H'26'

13 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 TO SPI_SI 1

14 RLF Rotate Left through Carry

15 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 left spi_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 bit set_out_bit: bsf PORTA,SPI_SI ; if carry (SI pin=1) ; now send bit to device doit_write_bit: call spi_strobe ; advance clock to send bit return

16 Strobe routine (read/write next bit)
; advance the clock in the serial EEPROM ; no delay needed if running from a 4MHz clock spi_strobe: bsf PORTA,SPI_CLK ; set clock high bcf PORTA,SPI_CLK ; set clock low return

17 Writing one byte to the SPI device
; routine to write SPI_OUT_BUF reg to the SPI device spi_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 return

18 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 reading spi_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

19 Outcomes. describe how the SPI protocol can be used to interface devices. describe the sequence of events required to read/write using SPI


Download ppt "Serial Peripheral Interface SPI I2C (i-squared cee)"

Similar presentations


Ads by Google