' ' Serial output routines for debugging ' Fixed baud rate and output port/pin ' Reqired the following defines and constants: ' Note: formula is DEBUG_DELAY = (1000000 / (DEBUG_BAUD)) - (52 / OSC) '#define DEBUG_REG PORTB ' The port to use for the serial output '#define DEBUG_TRIS TRISB '#define DEBUG_BIT 7 ' The bit to use for serial output '#define DEBUG_POLARITY TRUE|INVERTED 'default is TRUE: direct to RS232 ' ' Inverted means via a MAX232 or equivalent 'const DEBUG_DELAY = 102 ' To use the Basic18 delayus() routine '#define DEBUG_DELAY 102 ' To use LHB's pauseus() routine Sub debugc(character As uByte) Dim Banka count As uByte Dim Banka chr As uByte Dim saveTRIS As Boolean chr = character ' We want to save the status of the TRIS bit for the port, then set to ' output, then restore at the end #ifdef DEBUG_TRIS bcf saveTRIS ' Assume pin was an output btfss DEBUG_TRIS , DEBUG_BIT ' If it wasn't, skip to code to change bra start ' Was an output, all is well bcf DEBUG_TRIS , DEBUG_BIT ' Set to output bsf saveTRIS ' Flag as having been an input #endif start: movlw 9 ' 1 start bit + 8 Data bits movwf count bcf STATUS, C ' Start bit low loop: Gosub sendbit ' 9 (13) Send the bit rrcf chr, F ' 1 Move To Next bit decfsz count, F ' 1 / 2 Do Next bit, If any bra loop ' 2 / 0 bsf STATUS, C ' 1 Stop bit high Gosub sendbit ' 9 Send stop bit bsf STATUS, C ' Set no timeout For Serout2mod goto fini sendbit: btfsc STATUS, C ' 1 / 2 Bit On Or off? bra highbit ' 2 nop ' 1 Nominalize time #If DEBUG_POLARITY = INVERTED bcf DEBUG_REG , DEBUG_BIT ' 1 Set inverted low bit #else bsf DEBUG_REG , DEBUG_BIT ' 1 Set inverted high bit #endif bra delayx ' 3 Delay rest of bit time highbit: #If DEBUG_POLARITY = INVERTED bsf DEBUG_REG , DEBUG_BIT ' 1 Set inverted high bit #else bcf DEBUG_REG , DEBUG_BIT ' 1 Set inverted low bit #endif bra delayx ' 3 Delay rest of bit time delayx: #ifdef DEBUG_DELAY pauseus( DEBUG_DELAY ) #else ' Use this if DEBUG_DELAY is a Const rather than a #define DelayuS(DEBUG_DELAY) #endif return fini: #ifdef DEBUG_TRIS ' Check if we have to change the TRIS back to i/p btfsc saveTRIS bsf DEBUG_TRIS , DEBUG_BIT ' Set to input #endif End Sub Sub debugs(s As String) Dim saveFSR0 As uInteger saveFSR0 = FSR0 FSR0 = addr(s) While INDF0 <> 0 debugc(POSTINC0) End While FSR0 = SaveFSR0 End Sub Sub debugNL() ' New line debugc(13) debugc(10) End Sub Sub debugCRLF() ' New line debugc(13) debugc(10) End Sub Sub DumpMem(FSR0 As uInteger,len As uByte,s As String) ' Dump "len" bytes of memory starting at location in "FSR0". ' Dump in 4 byte chunks Dim n As uByte Dim SaveFSR2 As uInteger SaveFSR2 = FSR2 'n is the number of 4 byte chunks to display n = (len-1)/4 + 1 While n > 0 ' Convert 4 bytes at a time into 8 hex characters FSR2 = addr(s) WREG = POSTINC0 B2Hex(WREG) WREG = POSTINC0 B2Hex(WREG) WREG = POSTINC0 B2Hex(WREG) WREG = POSTINC0 B2Hex(WREG) POSTINC2 = " " INDF2 = 0 debugs(s) n = n - 1 End While FSR2 = SaveFSR2 End Sub