1                       ; This code was copied from Microchip Application Note AN593 --
2                       ; `Serial Port Routines Without Using the RTCC'
3                               
4                       ; These routines were written to work on the PICDEM1 hardware
5                       ; The frequency of the clock is 16MHz and the hardware uses no
6                       ; handshake
7                       ;     TX -> RA3
8                       ;     RX -> RA2
9                               
10                      indf: equ 0             ; Indirect File
11                      tmr0: equ 1             ; Timer 0
12                      pcl: equ 2              ; Program Counter Low
13                              
14                      status: equ 3           ; Status Register
15                      #define irp status,7    ; Indirect Register Bank Select
16                      #define rp1 status,6    ; Register Page 1
17                      #define rp0 status,6    ; Register Page 0
18                      #define to status,4     ; Time Out
19                      #define pd status,3     ; Power Down
20                      #define z status,2      ; Zero Flag
21                      #define dc status,1     ; Digital Carry Flag
22                      #define c status,0      ; Carry Flag
23                              
24                      fsr: equ 4              ; FSR
25                              
26                      porta: equ 5            ; Port A I/O Register
27                      #define ra5 porta,5     ; Port A Bit 5
28                      #define ra4 porta,4     ; Port A Bit 4
29                      #define ra3 porta,3     ; Port A Bit 3
30                      #define ra2 porta,2     ; Port A Bit 2
31                      #define ra1 porta,1     ; Port A Bit 1
32                      #define ra0 porta,0     ; Port A Bit 0
33                              
34                      portb: equ 6            ; Port B I/O Register
35                      #define rb7 portb,7     ; Port B Bit 7
36                      #define rb6 portb,6     ; Port B Bit 6
37                      #define rb5 portb,5     ; Port B Bit 5
38                      #define rb4 portb,4     ; Port B Bit 4
39                      #define rb3 portb,3     ; Port B Bit 3
40                      #define rb2 portb,2     ; Port B Bit 2
41                      #define rb1 portb,1     ; Port B Bit 1
42                      #define rb0 portb,0     ; Port B Bit 0
43                              
44                      portc: equ 6            ; Port B I/O Register
45                      #define rc7 portc,7     ; Port C Bit 7
46                      #define rc6 portc,6     ; Port C Bit 6
47                      #define rc5 portc,5     ; Port C Bit 5
48                      #define rc4 portc,4     ; Port C Bit 4
49                      #define rc3 portc,3     ; Port C Bit 3
50                      #define rc2 portc,2     ; Port C Bit 2
51                      #define rc1 portc,1     ; Port C Bit 1
52                      #define rc0 portc,0     ; Port C Bit 0
53                              
54                      option: equ 1           ; Option register
55                      #define rbpu option,7   ; Register B Pull-up (1=on;0=off)
56                      #define intedg option,6 ; Interrupt Edge Select (1=rising;0=falling)
57                      #define t0cs option,5   ; Timer 0 Clock Source Select (1=RA4;0=Ins. Clock)
58                      #define t0se option,4   ; Timer 0 Source Edge Select (1=falling;0=rising)
59                      #define psa option,3    ; Prescaler Assignment (1=WDT;0=Timer0)
60                      #define ps2 option,2    ; Prescaler Bit 2
61                      #define ps1 option,1    ; Prescaler Bit 1
62                      #define ps0 option,0    ; Prescaler Bit 0
63                              
64                      intcon: equ 0xe         ; Interrupt Control
65                      #define gie intcon,7    ; Global Interrupt Enable
66                      #define peie intcon,6   ; Peripheral Inerrupt Enable
67                      #define t0ie intcon,5   ; Timer 0 Overflow Interrupt Enable
68                      #define inte intcon,4   ; External Interrupt Enable
69                      #define rbie intcon,3   ; RB Port Change Interrupt Enable
70                      #define t0if intcon,2   ; TMR0 Overflow Interrupt Enable
71                      #define intf intcon,1   ; RB0/INT External Interrupt Enable
72                      #define rbif intcon,0   ; RB7:RB4 Port Change Status
73                              
74                      last_program_address: equ 0xfff; Last program address
75                              
76                              
77                              processor pic16c63a
78                              configure osc=xt, wdte=off, pwrte=off, cp=off, mclre=off, bowden=off
79                              
80                      clockrate: equ 16000000
81                      baudrate: equ 9600
82                              
83                      fclk: equ (clockrate/4)
84                      ; The value baudconst must be an 8-bit value only
85                      baudconst: equ (((fclk/baudrate)/3)-2)
86                      count: equ 0x10
87                      txreg: equ 0x11
88                      rcreg: equ 0x11
89                      delay: equ 0x12
90                      tempa: equ 0x13
91                      hi: equ 0x10
92                      lo: equ 0x11
93                      gpram: equ 0x15
94                      #define tx porta,3
95                      #define rx porta,2
96                      #define tempa7 tempa,7
97                              org 0x0
98                      start:
99   0000 205a                  call wait
100  0001 300f                  movlw 0xf
101  0002 0085                  movwf porta
102  0003 3007                  movlw 0x7
103  0004 0065                  tris porta
104  0005 0186                  clrf portb
105  0006 0100                  clrw 
106  0007 0066                  tris portb
107  0008 30a5                  movlw 0xa5
108  0009 0615                  xorwf gpram,w
109  000a 1903                  btfsc z
110  000b 2064                  call mclr
111  000c 30a5                  movlw 0xa5
112  000d 0095                  movwf gpram
113                             
114                             
115  000e 302f                  movlw 0x2f
116  000f 0093                  movwf tempa
117  0010 3038                  movlw 0x38
118  0011 0062                  option 
119  0012 0000                  nop             ; clrf rtcc
120                     s1check:
121  0013 0000                  nop             ; movf rtcc,w
122  0014 1903                  btfsc z         ; if S1 pressed then skip
123  0015 2813                  goto s1check
124                             
125                     next:
126  0016 0a93                  incf tempa,f
127  0017 1b93                  btfsc tempa7
128  0018 2861                  goto AllDone
129  0019 0813                  movf tempa,w
130  001a 2020                  call transmit
131  001b 2037                  call receive
132  001c 0213                  subwf tempa,w
133  001d 1903                  btfsc z
134  001e 2847                  goto fail
135  001f 2816                  goto next
136                             
137                             
138                     transmit:
139  0020 0091                  movwf txreg
140  0021 1185                  bcf tx          ; send start bit
141  0022 3088                  movlw baudconst
142  0023 0092                  movwf delay
143  0024 3009                  movlw 9
144  0025 0090                  movwf count
145                     txbaudwait:
146  0026 0b92                  decfsz delay,f
147  0027 2826                  goto txbaudwait
148  0028 3088                  movlw baudconst
149  0029 0092                  movwf delay
150  002a 0b90                  decfsz count,f
151  002b 2830                  goto SendNextBit
152  002c 3009                  movlw 9
153  002d 0090                  movwf count
154  002e 1585                  bsf tx
155  002f 3400                  retlw 0
156                     SendNextBit:
157  0030 0c91                  rrf txreg,f
158  0031 1c03                  btfss c
159  0032 2835                  goto setlo
160  0033 1585                  bsf tx
161  0034 2826                  goto txbaudwait
162                     setlo:
163  0035 1185                  bcf tx
164  0036 2826                  goto txbaudwait
165                             
166                             
167                     receive:
168  0037 1905                  btfsc rx        ; wait for receive
169  0038 2837                  goto receive
170                     rxbaudwait:
171  0039 0b92                  decfsz delay,f
172  003a 2839                  goto rxbaudwait
173  003b 3088                  movlw baudconst
174  003c 0092                  movwf delay
175  003d 0b90                  decfsz count,f
176  003e 2842                  goto RecvNextBit
177  003f 3009                  movlw 9
178  0040 0090                  movwf count
179  0041 3400                  retlw 0
180                     RecvNextBit:
181  0042 1003                  bcf c
182  0043 1905                  btfsc rx
183  0044 1403                  bsf c
184  0045 0c91                  rrf rcreg,f
185  0046 2839                  goto rxbaudwait
186                             
187                     fail:
188  0047 0986                  comf portb,f
189  0048 204a                  call halfsec
190  0049 2847                  goto fail
191                     halfsec:
192  004a 0190                  clrf hi
193  004b 0191                  clrf lo
194                     hsloop:
195  004c 0000                  nop 
196  004d 0000                  nop 
197  004e 0000                  nop 
198  004f 0000                  nop 
199  0050 0000                  nop 
200  0051 0000                  nop 
201  0052 0000                  nop 
202  0053 0000                  nop 
203  0054 0000                  nop 
204  0055 0b91                  decfsz lo,f
205  0056 284c                  goto hsloop
206  0057 0b90                  decfsz hi,f
207  0058 284c                  goto hsloop
208  0059 3400                  retlw 0
209                             
210                     wait:
211  005a 0190                  clrf hi
212  005b 0191                  clrf lo
213                     dly:
214  005c 0b91                  decfsz lo,f
215  005d 285c                  goto dly
216  005e 0b90                  decfsz hi,f
217  005f 285c                  goto dly
218  0060 3400                  retlw 0
219                             
220                     AllDone:
221  0061 3055                  movlw 0x55
222  0062 0086                  movwf portb
223                     inifinite:
224  0063 2863                  goto inifinite
225                             
226                     mclr:
227  0064 0186                  clrf portb
228  0065 0386                  decf portb,f
229  0066 0195                  clrf gpram
230                     s3check:
231  0067 1885                  btfsc ra1
232  0068 2867                  goto s3check
233  0069 0186                  clrf portb
234                     infloop:
235  006a 286a                  goto infloop
236                             org 0x1ff
237  01ff 2800                  goto start
238                             end 
