The Serial Peripheral Interface (SPI)

36
The Serial Peripheral Interface (SPI) Lecture L4.5

description

The Serial Peripheral Interface (SPI). Lecture L4.5. Reference. SPI Block Guide V03.06. S12SPIV3.pdf. PIM_9C32 Block Diagram. SPI module. $DA. $D8. $D8. $D8. $D9. $D9. $DB. $DB. Liquid Crystal Display. Relationship between RS, R/W, and E. CLM912C32 LCD PORT 4-bit SPI - PowerPoint PPT Presentation

Transcript of The Serial Peripheral Interface (SPI)

Page 1: The Serial Peripheral Interface (SPI)

The Serial Peripheral Interface (SPI)

Lecture L4.5

Page 2: The Serial Peripheral Interface (SPI)

Reference

SPIBlock GuideV03.06

S12SPIV3.pdf

Page 3: The Serial Peripheral Interface (SPI)

PIM_9C32

Block Diagram

SPI module

Page 4: The Serial Peripheral Interface (SPI)
Page 5: The Serial Peripheral Interface (SPI)

68HCS12 SPI0 Signals Pin SPI0 signal Name

PM2 MISO Master-In-Slave-Out PM3 MOSI Master-Out-Slave-In PM4 SCK Serial Clock PM5 SS Slave Select

7 6 5 4 3 2 1 0

SP0DR

MASTER

7 6 5 4 3 2 1 0

SP0DR

SLAVE

MOSI MOSIMISO MISO

SCK

SS SS

SCK

Page 6: The Serial Peripheral Interface (SPI)

SPI0 Registers in the 9S12C32 Name Register Addr Description SPICR1 00D8 SPI Control Register 1 SPICR2 00D9 SPI Control Register 2 SPIBR 00DA SPI Baud Rate Register SPISR 00DB SPI Status Register SPDR 00DD SPI Data Register

Page 7: The Serial Peripheral Interface (SPI)

$DA

Page 8: The Serial Peripheral Interface (SPI)
Page 9: The Serial Peripheral Interface (SPI)
Page 10: The Serial Peripheral Interface (SPI)

$D8

Page 11: The Serial Peripheral Interface (SPI)

$D8

Page 12: The Serial Peripheral Interface (SPI)
Page 13: The Serial Peripheral Interface (SPI)
Page 14: The Serial Peripheral Interface (SPI)

$D8

Page 15: The Serial Peripheral Interface (SPI)

$D9

Page 16: The Serial Peripheral Interface (SPI)

$D9

Page 17: The Serial Peripheral Interface (SPI)

$DB

Page 18: The Serial Peripheral Interface (SPI)

$DB

Page 19: The Serial Peripheral Interface (SPI)

Liquid Crystal Display

2 3 4 5 6 7 9 10 1 8 11 12 13 14

16 x 1 Liquid Crystal Display

E R/W

RS V DD

V o 20K

DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

Page 20: The Serial Peripheral Interface (SPI)

RS R/W E

0 0

0 1

1 0

1

Operation

Write instruction code

Read busy flag and address counter

Write data

Read data1

Relationship between RS, R/W, and E

Page 21: The Serial Peripheral Interface (SPI)

Table 7.4 HD44780 Instruction SetInstruction DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description

Clear display 0 0 0 0 0 0 0 1 Clears display & returns cursor to home.Sets I/D=1 in Entry Mode.

Return home 0 0 0 0 0 0 1 x Returns cursor to home position (Address 0)Set DD RAM address to zero.

Entry mode set 0 0 0 0 0 1 I/D S I/D=1: increment cursor; S=0: normal ;I/D=0: decrement cursor; S=1 shift display.

Display ON/OFFcontrol

0 0 0 0 1 D C B Sets ON/OFF all display (D), cursor (C),and blink of cursor (B).

Cursor or displayshift

0 0 0 1 S/C R/L x x S/C=1: display shift; S/C=0: cursor move;R/L=1: shift right; R/L=0: shift left.

Function set 0 0 1 DL N F x x DL=1: 8 bits; DL=0: 4 bits; N=1: 2 line;N=0: 1 line; F=1: 5x10 dots; F=0; 5x7 dots.

Set the CG RAMaddress

0 1 CG RAM address Sets the CG RAM address, after whichCG RAM data is sent and received.

Set the DD RAMaddress

1 DD RAM address Sets the DD RAM address, after whichDD RAM data is sent and received.

Read busy flag &address

BF Address counter Read busy flag (BF) and address countercontents.

Page 22: The Serial Peripheral Interface (SPI)

CLM912C32LCD PORT4-bit SPIInterface

Write only

Page 23: The Serial Peripheral Interface (SPI)
Page 24: The Serial Peripheral Interface (SPI)

File: spi.asm

; Serial Peripheral Interface SPI0

SPI0CR1 equ 00D8 ; SPI Control Register 1SPI0CR2 equ 00D9 ; SPI Control Register 1SPI0BR equ 00DA ; SPI Baud Rate Register SPI0SR equ 00DB ; SPI Status Register SP0DR equ 00DD ; SPI Data Register spi_init ; Initialize SPI port

ldaa #$10staa SPI0CR2 ; enable /SSclr SPI0BR ; 4 MHz (/2)ldaa #$52staa SPI0CR1 ; CPHA = 0, CPO; = 0rts

Page 25: The Serial Peripheral Interface (SPI)

; send A out spi send_spi

psha ; save Ass0 ldaa SPI0SR

anda #$20beq ss0 ; wait for trans. buff emptypula ; get Apsha ; save it again staa SP0DR ; send char

ss1 ldaa SPI0SRanda #$80beq ss1 ; wait till sentldaa SP0DR ; clear SPIFpula ; restore Arts

File: spi.asm (cont.)

Page 26: The Serial Peripheral Interface (SPI)

$D8 SPI0CR1

Page 27: The Serial Peripheral Interface (SPI)

; LCD for Axiom CML9S12-C32#include hex2asc.asm#include spi.asm#include ms_delay.asm ; Write 4-bit instruction in Ainstr4

psha ;save Aanda #$0fjsr send_spi ;EN LO, RS LOpsha ;save Aoraa #$80 ;EN HIjsr send_spipula ;get Ajsr send_spi ;EN LOpula ;restore Arts

Page 28: The Serial Peripheral Interface (SPI)

; Write 8-bit instruction in Ainstr8

psha ;save Alsralsralsralsrabsr instr4 ;send hi nibblepula ;get Absr instr4 ;send lo nibblerts

Page 29: The Serial Peripheral Interface (SPI)

; Write 4-bit data in Adata4

psha ;save Aanda #$0fpsha ;save Aoraa #$40 ;EN HIjsr send_spi ;EN LO, RS HIpula ;get Apsha ;save Aoraa #$C0 ;EN HIjsr send_spipula ;get Apshaoraa #$40jsr send_spi ;EN LOpulapula ;restore Arts

Page 30: The Serial Peripheral Interface (SPI)

; Write 8-bit data in Adata8

psha ;save Alsralsralsralsrabsr data4 ;send hi nibblepula ;get Absr data4 ;send lo nibblerts 

Page 31: The Serial Peripheral Interface (SPI)

; Initialize 4-bit widelcd_init

ldy #50jsr ms_delayldaa #3jsr instr4 ; function setldy #50jsr ms_delayldaa #3jsr instr4 ; function setldy #50jsr ms_delayldaa #3jsr instr4 ; function setldy #50jsr ms_delayldaa #2jsr instr4 ; function setldy #50jsr ms_delayldaa #$2Cjsr instr8 ; 4-bits, 2 linesldy #10jsr ms_delayldaa #$06jsr instr8 ; inc cursorldy #10jsr ms_delayldaa #$0fjsr instr8 ; display onldy #10jsr ms_delayldaa #$01jsr instr8 ; clear displayldy #20jsr ms_delayldaa #$80jsr instr8 ; set addr to 0ldy #10jsr ms_delayldaa #$00jsr send_spi ; turn off all signalsrts

Page 32: The Serial Peripheral Interface (SPI)

clear_lcd ldaa #1jsr instr8ldy #10jsr ms_delayrts

; display hex value in A on LCD hex2lcd

jsr hex2asc ;convert to asciijsr data8 ;display itrts

; display asciiz string on LCD; X -> asciiz string type_lcd

ldaa 1,x+ ;get next charbeq done ;if null, quitjsr data8 ;else display itldy #10jsr ms_delay ; delay 10 msbra type_lcd ;and repeat

done rts

Page 33: The Serial Peripheral Interface (SPI)

; lcdtest.asm

org $800mess1 db 'Haskell',0

org $4000

mainjsr spi_initjsr lcd_initldx #mess1jsr type_lcdswi

#include lcd.asm

Page 34: The Serial Peripheral Interface (SPI)

1: ; lcdtest.asm 2: 3: =00000800 org $800 4: 0800 48 61 73 6B 65 6C mess1 db 'Haskell',0 0806 6C 00 5: 6: =00004000 org $4000 7: 8: 4000 main 9: 4000 16 4019 jsr spi_init 10: 4003 16 4088 jsr lcd_init 11: 4006 CE 0800 ldx #mess1 12: 4009 16 410A jsr type_lcd 13: 400C 3F swi

Page 35: The Serial Peripheral Interface (SPI)

74165

1

2

3

4

5

6

7

8 9

10

11

12

13

14

15

16

GND

VccSH/LD

CLK

E

F

G

Q

H

H

CLK INH

D

C

B

A

SER

QH

74165

1

2

3

4

5

6

7

8 9

10

11

12

13

14

15

16

GND

VccSH/LD

CLK

E

F

G

Q

H

H

CLK INH

D

C

B

A

SER

QH

+5V

3.3K

+5V+5V

3.3K3.3K

3.3K

+5V

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

MISO

SCK SS

16 x 1Hex Keypad

Page 36: The Serial Peripheral Interface (SPI)

74165

1

2

3

4

5

6

7

8 9

10

11

12

13

14

15

16

GND

VccSH/LD

CLK

E

F

G

Q

H

H

CLK INH

D

C

B

A

SER

QH

74165

1

2

3

4

5

6

7

8 9

10

11

12

13

14

15

16

GND

VccSH/LD

CLK

E

F

G

Q

H

H

CLK INH

D

C

B

A

SER

QH

+5V

3.3K

+5V+5V

3.3K3.3K

3.3K

+5V

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

MISO

SCK SS

0 1 2 3 4 5 6 7 8 9 10 11 12 12 14 153 2 1 0 8 9 A B 7 6 5 4 C D E F