/ARTICLES/2000MAY/2000MAY10_MEM_NTEK_AN.PDF
|
? 1997 Microchip Technology Inc. DS00510E-page 1
INTRODUCTION
The PIC16C5X series from Microchip Technology Inc.,
are 8-bit, high-speed, EPROM-based microcontrollers.
This application note describes an implementation of
an asynchronous serial I/O using a PIC16C5X micro-
controller. These microcontrollers can operate at very
high speeds with a minimum cycle time of 200 ns
@ 20 MHz input clock. Many microcontroller applica-
tions require chip-to-chip serial data communications.
Since the PIC16C5X series has no on-chip serial ports,
serial communication has to be performed in software.
For many cost-sensitive high volume applications,
implementation of a serial I/O through software pro-
vides a more cost effective solution than dedicated
logic. This application note provides code for the
PIC16C5X to simulate a serial port using two I/O pins
(one as input for reception and the other as output for
transmission).
Author: Amar Palacherla
Microchip Technology Inc.
IMPLEMENTATION
Two programs are provided in this application note.
One program (Appendix B) simulates full duplex
RS-232 communication and the other (Appendix A)
provides implementation of half-duplex communication.
Using half-duplex, baud rates up to 19200 can be
implemented using an 8 MHz input clock. For
full-duplex, the software can handle up to 9600 baud at
8 MHz and 19200 baud at 20 MHz, one or two stop bits,
eight or seven data bits, no Parity and can transmit or
receive with either LSb first (normal mode) or MSb first
(CODEC-like mode). It should be noted that the higher
the input clock the better the resolution. These options
should be set up during assembly time and not during
run time.The user simply has to change the header file
for the required communication options. The software
does not provide any handshaking protocols. With
minor modifications, the user may incorporate software
handshaking using XON/XOFF. To implement hard-
ware handshaking, an additional two digital I/O pins
may be used as RTS (ready to send) and CTS (clear to
send) lines.
AN510
Implementation of an Asynchronous Serial I/O
AN510
DS00510E-page 2 ? 1997 Microchip Technology Inc.
Figure 1 shows a flowchart for serial transmission and
Figure 2 shows a flowchart for reception. The
flowcharts show case transmission/reception with LSb
first and eight data bits. For reception, the data receive
pin, DR, is polled approximately every B/2 seconds (52
?s for 9600 baud) to detect the start bit, where B is the
time duration of one bit (B = 1/Baud). If a start bit is
found, then the first data bit is checked for after 1.25B
seconds. From then on, the other data bits are checked
every B seconds (104 ?s for 9600 baud).
In the case of transmission, first a start bit is sent by set-
ting the transmit data pin, DX, to zero for B seconds,
and from then on the DX pin is set/cleared
corresponding to the data bit every B seconds.
Assembly language code corresponding to the
following flowcharts is given in Example 1 and
Example 2.
FIGURE 1: TRANSMISSION FLOWCHART
Input
Load Xmit Reg
X-Count = 8
DX pin = 0
Delay
Right Shift Xmt reg
DX pin = 1
X-Count = X-Count - 1
DX pin = 0
Transmission Over
Is Carry = 1?
Is X-Count
= 0?
No
Yes
No
Yes
FIGURE 2: RECEPTION FLOWCHART
Input
Test Pin DR
Is DR = 0?
R - Count = 8
Rcv Reg = 0
Delay
Clear Carry bit
Right Shift Rcv Reg
Test Pin DR
Is DR = 1?
Set MSb of Rcv Reg
R-Count = R-Count - 1
Is R-Count
= 0?
Stop
Yes
No
Start Bit Detected
No
Yes
Yes
No
? 1997 Microchip Technology Inc. DS00510E-page 3
AN510
EXAMPLE 1: TRANSMIT ASSEMBLY CODE (CORRESPONDING TO FIGURE 1)
EXAMPLE 2: RECEIVE ASSEMBLY CODE (CORRESPONDING TO FIGURE 2)
;********************* Transmitter*****************************
Xmtr movlw 8 ; Assume XmtReg contains data to be Xmted
movwf XCount ; 8 data bits
bcf Port_A,DX ; Send Start Bit
X_next call Delay ; Delay for B/2 Seconds
rrf XmtReg
btfsc STATUS,CARRY ; Test the bit to be transmitted
bsf Port_A,DX ; Bit is a one
btfss STATUS,CARRY
bcf Port_A,DX ; Bit is zero
decfsz Count ; If count = 0, then transmit a stop bit
goto X_next ; transmit next bit
;
X_Stop call Delay
bsf Port_A,DX ; Send Stop Bit
X_Over goto X_Over
;**************** Receiver *********************
;
Rcvr btfsc Port_A,DR ; Test for Start Bit
goto Rcvr ; Start Bit not found
movlw 8 ; Start Bit Detected
movwf RCount ; 8 Data Bits
clrf RcvReg ; Receive Data Register
R_next call Delay ; Delay for B/2 Seconds, B=Time duration of
; 1bit
bcf STATUS,CARRY ; Clear CARRY bit
rrf RcvReg ; to set if MSB first or LSB first
btfsc Port_A,DR ; Is the bit a zero or one ?
bsf RcvReg,MSB ; Bit is a one
call Delay
decfsz RCount
goto R_next
R_Over goto R_Over ; Reception done
The software is organized such that the communication
software acts as a Real-Time Operating System
(RTOS) which gives control to the user routine for a cer-
tain time interval. After this predetermined time slot, the
user must give back control to the Operating System.
This is true only in the case of full-duplex implementa-
tion. Timing considerations are such that the user gets
control for approximately half the time of the bit rate and
the rest of the time is used up by the Operating System
(and software delays). Please refer to Table 1 for the
delay constants and the time the user gets using an 8
MHz input clock. Delay constants and the time that the
user gets at 20 MHz and 4 MHz input clock speeds are
given in the source code listing of the full-duplex rou-
tine. At frequencies other than 4, 8, or 20 MHz, the
delay constants and the time the user gets can be com-
puted from the equations given in Equation 1.
FIGURE 3: FULL-DUPLEX BLOCK
DIAGRAM
RTOS User
Xmtr
Rcur
AN510
DS00510E-page 4 ? 1997 Microchip Technology Inc.
EQUATION 1: EQUATIONS FOR DELAY
CONSTANTS
TABLE 1: DELAY CONSTANTS AT 8 MHZ
INPUT CLOCK
TABLE 2: DELAY CONSTANTS AT 20 MHZ
INPUT CLOCK
For example, if the baud rate selected is 9600 bps
(@ 8 MHz), then the total time frame for one bit is
approximately 104 ?s. Out of this 104 ?s, 61 ?s are
used by the Operating System and the other 43 ?s are
available to the user. It is the user's responsibility to
return control to the Operating System exactly after the
time specified in Table 1. For very accurate timing (with
resolution up to one clock cycle) the user may set up
Timer0 with the Prescaler option for calculating the
real-time. With TMR0 configured to increment on inter-
nal CLKOUT (500 ns @ 8 MHz CLKIN) and the pres-
caler assigned to it, very accurate and long timing delay
loops may be assigned. This method of attaining accu-
Note: CLKOUT = FOSC/4
Baud_Cycles = Clkout/Baud;
User_time = Baud_Cycles ? (float) 0.5;
K0 = (1.25 ? Baud_Cycles - 2.0 ? User_time - 89)/3.0; IF (K0
? 1997 Microchip Technology Inc. DS00510E-page 5
AN510
SUMMARY
The PIC16C5X family of microcontrollers allow users to
implement half or full duplex RS-232 communication in
software.
AN510
DS00510E-page 6 ? 1997 Microchip Technology Inc.
APPENDIX A: ASSEMBLY LANGUAGE FOR HALF DUPLEX
MPASM 01.40 Released HALF_DUP.ASM 1-16-1997 11:48:17 PAGE 1
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00001 LIST P = 16C54, n = 66
00002 ;
00003 ;*********************************************************************
00004 ; RS-232 Communication With PIC16C54
00005 ;
00006 ; Half Duplex Asynchronous Communication
00007 ;
00008 ; This program has been tested at Bauds from 1200 to 19200 Baud
00009 ; ( @ 8,16,20 Mhz CLKIN )
00010 ;
00011 ; As a test, this program will echo back the data that has been
00012 ; received.
00013 ;
00014 ; Program: HALF_DUP.ASM
00015 ; Revision Date:
00016 ; 1-13-97 Compatibility with MPASMWIN 1.40
00017 ;
00018 ;********************************************************************
00019 ;
00020 INCLUDE
00001 LIST
00002 ; P16C5X.INC Standard Header File, Ver. 3.30 Microchip Technology, Inc.
00224 LIST
00021
000001FF 00022 PIC54 equ 1FFH ; Define Reset Vector
00000001 00023 Same equ 1
00000007 00024 MSB equ 7
00025
00026 ;***************** Communication Parameters **********************
00027 ;
00000001 00028 X_MODE equ 1 ; If ( X_MODE==1) Then transmit LSB first
00029 ; if ( X_MODE==0) Then transmit MSB first (CODEC like)
00000001 00030 R_MODE equ 1 ; If ( R_MODE==1) Then receive LSB first
00031 ; if ( X_MODE==0) Then receive MSB first (CODEC like)
00000001 00032 X_Nbit equ 1 ; if (X_Nbit==1) # of data bits (Transmission is 8 else 7
00000001 00033 R_Nbit equ 1 ; if (R_Nbit==1) # of data bits (Reception) is 8 else 7
00034 ;
00000000 00035 Sbit2 equ 0 ; if Sbit2 = 0 then 1 Stop Bit else 2 Stop Bits
00036 ;
00037 ;********************************************************************
00000005 00038 X_flag equ PA0 ; Bit 5 of F3 ( PA0 )
00000006 00039 R_flag equ PA1 ; Bit 6 of F3 ( PA1 )
00040 ;
00000000 00041 DX equ 0 ; Transmit Pin ( Bit 0 of Port A )
00000001 00042 DR equ 1 ; Reciive Pin ( Bit 1 of Port A )
00043 ;
00044 ;
00000044 00045 BAUD_1 equ .68 ; 3+3X = CLKOUT/Baud
00000043 00046 BAUD_2 equ .67 ; 6+3X = CLKOUT/Baud
00000022 00047 BAUD_3 equ .34 ; 3+3X = 0.5*CLKOUT/Baud
00000056 00048 BAUD_4 equ .86 ; 3+3X = 1.25*CLKOUT/Baud
00000042 00049 BAUD_X equ .66 ; 11+3X = CLKOUT/Baud
00000042 00050 BAUD_Y equ .66 ; 9 +3X = CLKOUT/Baud
Please check the Microchip BBS for the latest version of the source code. Microchip's Worldwide Web Address:
www.microchip.com; Bulletin Board Support: MCHIPBBS using CompuServe?
(CompuServe membership not
required).
? 1997 Microchip Technology Inc. DS00510E-page 7
AN510
00051 ;
00052 ;************************ Data RAM Assignments *********************
00053 ;
0008 00054 ORG 08H ; Dummy Origin
00055 ;
0008 00056 RcvReg RES 1 ; Data received
0009 00057 XmtReg RES 1 ; Data to be transmitted
000A 00058 Count RES 1 ; Counter for #of Bits Transmitted
000B 00059 DlyCnt RES 1
00060 ;***************************************************************
00061 ;
0000 00062 ORG 0
00063 ;
0000 0068 00064 Talk clrf RcvReg ; Clear all bits of RcvReg
0001 0625 00065 btfsc PORTA,DR ; check for a Start Bit
0002 0A30 00066 goto User ; delay for 104/2 uS
0003 0923 00067 call Delay4 ; delay for 104+104/4
00068 ;***************************************************************
00069 ; Receiver
00070 ;
0004 00071 Rcvr
00072 IF R_Nbit
0004 0C08 00073 movlw 8 ; 8 Data bits
00074 ELSE
00075 movlw 7 ; 7 data bits
00076 ENDIF
00077 ;
0005 002A 00078 movwf Count
0006 0403 00079 R_next bcf STATUS,C
00080 IF R_MODE
0007 0328 00081 rrf RcvReg,Same ; to set if MSB first or LSB first
00082 ELSE
00083 rlf RcvReg,Same
00084 ENDIF
0008 0625 00085 btfsc PORTA,DR
00086 ;
00087 IF R_MODE
00088 IF R_Nbit
0009 05E8 00089 bsf RcvReg,MSB ; Conditional Assembly
00090 ELSE
00091 bsf RcvReg,MSB-1
00092 ENDIF
00093 ELSE
00094 bsf RcvReg,LSB
00095 ENDIF
00096 ;
000A 091F 00097 call DelayY
000B 02EA 00098 decfsz Count,Same
000C 0A06 00099 goto R_next
00100 ;****************************************************
000D 0208 00101 R_over movf RcvReg,0 ; Send back What is Just Received
000E 0029 00102 movwf XmtReg
00103 ;****************************************************
00104 ; Transmitter
00105 ;
000F 00106 Xmtr
00107 IF X_Nbit
000F 0C08 00108 movlw 8
00109 ELSE
00110 movlw 7
00111 ENDIF
0010 002A 00112 movwf Count
00113 ;
00114 IF X_MODE
00115 ELSE
00116 IF X_Nbit
AN510
DS00510E-page 8 ? 1997 Microchip Technology Inc.
00117 ELSE
00118 rlf XmtReg,Same
00119 ENDIF
00120 ENDIF
00121 ;
0011 0405 00122 bcf PORTA,DX ; Send Start Bit
0012 0925 00123 call Delay1
0013 0403 00124 X_next bcf STATUS,C
00125 ;
00126 IF X_MODE
0014 0329 00127 rrf XmtReg,Same ; Conditional Assembly
00128 ELSE ; to set if MSB first or LSB first
00129 rlf XmtReg,Same
00130 ENDIF
00131 ;
0015 0603 00132 btfsc STATUS,C
0016 0505 00133 bsf PORTA,DX
0017 0703 00134 btfss STATUS,C
0018 0405 00135 bcf PORTA,DX
0019 0921 00136 call DelayX
001A 02EA 00137 decfsz Count,Same
001B 0A13 00138 goto X_next
001C 0505 00139 bsf PORTA,DX ; Send Stop Bit
001D 0925 00140 call Delay1
00141 ;
00142 IF Sbit2
00143 bsf PORTA,DX
00144 call Delay1
00145 ENDIF
00146 ;
001E 0A00 00147 goto Talk ; Back To Reception & Transmision
00148 ;
00149 ; End of Transmission
00150 ;
001F 0C42 00151 DelayY movlw BAUD_Y
0020 0A28 00152 goto save
0021 0C42 00153 DelayX movlw BAUD_X
0022 0A28 00154 goto save
0023 0C56 00155 Delay4 movlw BAUD_4
0024 0A28 00156 goto save
0025 0C44 00157 Delay1 movlw BAUD_1 ; 104 uS for 9600 baud
0026 0A28 00158 goto save
0027 0C43 00159 Delay2 movlw BAUD_2
0028 002B 00160 save movwf DlyCnt
0029 02EB 00161 redo_1 decfsz DlyCnt,Same
002A 0A29 00162 goto redo_1
002B 0800 00163 retlw 0
00164 ;
002C 0C0E 00165 main movlw 0EH ; Bit 0 of Port A is Output
002D 0005 00166 tris PORTA ; Set PORTA.0 as output ( DX )
002E 0525 00167 bsf PORTA,DR
00168 ;
002F 0A00 00169 goto Talk
00170 ;
00171 ;
0030 0C22 00172 User movlw BAUD_3
0031 002B 00173 movwf DlyCnt
0032 02EB 00174 redo_2 decfsz DlyCnt,Same
0033 0A32 00175 goto redo_2
0034 0A00 00176 goto Talk ; Loop Until Start Bit Found
00177 ;
00178 ;
01FF 00179 ORG PIC54
01FF 0A2C 00180 goto main
00181 ;
00182 END
? 1997 Microchip Technology Inc. DS00510E-page 9
AN510
MEMORY USAGE MAP (`X' = Used, `-' = Unused)
0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXX-----------
01C0 : ---------------- ---------------- ---------------- ---------------X
All other memory blocks unused.
Program Memory Words Used: 54
Program Memory Words Free: 458
Errors : 0
Warnings : 0 reported, 0 suppressed
Messages : 0 reported, 0 suppressed
AN510
DS00510E-page 10 ? 1997 Microchip Technology Inc.
APPENDIX B: ASSEMBLY LANGUAGE LISTING FOR FULL DUPLEX
MPASM 01.40 Released RS232.ASM 1-16-1997 12:12:09 PAGE 1
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00001 LIST P = 16C54, n = 66
00002 ;
00003 ;************************************************************
00004 TITLE "RS232 Communication Using PIC16C54"
00005 ;
00006 ; Comments :
00007 ; (1) Full Duplex
00008 ; (2) Tested from 1200 to 9600 Baud( @ 8 Mhz )
00009 ; (3) Tested from 1200 to 19200 Baud(@ 16 & 20 Mhz)
00010 ;
00011 ; The User gets a total time as specified by the User Cycles
00012 ; in the table ( or from equations ). The user routine has to
00013 ; exactly use up this amount of time. After this time the User
00014 ; routine has to give up the control to the Operating System.
00015 ; If less than 52 uS is used, then the user should wait in a
00016 ; delay loop, until exactly 52 uS.
00017 ;
00018 ; Transmission :
00019 ; Transmit Data is output on DX pin (Bit DX of PORTA).
00020 ; In the user routine, the user should load the
00021 ; data to be transmitted in the XmtReg and Set the
00022 ; X_flag ( bsf FlagRX,X_flag ). This flag gets cleared
00023 ; after the transmission.
00024 ;
00025 ; Reception :
00026 ; Data is received on pin DR ( bit DR of PORTA ).
00027 ; The User should constantly check the "R_done" flag
00028 ; to see if reception is over. If the reception is
00029 ; in progress, R_flag is set to 1.
00030 ; If the reception is over, "R_done" flag is set to 1.
00031 ; The "R_done" flag gets reset to zero when a next start
00032 ; bit is detected. So, the user should constantly check
00033 ; the R_done flag, and if SET, then the received word
00034 ; is in Register "RcvReg". This register gets cleared
00035 ; when a new start bit is detected.
00036 ;
00037 ; Program Memory :
00038 ; Total Program Memory Locations Used ( except initialization
00039 ; in "main" & User routine ) = 132 locations.
00040 ;
00041 ; Data Memory :
00042 ; Total Data memory locations (file registers used) = 6
00043 ; 2 File registers to hold Xmt Data & Rcv Data
00044 ; 1 File registers for Xmt/Rcv flag test bits
00045 ; 3 File registers for delay count & scratch pad
00046 ;
00047 ; Stack :
00048 ; Only one level of stack is used in the Operating System/RS232
00049 ; routine. But this is freed as soon as the program returns to
00050 ; the user routine.
00051 ;
00052 ; Timer0 : Not Used
00053 ; WDT : Not Used
00054 ;
00055 ; Program: RS232.ASM
Please check the Microchip BBS for the latest version of the source code. Microchip's Worldwide Web Address:
www.microchip.com; Bulletin Board Support: MCHIPBBS using CompuServe?
(CompuServe membership not
required).
? 1997 Microchip Technology Inc. DS00510E-page 11
AN510
00056 ; Revision Date:
00057 ; 1-16-97 Compatibility with MPASMWIN 1.40
00058 ;
00059 ;*********************************************************************
00060 ;
00061 INCLUDE
00001 LIST
00002 ; P16C5X.INC Standard Header File, Ver. 3.30 Microchip Technology, Inc.
00224 LIST
00062
000001FF 00063 PIC54 equ 1FFH ; Define Reset Vector
00000001 00064 Same equ 1
00000007 00065 MSB equ 7
00066
00067 INCLUDE
00001 ;****************************************************************
00002 ; RS232 Communication Parameters
00003 ;
00004 ;
00000001 00005 X_MODE equ 1 ; If (X_MODE==1) Then transmit LSB first
00006 ; if (X_MODE==0) Then transmit MSB first (CODEC like)
00000001 00007 R_MODE equ 1 ; If (R_MODE==1) Then receive LSB first
00008 ; if ( R_MODE==0) Then receive MSB first (CODEC like)
00000001 00009 X_Nbit equ 1 ; if (X_Nbit==1)#of data bits (Transmission) is 8 else 7
00000001 00010 R_Nbit equ 1 ; if (R_Nbit==1) # of data bits (Reception) is 8 else 7
00011 ;
00000000 00012 SB2 equ 0 ; if SB2 = 0 then 1 Stop Bit
00013 ; ; if SB2 = 1 then 2 Stop Bit
00014 ;****************************************************************
00015 ; Transmit & Receive Test Bit Assignments
00016 ;
00000000 00017 X_flag equ 0 ; Bit 0 of FlagRX
00000002 00018 R_flag equ 2 ; Bit 1 of FlagRX
00000003 00019 S_flag equ 3 ; Bit 2 of FlagRX
00000004 00020 BitXsb equ 4 ; Bit 3 of FlagRX
00000005 00021 A_flag equ 5
00000006 00022 S_bit equ 6 ; Xmt Stop Bit Flag( for 2/1 Stop bits )
00023 ;
00000001 00024 R_done equ 1 ; When Reception complete, this bit is SET
00000000 00025 X_done equ X_flag ; When Xmission complete, this bit is Cleared
00026 ;
00000000 00027 DX equ 0 ; Transmit Pin ( Bit 0 of Port A )
00000001 00028 DR equ 1 ; Receive Pin ( Bit 1 of Port A )
00029 ;
00030 ;************************ Data RAM Assignments *********************
00031 ;
0008 00032 ORG 08H ; Dummy Origin
00033 ;
0008 00034 RcvReg RES 1 ; Data received
0009 00035 XmtReg RES 1 ; Data to be transmitted
000A 00036 Xcount RES 1 ; Counter for #of Bits Transmitted
000B 00037 Rcount RES 1 ; Counter for #of Bits to be Received
000C 00038 DlyCnt RES 1 ; Counter for Delay constant
000D 00039 FlagRX RES 1 ; Transmit & Receive test flag hold register
00040 ;______________________________________________________________
00041 ; Constants 19200 9600 4800 2400 1200
00042 ; ( @ 20 Mhz )
00043 ;______________________________________________________________
00044 ; K0 0 13 57 143 317*
00045 ; K1 49 98 184 358* 705*
00046 ; K2 34 60 103 191 364*
00047 ; K3 27 53 96 184 357*
00048 ; K4 29 55 98 186 359*
00049 ; K5 30 56 99 187 360*
00050 ; K6 0 0 0 0 0
00051 ; K7 56 104 190 365* 712*
AN510
DS00510E-page 12 ? 1997 Microchip Technology Inc.
00052 ;
00053 ; User Cycles 118 260 521 1042 2083
00054 ; *************************************************************
00055 ;
00056 ;
00057 ;______________________________________________________________
00058 ; Constants 19200 9600 4800 2400 1200
00059 ; ( @ 8 Mhz )
00060 ;______________________________________________________________
00061 ; K0 -- 0 5 39 109
00062 ; K1 -- 39 80 150 288*
00063 ; K2 -- 27 51 86 155
00064 ; K3 -- 21 44 80 148
00065 ; K4 -- 23 46 82 150
00066 ; K5 -- 24 47 83 151
00067 ; K6 -- 0 0 0 0
00068 ; K7 -- 45 86 156 295*
00069 ;
00070 ; User_Cycles -- 86 208 416 832
00071 ; *************************************************************
00072 ;
00073 ;______________________________________________________________
00074 ; Constants 19200 9600 4800 2400 1200
00075 ; ( @ 4 Mhz )
00076 ;______________________________________________________________
00077 ; K0 -- -- 0 5 39
00078 ; K1 -- -- 39 80 150
00079 ; K2 -- -- 27 51 86
00080 ; K3 -- -- 21 44 80
00081 ; K4 -- -- 23 46 82
00082 ; K5 -- -- 24 47 83
00083 ; K6 -- -- 0 0 0
00084 ; K7 -- -- 45 86 156
00085 ;
00086 ; User_Cycles -- -- 86 208 416
00087 ; *************************************************************
00088
00089 ;
00090 ; The constants marked " * " are >255. To implement these constants
00091 ; in delay loops, the delay loop should be broken into 2 or more loops.
00092 ; For example, 357 = 255+102. So 2 delay loops, one with 255 and
00093 ; the other with 102 may be used.
00094 ;********************************************************************
00095 ; Set Delay Constants for 9600 Baud @ CLKIN = 8 Mhz
00096 ;
00000000 00097 K0 EQU .0
00000027 00098 K1 EQU .39
0000001B 00099 K2 EQU .27
00000015 00100 K3 EQU .21
00000017 00101 K4 EQU .23
00000018 00102 K5 EQU .24
00000000 00103 K6 EQU .0
0000002D 00104 K7 EQU .45
00105 ;
00106 ;********************************************************************
00107
00068 ;
0000 00069 ORG 0
00070 ;********************************************************************
00071 ;
0000 0C01 00072 Delay movlw K0+1
0001 002C 00073 movwf DlyCnt ; Total Delay = 3K+6
0002 02EC 00074 redo decfsz DlyCnt,Same
0003 0A02 00075 goto redo
0004 0800 00076 retlw 0
00077 ;
? 1997 Microchip Technology Inc. DS00510E-page 13
AN510
0005 002C 00078 Delay1 movwf DlyCnt
0006 02EC 00079 redo_1 decfsz DlyCnt,Same ;
0007 0A06 00080 goto redo_1
0008 0A8D 00081 goto User
00082 ;
0009 002C 00083 Delay2 movwf DlyCnt
000A 02EC 00084 redo_2 decfsz DlyCnt,Same ; Delay = = 260 Cycles
000B 0A0A 00085 goto redo_2
000C 0A67 00086 goto User_1
00087 ;
000D 0625 00088 R_strt btfsc PORTA,DR ; check for a Start Bit
000E 0A17 00089 goto ShellY ; delay for 104/2 uS
000F 042D 00090 bcf FlagRX,R_done ; Reset Receive done flag
0010 054D 00091 bsf FlagRX,R_flag ; Set flag for Reception in Progress
0011 078D 00092 btfss FlagRX,BitXsb
0012 05AD 00093 bsf FlagRX,A_flag ; A_flag is for start bit detected in R_strt
0013 0068 00094 clrf RcvReg ; Clear all bits of RcvReg
00095 IF R_Nbit
0014 0C08 00096 movlw 8 ; 8 Data bits
00097 ELSE
00098 movlw 7 ; 7 data bits
00099 ENDIF
0015 002B 00100 movwf Rcount
0016 0A78 00101 goto Shell ; delay for 104+104/4
00102 ;
0017 078D 00103 ShellY btfss FlagRX,BitXsb
0018 0A78 00104 goto Shell
0019 054D 00105 bsf FlagRX,R_flag
001A 0A78 00106 goto Shell
00107 ;
001B 0403 00108 R_next bcf STATUS,C
00109 IF R_MODE
001C 0328 00110 rrf RcvReg,Same ; to set if MSB first or LSB first
00111 ELSE
00112 rlf RcvReg,Same
00113 ENDIF
001D 0625 00114 btfsc PORTA,DR
00115 IF R_MODE
00116 IF R_Nbit
001E 05E8 00117 bsf RcvReg,MSB ; Conditional Assembly
00118 ELSE
00119 bsf RcvReg,MSB-1
00120 ENDIF
00121 ELSE
00122 bsf RcvReg,LSB
00123 ENDIF
001F 02EB 00124 decfsz Rcount,Same
0020 0A78 00125 goto Shell
0021 044D 00126 bcf FlagRX,R_flag
0022 056D 00127 bsf FlagRX,S_flag
0023 052D 00128 bsf FlagRX,R_done
0024 0A78 00129 goto Shell
00130 ;
00131 ; Reception Done
00132 ;
0025 0405 00133 X_strt bcf PORTA,DX ; Send Start Bit
00134 IF X_Nbit
0026 0C08 00135 movlw 8
00136 ELSE
00137 movlw 7
00138 ENDIF
0027 002A 00139 movwf Xcount
00140 IF X_MODE
00141 ELSE
00142 IF X_Nbit
00143 ELSE
AN510
DS00510E-page 14 ? 1997 Microchip Technology Inc.
00144 rlf XmtReg,Same
00145 ENDIF
00146 ENDIF
0028 0A50 00147 goto X_SB
00148 ;
0029 0403 00149 X_next bcf STATUS,C
00150 IF X_MODE
002A 0329 00151 rrf XmtReg,Same ; Conditional Assembly
00152 ELSE ; to set if MSB first or LSB first
00153 rlf XmtReg,Same
00154 ENDIF
002B 0603 00155 btfsc STATUS,C
002C 0505 00156 bsf PORTA,DX
002D 0703 00157 btfss STATUS,C
002E 0405 00158 bcf PORTA,DX
002F 00EA 00159 decf Xcount,Same
0030 0A52 00160 goto X_Data
00161 ;
0031 040D 00162 X_SB_1 bcf FlagRX,X_flag ; Xmt flag = 0 -- transmission over
0032 0C09 00163 movlw 9
0033 002A 00164 movwf Xcount
0034 0505 00165 bsf PORTA,DX ; Send Stop Bit
0035 0A60 00166 goto X_Stop
00167 ;
0036 0505 00168 X_SB_2 bsf PORTA,DX
0037 04CD 00169 bcf FlagRX,S_bit
0038 0A60 00170 goto X_Stop
00171 ;
00172 ; End of Transmission
00173 ;
0039 076D 00174 R0_X0 btfss FlagRX,S_flag
003A 0A8D 00175 goto User
003B 046D 00176 bcf FlagRX,S_flag
003C 0900 00177 call Delay
003D 0C2E 00178 movlw K7+1
003E 0A05 00179 goto Delay1
00180 ;
003F 00181 R1_X0
003F 0900 00182 call Delay
0040 0C28 00183 movlw K1+1 ; delay for 1st bit is 104+104/4
0041 002C 00184 movwf DlyCnt
00185 IF R_Nbit
0042 0C08 00186 movlw 8 ; 8 Data bits
00187 ELSE
00188 movlw 7 ; 7 data bits
00189 ENDIF
0043 018B 00190 xorwf Rcount,W
0044 0643 00191 btfsc STATUS,Z
0045 0A06 00192 goto redo_1
0046 0C1C 00193 movlw K2+1
0047 0A05 00194 goto Delay1
00195 ;
0048 00196 R1_X1 ; same as R0_X1
0048 0C09 00197 R0_X1 movlw 9
0049 008A 00198 subwf Xcount,W
004A 0643 00199 btfsc STATUS,Z
004B 0A25 00200 goto X_strt
004C 022A 00201 movf Xcount,Same ; to check if All data bits Xmted
004D 0743 00202 btfss STATUS,Z
004E 0A29 00203 goto X_next
00204 IF SB2
00205 btfsc FlagRX,S_bit
00206 goto X_SB_2
00207 bsf FlagRX,S_bit
00208 goto X_SB_1
00209 ELSE
? 1997 Microchip Technology Inc. DS00510E-page 15
AN510
004F 0A31 00210 goto X_SB_1
00211 ENDIF
00212 ;
00213 ;
0050 0A51 00214 X_SB goto cycle4
0051 0A52 00215 cycle4 goto X_Data
00216 ;
0052 06AD 00217 X_Data btfsc FlagRX,A_flag
0053 0A59 00218 goto SbDly
0054 068D 00219 btfsc FlagRX,BitXsb
0055 0A5D 00220 goto ABC
0056 0900 00221 call Delay
0057 0C16 00222 movlw K3+1
0058 0A09 00223 goto Delay2
00224 ;
0059 04AD 00225 SbDly bcf FlagRX,A_flag
005A 0900 00226 call Delay
005B 0C18 00227 movlw K4+1
005C 0A09 00228 goto Delay2
00229 ;
005D 048D 00230 ABC bcf FlagRX,BitXsb
005E 0900 00231 call Delay
005F 0A67 00232 goto User_1
00233 ;
0060 00234 X_Stop
0060 06AD 00235 btfsc FlagRX,A_flag
0061 0A59 00236 goto SbDly
0062 068D 00237 btfsc FlagRX,BitXsb
0063 0A5D 00238 goto ABC
0064 0900 00239 call Delay
0065 0C19 00240 movlw K5+1
0066 0A09 00241 goto Delay2
00242 ;
0067 064D 00243 User_1 btfsc FlagRX,R_flag
0068 0A77 00244 goto Sync_1 ; Reception already in progress
0069 066D 00245 btfsc FlagRX,S_flag
006A 0A74 00246 goto Sync_3
006B 0625 00247 btfsc PORTA,DR ; check for a Start Bit
006C 0A77 00248 goto Sync_2 ; No Start Bit - goto User routine
006D 042D 00249 bcf FlagRX,R_done ; Reset Receive done flag
006E 044D 00250 bcf FlagRX,R_flag
006F 058D 00251 bsf FlagRX,BitXsb ; Set flag for Reception in Progress
0070 0068 00252 clrf RcvReg ; Clear all bits of RcvReg
00253 IF R_Nbit
0071 0C08 00254 movlw 8 ; 8 Data bits
00255 ELSE
00256 movlw 7 ; 7 data bits
00257 ENDIF
0072 002B 00258 movwf Rcount
0073 0A8D 00259 goto User
00260 ;
0074 046D 00261 Sync_3 bcf FlagRX,S_flag
0075 0C01 00262 movlw K6+1
0076 0A05 00263 goto Delay1
00264 ;
0077 00265 Sync_1
0077 0A8D 00266 Sync_2 goto User
00267 ;
00268 ;********************************************************************
00269 ;
0078 064D 00270 Shell btfsc FlagRX,R_flag
0079 0A7D 00271 goto Chek_X
007A 060D 00272 btfsc FlagRX,X_flag
007B 0A48 00273 goto R0_X1
007C 0A39 00274 goto R0_X0 ; Case for R0_X0
007D 060D 00275 Chek_X btfsc FlagRX,X_flag
AN510
DS00510E-page 16 ? 1997 Microchip Technology Inc.
007E 0A48 00276 goto R1_X1
007F 0A3F 00277 goto R1_X0
00278 ;
00279 ;
00280 ;********************************************************************
00281 ; Operating System
00282 ; The User routine after time = B/2, should branch Here
00283 ;
0080 074D 00284 Op_Sys btfss FlagRX,R_flag
0081 0A0D 00285 goto R_strt
0082 0A1B 00286 goto R_next
00287 ;
00288 ;********************************************************************
00289 ;
0083 0C0E 00290 main movlw 0EH ; Bit 0 of Port A is Output
0084 0005 00291 tris PORTA ; Set PORTA.0 as output ( DX )
00292 ; & PORTA.1 is input ( DR )
0085 0505 00293 bsf PORTA,DX
0086 0C09 00294 movlw 9
0087 002A 00295 movwf Xcount ; If Xcount == 9, Then send start bit
0088 006D 00296 clrf FlagRX ; Clear All flag bits.
00297 IF SB2
00298 bsf FlagRX,S_bit ; Set Xmt Stop bit flag(2 Stop Bits)
00299 ELSE
0089 04CD 00300 bcf FlagRX,S_bit ; Clear Xmt Stop bit flag
00301 ENDIF
008A 0C1F 00302 movlw 1FH ; Prescaler = 4
008B 0002 00303 OPTION ; Set TMR0 increment on internal Clock
008C 0A80 00304 goto Op_Sys
00305 ;
00306 ;********************************************************************
00307 ;
00308 ; ******************* User Routine ***************
00309 ; The User routine should use up time exactly = User time as given
00310 ; in the Constants Table ( or by Equations for constants ).
00311 ; At 9600, this 86 Clock Cycles. TMR0 timer is used here to count
00312 ; upto 86 cycles ( From 128-86 To 0 ) by examining Bit 7 of TMR0.
00313 ;
00000030 00314 K_user equ .128+.6-.86
00315 ;
008D 0C30 00316 User movlw K_user
008E 0021 00317 movwf TMR0
008F 062D 00318 btfsc FlagRX,R_done
0090 0A97 00319 goto ErrChk
0091 060D 00320 SetXmt btfsc FlagRX,X_flag
0092 0A9C 00321 goto Op
0093 0C41 00322 movlw 41H
0094 0029 00323 movwf XmtReg
0095 050D 00324 bsf FlagRX,X_flag ; Enable Xmission
0096 0A9C 00325 goto Op
00326 ;
0097 00327 ErrChk
0097 0C5A 00328 movlw "Z"
0098 0188 00329 xorwf RcvReg,W
0099 0643 00330 btfsc STATUS,Z
009A 0A91 00331 goto SetXmt
009B 0A9B 00332 err1 goto err1 ; Received word is not "Z"
00333 ;
009C 07E1 00334 Op btfss TMR0,MSB ; Test for TMR0 bit 7
009D 0A9C 00335 goto Op ; If Set, Then TMR0 has incremented
009E 0A80 00336 Oflow goto Op_Sys ; to 128.
00337 ;
00338 ; ***********************************************************
00339 ;
01FF 00340 ORG PIC54
01FF 0A83 00341 goto main
? 1997 Microchip Technology Inc. DS00510E-page 17
AN510
00342
00343 END
MEMORY USAGE MAP (`X' = Used, `-' = Unused)
0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
0080 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX- ---------------- ----------------
01C0 : ---------------- ---------------- ---------------- ---------------X
All other memory blocks unused.
Program Memory Words Used: 160
Program Memory Words Free: 352
Errors : 0
Warnings : 0 reported, 0 suppressed
Messages : 0 reported, 0 suppressed
Information contained in this publication regarding device applications and the like is intended for suggestion only and may be superseded by updates. No representation or warranty is given and no liability is assumed
by Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. Use of Microchip's products
as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights. The Microchip
logo and name are registered trademarks of Microchip Technology Inc. in the U.S.A. and other countries. All rights reserved. All other trademarks mentioned herein are the property of their respective companies.
1999 Microchip Technology Inc.
All rights reserved. ? 1999 Microchip Technology Incorporated. Printed in the USA. 11/99 Printed on recycled paper.
AMERICAS
Corporate Office
Microchip Technology Inc.
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-786-7200 Fax: 480-786-7277
Technical Support: 480-786-7627
Web Address: http://www.microchip.com
Atlanta
Microchip Technology Inc.
500 Sugar Mill Road, Suite 200B
Atlanta, GA 30350
Tel: 770-640-0034 Fax: 770-640-0307
Boston
Microchip Technology Inc.
5 Mount Royal Avenue
Marlborough, MA 01752
Tel: 508-480-9990 Fax: 508-480-8575
Chicago
Microchip Technology Inc.
333 Pierce Road, Suite 180
Itasca, IL 60143
Tel: 630-285-0071 Fax: 630-285-0075
Dallas
Microchip Technology Inc.
4570 Westgrove Drive, Suite 160
Addison, TX 75248
Tel: 972-818-7423 Fax: 972-818-2924
Dayton
Microchip Technology Inc.
Two Prestige Place, Suite 150
Miamisburg, OH 45342
Tel: 937-291-1654 Fax: 937-291-9175
Detroit
Microchip Technology Inc.
Tri-Atria Office Building
32255 Northwestern Highway, Suite 190
Farmington Hills, MI 48334
Tel: 248-538-2250 Fax: 248-538-2260
Los Angeles
Microchip Technology Inc.
18201 Von Karman, Suite 1090
Irvine, CA 92612
Tel: 949-263-1888 Fax: 949-263-1338
New York
Microchip Technology Inc.
150 Motor Parkway, Suite 202
Hauppauge, NY 11788
Tel: 631-273-5305 Fax: 631-273-5335
San Jose
Microchip Technology Inc.
2107 North First Street, Suite 590
San Jose, CA 95131
Tel: 408-436-7950 Fax: 408-436-7955
AMERICAS (continued)
Toronto
Microchip Technology Inc.
5925 Airport Road, Suite 200
Mississauga, Ontario L4V 1W1, Canada
Tel: 905-405-6279 Fax: 905-405-6253
ASIA/PACIFIC
Hong Kong
Microchip Asia Pacific
Unit 2101, Tower 2
Metroplaza
223 Hing Fong Road
Kwai Fong, N.T., Hong Kong
Tel: 852-2-401-1200 Fax: 852-2-401-3431
Beijing
Microchip Technology, Beijing
Unit 915, 6 Chaoyangmen Bei Dajie
Dong Erhuan Road, Dongcheng District
New China Hong Kong Manhattan Building
Beijing 100027 PRC
Tel: 86-10-85282100 Fax: 86-10-85282104
India
Microchip Technology Inc.
India Liaison Office
No. 6, Legacy, Convent Road
Bangalore 560 025, India
Tel: 91-80-229-0061 Fax: 91-80-229-0062
Japan
Microchip Technology Intl. Inc.
Benex S-1 6F
3-18-20, Shinyokohama
Kohoku-Ku, Yokohama-shi
Kanagawa 222-0033 Japan
Tel: 81-45-471- 6166 Fax: 81-45-471-6122
Korea
Microchip Technology Korea
168-1, Youngbo Bldg. 3 Floor
Samsung-Dong, Kangnam-Ku
Seoul, Korea
Tel: 82-2-554-7200 Fax: 82-2-558-5934
Shanghai
Microchip Technology
RM 406 Shanghai Golden Bridge Bldg.
2077 Yan'an Road West, Hong Qiao District
Shanghai, PRC 200335
Tel: 86-21-6275-5700 Fax: 86 21-6275-5060
ASIA/PACIFIC (continued)
Singapore
Microchip Technology Singapore Pte Ltd.
200 Middle Road
#07-02 Prime Centre
Singapore 188980
Tel: 65-334-8870 Fax: 65-334-8850
Taiwan, R.O.C
Microchip Technology Taiwan
10F-1C 207
Tung Hua North Road
Taipei, Taiwan, ROC
Tel: 886-2-2717-7175 Fax: 886-2-2545-0139
EUROPE
United Kingdom
Arizona Microchip Technology Ltd.
505 Eskdale Road
Winnersh Triangle
Wokingham
Berkshire, England RG41 5TU
Tel: 44 118 921 5858 Fax: 44-118 921-5835
Denmark
Microchip Technology Denmark ApS
Regus Business Centre
Lautrup hoj 1-3
Ballerup DK-2750 Denmark
Tel: 45 4420 9895 Fax: 45 4420 9910
France
Arizona Microchip Technology SARL
Parc d'Activite du Moulin de Massy
43 Rue du Saule Trapu
Batiment A - ler Etage
91300 Massy, France
Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79
Germany
Arizona Microchip Technology GmbH
Gustav-Heinemann-Ring 125
D-81739 M?nchen, Germany
Tel: 49-89-627-144 0 Fax: 49-89-627-144-44
Italy
Arizona Microchip Technology SRL
Centro Direzionale Colleoni
Palazzo Taurus 1 V. Le Colleoni 1
20041 Agrate Brianza
Milan, Italy
Tel: 39-039-65791-1 Fax: 39-039-6899883
11/15/99
WORLDWIDE SALES AND SERVICE
Microchip received QS-9000 quality system
certification for its worldwide headquarters,
design and wafer fabrication facilities in
Chandler and Tempe, Arizona in July 1999. The
Company's quality system processes and
procedures are QS-9000 compliant for its
PICmicro? 8-bit MCUs, KEELOQ? code hopping
devices, Serial EEPROMs and microperipheral
products. In addition, Microchip's quality
system for the design and manufacture of
development systems is ISO 9001 certified.