37
The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

  • View
    231

  • Download
    1

Embed Size (px)

Citation preview

Page 1: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

The Serial Communication Interface (SCI)

MC9S12-DP256Chapter 11

Page 2: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

SCI

• Asynchronous Serial I/O

• The 68HC12 SCI Interface

• Programming the SCI in WHYP

• SCI Interface Using Interrupts

Page 3: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

MARK

SPACESTART

STOP

PARITY

D0 D1 D2 D3 D4 D5 D6 D7

Fig. 11.1 ASCII code 54H = 1010100 ("T") sent with odd parity

Table 11.1Common Asynchronous Serial Baud Rates

Baud rate Bit time(msec)

No. of STOPbits

Char. time (msec.) Char./sec.

110 9.09 2 100.00 10300 3.33 1 33.3 3 30600 1.67 1 16.67 60

1200 0.833 1 8.33 1202400 0.417 1 4.17 2404800 0.208 1 2.08 4809600 0.104 1 1.04 960

14400 0.069 1 0.69 144019200 0.052 1 0.52 192028800 0.035 1 0.35 288038400 0.026 1 0.26 3840

Page 4: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

SCI

• Asynchronous Serial I/O

• The 68HC12 SCI Interface

• Programming the SCI in WHYP

• SCI Interface Using Interrupts

Page 5: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

Table 11.2 SCI Registers in the MC9S12-DP256 Name Register Addr Description SC0BDH 00C8 SCI Baud Rate Control Register High SC0BDL 00C9 SCI Baud Rate Control Register Low SC0CR1 00CA SCI Control Register 1 SC0CR2 00CB SCI Control Register 2 SC0SR1 00CC SCI Status Register 1 SC0SR2 00CD SCI Status Register 2 SC0DRH 00CE SCI Data Register High SC0DRL 00CF SCI Data Register Low SC1BDH 00D0 SCI Baud Rate Control Register High SC1BDL 00D1 SCI Baud Rate Control Register Low SC1CR1 00D2 SCI Control Register 1 SC1CR2 00D3 SCI Control Register 2 SC1SR1 00D4 SCI Status Register 1 SC1SR2 00D5 SCI Status Register 2 SC1DRH 00D6 SCI Data Register High SC1DRL 00D7 SCI Data Register Low

Page 6: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

D7 D6 D5 D4 D3 D2 D1 D0

D7 D6 D5 D4 D3 D2 D1 D0

Receive Data Register (RDR)

Transmit Data Register (TDR)

SCI Status Register 1 (SCxSR1)

SCI Baud Rate Control H (SCxBDH)

SCI Control Register 2 (SCxCR2)

Transmit Shift Register

Receive Shift Register

Serial Data Out

Serial Data In

TxD

RxD

SCI Control Register 1 (SCxCR1)

D8

D8

SCxDRL

PS1 (PS3)

PS0 (PS2)

SCI Status Register 2 (SCxSR2)

SCI Baud Rate Control L (SCxBDL)

SCI Data Register H (SCxDRH)

Fig. 11.2 Functional diagram of the Serial Communications Interface (SCI)

Page 7: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00CF $00D7

R7/T7 R6/T6 R5/T5 R4/T4 R3/T3 R2/T2 R1/T1 R0/T0 SCxDRL

Read: Reads the receive data buffer Write: Writes to the transmit data buffer

Figure 11.3 The SCI Data Register Low

Page 8: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00CC $00D4

TDRE TC RDRF IDLE OR NF FE PF SCxSR1

TDRE: Transmit Data Register Empty Flag 0 – SCxDR busy 1 – Transmit data can be written to SCxDR

TC: Transmit Complete Flag 0 – Transmitter busy 1 – Transmitter is idle

RDRF: Receive Data Register Full Flag 0 – SCxDR empty. Cleared by SCxSR1 read with RDRF set, followed by SCxDR read 1 – SCxDR full. Received character can be read from SCxDR

IDLE: Idle Line Detected Flag 0 – RxD line is idle 1 – RxD line is active

OR: Overrun Error Flag 0 – No overrun 1 – Overrun detected

NF: Noise Error Flag 0 – Unanimous decision 1 – Noise on a valid start bit, any data bit, or the stop bit

FE: Framing Error Flag 0 – Stop bit detected 1 – Zero detected instead of a stop bit

PF: Parity Error Flag 0 – Correct parity 1 – Incorrect parity

Figure 11.4 The SCI Status Register 1

Page 9: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00CA $00D2

LOOPS WOMS RSRC M WAKE ILT PE PT SCxCR1

LOOPS: SCI Loop Mode/Single Wire Mode Enable 0 – Normal operation of TxD and RxD 1 – LOOP mode or single wire mode enabled

WOMS: Wire-Or Mode for Serial Pins 0 – Pins operate in normal mode 1 – Pins declared as outputs operate in open drain fashion

RSRC: Receiver Source (LOOPS = 1) 0 – Receiver input connected to transmitter internally (not TxD pin) 1 – Receiver input connected to TxD pin (single wire mode)

M: Mode (Select Character Format) 0 – 8-bit data 1 – 9-bit data

WAKE: Wakeup by Address Mark/Idle 0 – Wakeup by IDLE line recognition 1 – Wakeup by address mark (most significant data bit set)

ILT: Idle Line Type 0 – Short idle line mode 1 – Long idle line mode

PE: Parity Enable 0 – Parity is disabled 1 – Parity is enabled

PT: Parity Type 0 – Even parity 1 – Odd parity

Figure 11.5 The SCI Control Register 1

Page 10: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00CE $00D6

R8 T8 0 0 0 0 0 0 SCxDRH

R8: Receive Data Bit 8 If M bit in SCxCR1 is set, R8 stores the ninth bit of the received data

T8: Transmit Data Bit 8 If M bit in SCxCR1 is set, T8 stores the ninth bit of the transmitted data

Figure 11.6 The SCI Data Register High

Page 11: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00CB $00D3

TIE TCIE RIE ILIE TE RE RWU SBK SCxCR2

TIE: Transmit Interrupt Enable 0 – TDRE interrupts disabled 1 – TDRE interrupts enabled

TCIE: Transmit Complete Interrupt Enable 0 – TC interrupts disabled 1 – TC interrupts enabled

RIE: Receiver Interrupt Enable 0 – RDRF interrupts disabled 1 – RDRF and OR interrupts enabled

ILIE: Idle-Line Interrupt Enable 0 – IDLE interrupts disabled 1 – IDLE interrupts enabled

TE: Transmitter Enable 0 – Transmitter disabled 1 – Transmitter enabled

RE: Receiver Enable 0 – Receiver disabled 1 – Receiver enabled

RWU: Receiver Wakeup Control 0 – Normal SCI Receiver 1 – Wakeup enabled and receiver interrupts inhibited

SBK: Send Break 0 – Break generator off 1 – Break codes generated as long as SBK = 1

Figure 11.7 The SCI Control Register 2

Page 12: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00C8 $00D0

BTST BSPL BRDL SBR12 SBR11 SBR10 SBR9 SBR8 SCxBDH

7 6 5 4 3 2 1 0 $00C9 $00D1

SBR7 SBR6 SBR5 SBR4 SBR3 SBR2 SBR1 SBR0 SCxBDL

SBR[12:0]: BR divisor; SCI Baud Rate = MCLK / (16 x BR )

Figure 11.8 The SCI Baud Rate Control Register

Table 11.3 Baud Rate SelectionDesired

SCI Baud RateBR Divisor for

MCLK = 8.0 MHz110 4545300 1667600 8331200 4172400 2084800 1049600 5214400 3519200 2638400 13

Page 13: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

WHYP Kernel; 68HC12 KERNEL FOR WHYP12; Modified July, 2002 for the MC9S12DP256; Modified January, 2003 to add EEPROM words; Copyright 1998 by Richard E. Haskell; CSE Dept., Oakland University; Rochester, Michigan 48309 ***************************************************************** Change the following five addresses ***************** for a particular 68HC12 environment ******************************************************************REGBASE EQU $0000 ;register baseRAMBASE EQU $1100 ;ram baseEESTART EQU $0400 ;start address of EEPROMEESTOP EQU $0FFF ;stop address of EEPROMWHYPBASE EQU $4000 ;start of WHYP kernel ******** To make a turnkey system change START *************** to the address of your main word. *************** The default, MN1, will run WHYP. ******************************************************************START EQU MN1

Page 14: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

**** NOTE: SCI0 on MC9S12DP256 uses $C9-$CF instead of $C1-$C7 ***************************************************************SC0BDL EQU REGBASE+$C9 ;baud rate controlSC0CR1 EQU REGBASE+$CA ;SCI control reg 1SC0CR2 EQU REGBASE+$CB ;SCI control reg 2SC0SR EQU REGBASE+$CC ;SCI status regSC0DR EQU REGBASE+$CF ;SCI data reg COPCTL EQU REGBASE+$3C ;COP control reg****** MC9S12DP256 uses different method to program EEPROM *******EEPROT EQU REGBASE+$114 ;EEPROM Block Protect regESTAT EQU REGBASE+$115 ;EEPROM status regECMD EQU REGBASE+$116 ;EEPROM programming command regRDRF EQU $20 ;SCSR maskSTACK EQU RAMBASE+$100 ;system (return) stack DATSTK EQU RAMBASE+$80 ;data stack ****** WHYP System Variables ******SP0 EQU RAMBASE ;initial data stack pointerRP0 EQU RAMBASE+$02 ;initial return stack pointerLAST EQU RAMBASE+$04 ;address called by CREATE

Page 15: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

WHYP Kernel

; INITIALIZE SCIINITSER

CLR SCXCR1 ;8 bitLDD #52STD SCXBDH ;9600 baudLDAA #$0CSTAA SCXCR2 ;enable tx & rx

NOOP RTS

; INPUT BYTE FROM SERIAL PORT INTO AINCHAR

LDAA SCXSR1 ;check statusANDA #RDRF ;check rdrfBEQ INCHAR ;wait for charLDAA SCXDRL ;get char in ARTS

; OUTPUT BYTE IN A TO SERIAL PORTOUTPUT TST SCXSR1

BPL OUTPUT ;loop until tdreSTAA SCXDRL ;send ARTS

Page 16: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

SCI

• Asynchronous Serial I/O

• The 68HC12 SCI Interface

• Programming the SCI in WHYP

• SCI Interface Using Interrupts

Page 17: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

\ Serial Communications Interface - SCIHEX00C8 CONSTANT SC0BDH \ SCI Baud Rate Control00CA CONSTANT SC0CR1 \ SCI Control Register 100CB CONSTANT SC0CR2 \ SCI Control Register 200CC CONSTANT SC0SR1 \ SCI Status Register 100CF CONSTANT SC0DRL \ SCI Data (Read RDR, Write TDR)

Registers needed to program SCI0

Page 18: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

: SCI0.INIT ( -- )0 SC0CR1 C! \ 8 data bits0C SC0CR2 C! \ enable TE & RE34 SC0BDH ! ; \ 9600 BAUD

Figure 11.15 WHYP word to initialize SCI0

7 6 5 4 3 2 1 0 $00CA $00D2

LOOPS WOMS RSRC M WAKE ILT PE PT SCxCR1

7 6 5 4 3 2 1 0 $00CB $00D3

TIE TCIE RIE ILIE TE RE RWU SBK SCxCR2

7 6 5 4 3 2 1 0 $00C8 $00D0

BTST BSPL BRDL SBR12 SBR11 SBR10 SBR9 SBR8 SCxBDH

7 6 5 4 3 2 1 0 $00C9 $00D1

SBR7 SBR6 SBR5 SBR4 SBR3 SBR2 SBR1 SBR0 SCxBDL

SBR[12:0]: BR divisor; SCI Baud Rate = MCLK / (16 x BR )

Page 19: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

7 6 5 4 3 2 1 0 $00CF $00D7

R7/T7 R6/T6 R5/T5 R4/T4 R3/T3 R2/T2 R1/T1 R0/T0 SCxDRL

Read: Reads the receive data buffer Write: Writes to the transmit data buffer

7 6 5 4 3 2 1 0 $00CC $00D4

TDRE TC RDRF IDLE OR NF FE PF SCxSR1

: TX! ( c -- ) \ send characterBEGIN 7 SC0SR1 ?HIUNTILSC0DRL C! ;

: ?RX ( -- c tf | ff ) \ receive character5 SC0SR1 ?HIIF SC0DRL C@ TRUEELSE FALSETHEN ;

Page 20: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

DECIMALCREATE BAUD.RATE

110 , 300 , 600 , 1200 , 2400 , 4800 ,9600 , 14400 , 19200 , 38400 ,

CREATE BR.DIVISOR4545 , 1667 , 833 , 417 , 208 , 104 ,52 , 35 , 26 , 13 ,

: FIND.N ( imax n -- ff | index tf )0 SWAP ROT \ 0 n imax0 DO \ 0 n

DUP I TABLE \ 0 n n ix pfaSWAP 2* + \ 0 n n pfa+2*ix@ = \ 0 n fIF \ 0 n DROP I TRUE \ 0 ix tf ROT LEAVE \ ix tf 0THEN

LOOP \ 0 nDROP ; \ 0 | ix tf

: BAUD ( n -- ) ( Ex. 1200 BAUD )10 SWAP FIND.NIF 2* BR.DIVISOR + @ \ BR SC0BDH !THEN ;

Figure 11.17 WHYP word to set the baud rate

Page 21: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

Communicating with a PC

loop: if key has been pressedthen send character to remote computerif remote computer has sent characterthen display character

repeat loop

Figure 11.18 Algorithm for a dumb terminal

Page 22: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

\ Serial Communications Interface - SCI File: SCIECHO.WHPHEX00C8 CONSTANT SC0BDH \ SCI0 Baud Rate Control00CA CONSTANT SC0CR1 \ SCI0 Control Register 100CB CONSTANT SC0CR2 \ SCI0 Control Register 200CC CONSTANT SC0SR1 \ SCI0 Status Register 100CF CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR) : SCI0.INIT ( -- )

0 SC0CR1 C! \ 8 DATA BITS 0C SC0CR2 C! \ ENABLE TE & RE 34 SC0BDH ! ; \ 9600 BAUD

 : TX! ( c -- ) \ send character

BEGIN 7 SC0SR1 ?HIUNTILSC0DRL C! ;

 : ?RX ( -- c tf | ff ) \ receive character

5 SC0SR1 ?HIIF SC0DRL C@ TRUEELSE FALSETHEN ;

 : sci.echo ( -- )

SCI0.INITBEGIN BEGIN ?RX UNTIL TX!AGAIN ;

Page 23: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

Testing SCI1 with the LOOP Function\ Serial Communications Interface - SCI File: SCILOOP.WHP\ Works on a 9S12DP256 using the SCI1 portHEX00D0 CONSTANT SC1BDH \ SCI1 Baud Rate Control00D2 CONSTANT SC1CR1 \ SCI1 Control Register 100D3 CONSTANT SC1CR2 \ SCI1 Control Register 200D4 CONSTANT SC1SR1 \ SCI1 Status Register 100D7 CONSTANT SC1DRL \ SCI1 Data (Read RDR, Write TDR) : SCI1.INIT ( -- )

80 SC1CR1 C! \ 8 DATA BITS - LOOP mode 0C SC1CR2 C! \ ENABLE TE & RE 34 SC1BDH ! ; \ 9600 BAUD

 : TX1! ( c -- ) \ send character

BEGIN 7 SC1SR1 ?HIUNTILSC1DRL C! ;

 : ?RX1 ( -- c tf | ff ) \ receive character

5 SC1SR1 ?HIIF SC1DRL C@ TRUEELSE FALSETHEN ;

Page 24: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

 DECIMAL CREATE BAUD.RATE

110 , 300 , 600 , 1200 , 2400 , 4800 ,9600 , 14400 , 19200 , 38400 ,

 CREATE BR.DIVISOR

4545 , 1667 , 833 , 417 , 208 , 104 ,52 , 35 , 26 , 13 ,

 : FIND.N ( imax n -- ff | index tf )

0 SWAP ROT \ 0 n imax0 DO \ 0 n

DUP I BAUD.RATE \ 0 n n ix pfaSWAP 2* + \ 0 n n pfa+2*ix@ = \ 0 n fIF \ 0 n DROP I TRUE \ 0 ix tf ROT LEAVE \ ix tf 0THEN

LOOP \ 0 nDROP ; \ 0 | ix tf

 

Page 25: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

 : BAUD ( n -- ) ( Ex. 1200 BAUD )

cr dup u.10 SWAP FIND.NIFcr dup . 2* BR.DIVISOR + @ \ BR cr dup . SC1BDH !THEN ;

 : sci1.test ( -- )

ASCII A \ c26 FOR DUP TX1! \ c BEGIN \ wait for char ?RX1 UNTIL \ c c EMIT \ c 1+ \ c'NEXT DROP ;

 

Page 26: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

\ Serial Communications Interface - SCI 9S12DP256\ File: SCIREG.WHP LOAD HEX2ASC.WHPHEX00C8 CONSTANT SC0BDH \ SCI0 Baud Rate Control00CA CONSTANT SC0CR1 \ SCI0 Control Register 100CB CONSTANT SC0CR2 \ SCI0 Control Register 200CC CONSTANT SC0SR1 \ SCI0 Status Register 100CF CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR)0250 CONSTANT PTM \ Port M0258 CONSTANT PTP \ Port P0260 CONSTANT PTH \ Port H0268 CONSTANT PTJ \ Port J0240 CONSTANT PTT \ Port T : SCI0.INIT ( -- )

0 SC0CR1 C! \ 8 DATA BITS 0C SC0CR2 C! \ ENABLE TE & RE 34 SC0BDH ! ; \ 9600 BAUD

 : TX! ( c -- ) \ send character

BEGIN 7 SC0SR1 ?HIUNTILSC0DRL C! ;

 : ?RX ( -- c tf | ff ) \ receive character

5 SC0SR1 ?HIIF SC0DRL C@ TRUEELSE FALSETHEN ;

 

Page 27: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

 : send.reg ( n -- )

DUP 4 RSHIFT \ high nibblehex2asc TX!0F AND \ low nibblehex2asc TX! ;

 : send.regs ( -- )

SCI0.INITBEGIN ?RX IF ASCII R = IF 0D TX! \ send CR PTM C@ send.reg \ send Port M 0D TX! \ send CR PTP C@ send.reg \ send Port P 0D TX! \ send CR PTH C@ send.reg \ send Port H 0D TX! \ send CR PTJ C@ send.reg \ send Port J 0D TX! \ send CR PTT C@ send.reg \ send Port T 0D TX! \ send CR THEN THENAGAIN ;

 DECIMAL 

Page 28: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

\ SCI Interface using interrupts File: SCIINT.WHP\ 9S12DP256LOAD HEX2ASC.WHPLOAD QUEUE.WHP HEX00C8 CONSTANT SC0BDH \ SCI0 Baud Rate Control00CA CONSTANT SC0CR1 \ SCI0 Control Register 100CB CONSTANT SC0CR2 \ SCI0 Control Register 200CC CONSTANT SC0SR1 \ SCI0 Status Register 100CF CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR)0250 CONSTANT PTM \ Port M0258 CONSTANT PTP \ Port P0260 CONSTANT PTH \ Port H0268 CONSTANT PTJ \ Port J0240 CONSTANT PTT \ Port T3FD6 CONSTANT SCI0.IVEC \ SCI0 user vector address : SCINT.INIT ( -- )

0 SC0CR1 C! \ 8 DATA BITS 2C SC0CR2 C! \ ENABLE TE & RE, RX INT 34 SC0BDH C! ; \ 9600 BAUD

 INT: SCI.INTSER ( -- )

5 SC0SR1 ?HI \ read to clear RDRF flagIF SC0DRL C@ QSTORETHEN

RTI;

Page 29: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

: SET.SCI.INTVEC ( -- ) [ ' SCI.INTSER ] LITERAL SCI0.IVEC ! ;

: send.reg ( n -- )DUP 4 RSHIFT \ high nibblehex2asc TX!0F AND \ low nibblehex2asc TX! ;

\ Transmit all registers when 'R' is received: main.SCIports ( -- )

SEIINITQSCINT.INITSET.SCI.INTVECCLI BEGIN CHECKQ IF 52 = \ if R IF 0D TX! \ send CR PTM C@ send.reg \ send Port M 0D TX! \ send CR PTP C@ send.reg \ send Port P 0D TX! \ send CR PTH C@ send.reg \ send Port H 0D TX! \ send CR PTJ C@ send.reg \ send Port J 0D TX! \ send CR PTT C@ send.reg \ send Port T 0D TX! \ send CR THEN THENAGAIN ;

Page 30: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

Master-Slave SCI Communications\ MASTER - SEND 9-BIT REQUEST TO SLAVE 1 FOR ADCONV READHEX00C8 CONSTANT SC0BDH \ SCI0 Baud Rate Control00CA CONSTANT SC0CR1 \ SCI0 Control Register 100CB CONSTANT SC0CR2 \ SCI0 Control Register 200CC CONSTANT SC0SR1 \ SCI0 Status Register 100CE CONSTANT SC0DRH \ SCI0 Data HI (R8, T8)00CF CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR) : SCINIT.MASTER ( -- )

10 SC0CR1 C! \ 9 data bits 40 SC0DRH C! \ set T8 0C SC0CR2 C! \ ENABLE TE & RE 34 SC0BDH C! ; \ 9600 BAUD

 : MAIN.MASTER ( -- )

SCINIT.MASTERBEGIN 1 TX! \ send 1 with T8 set BEGIN 5 SC0SR1 ?HI UNTIL SC0DRL C@ . CRAGAIN ;

Page 31: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

\ SCI SLAVE - INT ROUTINE WAKE UP - READ ADCONV - SEND LOAD ATD.WHP \ for ADCONVLOAD SCIREG.WHP \ SCI registers : SCINIT.SLAVE ( -- )

18 SC0CR1 C! \ 9 DATA BITS - ADDR MARK WAKE2E SCCR2 C! \ ENABLE TE & RE, REI 34 SC0BDH C! ; \ 9600 BAUD

 INT: SCISL.INTSER ( -- )

5 SC0SR1 ?HIIF SC0DRL C@ 1 = \ if addr = 1 IF 2 ADCONV \ read A/D TX! \ and send it ELSE \ else 1 SC0CR2 HI \ go back to sleep THENTHEN

RTI; : SET.SCISL.INTVEC ( -- )

[ ' SCISL.INTSER ] LITERAL SCI.IVEC ! ;

: MAIN.SLAVE ( -- )SEISCINIT.SLAVESET.SCISL.INTVECCLI ;

Page 32: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

SummaryBox 11.1 WHYP Words Introduced in Chapter 11

ASCII ( -- ascii_code )Used to find the ASCII code of characters.The statement ASCII <char> will leave the ASCII code of <char> on the data stack.For example, ASCII A, will leave a $41 on the data stack.

CONTROL ( -- control_code )Used to find the ASCII code of control characters.The statement CONTROL <char> will leave the ASCII code of ^<char> on the data stack.For example, CONTROL G, will leave a $07 on the data stack.

EMIT ( c -- )Displays the character whose ASCII code is c on the PC screen.

Page 33: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

ExercisesExercise 11.6

Assume a C++ (or other language such as Visual Basic) is running on the PC andwants to access the external world through a 68HC12 connected to the PC through anasynchronous serial line. This can be done by sending an 8-bit "opcode" to the 68HC12SCI port followed by possible parameters and then receiving possible data from the68HC12. For example, suppose the C++ program wants to read the hex keypad shown inFigure 6.12 that is connected to a 68HC12. The C++ function int GetHexKey() might dothis by sending the opcode $41 (ASCII code for 'A') out the serial port and then wait forthe hex key value to be sent from the 68HC12 to the PC. Meanwhile the program runningon the 68HC12 will wait to receive an opcode in the SCI0 port. If it receives a $41 it willwait for a key on the keypad to be pressed by calling the word keypad ( -- n ) given inFigure 6.15, and then send the key value, n, out the SCI0 port to the PC. We willdescribe this program running of the 68HC12 by the WHYP word Get HexKey ( -- ) {$41 -- c } where the braces contain a description of the bytes sent by the PC and the bytessent to the PC according to the format { <bytes from PC to 68HC12> -- <bytes from68HC12 to PC> }. The following examples will use a different opcode to performdifferent 68HC12 I/O operations. When sending 16-bit data from the PC to the 68HC12,send the low byte first. When sending 16-bit data from the 68HC12 to the PC, send thehigh byte first.

Page 34: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

a. Opcode = $41 ('A'): Write a WHYP word called Get HexKey ( -- ) { $41 -- c }that will wait for a key to be pressed in response to the opcode $41 and send thehex key value to the PC through the SCI0 port. Use the hex keypad shown inFigure 7.12.

b. Opcode = $42 ('B'): Write a WHYP word called DotDigit ( -- ) { $42 dig# n -- }that will display the value n (0-F) on the 7-segment digit number dig#. Use theword .digit ( dig# n -- ) shown in Figure 6.11 for the connection shown in Figure7.10.

c. Opcode = $43 ('C'): Write a WHYP word called Hex2LCD ( -- ) { $43 hex -- }that will display the hex number hex on the LCD shown in Figure 6.22. Use theword hex>lcd ( hex -- ) given in Listing 7.11.

d. Opcode = $44 ('D'): Write a WHYP word called Dot4Leds ( -- ) { $44 n -- } thatwill display the decimal number n on up to four 7-segment displays shown inFigure 8.10. Use the word .4leds ( n -- ) given in Figure 8.11.

Page 35: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

e. Opcode = $45 ('E'): Write a WHYP word called D2A ( -- ) { $45 n -- } that willoutput a voltage on the D/A converter described in Exercise 7.3 that isproportional to the lower six bit of the byte n. Use the word DACONV ( n -- )found in Exercise 8.3b.

f. Opcode = $46 ('F'): Write a WHYP word called Write_EEPROM ( -- ) { $46addr #bytes <bytes> -- } that will write the #byte (up to 32) bytes, <bytes>,starting at address, addr, to the 25C320 serial EEPROM described in Exercise 8.6.

g. Opcode = $47 ('G'): Write a WHYP word called Read_EEPROM ( -- ) { $47addr #bytes -- <bytes> } that will read the #bytes bytes, <bytes>, starting ataddress, addr, from the 25C320 serial EEPROM described in Exercise 8.6 andsend these bytes to the PC.

h. Opcode = $48 ('H'): Write a WHYP word called SetR ( -- ) { $48 n -- } that willset the resistance of the AD7376 Digital Potentiometer described in Exercise 8.7to n ohms.

i. Opcode = $49 ('I'): Write a WHYP word called Record_ChipCorder ( -- ) { $49 -- } that will allow you to record a message (starting at address 00) on theISD4004 ChipCorder described in Exercise 8.8.

Page 36: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

j. Opcode = $4A ('J'): Write a WHYP word called Play_ChipCorder ( -- ) { $4A --} that will allow you to play a message (starting at address 00) on the ISD4004ChipCorder described in Exercise 8.8.

k. Opcode = $4B ('K'): Write a WHYP word called ReadCompass ( -- ) { $4B --deg } that will read the compass direction (0 - 360° ) for the compass transducershown in Figures 8.9 and 8.10. Use the word read.compass ( -- deg ) given inListing 9.3.

l. Opcode = $4C ('L'): Write a WHYP word called GetTemp ( -- ) { $4C -- temp }that will read the temperature (in degrees Celsius) using the temperature sensordescribed in either Exercise 9.2 or 9.3.

m. Opcode = $4D ('M'): Write a WHYP word called GetLevelDegree ( -- ) { $4D --angle } that will read the tilt angle, in tenths of a degree, using the ADXL05accelerometer described in Exercise 9.5.

n. Opcode = $4E ('N'): Write a WHYP word called GetPressure ( -- ) { $4E -- n }that will read the pressure, in kPa, using the pressure sensor described in eitherExercise 9.7 or 9.8.

Page 37: The Serial Communication Interface (SCI) MC9S12-DP256 Chapter 11

o. Opcode = $4F ('O'): Write a WHYP word called PulseTrain ( -- ) { $4F periodwidth -- } that will generate the pulse train shown in Figure 9.11 on pin PT6. Usethe word PULSE ( -- ) given in Listing 10.2.

p. Opcode = $50 ('P'): Write a WHYP word called GetPulseWidth ( -- ) { $50 --n } that will read the width of a single pulse connected to pin PT2 using the wordPULSE.WIDTH ( -- ) given in Listing 10.3.

q. Opcode = $51 ('Q'): Write a WHYP word called GetLightLevel ( -- ) { $51 -- n }that will read the light intensity using the light sensor described in either Exercise10.6 or 10.7.

r. Opcode = $52 ('R'): Write a WHYP word called GetPitchRoll ( -- ) { $52 --pitch roll } that will read the pitch and roll angles, in tenths of a degree, using theADXL202 Dual Axis Accelerometer described in Exercises 10.8 or 10.9.

s. Opcode = $53 ('S'): Write a WHYP word called GetAngle ( -- ) { $53 -- angle }that will read the angle (0 - 360° ) of a wheel in the vertical plane using theADXL202 Dual Axis Accelerometer as described in Exercise 10.10.