The Serial Peripheral Interface (SPI)

Download Report

Transcript The Serial Peripheral Interface (SPI)

The Serial Peripheral Interface
(SPI)
Chapter 8
CML9S12-DP256
PIM_9DP256
Block Diagram
Pin
PS4
PS5
PS6
PS7
Table 8.1 68HC12 SPI0 Signals
SPI0 signal
Name
MISO
Master-In-Slave-Out
MOSI
Master-Out-Slave-In
SCK
Serial Clock
SS
Slave Select
MASTER
MOSI
7 6 5 4 3 2 1 0
SCK
SCK
MISO
MISO
SP0DR
SLAVE
7 6 5 4 3 2 1 0
SP0DR
SS
SS
MOSI
Table 8.2 SPI0 Registers in the 9S12DP256
Name
Register Addr
Description
SPI0CR1
00D8
SPI Control Register 1
SPI0CR2
00D9
SPI Control Register 2
SPI0BR
00DA
SPI Baud Rate Register
SPI0SR
00DB
SPI Status Register
SP0DR
00DD
SPI Data Register
Table 8.2a SPI1 Registers in the 9S12DP256
Name
Register Addr
Description
SPI1CR1
00F0
SPI Control Register 1
SPI1CR2
00F1
SPI Control Register 2
SPI1BR
00F2
SPI Baud Rate Register
SPI1SR
00F3
SPI Status Register
SP1DR
00F5
SPI Data Register
Table 8.2b SPI2 Registers in the 9S12DP256
Name
Register Addr
Description
SPI2CR1
00F8
SPI Control Register 1
SPI2CR2
00F9
SPI Control Register 2
SPI2BR
00FA
SPI Baud Rate Register
SPI2SR
00FB
SPI Status Register
SP2DR
00FD
SPI Data Register
Liquid Crystal Display
VDD
20K
PH0 ( DB0)
PH1 ( DB1)
PH2 ( DB2)
PH3 ( DB3)
PH4 ( DB4)
PH5 ( DB5)
PH6 ( DB6)
PH7 ( DB7)
PJ2 (E)
PJ1 (R/W)
PJ0 (RS)
Vo
1 2 3
4 5 6 7 8 9 10 11 12 13 14
16 x 1 Li quid Crys tal Di splay
Relationship between RS, R/W, and E
RS
R/W
E
Operat ion
0
0
Write instruct ion code
0
1
Read busy f lag and address counter
1
0
Write data
1
1
Read data
Table 7.4 HD44780 Instruction Set
Instruction
Clear display
Return home
Entry mode set
Display ON/OFF
control
Cursor or display
shift
Function set
Set the CG RAM
address
Set the DD RAM
address
Read busy flag &
address
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
Description
0
0
0
0
0
0
0
1 Clears display & returns cursor to home.
Sets I/D=1 in Entry Mode.
0
0
0
0
0
0
1
x Returns cursor to home position (Address 0)
Set DD RAM address to zero.
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.
0
0
0
0
1
D
C
B Sets ON/OFF all display (D), cursor (C),
and blink of cursor (B).
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.
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.
0
1
CG RAM address
Sets the CG RAM address, after which
CG RAM data is sent and received.
1
DD RAM address
Sets the DD RAM address, after which
DD RAM data is sent and received.
BF
Address counter
Read busy flag (BF) and address counter
contents.
CLM912-DP256
LCD PORT
4-bit SPI
Interface
Write only
File: SPI0.WHP
\
HEX
00D8
00D9
00DA
00DB
00DD
Serial Peripheral Interface SPI0
CONSTANT
CONSTANT
CONSTANT
CONSTANT
CONSTANT
SPI0CR1
SPI0CR2
SPI0BR
SPI0SR
SP0DR
\
\
\
\
\
SPI
SPI
SPI
SPI
SPI
Control Register 1
Control Register 1
Baud Rate Register
Status Register
Data Register
Initialize SPI port
enable /SS
4 MHz (/2)
CPHA = 0, CPO; = 0
: SPI.INIT
( -- )
10 SPI0CR2 C!
00 SPI0BR C!
52 SPI0CR1 C! ;
\
\
\
\
: ?SPI.DONE
( -- f )
7 SPI0SR ?HI ;
\ Is SPI data sent?
: SEND.SPI
( c -- )
SP0DR C!
BEGIN
?SPI.DONE
UNTIL
SP0DR C@ DROP ;
\ send char
\ wait till sent
\ clear SPIF
\
LCD for Axiom CML9S12-DP256
load hex2asc.whp
load string.whp
load spi0.whp
DECIMAL
: 10ms.delay
( -- )
7267 FOR NEXT ;
: 20ms.delay ( -- )
10ms.delay
10ms.delay ;
: 50ms.delay
( -- )
5 FOR 10ms.delay NEXT ;
HEX
\
Write 4-bit instruction
: >instr4
( n -- )
0F AND
DUP send.spi
DUP 80 OR
send.spi
send.spi ;
\ EN LO, RS LO
\ EN HI
\ EN LO
\
Write 8-bit instruction
: >instr8
( n -- )
DUP 4 RSHIFT
>instr4
>instr4 ;
\
Write 4-bit data
: >data4
( c -- )
0F AND
DUP 40 OR send.spi
DUP C0 OR send.spi
40 OR send.spi ;
\
Write 8-bit data
: >data8
( n -- )
DUP 4 RSHIFT
>data4
>data4 ;
\ EN LO, RS HI
\ EN HI
\ EN LO
\
Initialize 4-bit wide
: lcd.init
( -- )
50ms.delay
3 >instr4
50ms.delay
3 >instr4
50ms.delay
3 >instr4
50ms.delay
2 >instr4
50ms.delay
2C >instr8
10ms.delay
06 >instr8
10ms.delay
0f >instr8
10ms.delay
01 >instr8
20ms.delay
80 >instr8
10ms.delay
0 send.spi
0A BASE ! ;
\ function set
\ function set
\ function set
\ function set
\ 4-bits, 2 lines
\ inc cursor
\ display on
\ clear display
\ set addr to 0
\ turn off all signals
\ decimal default
: clear.lcd
( -- )
1 >instr8
10ms.delay ;
: hex>lcd
( hex -- )
HEX2ASC >data8 ;
: type.lcd
( addr len -- )
?DUP
IF
0 DO
DUP C@
>data8 10ms.delay 1+
LOOP
THEN
DROP ;
: .lcd
( n -- )
(.) type.lcd ;
: u.lcd
( u -- )
(u.) type.lcd ;
DECIMAL