gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 1 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00001 ; Model A1 Programmer Adapter for the Model 01a Control Engine. 00002 ; 00003 ; $Id: ma1.asm,v 1.2 2003/11/17 01:01:31 tomj Exp tomj $ 00004 00005 ; DESCRIPTION: 00006 ; 00007 ; Programming adapter for the Model 01 revision A board, using the 00008 ; in-circuit PROG connector. The host talks to this adapter via 00009 ; serial interface. Supports PIC16F627A/628A/648A. 00010 00011 ; INTERFACE: 00012 ; 00013 ; To the host: Serial, 115200 baud, 8 data bits, no parity; TxD, RxD. 00014 ; To the target: 6-wire .1" SIP jack. 00015 ; 00016 ; HARDWARE: 00017 ; 00018 ; This assumes the Model A1 Programming Adapter hardware, which 00019 ; consists of a PIC16F627A, a MAX662A to make the flash programming 00020 ; voltage, and a transistor to shutdown the target VDD. 00021 ; 00022 ; The hardware is glitch-proof; it can be plugged on or off at 00023 ; any time it's in IDLEMODE (eg. not actively programming the 00024 ; target). +12V is clamped and shutdown by default, and the 00025 ; driving PIC pins are left as inputs when not actively 00026 ; driving +12V on. 00027 ; 00028 ; This code runs in a PIC16F627A or better. A 18.432 MHz crystal 00029 ; is required, or you'll have to adjust the software delay routines 00030 ; and the UART BRG values. See schematic for details. 00031 ; 00032 ; SOFTWARE: 00033 ; 00034 ; The programmer is structured such that the PC side performs 00035 ; the Microchip-recommended algorithm, while this adapter 00036 ; handles the bit-twiddling and hardware interface. A command 00037 ; line Perl script should be just fine. The programmer should 00038 ; be forward-compatible with future products and can identify 00039 ; programmer version and PIC-in-place. 00040 ; 00041 ; The PC sends one or three bytes to this programmer (see below), 00042 ; a command is issued to the PIC, 00043 ; when complete and ready for the next command, the PIC 00044 ; issues an ACK character. 00045 00046 ; PIC commands seem to fall into three categories: 00047 ; A. Command word, no args (PROGRAM, INCREMENT PC, etc) 00048 ; B. Command word, two byte arg (LOAD DATA) 00049 ; C. Command word, reads two bytes (READ DATA) 00050 00051 ; NOTE: This code does the required programming delays; the host 00052 ; can simply issue commands as fast as possible. 00053 00054 ; This code does no procedural work nor massaging of gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 2 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00055 ; data. The host-side needs to put the fill 0's in the 00056 ; data for type B and C commands and all that. 00057 00058 ; PIC commands are given an ASCII offset; PIC commands 0 - 63 are 00059 ; offset to 32 to 95, eg. nice clean ASCII. Programmer commands 00060 ; are offset by 65 -- not 64! -- starting with 'a'. 00061 00062 ; ASCII enc offset 00063 ; 32 - 95 +32 Microchip Inc commands 00064 ; 65 - 126 +65 (not 64! skip `) M01-programmer commands 00065 00066 ; ASCII PIC TYPE 00067 ; LOADCONF= space 0 B load config 00068 ; ! 1 n/a 00069 ; LOADPMEM= " 2 B load program memory 00070 ; LOADDMEM= # 3 B load data memory 00071 ; READPMEM= $ 4 C read program memory 00072 ; READDMEM= % 5 C read data memory 00073 ; INCREMENT= & 6 A increment address 00074 ; ' 7 n/a 00075 ; PROGRAM= ( 8 A program cycle start 00076 ; BULKPROG= ) 9 A bulk erase program memory 00077 ; * 10 n/a 00078 ; BULKDATA= + 11 A bulk erase data memory 00079 00080 ; Programmer commands. 00081 ; VERSION= A 0 C returns programmer version 00082 ; IDLE= B 1 A reset target to idle 00083 ; PROGMODE= C 2 A set target to program mode 00084 ; NOOP= D 3 A guarenteed to be ignored 00085 ; ACKON E 4 A NOT USED enable ACK every command, 00086 ; ACKOFF F 5 A NOT USED disable ACK every command, 00087 ; RESET G 6 A reset target 00088 00089 ; These Microchip Inc recommended timing values are 00090 ; hard-coded in the code. 00091 ; 00092 ; THLD0= 5uS data hold time 00093 ; TDLY1= 1uS post-command delay 00094 ; TDLY2= 1uS min. time -clock to +clock 00095 ; TDIS= 0.5uS prog to compare time 00096 ; TPROG= 2.5 prog cycle time 00097 ; TDPROG= 6 data EE prog time 00098 ; TERA= 6 bulk erase time 00099 00100 00101 00102 00103 processor 16f628a 00104 include "p16f628a.inc" 00001 LIST 00002 ; P16F628A.INC Standard Header File, Version 1.00 Microchip Technology, Inc. 00258 LIST 00105 ; __CONFIG 0x3f62 ; HS oscillator, /MCLR gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 3 LOC OBJECT CODE LINE SOURCE TEXT VALUE 002007 3F42 00106 __CONFIG 0x3f42 ; HS oscillator sans /MCLR pin 00107 errorlevel -302 ; MOST annoying. 00108 00109 radix dec 00110 00111 ; Slightly configurable items. 00112 00113 constant VERS= "2" 00114 constant SPEED= 9 ; see comments in SERinit() 00115 constant ACKCHAR= "_" ; character that ACKs each command 00116 00117 ; Defined by the PCB. 00118 00119 00120 constant DATA= 7 ; PORTB pin, to target pin 13, RB7/DATA 00121 constant CLOCK= 6 ; PORTB pin, to target pin 12, RB6/CLOCK 00122 constant RB4CLAMP= 5 ; PORTB pin, to target pin 10, RB4/PGM 00123 constant HVCLAMP= 4 ; PORTB HV clamp, grounds target /MCLR 00124 constant SHORTVDD= 3 ; PORTB short Vdd, 00125 constant RXD= 1 ; PORTB hardware UART RX, 00126 constant TXD= 2 ; PORTB hardware UART TX, 00127 constant HVSHDN= 0 ; PORTB HV MAX662A SHDN pin (shutdown) 00128 00129 ; ------------------------------------------------------------- 00130 00131 ; Variables. 00132 00133 constant __DC1= 32 ; delay counter (inner), 00134 constant __DC2= 33 ; delay counter (outer), 00135 constant BYTE0= 34 ; command from host, 00136 constant BYTE1= 35 ; command arg1 00137 constant BYTE2= 36 ; command arg2 00138 00139 ; ------------------------------------------------------------- 00140 0000 00141 org 0 00142 00143 ; Hardware init; simple. We set up CTS then branch to idlemode(), 00144 ; which branches to cmdloop. 00145 0000 1683 1303 00146 banksel TRISA 0002 1283 1303 00147 banksel PORTA 00148 0004 3009 00149 movlw SPEED 0005 218E 00150 call serinit ; set up USART, 0006 2841 00151 goto idlemode ; setup hardware and reset, 00152 00153 00154 00155 ; Wait for commands from the PC and execute them. 00156 0007 00157 cmdloop 0007 305F 00158 movlw ACKCHAR ; send the ACK character, 0008 219C 00159 call serout ; gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 4 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0009 21A2 00160 call serin ; get command, 00161 00162 ; Remove the ASCII bias and attempt to dispatch. If it's still 00163 ; out of range, it must be a programmer command. 00164 000A 3EE0 00165 addlw -32 ; subtract ASCII " " offset, 000B 00A2 00166 movwf BYTE0 ; (save because we ruin it) 000C 1C03 00167 btfss STATUS, C ; (test < 0) 000D 2820 00168 goto cmd2 ; branch if out of range 000E 3EF4 00169 addlw -12 ; (ruins cmd offset) 000F 1803 00170 btfsc STATUS, C ; (test > 12) 0010 2820 00171 goto cmd2 ; branch if out of range 00172 00173 ; All PCL/PCLATH branches must be contained in program 00174 ; addresses 0 - 0xff... 00175 0011 018A 00176 clrf PCLATH ; 0012 0822 00177 movf BYTE0, w ; fetch command offset, 0013 0782 00178 addwf PCL, f ; 00179 00180 ; PIC programming commands. 00181 0014 28AD 00182 goto typeB ; 0 LOADCONFIG 0015 2807 00183 goto cmdloop ; 1 n/a 0016 28AD 00184 goto typeB ; 2 LOADPMEM 0017 28AD 00185 goto typeB ; 3 LOADDMEM 0018 2927 00186 goto typeC ; 4 READPMEM 0019 2927 00187 goto typeC ; 5 READDMEM 001A 2889 00188 goto typeA ; 6 INCREMENT 001B 2807 00189 goto cmdloop ; 7 n/a 001C 2881 00190 goto typeA25 ; 8 PROGRAM 001D 2885 00191 goto typeA6 ; 9 BULKPROG 001E 2807 00192 goto cmdloop ; 10 n/a 001F 2885 00193 goto typeA6 ; 11 BULKDATA 00194 00195 ; Not a PIC command; must be a programmer command. 00196 0020 0822 00197 cmd2 movf BYTE0, w ; try again, 0021 3EDF 00198 addlw -33 ; subtract remaining ASCII "A" offset, 0022 00A2 00199 movwf BYTE0 ; (because we ruin it again) 0023 1C03 00200 btfss STATUS, C 0024 2807 00201 goto cmdloop ; branch if out of range 0025 3EF9 00202 addlw -7 ; 0026 1803 00203 btfsc STATUS, C 0027 2807 00204 goto cmdloop ; branch if out of range 00205 0028 018A 00206 clrf PCLATH 0029 0822 00207 movf BYTE0, w 002A 0782 00208 addwf PCL, f 00209 002B 2832 00210 goto version ; A VERSION 002C 2841 00211 goto idlemode ; B IDLEMODE 002D 285A 00212 goto progmode ; C PROGMODE 002E 2807 00213 goto cmdloop ; D NOOP gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 5 LOC OBJECT CODE LINE SOURCE TEXT VALUE 002F 2807 00214 goto cmdloop ; E NOOP 0030 2807 00215 goto cmdloop ; F NOOP 0031 287C 00216 goto reset ; G RESET 00217 00218 00219 00220 ; ================================================================= 00221 00222 ; Output version string, terminated with CR LF. 00223 0032 304D 00224 version movlw "M" 0033 219C 00225 call serout 0034 3041 00226 movlw "A" 0035 219C 00227 call serout 0036 3031 00228 movlw "1" 0037 219C 00229 call serout 0038 302D 00230 movlw "-" 0039 219C 00231 call serout 003A 3032 00232 movlw VERS 003B 219C 00233 call serout 003C 300D 00234 movlw 13 003D 219C 00235 call serout 003E 300A 00236 movlw 10 003F 219C 00237 call serout 0040 2807 00238 goto cmdloop 00239 00240 ; Make the A1 programmer inert by making the interface pins be 00241 ; inputs and resetting the target. The target PIC then runs. 00242 ; The programmer won't glitch HV on if it's powered off in 00243 ; the IDLEMODE state, as the HV outputs are high impedance 00244 ; already (same as powerdown state). 00245 0041 00246 idlemode 00247 00248 ; First, remove +12V from the target's /MCLR, then short VDD. 00249 0041 1683 1303 00250 banksel TRISB 0043 1206 00251 bcf TRISB, HVCLAMP ; (this is the port initialization) 0044 1186 00252 bcf TRISB, SHORTVDD ; make these outputs, 00253 0045 1283 1303 00254 banksel PORTB 0047 1606 00255 bsf PORTB, HVCLAMP ; clamp /MCLR to ground, 0048 1586 00256 bsf PORTB, SHORTVDD ; clamp VDD to ground, 00257 00258 ; Wait for the target's VDD to drain and become inert. 00259 0049 3005 00260 movlw 5 ; 50 mS, 004A 2188 00261 call dly10mS ; let Vdd drain to zero, 00262 00263 ; Before we bring the target up, make the programmer interface 00264 ; inert by making our pins inputs. This has the side effect of 00265 ; leaving the adapter ready for power off. 00266 004B 1683 1303 00267 banksel TRISB gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 6 LOC OBJECT CODE LINE SOURCE TEXT VALUE 004D 1706 00268 bsf TRISB, CLOCK ; make these all inputs, 004E 1786 00269 bsf TRISB, DATA ; eg. inert, 004F 1686 00270 bsf TRISB, RB4CLAMP ; pulled up pins then go high, 00271 00272 ; Note that we leave like HVSHDN as input, and rely on the 00273 ; external pullups to keep things inert; this is much safer than 00274 ; relying on the PIC pin staying high to do the job. If power 00275 ; is removed ungracefully from the Adapter 00276 ; the pins become high-impedance as 00277 ; brownout-detect trips when local Vdd droops. The same is true for 00278 ; HVCLAMP; as local Vdd droops, the MAX662A will already be shutdown, 00279 ; but at brownout time HVCLAMP will assert also.) 00280 0050 1406 00281 bsf TRISB, HVSHDN ; MAX662A goes into shutdown (+5V out) 00282 00283 ; With /MCLR still clamped to ground, bring VDD up and wait 00284 ; for everything to settle. 00285 0051 1283 1303 00286 banksel PORTB 0053 1186 00287 bcf PORTB, SHORTVDD ; let Vdd rise, 0054 3005 00288 movlw 5 0055 2188 00289 call dly10mS ; let everything restart, 00290 00291 ; Now let /MCLR return to VDD, allowing the target to come up. 00292 0056 1206 00293 bcf PORTB, HVCLAMP ; let /MCLR rise to Vdd normally, 0057 3001 00294 movlw 1 0058 2188 00295 call dly10mS ; 0059 2807 00296 goto cmdloop 00297 00298 00299 ; Put the A1 programmer into program mode. First we reset 00300 ; the target, then power it off by shorting it's Vdd (and not 00301 ; taking too long...). The CLOCK, DATA and RB4CLAMP lines are 00302 ; asserted idle, Vihh turned on (MAX662A) and allowed to settle, 00303 ; power is restored, and the chip taken out of reset. This is 00304 ; the manufacturers recommended sequence with generous time 00305 ; allowed. 00306 00307 ; Note that HVSHDN and HVCLAMP are inputs when the Model A1 00308 ; is idle; we make them outputs only during the programming 00309 ; sequence. This adds a safety margin; should the Model A1 00310 ; be turned off while still attached to the target, 00311 ; no HV will be applied to the target: as the A1 CPU 00312 ; browns out, the pins become inputs, allowing HVSHDN 00313 ; and SHORTVDD to float high, and the pullups on the 00314 ; transistors will keep the target safe. 00315 005A 00316 progmode 00317 00318 ; Reset the target (/MCLR low) then turn off it's power. 00319 005A 1283 1303 00320 banksel PORTB 005C 1606 00321 bsf PORTB, HVCLAMP ; drive /MCLR low, resetting target, gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 7 LOC OBJECT CODE LINE SOURCE TEXT VALUE 005D 3001 00322 movlw 1 005E 2180 00323 call dly100uS ; reset first, 00324 005F 1586 00325 bsf PORTB, SHORTVDD ; drive VDD low, power down, 0060 3005 00326 movlw 5 0061 2188 00327 call dly10mS ; wait 50mS for power to drain, 00328 0062 1683 1303 00329 banksel TRISB ; target is OFF, 0064 1386 00330 bcf TRISB, DATA ; make these lines outputs 0065 1306 00331 bcf TRISB, CLOCK ; 0066 1286 00332 bcf TRISB, RB4CLAMP 0067 1006 00333 bcf TRISB, HVSHDN 00334 0068 1283 1303 00335 banksel PORTB 006A 1386 00336 bcf PORTB, DATA ; set DATA and CLOCK initial states, 006B 1306 00337 bcf PORTB, CLOCK ; 006C 1286 00338 bcf PORTB, RB4CLAMP ; hold RB4 low, 00339 006D 1006 00340 bcf PORTB, HVSHDN ; turn on MAX662A, 006E 3005 00341 movlw 5 ; let it settle, 006F 2180 00342 call dly100uS 00343 0070 1283 1303 00344 banksel PORTB 0072 1206 00345 bcf PORTB, HVCLAMP ; let /MCLR rise to +12V 0073 0000 00346 nop ; (Vihh) 0074 0000 00347 nop ; wait one microsecond, 0075 0000 00348 nop 0076 0000 00349 nop 0077 0000 00350 nop 00351 0078 1186 00352 bcf PORTB, SHORTVDD ; un-short Vdd 0079 3005 00353 movlw 5 ; let everything settle. 007A 2188 00354 call dly10mS 00355 007B 2807 00356 goto cmdloop 00357 00358 ; Reset the target. This assumes that IDLEMODE has been called 00359 ; before to init things. 00360 007C 1606 00361 reset: bsf PORTB, HVCLAMP ; drag /MCLR to ground, 007D 300A 00362 movlw 10 007E 2180 00363 call dly100uS ; for one mS, 007F 1206 00364 bcf PORTB, HVCLAMP ; release it. 0080 2807 00365 goto cmdloop 00366 00367 ; ================================================================= 00368 00369 ; Type A command with 2.5mS execution time. 00370 0081 00371 typeA25 0081 208B 00372 call typeAx ; issue the command, 0082 3019 00373 movlw 25 ; 10th-mS increments, 0083 2180 00374 call dly100uS 0084 2807 00375 goto cmdloop gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 8 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00376 00377 ; Type A commands with a 6mS execution time. 00378 0085 208B 00379 typeA6 call typeAx 0086 303C 00380 movlw 60 0087 2180 00381 call dly100uS 0088 2807 00382 goto cmdloop 00383 00384 ; Type A commands with no delay. 00385 0089 208B 00386 typeA call typeAx 008A 2807 00387 goto cmdloop 00388 00389 00390 ; Local subroutine: 00391 ; 00392 ; Output 6 bits of BYTE0 to the target PIC. This brute force 00393 ; unwound loop is as fast as a PIC will go. 00394 008B 1283 1303 00395 typeAx banksel PORTB 00396 008D 1386 00397 bcf PORTB, DATA ; set data 0 008E 1822 00398 btfsc BYTE0, 0 ; or 1 008F 1786 00399 bsf PORTB, DATA 0090 1706 00400 bsf PORTB, CLOCK ; assert clock, 0091 1306 00401 bcf PORTB, CLOCK ; 00402 0092 1386 00403 bcf PORTB, DATA 0093 18A2 00404 btfsc BYTE0, 1 0094 1786 00405 bsf PORTB, DATA 0095 1706 00406 bsf PORTB, CLOCK 0096 1306 00407 bcf PORTB, CLOCK 00408 0097 1386 00409 bcf PORTB, DATA 0098 1922 00410 btfsc BYTE0, 2 0099 1786 00411 bsf PORTB, DATA 009A 1706 00412 bsf PORTB, CLOCK 009B 1306 00413 bcf PORTB, CLOCK 00414 009C 1386 00415 bcf PORTB, DATA 009D 19A2 00416 btfsc BYTE0, 3 009E 1786 00417 bsf PORTB, DATA 009F 1706 00418 bsf PORTB, CLOCK 00A0 1306 00419 bcf PORTB, CLOCK 00420 00A1 1386 00421 bcf PORTB, DATA 00A2 1A22 00422 btfsc BYTE0, 4 00A3 1786 00423 bsf PORTB, DATA 00A4 1706 00424 bsf PORTB, CLOCK 00A5 1306 00425 bcf PORTB, CLOCK 00426 00A6 1386 00427 bcf PORTB, DATA 00A7 1AA2 00428 btfsc BYTE0, 5 00A8 1786 00429 bsf PORTB, DATA gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 9 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00A9 1706 00430 bsf PORTB, CLOCK 00AA 1306 00431 bcf PORTB, CLOCK 00432 00AB 1386 00433 bcf PORTB, DATA 00AC 0008 00434 return 00435 00436 00437 00438 ; Fetch two bytes from the host, then output 00439 ; the command and two bytes to the target PIC. 00440 00AD 1283 1303 00441 typeB banksel PORTB 00442 ; bsf PORTA, CTS ; tell host we're ready, 00443 00AF 21A2 00444 call serin ; read LS byte, 00B0 00A3 00445 movwf BYTE1 ; 00B1 21A2 00446 call serin ; read MS byte, 00B2 00A4 00447 movwf BYTE2 00448 00449 ; bcf PORTA, CTS ; tell host to wait, 00450 00451 ; Output 6 bits of BYTE0, 16 bits of BYTE1:BYTE2 00452 ; to the target PIC. This brute force unwound 00453 ; loop is as fast as a PIC will go. 00454 00B3 1386 00455 bcf PORTB, DATA ; set data 0 00B4 1822 00456 btfsc BYTE0, 0 ; or 1 00B5 1786 00457 bsf PORTB, DATA 00B6 1706 00458 bsf PORTB, CLOCK ; assert clock, 00B7 1306 00459 bcf PORTB, CLOCK ; 00460 00B8 1386 00461 bcf PORTB, DATA 00B9 18A2 00462 btfsc BYTE0, 1 00BA 1786 00463 bsf PORTB, DATA 00BB 1706 00464 bsf PORTB, CLOCK 00BC 1306 00465 bcf PORTB, CLOCK 00466 00BD 1386 00467 bcf PORTB, DATA 00BE 1922 00468 btfsc BYTE0, 2 00BF 1786 00469 bsf PORTB, DATA 00C0 1706 00470 bsf PORTB, CLOCK 00C1 1306 00471 bcf PORTB, CLOCK 00472 00C2 1386 00473 bcf PORTB, DATA 00C3 19A2 00474 btfsc BYTE0, 3 00C4 1786 00475 bsf PORTB, DATA 00C5 1706 00476 bsf PORTB, CLOCK 00C6 1306 00477 bcf PORTB, CLOCK 00478 00C7 1386 00479 bcf PORTB, DATA 00C8 1A22 00480 btfsc BYTE0, 4 00C9 1786 00481 bsf PORTB, DATA 00CA 1706 00482 bsf PORTB, CLOCK 00CB 1306 00483 bcf PORTB, CLOCK gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 10 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00484 00CC 1386 00485 bcf PORTB, DATA 00CD 1AA2 00486 btfsc BYTE0, 5 00CE 1786 00487 bsf PORTB, DATA 00CF 1706 00488 bsf PORTB, CLOCK 00D0 1306 00489 bcf PORTB, CLOCK 00490 00491 ; Tdly2; 1 uS. 00492 00D1 0000 00493 nop 00D2 0000 00494 nop 00D3 0000 00495 nop 00D4 0000 00496 nop 00497 00498 ; Output 16 bits in BYTE1:BYTE2 to the target PIC. 00499 00D5 1386 00500 bcf PORTB, DATA 00D6 1823 00501 btfsc BYTE1, 0 00D7 1786 00502 bsf PORTB, DATA 00D8 1706 00503 bsf PORTB, CLOCK 00D9 1306 00504 bcf PORTB, CLOCK 00505 00DA 1386 00506 bcf PORTB, DATA 00DB 18A3 00507 btfsc BYTE1, 1 00DC 1786 00508 bsf PORTB, DATA 00DD 1706 00509 bsf PORTB, CLOCK 00DE 1306 00510 bcf PORTB, CLOCK 00511 00DF 1386 00512 bcf PORTB, DATA 00E0 1923 00513 btfsc BYTE1, 2 00E1 1786 00514 bsf PORTB, DATA 00E2 1706 00515 bsf PORTB, CLOCK 00E3 1306 00516 bcf PORTB, CLOCK 00517 00E4 1386 00518 bcf PORTB, DATA 00E5 19A3 00519 btfsc BYTE1, 3 00E6 1786 00520 bsf PORTB, DATA 00E7 1706 00521 bsf PORTB, CLOCK 00E8 1306 00522 bcf PORTB, CLOCK 00523 00E9 1386 00524 bcf PORTB, DATA 00EA 1A23 00525 btfsc BYTE1, 4 00EB 1786 00526 bsf PORTB, DATA 00EC 1706 00527 bsf PORTB, CLOCK 00ED 1306 00528 bcf PORTB, CLOCK 00529 00EE 1386 00530 bcf PORTB, DATA 00EF 1AA3 00531 btfsc BYTE1, 5 00F0 1786 00532 bsf PORTB, DATA 00F1 1706 00533 bsf PORTB, CLOCK 00F2 1306 00534 bcf PORTB, CLOCK 00535 00F3 1386 00536 bcf PORTB, DATA 00F4 1B23 00537 btfsc BYTE1, 6 gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 11 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00F5 1786 00538 bsf PORTB, DATA 00F6 1706 00539 bsf PORTB, CLOCK 00F7 1306 00540 bcf PORTB, CLOCK 00541 00F8 1386 00542 bcf PORTB, DATA 00F9 1BA3 00543 btfsc BYTE1, 7 00FA 1786 00544 bsf PORTB, DATA 00FB 1706 00545 bsf PORTB, CLOCK 00FC 1306 00546 bcf PORTB, CLOCK 00547 00548 ; BYTE2 00549 00FD 1386 00550 bcf PORTB, DATA 00FE 1824 00551 btfsc BYTE2, 0 00FF 1786 00552 bsf PORTB, DATA 0100 1706 00553 bsf PORTB, CLOCK 0101 1306 00554 bcf PORTB, CLOCK 00555 0102 1386 00556 bcf PORTB, DATA 0103 18A4 00557 btfsc BYTE2, 1 0104 1786 00558 bsf PORTB, DATA 0105 1706 00559 bsf PORTB, CLOCK 0106 1306 00560 bcf PORTB, CLOCK 00561 0107 1386 00562 bcf PORTB, DATA 0108 1924 00563 btfsc BYTE2, 2 0109 1786 00564 bsf PORTB, DATA 010A 1706 00565 bsf PORTB, CLOCK 010B 1306 00566 bcf PORTB, CLOCK 00567 010C 1386 00568 bcf PORTB, DATA 010D 19A4 00569 btfsc BYTE2, 3 010E 1786 00570 bsf PORTB, DATA 010F 1706 00571 bsf PORTB, CLOCK 0110 1306 00572 bcf PORTB, CLOCK 00573 0111 1386 00574 bcf PORTB, DATA 0112 1A24 00575 btfsc BYTE2, 4 0113 1786 00576 bsf PORTB, DATA 0114 1706 00577 bsf PORTB, CLOCK 0115 1306 00578 bcf PORTB, CLOCK 00579 0116 1386 00580 bcf PORTB, DATA 0117 1AA4 00581 btfsc BYTE2, 5 0118 1786 00582 bsf PORTB, DATA 0119 1706 00583 bsf PORTB, CLOCK 011A 1306 00584 bcf PORTB, CLOCK 00585 011B 1386 00586 bcf PORTB, DATA 011C 1B24 00587 btfsc BYTE2, 6 011D 1786 00588 bsf PORTB, DATA 011E 1706 00589 bsf PORTB, CLOCK 011F 1306 00590 bcf PORTB, CLOCK 00591 gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 12 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0120 1386 00592 bcf PORTB, DATA 0121 1BA4 00593 btfsc BYTE2, 7 0122 1786 00594 bsf PORTB, DATA 0123 1706 00595 bsf PORTB, CLOCK 0124 1306 00596 bcf PORTB, CLOCK 00597 0125 1386 00598 bcf PORTB, DATA ; pretty on the 'scope. 0126 2807 00599 goto cmdloop 00600 00601 00602 ; Output the 6-bit command, read 16 bits from the target PIC and 00603 ; output them to the host. Another unwound loop. 00604 ; 00605 ; The target PIC switches it's data input pin (RB7) 00606 ; to an output on the 2nd rising clock edge. 00607 0127 208B 00608 typeC call typeAx ; issue command first, 00609 0128 01A3 00610 clrf BYTE1 ; must clear; we only 0129 01A4 00611 clrf BYTE2 ; BSF if DATA=1 00612 00613 ; The documentation calls this first bit the "start bit", 00614 ; and it's definitely ignored in the host software. 00615 ; Don't bother actually reading it, just clock it, 00616 ; the DATA pin isn't even an input yet. 00617 012A 1706 00618 bsf PORTB, CLOCK ; +clock, target asserts data, 012B 1306 00619 bcf PORTB, CLOCK ; remove clock, Tdly3 00620 ; btfsc PORTB, DATA 00621 ; bsf BYTE1, 0 00622 00623 ; Now make our DATA an input. 00624 012C 1683 1303 00625 banksel TRISB 012E 1786 00626 bsf TRISB, DATA ; make DATA an input, 012F 1283 1303 00627 banksel PORTB 00628 00629 ; Now read the remaining 15 bits; the MS bit is 00630 ; called the "stop bit" and is also ignored. 00631 ; (That's bit7 of BYTE2.) 00632 0131 1706 00633 bsf PORTB, CLOCK ; target changes its DATA to be 0132 1306 00634 bcf PORTB, CLOCK ; an output here 0133 1B86 00635 btfsc PORTB, DATA 0134 14A3 00636 bsf BYTE1, 1 00637 0135 1706 00638 bsf PORTB, CLOCK 0136 1306 00639 bcf PORTB, CLOCK 0137 1B86 00640 btfsc PORTB, DATA 0138 1523 00641 bsf BYTE1, 2 00642 0139 1706 00643 bsf PORTB, CLOCK 013A 1306 00644 bcf PORTB, CLOCK 013B 1B86 00645 btfsc PORTB, DATA gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 13 LOC OBJECT CODE LINE SOURCE TEXT VALUE 013C 15A3 00646 bsf BYTE1, 3 00647 013D 1706 00648 bsf PORTB, CLOCK 013E 1306 00649 bcf PORTB, CLOCK 013F 1B86 00650 btfsc PORTB, DATA 0140 1623 00651 bsf BYTE1, 4 00652 0141 1706 00653 bsf PORTB, CLOCK 0142 1306 00654 bcf PORTB, CLOCK 0143 1B86 00655 btfsc PORTB, DATA 0144 16A3 00656 bsf BYTE1, 5 00657 0145 1706 00658 bsf PORTB, CLOCK 0146 1306 00659 bcf PORTB, CLOCK 0147 1B86 00660 btfsc PORTB, DATA 0148 1723 00661 bsf BYTE1, 6 00662 0149 1706 00663 bsf PORTB, CLOCK 014A 1306 00664 bcf PORTB, CLOCK 014B 1B86 00665 btfsc PORTB, DATA 014C 17A3 00666 bsf BYTE1, 7 00667 00668 ; BYTE2 00669 014D 1706 00670 bsf PORTB, CLOCK 014E 1306 00671 bcf PORTB, CLOCK 014F 1B86 00672 btfsc PORTB, DATA 0150 1424 00673 bsf BYTE2, 0 00674 0151 1706 00675 bsf PORTB, CLOCK 0152 1306 00676 bcf PORTB, CLOCK 0153 1B86 00677 btfsc PORTB, DATA 0154 14A4 00678 bsf BYTE2, 1 00679 0155 1706 00680 bsf PORTB, CLOCK 0156 1306 00681 bcf PORTB, CLOCK 0157 1B86 00682 btfsc PORTB, DATA 0158 1524 00683 bsf BYTE2, 2 00684 0159 1706 00685 bsf PORTB, CLOCK 015A 1306 00686 bcf PORTB, CLOCK 015B 1B86 00687 btfsc PORTB, DATA 015C 15A4 00688 bsf BYTE2, 3 00689 015D 1706 00690 bsf PORTB, CLOCK 015E 1306 00691 bcf PORTB, CLOCK 015F 1B86 00692 btfsc PORTB, DATA 0160 1624 00693 bsf BYTE2, 4 00694 0161 1706 00695 bsf PORTB, CLOCK 0162 1306 00696 bcf PORTB, CLOCK 0163 1B86 00697 btfsc PORTB, DATA 0164 16A4 00698 bsf BYTE2, 5 00699 gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 14 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0165 1706 00700 bsf PORTB, CLOCK 0166 1306 00701 bcf PORTB, CLOCK 0167 1B86 00702 btfsc PORTB, DATA 0168 1724 00703 bsf BYTE2, 6 00704 00705 ; This is the "stop bit" and unused; let's not read it. 00706 0169 1706 00707 bsf PORTB, CLOCK 016A 1306 00708 bcf PORTB, CLOCK 00709 ; btfsc PORTB, DATA 00710 ; bsf BYTE2, 7 00711 00712 ; Put our DATA back to being an output. The target will 00713 ; be doing the same (making it's DATA an input). 00714 016B 1683 1303 00715 banksel TRISB 016D 1386 00716 bcf TRISB, DATA ; make DATA an output, 016E 1283 1303 00717 banksel PORTB 00718 00719 ; Output the two bytes to the host computer. 00720 0170 0823 00721 movf BYTE1, w 0171 219C 00722 call serout 00723 0172 0824 00724 movf BYTE2, w 0173 219C 00725 call serout 0174 2807 00726 goto cmdloop 00727 00728 00729 00730 ; Delay W 10 uS intervals. This was hand calibrated on a 00731 ; 20 MHz HS oscillator. It measured precisely 2.5mS at 00732 ; W=250; 1.0mS at W=100; 101uS at W=10, and 11.8uS at 00733 ; W=1. 00734 0175 00A0 00735 dly10uS movwf __DC1 ; HAND CALIBRATED 0176 300B 00736 dly1 movlw 11 ; HAND CALIBRATED 0177 3EFF 00737 dly2 addlw 255 ; HAND CALIBRATED 0178 1D03 00738 btfss STATUS, Z ; HAND CALIBRATED 0179 2977 00739 goto dly2 ; HAND CALIBRATED 017A 0000 00740 nop ; HAND CALIBRATED 017B 0000 00741 nop ; HAND CALIBRATED 017C 0000 00742 nop ; HAND CALIBRATED 017D 0BA0 00743 decfsz __DC1, f ; HAND CALIBRATED 017E 2976 00744 goto dly1 ; HAND CALIBRATED 017F 0008 00745 return ; HAND CALIBRATED 00746 00747 ; Delay W 100 uS intervals. This was hand calibrated on a 00748 ; 20 MHz HS oscillator. It measured precisely 25mS at 00749 ; W=250; 10mS at W=100; 1.01mS at W=10; 100uS at W=1. 00750 0180 00751 dly100uS ; HAND CALIBRATED 0180 00A0 00752 movwf __DC1 ; HAND CALIBRATED 0181 307D 00753 dly3 movlw 125 ; HAND CALIBRATED gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 15 LOC OBJECT CODE LINE SOURCE TEXT VALUE 0182 3EFF 00754 dly4 addlw 255 ; HAND CALIBRATED 0183 1D03 00755 btfss STATUS, Z ; HAND CALIBRATED 0184 2982 00756 goto dly4 ; HAND CALIBRATED 0185 0BA0 00757 decfsz __DC1, f ; HAND CALIBRATED 0186 2981 00758 goto dly3 ; HAND CALIBRATED 0187 0008 00759 return ; HAND CALIBRATED 00760 00761 ; Delay W 10mS intervals. 00762 0188 00A1 00763 dly10mS movwf __DC2 0189 3064 00764 dly5 movlw 100 018A 2180 00765 call dly100uS ; delay 10 mS, etc 018B 0BA1 00766 decfsz __DC2, f 018C 2989 00767 goto dly5 018D 0008 00768 return 00769 00770 00771 ; Initialize the UART for asynchronous in and out 00772 ; and set the bit rate. This sets PB2 (TD) as output, 00773 ; PB1 (RD) as input and sets the bit rate to the factor 00774 ; W, from table below. 00775 ; 00776 ; Set bit rate; see PIC16F628a data sheet PDF page 73-75. 00777 ; 00778 ; 18 432 000 clock rate 00779 ; Bit-rate= ----------- 00780 ; 16 * (X + 1) 64 if TXSTA.BRGH=0 (low range) 00781 ; 00782 ; X= 25.042 00783 ; 00784 ; HIGH LOW 00785 ; speed speed W 00786 ; 576000 144000 1 00787 ; 115200 28800 9 00788 ; 76800 19200 14 00789 ; 57600 14400 19 00790 ; 19200 4800 59 00791 ; 9600 2400 119 00792 ; 4800 1200 239 00793 018E 00794 serinit 018E 1683 1303 00795 banksel TRISB 0190 1486 00796 bsf TRISB, RXD ; 1 RB1 is input, RD 0191 1106 00797 bcf TRISB, TXD ; 1 RB2 is output, TD 00798 0192 1683 1303 00799 banksel TXSTA 0194 1518 00800 bsf TXSTA, BRGH ; 1 set HIGH B.R. range, 0195 0099 00801 movwf SPBRG ; 1 set bit rate divisor, 00802 0196 1698 00803 bsf TXSTA, TXEN ; 1 Tx enable 00804 0197 1283 1303 00805 banksel RCSTA 0199 1798 00806 bsf RCSTA, SPEN ; 0 Rx enable, 019A 1618 00807 bsf RCSTA, CREN ; 0 continuous Rx, gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 16 LOC OBJECT CODE LINE SOURCE TEXT VALUE 00808 019B 0008 00809 return 00810 00811 ; Output W to the UART. 00812 019C 1283 1303 00813 serout banksel PIR1 019E 1E0C 00814 __so1 btfss PIR1, TXIF ; Tx full 019F 299E 00815 goto __so1 ; wait... 01A0 0099 00816 movwf TXREG ; output to transmitter. 01A1 0008 00817 return 00818 00819 ; Wait for a character from the UART. This is a blocking read. 00820 01A2 1283 1303 00821 serin banksel PIR1 01A4 1E8C 00822 __si1 btfss PIR1, RCIF 01A5 29A4 00823 goto __si1 ; wait for RX buffer, 01A6 081A 00824 movf RCREG, w ; read it 01A7 0008 00825 return 00826 00827 00828 gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 17 SYMBOL TABLE LABEL VALUE ACKCHAR 0000005F ADEN 00000003 BRGH 00000002 BYTE0 00000022 BYTE1 00000023 BYTE2 00000024 C 00000000 C1INV 00000004 C1OUT 00000006 C2INV 00000005 C2OUT 00000007 CCP1CON 00000017 CCP1IE 00000002 CCP1IF 00000002 CCP1M0 00000000 CCP1M1 00000001 CCP1M2 00000002 CCP1M3 00000003 CCP1X 00000005 CCP1Y 00000004 CCPR1H 00000016 CCPR1L 00000015 CIS 00000003 CLOCK 00000006 CM0 00000000 CM1 00000001 CM2 00000002 CMCON 0000001F CMIE 00000006 CMIF 00000006 CREN 00000004 CSRC 00000007 DATA 00000007 DC 00000001 EEADR 0000009B EECON1 0000009C EECON2 0000009D EEDATA 0000009A EEIE 00000007 EEIF 00000007 F 00000001 FERR 00000002 FSR 00000004 GIE 00000007 HVCLAMP 00000004 HVSHDN 00000000 INDF 00000000 INTCON 0000000B INTE 00000004 INTEDG 00000006 INTF 00000001 IRP 00000007 NOT_BO 00000000 gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 18 LOC OBJECT CODE LINE SOURCE TEXT VALUE NOT_BOD 00000000 NOT_BOR 00000000 NOT_PD 00000003 NOT_POR 00000001 NOT_RBPU 00000007 NOT_T1SYNC 00000002 NOT_TO 00000004 OERR 00000001 OPTION_REG 00000081 OSCF 00000003 PCL 00000002 PCLATH 0000000A PCON 0000008E PEIE 00000006 PIE1 0000008C PIR1 0000000C PORTA 00000005 PORTB 00000006 PR2 00000092 PS0 00000000 PS1 00000001 PS2 00000002 PSA 00000003 RB4CLAMP 00000005 RBIE 00000003 RBIF 00000000 RCIE 00000005 RCIF 00000005 RCREG 0000001A RCSTA 00000018 RD 00000000 RP0 00000005 RP1 00000006 RX9 00000006 RX9D 00000000 RXD 00000001 SHORTVDD 00000003 SPBRG 00000099 SPEED 00000009 SPEN 00000007 SREN 00000005 STATUS 00000003 SYNC 00000004 T0CS 00000005 T0IE 00000005 T0IF 00000002 T0SE 00000004 T1CKPS0 00000004 T1CKPS1 00000005 T1CON 00000010 T1OSCEN 00000003 T2CKPS0 00000000 T2CKPS1 00000001 T2CON 00000012 gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 19 LOC OBJECT CODE LINE SOURCE TEXT VALUE TMR0 00000001 TMR1CS 00000001 TMR1H 0000000F TMR1IE 00000000 TMR1IF 00000000 TMR1L 0000000E TMR1ON 00000000 TMR2 00000011 TMR2IE 00000001 TMR2IF 00000001 TMR2ON 00000002 TOUTPS0 00000003 TOUTPS1 00000004 TOUTPS2 00000005 TOUTPS3 00000006 TRISA 00000085 TRISB 00000086 TRMT 00000001 TX9 00000006 TX9D 00000000 TXD 00000002 TXEN 00000005 TXIE 00000004 TXIF 00000004 TXREG 00000019 TXSTA 00000098 VERS 00000032 VR0 00000000 VR1 00000001 VR2 00000002 VR3 00000003 VRCON 0000009F VREN 00000007 VROE 00000006 VRR 00000005 W 00000000 WR 00000001 WREN 00000002 WRERR 00000003 Z 00000002 _BODEN_OFF 00003FBF _BODEN_ON 00003FFF _CP_OFF 00003FFF _CP_ON 00001FFF _DATA_CP_OFF 00003FFF _DATA_CP_ON 00003EFF _EXTCLK_OSC 00003FEF _HS_OSC 00003FEE _INTOSC_OSC_CLKOUT 00003FFD _INTOSC_OSC_NOCLKOUT 00003FFC _LP_OSC 00003FEC _LVP_OFF 00003F7F _LVP_ON 00003FFF _MCLRE_OFF 00003FDF gpasm-0.11.7 alpha ma1.asm 11-18-2003 23:23:47 PAGE 20 LOC OBJECT CODE LINE SOURCE TEXT VALUE _MCLRE_ON 00003FFF _PWRTE_OFF 00003FFF _PWRTE_ON 00003FF7 _RC_OSC_CLKOUT 00003FFF _RC_OSC_NOCLKOUT 00003FFE _WDT_OFF 00003FFB _WDT_ON 00003FFF _XT_OSC 00003FED __16F628A 00000001 __DC1 00000020 __DC2 00000021 __si1 000001A4 __so1 0000019E cmd2 00000020 cmdloop 00000007 dly1 00000176 dly100uS 00000180 dly10mS 00000188 dly10uS 00000175 dly2 00000177 dly3 00000181 dly4 00000182 dly5 00000189 idlemode 00000041 progmode 0000005A reset 0000007C serin 000001A2 serinit 0000018E serout 0000019C typeA 00000089 typeA25 00000081 typeA6 00000085 typeAx 0000008B typeB 000000AD typeC 00000127 version 00000032 MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0080 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 00c0 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0100 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0140 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0180 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXX-------- ---------------- 2000 : -------X-------- ---------------- ---------------- ---------------- All other memory blocks unused. Program Memory Words Used: 425 Errors : 0 Warnings : 0 reported, 0 suppressed Messages : 0 reported, 17 suppressed