lect4xxx

advertisement
Department of Electronic & Electrical Engineering
Lect 4: Serial interfaces
Serial Interfaces allow communication between devices sending one bit
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)
Department of Electronic & Electrical Engineering
Resources
●
●
●
●
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Quintessential PIC chapter 12
25AA040 serial eeprom data sheet.
MCP3001 ADC data sheet.
Department of Electronic & Electrical Engineering
Serial Peripheral Interface Bus (SPI)
- Lots of devices use SPI to communicate
- Pronounced ess-pee-eye OR spy
- Protocol using 4 lines
- Master Device (eg. PIC )
- Slave Device(s) (eg. SPI eeprom OR an ADC)
- Many PICs have inbuilt SPI or I2C
- The PIC16F84A does not!
(we will implement it in software)
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!
Department of Electronic & Electrical Engineering
Connections for the lab example.
Department of Electronic & Electrical Engineering
Example:
25AA040A (see data sheet)
2.1 Principles of Operation
The 25XX040A is a 512-byte Serial EEPROM designed to interface
directly with the Serial Peripheral Interface (SPI) port of many of
today’s popular microcontroller families, including Microchip’s
PIC®microcontrollers. It may also interface with microcontrollers that
do not have a built-in SPI port by using discrete I/O lines programmed
properly in firmware to match the SPI protocol.
Department of Electronic & Electrical Engineering
Example:
SPI eeprom 25AA040
Department of Electronic & Electrical Engineering
Memory
8bit
Memory
04
Address
01011101
01111100
01010100
11010100
01011101
00011001
01010100
11010100
01011101
00011001
Data
Department of Electronic & Electrical Engineering
25A040 Functional diagram
Commands
address
Data out
Department of Electronic & Electrical Engineering
25A040
instruction set
Department of Electronic & Electrical Engineering
Accessing the Device.
●
●
●
●
●
●
We initiate an operations by taking CS low.
We write instructions via the SI pin.
We read via the SO pin
Data is clocked by sending a pulse to SCK (low-high-low).
We finish an operation by taking CS high.
Device is ready for next operation.
Department of Electronic & Electrical Engineering
Timing Diagrams
Department of Electronic & Electrical Engineering
Timing values
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
Department of Electronic & Electrical Engineering
25AA040 Instruction set
Department of Electronic & Electrical Engineering
Sequence to initiate reading
Send read instruction
(SI)
Send address to be read
Read data
(SO)
Department of Electronic & Electrical Engineering
Reading data.
•
•
•
•
Once we have set the address we can read data from the EEPROM.
The 25A040 automatically advances the address every byte (8bits)
So it is easy to read sequentially from memory
For the lab exercise we have a 7seg display.
• This can display a HEX number (4bits)
• The example reads data in nibbles (4bits)
• Each data value is displayed (with a delay)
Department of Electronic & Electrical Engineering
Example Code . . . Sketch
●
●
●
●
●
●
●
Initialize PORTS
Initialize device
(set CS low)
Send read instruction
Send address
Read data (4bits)
Display data
Loop read and display . . .
Department of Electronic & Electrical Engineering
Connections for the lab example.
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
SPI_CLK
SPI_SO
SPI_CS
EQU
EQU
EQU
EQU
H'0'
H'1'
H'2'
H'3'
; See diagram in previous slide
; 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'
Department of Electronic & Electrical Engineering
Setting up the PORT
;
;
;
;
RA0 --> SI
RA1 --> SCK
RA2 --> SO
RA3 --> !CS
serial in
(PIC out)
clock
(PIC out)
serial out (PIC in)
chip enable (PIC out) + 4.7k pull up
movlw B'00000100'
movwf TRISA
; port A - just RA2 --> SPI_SO input
; all others are outputs
Department of Electronic & Electrical Engineering
Sequence to initiate reading
Send read instruction
(SI)
Send address to be read
Read data
(SO)
Department of Electronic & Electrical Engineering
Initialize and send address.
; start of spi READ (see Data sheet FIG2-1)
bcf
bsf
bcf
movlw
movwf
call
movlw
movwf
call
PORTA , SPI_CLK ;(1) set spi clock low
PORTA , SPI_CS
;(2) make CS (chip select) high
PORTA , SPI_CS
;(3) start by pulling CS low
B'00000011'
;(4) read instruction and A8=0
SPI_OUT_BUF ; send to chip
spi_out
; spi_out sends contents of SPI_OUT_BUF
B'00000000'
;(5) low byte of address
SPI_OUT_BUF ;send to chip
spi_out
Department of Electronic & Electrical Engineering
Routine to write a byte to the SPI device
●
●
●
●
●
●
SPI_OUT_BUF contains the data.
We need to send the most significant bit first.
Copy the left most (MSB) of SPI_OUT_BUF to the SPI_SI pin
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
SPI_SI=0
strobe
SPI_SI=1
strobe
0
1
rlf
SPI_OUT_BUF,f
rlf
SPI_OUT_BUF,f
11010010
10100100
Department of Electronic & Electrical Engineering
RLF
Rotate Left through Carry
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 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 eeprom
doit_write_bit:
call spi_strobe
; advance clock to send bit
return
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 clock
spi_strobe:
bsf PORTA,SPI_CLK ; set clock high
bcf PORTA,SPI_CLK ; set clock low
return
Department of Electronic & Electrical Engineering
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
call spi_write_bit
call spi_write_bit
call spi_write_bit
call spi_write_bit
call spi_write_bit
return
Department of Electronic & Electrical Engineering
Recap --- Initialize and send address.
; start of spi READ (see Data sheet FIG2-1)
bcf
bsf
bcf
movlw
movwf
call
movlw
movwf
call
PORTA , SPI_CLK ;(1) set spi clock low
PORTA , SPI_CS
;(2) make CS (chip select) high
PORTA , SPI_CS
;(3) start by pulling CS low
B'00000011'
;(4) read instruction and A8=0
SPI_OUT_BUF ; send to chip
spi_out
; spi_out sends contents of SPI_OUT_BUF
B'00000000'
;(5) low byte of address
SPI_OUT_BUF ;send to chip
spi_out
Department of Electronic & Electrical Engineering
Reading the data.
•
•
•
•
We are now ready to start reading data.
We read the data in chunks of 4bits so we can use 7seg display
We put a delay in the loop so we can see each value.
The example code does this forever!
Department of Electronic & Electrical Engineering
Main loop --- read data nibble at a time
; now ready to read from the SPI-EEPROM
loop:
call
spi_read_nibble ; (6) grab next nibble from the eeprom
call
conversion
; convert to 7seg code
xorlw H'FF'
; invert for common annode
movwf PORTB
; output to 7seg LED
call
delay
; wait a bit
goto loop
; carry on
Department of Electronic & Electrical Engineering
Routine to read a nibble
;---------------------------------------------------------------------------; read 1 nibble (4bits) from the spi eeprom into W
; uses SPI_IN_BUF to assemble nibble bit by bit
spi_read_nibble:
clrf
SPI_IN_BUF
; clear SPI_IN_BUF
call
spi_read_bit
; rotate left then read a bit
call
spi_read_bit
call
spi_read_bit
call
spi_read_bit
movfw
SPI_IN_BUF
; return value in W
return
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 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
Department of Electronic & Electrical Engineering
DEMO : readSpiEEPROM.asm
Download