1;
2;   The following 8051 code demonstrates reading and writing 92's, 93's, 94's
3; using universal data packets. It transmits the results to the serial port
4; at 9600 bps.
5;
6          EXTRN     CODE(TOUCHRESET, DATA_IN, TOUCHBYTE)
7;
8DATA_BIT  BIT       P1.3                ; Touch Memory data.
9GND_BIT   BIT       P1.4                ; Touch Memory ground.
10;
11PCON      EQU       87H                 ; Define special purpose
12MCON      EQU       0C6H                ;    registers specific
13TA        EQU       0C7H                ;       to the DS5000.
14;
15BUFF      EQU       900H                ; Buffer in external memory.
16;
17CODE_SEG  SEGMENT   CODE                ; Code is in this segment.
18;
19          DSEG AT   20H
20LO:       DS        1                   ; Low byte of CRC.
21;
22          DSEG AT   30H
23HI:       DS        1                   ; High byte of CRC.
24STACK:    DS        1                   ; Stack starts after data.
25;
26          CSEG      AT        RESET     ; Power on reset vector.
27          LJMP      START               ; Program begins here.
28;
29          RSEG      CODE_SEG            ; Code segment begins here.
30START:
31          MOV       TA,       #0AAH     ; Timed
32          MOV       TA,       #55H      ;    access.
33          MOV       PCON,     #0        ; Disable watchdog timer.
34          MOV       TA,       #0AAH     ; Timed
35          MOV       TA,       #55H      ;    access.
36          ORL       MCON,     #2        ; Enable partition access.
37          MOV       MCON,     #12H      ; Set partition at 800H.
38          MOV       TA,       #0AAH     ; Timed
39          MOV       TA,       #55H      ;    access.
40          ANL       MCON,     #0FDH     ; Disable partition access.
41;
42          MOV       SP,       #STACK-1  ; Reset the stack pointer.
43          CLR       GND_BIT             ; Touch Memory ground level.
44;
45          CLR       TR1                 ; Stop the serial timer.
46          MOV       TH1,      #0FAH     ; Set for 9600 bps when using
47          MOV       TL1,      #0FAH     ;    a 11.0592 MHz crystal.
48          ANL       TMOD,     #0FH      ; Set timer for the eight
49          ORL       TMOD,     #20H      ;    bit auto-reload mode.
50          ORL       PCON,     #80H      ; Set double bit rate.
51          MOV       SCON,     #52H      ; Setup the serial port.
52          SETB      TR1                 ; Start the serial timer.
53MAIN_LOOP:
54          JB        DATA_BIT, $         ; Wait for presence start.
55          JNB       DATA_BIT, $         ; Wait for presence end.
56          CALL      KEYMACRO            ; *** Try to read Touch Memory.
57          JNC       MAIN_LOOP           ; No valid packet, try again.
58          LJMP      SPILLIT             ; Send valid data packet to CPU.
59          SJMP      MAIN_LOOP           ; Look for touch memory.
60SPILLIT:
61          MOV       DPTR,     #BUFF     ; Initialize the data pointer.
62          MOV       B,        #1        ; Assume short string.
63          MOVX      A,        @DPTR     ; Get string length.
64          INC       DPTR                ; Increment data pointer.
65          CJNE A,   #0FFH,    LA        ; Branch if short string.
66          INC       B                   ; Set for long string.
67          MOVX      A,        @DPTR     ; Get string length - 255.
68          INC       DPTR                ; Increment data pointer.
69LA:
70          PUSH      B                   ; Set long or short string.
71          MOV       B,        A         ; Set character count.
72          SJMP      LC                  ; Start getting characters.
73LB:
74          PUSH      B                   ; Set long or short string.
75          MOV       B,        #0FFH     ; Do 255 more characters.
76LC:
77          MOVX      A,        @DPTR     ; Get a character.
78          INC       DPTR                ; Increment data pointer.
79          JNB       TI,       $         ; Wait until transmitter not busy.
80          MOV       SBUF,     A         ; Send out character in accumulator.
81          CLR       TI                  ; Indicate transmitter bust.
82          DJNZ      B,        LC        ; Go transmit next character.
83          POP       B                   ; Get extended length.
84          DJNZ      B,        LB        ; Go get next group of 255.
85          CALL      GO_AWAY             ; *** Wait for part to leave.
86          LJMP      MAIN_LOOP           ; Wait for next touch.
87;
88;********************************************************************
89;
90;         Procedure KeyMacro
91;
92;         This procedure executes a debounced call to GetData
93;         and returns with the Carry cleared or set according
94;         to the result of GetData.
95;
96KEYMACRO:
97          MOV       B,        #255      ; Number of trys allowed.
98KEYLA:
99          CALL      GETDATA             ; Try to read the data.
100          JC        KEYLB               ; Continue if successful.
101          DJNZ      B,        KEYLA     ; Otherwise try again.
102KEYLB:
103          RET                           ; Return.
104;
105;         Procedure Go_Away
106;
107;         This procedure insures that the Touch Memory is gone
108;         before returning so that it will not be read more than
109;         once.
110;
111GO_AWAY:
112          MOV       B,        #128      ; Number of trys allowed.
113GO:
114          CALL      TOUCHRESET          ; See if there's a part
115          JC        GO_AWAY             ; Wait while there is.
116          DJNZ      B,        GO        ; Count while there's not.
117          RET                           ; Return.
118;
119;         Procedure GetData
120;
121;         This procedure should be called to attempt to read the
122;         data from a Touch Memory after a presence pulse is
123;         received on the one-wire bus. It attempts to read a
124;         string of characters from the Touch Memory into a RAM
125;         buffer and check the CRC16 which follows the string.
126;         If the CRC is correct, the procedure returns with the
127;         Carry bit set, indicating that the string in the RAM
128;         buffer is ready for output. The first byte in the RAM
129;         buffer is the number of subsequent characters to be
130;         output. If the Carry is cleared, then the attempt to
131;         read the Touch Memory was unsuccessful.
132;
133GETDATA:
134          PUSH      B                   ; Save the try count.
135          CALL      TOUCHRESET          ; Send out reset signal.
136          JNC       SA                  ; Abort if no presence.
137          SJMP      PD                  ; Skip jump.
138SA:
139          LJMP      ABORT               ; Take the hike to abort.
140PD:
141          MOV       A,        #33H      ; Get read rom command in accumulator.
142          CALL      TOUCHBYTE           ; Command to read the ROM.
143          CALL      DATA_IN             ; Read the family code.
144          CALL      TOUCHRESET          ; Restart at beginning.
145          JNC       ABORT               ; Abort if no presence.
146          CJNE      A,  #8,   NOT_1992  ; Branch if not a DS1992.
147          MOV       B,        #126      ; Max string length is 125.
148          SJMP      CONT
149NOT_1992:
150          MOV       B,        #0        ; Max string length is 508.
151          CJNE      A,  #6,   NOT_1993  ; Branch if not a DS1993.
152          SJMP      CONT
153NOT_1993:
154          CJNE      A,  #4,   ABORT     ; Abort if not a DS1994.
155CONT:
156          MOV       A,        #0CCH
157          CALL      TOUCHBYTE           ; Skip over ROM section.
158          MOV       A,        #0F0H
159          CALL      TOUCHBYTE           ; Command to read the RAM.
160          CLR       A
161          CALL      TOUCHBYTE           ; First address byte zero.
162          CLR       A
163          CALL      TOUCHBYTE           ; Second address byte zero.
164FINISH:
165          MOV       LO,       #0        ; Initialize the CRC accumulator
166          MOV       HI,       #0        ;   with the starting page number.
167          MOV       DPTR,     #BUFF     ; Point to start of buffer.
168          CALL      GET_BYTE            ; Read the string length.
169          INC       B                   ; Prepare to test B for zero.
170          DJNZ      B,        BNZ       ; Branch if B not zero.
171          CJNE A,   #0FFH,    ANZ
172          MOVX      @DPTR,    A         ; Store zero for extended length.
173          INC       DPTR                ; Increment buffer pointer.
174          CALL      GET_BYTE            ; Get the length - 256.
175          JZ        ANZ                 ; Branch if length = 256.
176          CJNE      A, #254,  $+3       ; Check the string length.
177          JNC       ABORT               ; Branch if length > 508.
178          MOV       B,        #2        ; Setup to read long string.
179          SJMP      SIZE                ; Set string size and go.
180ABORT:
181          CLR       C                   ; Indicate failure.
182          POP       B                   ; Retrieve the try count.
183          RET                           ; Return.
184BNZ:
185          JZ        ABORT               ; Abort if string too long.
186          CJNE      A,   B,   $+3       ; Check size of string.
187          JNC       ABORT               ; Abort if string too long.
188ANZ:
189          MOV       B,        #1        ; Setup to read short string.
190SIZE:
191          PUSH      B                   ; Store string size byte.
192          MOV       B,        A         ; Set string length.
193          MOVX      @DPTR,    A         ; First byte in buffer is length.
194          INC       DPTR                ; Increment buffer pointer.
195          SJMP      IN_LOOP             ; Go input the string.
196BIG_LOOP:
197          PUSH      B                   ; Save loop count.
198          MOV       B,        #0FFH     ; Set for next group.
199IN_LOOP:
200          CALL      GET_BYTE            ; Get a character.
201          MOVX      @DPTR,    A         ; Store it in the buffer.
202          INC       DPTR                ; Increment buffer pointer.
203          DJNZ      B,        IN_LOOP   ; Go for next character.
204          POP       B                   ; Check high byte of count.
205          DJNZ      B,        BIG_LOOP  ; Go for next character.
206          CALL      GET_BYTE            ; Include the CRC in
207          CALL      GET_BYTE            ;    the CRC calculation.
208          MOV       A,        HI        ; Check high byte of CRC.
209          CJNE      A, #0B0H, ABORT     ; Abort if it is wrong.
210          MOV       A,        LO        ; Check low byte of CRC.
211          CJNE      A, #001H, ABORT     ; Abort if it is wrong.
212SUCCESS:
213          SETB      C                   ; Indicate successful read.
214          POP       B                   ; Retrieve the try count.
215          RET                           ; Return.
216;
217;   This procedure takes the byte received from DATA_IN and calculates the
218; cummulative CRC16.
219;
220GET_BYTE:
221          CALL      DATA_IN
222CRC16:
223          PUSH     ACC                   ; Save the character.
224          XRL      A,      LO            ; Xor accumulator with val. in LO.
225          MOV      LO,     HI            ; Put high byte of crc in its dest.
226          MOV      HI,     A             ; Save data xor low(crc) for later
227          MOV      C,      P             ; Get parity bit in carry.
228          JNC      CRC0                  ; Don't XRL if carry clear.
229          XRL      LO,     #01h          ; Add the parity to crc bit 0
230CRC0:
231          RRC      A                     ; Get the low bit in c
232          JNC      CRC1                  ; Don't XRL if carry clear.
233          XRL      LO,     #40h          ; Need to fix bit 6 of the result
234CRC1:
235          MOV      C,      ACC.7         ; Get msb of accumulator in carry.
236          XRL      A,      HI            ; Compute the results for bits P...U
237          RRC      A                     ; Shift them into place
238          MOV      HI,     A             ; And save them
239          JNC      CRC2                  ; Don't flip msb LO if carry clear.
240          CPL      LO.7                  ; Now clean up bit 7
241CRC2:
242          POP      ACC                   ; Restore the character.
243          RET                            ; Return to caller.
244;
245          END                            ; End of Program.