The Serial Communication Interface (SCI)

40
The Serial Communication Interface (SCI) Chapter 11

description

The Serial Communication Interface (SCI). Chapter 11. SCI. Asynchronous Serial I/O The 68HC12 SCI Interface Programming the SCI in WHYP SCI Interface Using Interrupts. SCI. Asynchronous Serial I/O The 68HC12 SCI Interface Programming the SCI in WHYP SCI Interface Using Interrupts. - PowerPoint PPT Presentation

Transcript of The Serial Communication Interface (SCI)

Page 1: The Serial Communication Interface (SCI)

The Serial Communication Interface (SCI)

Chapter 11

Page 2: The Serial Communication Interface (SCI)

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)

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)

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)

Table 11.2 SCI Registers in the 68HC812A4Name Register Addr DescriptionSC0BDH 00C0 SCI Baud Rate Control Register HighSC0BDL 00C1 SCI Baud Rate Control Register LowSC0CR1 00C2 SCI Control Register 1SC0CR2 00C3 SCI Control Register 2SC0SR1 00C4 SCI Status Register 1SC0SR2 00C5 SCI Status Register 2SC0DRH 00C6 SCI Data Register HighSC0DRL 00C7 SCI Data Register LowSC1BDH 00C8 SCI Baud Rate Control Register HighSC1BDL 00C9 SCI Baud Rate Control Register LowSC1CR1 00CA SCI Control Register 1SC1CR2 00CB SCI Control Register 2SC1SR1 00CC SCI Status Register 1SC1SR2 00CD SCI Status Register 2SC1DRH 00CE SCI Data Register HighSC1DRL 00CF SCI Data Register Low

Page 6: The Serial Communication Interface (SCI)

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)

7 6 5 4 3 2 1 0$00C7(F) R7/T7 R6/T6 R5/T5 R4/T4 R3/T3 R2/T2 R1/T1 R0/T0 SCxDRL

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

Figure 11.3 The SCI Data Register Low

Page 8: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$00C4(C) TDRE TC RDRF IDLE OR NF FE PF SCxSR1

TDRE: Transmit Data Register Empty Flag0 – SCxDR busy1 – Transmit data can be written to SCxDR

TC: Transmit Complete Flag0 – Transmitter busy1 – Transmitter is idle

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

IDLE: Idle Line Detected Flag0 – RxD line is idle1 – RxD line is active

OR: Overrun Error Flag0 – No overrun1 – Overrun detected

NF: Noise Error Flag0 – Unanimous decision1 – Noise on a valid start bit, any data bit, or the stop bit

FE: Framing Error Flag0 – Stop bit detected1 – Zero detected instead of a stop bit

PF: Parity Error Flag0 – Correct parity1 – Incorrect parity

Figure 11.4 The SCI Status Register 1

Page 9: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$00C2(A) LOOPS WOMS RSRC M WAKE ILT PE PT SCxCR1

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

WOMS: Wire-Or Mode for Serial Pins0 – Pins operate in normal mode1 – 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 data1 – 9-bit data

WAKE: Wakeup by Address Mark/Idle0 – Wakeup by IDLE line recognition1 – Wakeup by address mark (most significant data bit set)

ILT: Idle Line Type0 – Short idle line mode1 – Long idle line mode

PE: Parity Enable0 – Parity is disabled1 – Parity is enabled

PT: Parity Type0 – Even parity1 – Odd parity

Figure 11.5 The SCI Control Register 1

Page 10: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$00C6(E) R8 T8 0 0 0 0 0 0 SCxDRH

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

T8: Transmit Data Bit 8If 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)

7 6 5 4 3 2 1 0$00C3(B) TIE TCIE RIE ILIE TE RE RWU SBK SCxCR2

TIE: Transmit Interrupt Enable0 – TDRE interrupts disabled1 – TDRE interrupts enabled

TCIE: Transmit Complete Interrupt Enable0 – TC interrupts disabled1 – TC interrupts enabled

RIE: Receiver Interrupt Enable0 – RDRF interrupts disabled1 – RDRF and OR interrupts enabled

ILIE: Idle-Line Interrupt Enable0 – IDLE interrupts disabled1 – IDLE interrupts enabled

TE: Transmitter Enable0 – Transmitter disabled1 – Transmitter enabled

RE: Receiver Enable0 – Receiver disabled1 – Receiver enabled

RWU: Receiver Wakeup Control0 – Normal SCI Receiver1 – Wakeup enabled and receiver interrupts inhibited

SBK: Send Break0 – Break generator off1 – Break codes generated as long as SBK = 1

Figure 11.7 The SCI Control Register 2

Page 12: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$00C0(8) BTST BSPL BRDL SBR12 SBR11 SBR10 SBR9 SBR8 SCxBDH

7 6 5 4 3 2 1 0$00C1(9) 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)

***************************************************************** Change the following five addresses ***************** for a particular 68HC12 environment ******************************************************************REGBASE EQU $0000 ;register baseRAMBASE EQU $0900 ;ram baseEESTART EQU $1000 ;start address of EEPROMEESTOP EQU $1FFF ;stop address of EEPROMWHYPBASE EQU $4000 ;start of WHYP kernel

******** To use SCI1 instead of SCI0 change $C0-$C7 *************** in the following five lines to $C8-$CF ******************************************************************SCXBDH EQU REGBASE+$C0 ;baud rate controlSCXCR1 EQU REGBASE+$C2 ;SCI control reg 1SCXCR2 EQU REGBASE+$C3 ;SCI control reg 2SCXSR1 EQU REGBASE+$C4 ;SCI status regSCXDRL EQU REGBASE+$C7 ;SCI data regRDRF EQU $20 ;SCXSR1 mask

WHYP Kernel

Page 14: The Serial Communication Interface (SCI)

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 15: The Serial Communication Interface (SCI)

Table 11.4 The 68HC11 SCI RegistersName Register Addr DescriptionBAUDRC 102B SCI Baud Rate Control RegisterSCCR1 00C2 SCI Control Register 1SCCR2 00C3 SCI Control Register 2SCSR 00C4 SCI Status RegisterSCDR 00CF SCI Data Register (Read RDR, Write TDR)

The 68HC11 SCI Registers

Page 16: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$102F R7/T7 R6/T6 R5/T5 R4/T4 R3/T3 R2/T2 R1/T1 R0/T0 SCDR

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

7 6 5 4 3 2 1 0$102E TDRE TC RDRF IDLE OR NF FE 0 SCSR

TDRE: Transmit Data Register Empty Flag0 – Cleared by SCSR read with TDRE set, followed by SCDR write1 – Transmit data can be written to SCDR

RDRF: Receive Data Register Full Flag0 – Cleared by SCSR read with RDRF set, followed by SCDR read1 – Received character can be read from SCDR

Figure 11.10 The 68HC11 SCI Data and Status Registers

Page 17: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$102C R8 T8 0 M WAKE 0 0 0 SCCR1

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

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

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

WAKE: Wakeup by Address Mark/Idle0 – Wakeup by IDLE line recognition1 – Wakeup by address mark (most significant data bit set)

Figure 11.11 The 68HC11 SCI Control Register 1

7 6 5 4 3 2 1 0$102D TIE TCIE RIE ILIE TE RE RWU SBK SCCR2

Figure 11.12 The 68HC11 SCI Control Register 2

Page 18: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$102B TCLR 0 SCP1 SCP0 RCKB SCR2 SCR1 SCR0 BAUDRC

SCP1, SCP0: SCI Baud Rate Prescaler Selects (Specifies highest possible baud rate)

SCP[1:0] Divide InternalClock by

Maximum Baud Rate(8 MHz Crystal Freq.)

0 0 1 125.0K0 1 3 41.67K1 0 4 31.25K1 1 13 9600

SCR[2:0]: SCI Baud Rate Selects (Subdivides highest possible baud rate)

SCR[2:0] Divide Prescalarby

Baud Rate(9600 Max. Baud Rate)

0 0 0 1 96000 0 1 2 48000 1 0 4 24000 1 1 8 12001 0 0 16 6001 0 1 32 3001 1 0 64 1501 1 1 128 75

Figure 11.13 The SCI 68HC11 Baud Rate Control Register

Page 19: The Serial Communication Interface (SCI)

SCI

• Asynchronous Serial I/O

• The 68HC12 SCI Interface

• Programming the SCI in WHYP

• SCI Interface Using Interrupts

Page 20: The Serial Communication Interface (SCI)

\ Serial Communications Interface - SCIHEX00C0 CONSTANT SC0BDH \ SCI Baud Rate Control00C2 CONSTANT SC0CR1 \ SCI Control Register 100C3 CONSTANT SC0CR2 \ SCI Control Register 200C4 CONSTANT SC0SR1 \ SCI Status Register 100C7 CONSTANT SC0DRL \ SCI Data (Read RDR, WriteTDR)

Figure 11.14 The SCI register names needed to program SCI0

Page 21: The Serial Communication Interface (SCI)

: 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$00C2(A) LOOPS WOMS RSRC M WAKE ILT PE PT SCxCR1

7 6 5 4 3 2 1 0$00C3(B) TIE TCIE RIE ILIE TE RE RWU SBK SCxCR2

7 6 5 4 3 2 1 0$00C0(8) BTST BSPL BRDL SBR12 SBR11 SBR10 SBR9 SBR8 SCxBDH

7 6 5 4 3 2 1 0$00C1(9) SBR7 SBR6 SBR5 SBR4 SBR3 SBR2 SBR1 SBR0 SCxBDL

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

Page 22: The Serial Communication Interface (SCI)

7 6 5 4 3 2 1 0$00C7(F) R7/T7 R6/T6 R5/T5 R4/T4 R3/T3 R2/T2 R1/T1 R0/T0 SCxDRL

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

7 6 5 4 3 2 1 0$00C4(C) 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 23: The Serial Communication Interface (SCI)

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 24: The Serial Communication Interface (SCI)

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 25: The Serial Communication Interface (SCI)

Listing 11.1 The file SCIECHO.WHP\ Serial Communications Interface - SCIHEX00C0 CONSTANT SC0BDH \ SCI0 Baud Rate Control00C2 CONSTANT SC0CR1 \ SCI0 Control Register 100C3 CONSTANT SC0CR2 \ SCI0 Control Register 200C4 CONSTANT SC0SR1 \ SCI0 Status Register 100C7 CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR)

: SCI0.INIT ( -- )0 SC0CR1 C! \ 8 DATA BITS0C SC0CR2 C! \ ENABLE TE & RE34 SC0BDH ! ; \ 9600 BAUD

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

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

: sci.echo ( -- )SCI0.INITBEGIN BEGIN ?RX UNTIL TX!AGAIN ;

Page 26: The Serial Communication Interface (SCI)

Listing 11.2 The file SCILOOP.WHP\ Serial Communications Interface - SCI\ Works only on a 68HC12A4 using the SCI1 portHEX00C8 CONSTANT SC1BDH \ SCI1 Baud Rate Control00CA CONSTANT SC1CR1 \ SCI1 Control Register 100CB CONSTANT SC1CR2 \ SCI1 Control Register 200CC CONSTANT SC1SR1 \ SCI1 Status Register 100CF CONSTANT SC1DRL \ SCI1 Data (Read RDR, Write TDR)

: SCI1.INIT ( -- )80 SC1CR1 C! \ 8 DATA BITS - LOOP mode0C SC1CR2 C! \ ENABLE TE & RE34 SC1BDH ! ; \ 9600 BAUD

: TX1! ( c -- ) \ send characterBEGIN 7 SC1SR1 ?HIUNTILSC1DRL C! ;

: ?RX1 ( -- c tf | ff ) \ receive character5 SC1SR1 ?HIIF SC1DRL C@ TRUEELSE FALSETHEN ;

Testing SCI1 with the LOOP Function

Page 27: The Serial Communication Interface (SCI)

DECIMAL

CREATE BAUD.RATE110 , 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 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 28: The Serial Communication Interface (SCI)

Listing 11.2 (cont) The file SCILOOP.WHP

: BAUD ( n -- ) \ Ex. 1200 BAUD10 SWAP FIND.NIF BR.DIVISOR + @ \ BR SC1BDH !THEN ;

: sci1.test ( -- )ASCII A \ c26 FOR DUP TX1! \ c BEGIN \ wait for char ?RX1 UNTIL \ c c EMIT \ c 1+ \ c'NEXTDROP ;

Page 29: The Serial Communication Interface (SCI)

Listing 11.3 The file SCIREG.WHP\ Serial Communications Interface - SCI

LOAD HEX2ASC.WHPHEX00C0 CONSTANT SC0BDH \ SCI0 Baud Rate Control00C2 CONSTANT SC0CR1 \ SCI0 Control Register 100C3 CONSTANT SC0CR2 \ SCI0 Control Register 200C4 CONSTANT SC0SR1 \ SCI0 Status Register 100C7 CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR)0030 CONSTANT PORTF0031 CONSTANT PORTG0024 CONSTANT PORTH0028 CONSTANT PORTJ00AE CONSTANT PORTT

: SCI0.INIT ( -- )0 SC0CR1 C! \ 8 DATA BITS0C SC0CR2 C! \ ENABLE TE & RE34 SC0BDH ! ; \ 9600 BAUD

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

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

Page 30: The Serial Communication Interface (SCI)

: 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 PORTF C@ send.reg \ send Port F 0D TX! \ send CR PORTG C@ send.reg \ send Port G 0D TX! \ send CR PORTH C@ send.reg \ send Port H 0D TX! \ send CR PORTJ C@ send.reg \ send Port J 0D TX! \ send CR PORTT C@ send.reg \ send Port T 0D TX! \ send CR THEN THENAGAIN ;

DECIMAL

Page 31: The Serial Communication Interface (SCI)

Listing 11.4 Using SCI receive interrupts\ SCI Interface using interrupts File: SCIINT.WHP

LOAD HEX2ASC.WHPLOAD QUEUE.WHP

HEX00C0 CONSTANT SC0BDH \ SCI0 Baud Rate Control00C2 CONSTANT SC0CR1 \ SCI0 Control Register 100C3 CONSTANT SC0CR2 \ SCI0 Control Register 200C4 CONSTANT SC0SR1 \ SCI0 Status Register 100C7 CONSTANT SC0DRL \ SCI0 Data (Read RDR, Write TDR)0030 CONSTANT PORTF0031 CONSTANT PORTG0024 CONSTANT PORTH0028 CONSTANT PORTJ00AE CONSTANT PORTT0B16 CONSTANT SCI0.IVEC \ SCI0 user vector address

: SCINT.INIT ( -- )0 SC0CR1 C! \ 8 DATA BITS2C SC0CR2 C! \ ENABLE TE & RE, RX INT34 SC0BDH C! ; \ 9600 BAUD

INT: SCI.INTSER ( -- )5 SC0SR1 ?HI \ read to clear RDRF flagIF SC0DRL C@ QSTORETHEN

RTI;

Page 32: The Serial Communication Interface (SCI)

Listing 11.4 (cont.) Using SCI receive interrupts

: 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.INTVECCLIBEGIN CHECKQ IF 52 = \ if R IF 0D TX! \ send CR PORTF C@ send.reg \ send Port F 0D TX! \ send CR PORTG C@ send.reg \ send Port G 0D TX! \ send CR PORTH C@ send.reg \ send Port H 0D TX! \ send CR PORTJ C@ send.reg \ send Port J 0D TX! \ send CR PORTT C@ send.reg \ send Port T 0D TX! \ send CR THEN THENAGAIN ;

Page 33: The Serial Communication Interface (SCI)

\ MASTER - SEND 9-BIT REQUEST TO SLAVE 1 FOR ADCONV READLOAD SCIREG.WHP \ SCI registers

: SCINIT.MASTER ( -- )10 SC0CR1 C! \ 9 data bits40 SC0DRH C! \ set T80C SC0CR2 C! \ ENABLE TE & RE34 SC0BDH C! ; \ 9600 BAUD

: MAIN.MASTER ( -- )SCINIT.MASTERBEGIN 1 TX! \ send 1 with T8 set BEGIN 5 SC0SR1 ?HI UNTIL SC0DRL C@ . CRAGAIN ;

Figure 11.19 Master program for master-slave SCI example

Master-Slave SCI Communications

Page 34: The Serial Communication Interface (SCI)

\ 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, REI34 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 SC0CR2 1 HI \ go back to sleep THENTHEN

RTI;

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

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

Figure 11.20 Slave program for master-slave SCI example

Page 35: The Serial Communication Interface (SCI)

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 36: The Serial Communication Interface (SCI)

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 37: The Serial Communication Interface (SCI)

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 38: The Serial Communication Interface (SCI)

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 39: The Serial Communication Interface (SCI)

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 40: The Serial Communication Interface (SCI)

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.