FlexScan Introduction - Lesson 3

by Oliver Scholz

First contact with J1850

Enough with those simple functions, you built FlexScan because you wanted to tinker with a scantool. Well, if you have a DLC installed, you can make "First Contact" with the J1850 bus with this short program.

This program gets a little long, so it's broken down into several parts.

	ORG	050H
buffer: DS	16			;buffer for received message

MAIN:   LCALL   CLS			;clear the screen before we get started

        MOV     DPTR,#buffer		;Initialize the J1850 handler and pass a buffer to it.
        MOV     A,#16			;This is the buffer size which the handler needs to know too
        LCALL   DLC_INIT

        LCALL   DLC_TX_READY		;Check to see if the DLC is ready to accept a message
        LCALL   LCDHEX			;it should be: 00 should be printed on the LCD.

        MOV     DPTR,#T2		;print a message showing the result: "00 after TX_READY"
        LCALL   STROUT
        LCALL   INPUT			;and wait for a keypress before continuing.

Transmitting a J1850 message

Now that the handler has been initialized, and the DLC is ready to accept a message, we can try to talk to the J1850 bus...

        MOV     DPTR,#T5		;Print string "Transmitting"
        LCALL   STROUT

        MOV     DPTR,#TEST_MESSAGE	;Pass a pointer to the test message $AA, $55, $C5
        MOV     A,#0			;return immediately if the DLC is ready
        LCALL   DLC_LOAD_MESSAGE	;DLC will transmit a message as soon as possible

        MOV     DPTR,#T3		;Print string "Message sent!"
        LCALL   STROUT
        LCALL   INPUT			;And wait for a keypress again.

Receiving a J1850 message

The message has been transmitted now, check if we received the echo, and print out the received message.

        LCALL   DLC_RX_READY		;Check if there is a message in the receive buffer
        LCALL   LCDHEX			;Print the result in the form: "xx after RX_READY"
        MOV     DPTR,#T6
        LCALL   STROUT
        LCALL   INPUT			;and wait for a keypress again.

        MOV     A,buffer		;get length byte from buffer
        MOV     R7,A			;store length in R7
        LCALL   LCDHEX			;Dump length byte first

        MOV     R0,#buffer+1		;point to first byte of message
        CJNE    R7,#0,MSGOUT		;check if any more bytes in message
        SJMP    MSGEND

MSGOUT: MOV     A,R0			;save R0
        PUSH    ACC
        MOV     A,@R0			;read next byte from buffer

        LCALL   LCDHEX			;and print it

        POP     ACC			;restore R0
        MOV     R0,A
        INC     R0			;and point to next byte
        DJNZ    R7,MSGOUT		;loop until all bytes are displayed

MSGEND: MOV     A,#'!'			;mark the end of the message
        LCALL   CHROUT
        LCALL   INPUT			;and wait for a keypress

Terminating the J1850 handler

We're all done, terminate the handler and exit.

        LCALL   CRLF			;terminate the J1850 handler
        LCALL   DLC_EXIT

        MOV     DPTR,#T4		;print a goodbye message
        LCALL   STROUT
        LCALL   INPUT			;and wait for a keypress

        MOV     DPTR,#buffer		;A different way to dump the buffer contents (at least 8 bytes)
        LCALL   HEXDUMP			;not as space efficient, but more clearly laid out
        LCALL   CRLF
        LCALL   HEXDUMP

LOOP:   SJMP LOOP

TEST_MESSAGE:   DB   3,0AAH,055H,0C5H

T2: 		DB ' after TX_READY',13,10,0
T3: 		DB 'Message sent!',13,10,0
T4: 		DB 'Deinit complete!',13,10,0
T5: 		DB 'Transmitting...',13,10,0
T6: 		DB ' after RX_READY',13,10,0

A few hints...

This is of course only a rudimentary approach to try some J1850 functions. You need to have a pretty good idea (or better yet: the documentation) of what messages you're dealing with. In any case, you need to be aware of what goes on when the DLC receives a message.

The DLC contains a buffer for a message of up to 20 bytes, but a normal J1850 message is usually under 12 bytes long. Once a complete message has been received, the DLC handler wants to copy it to the user supplied buffer. If the buffer has not been emptied yet, the message is lost. So you need to make sure to check for new messages often and process them as soon as possible. Since the length of a message is variable, you will never know how much time you have before the next message has been received, so make sure to unload a message from the buffer as soon as you can.

Message transmission is not critical, since you control when you have time to transmit a message, and the grunt work is done by the DLC anyway.

To lessen the load on the application you can use message filtering through the DLC_Filter function. This lets the handler discard messages that you are not interested in, and can make your life a lot easier. See an example of message filtering in the next chapter.

Two things the DLC handler does not support at this time are 4x mode (transmission of messages at four times the normal speed, usually used for flashing a new program into some module) and block download (i.e. one contiguous long message, for the same purpose). The reason these are not supported is that FlexScan has too little RAM for any download extravganza, so I tried to keep things simple.


Back to lesson 2 Click here to go to lesson 4

There have been visitors to this site since May 31, 2000.