Download - The Serial Peripheral Interface (SPI)
The Serial Peripheral Interface (SPI)
Chapter 8
CML9S12-DP256
PIM_9DP256
Block Diagram
Table 8.1 68HC12 SPI0 Signals Pin SPI0 signal Name
PS4 MISO Master-In-Slave-Out PS5 MOSI Master-Out-Slave-In PS6 SCK Serial Clock PS7 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
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
2 3 4 5 6 7 9 101 8 11 12 13 14
16 x 1 Liquid Crystal Display
PJ2 (E)
PJ1 (R/W)
PJ0 (RS)VDD
Vo20K
PH0 (DB0)
PH1 (DB1)
PH2 (DB2)
PH3 (DB3)
PH4 (DB4)
PH5 (DB5)
PH6 (DB6)
PH7 (DB7)
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
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.
CLM912-DP256LCD PORT4-bit SPIInterface
Write only
File: SPI0.WHP\ Serial Peripheral Interface SPI0HEX00D8 CONSTANT SPI0CR1 \ SPI Control Register 100D9 CONSTANT SPI0CR2 \ SPI Control Register 100DA CONSTANT SPI0BR \ SPI Baud Rate Register 00DB CONSTANT SPI0SR \ SPI Status Register 00DD CONSTANT SP0DR \ SPI Data Register : SPI.INIT ( -- ) \ Initialize SPI port
10 SPI0CR2 C! \ enable /SS00 SPI0BR C! \ 4 MHz (/2)52 SPI0CR1 C! ; \ CPHA = 0, CPO; = 0
: ?SPI.DONE ( -- f ) \ Is SPI data sent?
7 SPI0SR ?HI ; : SEND.SPI ( c -- )
SP0DR C! \ send charBEGIN ?SPI.DONE \ wait till sentUNTILSP0DR C@ DROP ; \ clear SPIF
\ LCD for Axiom CML9S12-DP256load hex2asc.whpload string.whpload 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 \ EN LO, RS LODUP 80 OR \ EN HIsend.spisend.spi ; \ 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 \ EN LO, RS HIDUP C0 OR send.spi \ EN HI40 OR send.spi ; \ EN LO
\ Write 8-bit data: >data8 ( n -- )
DUP 4 RSHIFT>data4>data4 ;
\ Initialize 4-bit wide: lcd.init ( -- )
50ms.delay 3 >instr4 \ function set
50ms.delay 3 >instr4 \ function set
50ms.delay 3 >instr4 \ function set
50ms.delay 2 >instr4 \ function set
50ms.delay2C >instr8 \ 4-bits, 2 lines10ms.delay06 >instr8 \ inc cursor10ms.delay
0f >instr8 \ display on10ms.delay
01 >instr8 \ clear display20ms.delay
80 >instr8 \ set addr to 010ms.delay0 send.spi \ turn off all signals
0A BASE ! ; \ 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