list p=16f877 __config H'3D31' #include p16f877.inc #define charBuf 0x70 #define printf_temp 0x71 #define ISR_w 0x72 #define ISR_status 0x73 #define curMode 0x74 #define ISR_charBuf 0x75 #define temp 0x76 #define hex_temp 0x77 #define hex_temp2 0x78 #define trig_status 0x79 #define trig_byte1 0x7a #define trig_byte2 0x7b #define trig_mask 0x7c #define line_count 0x20 #define page_count 0x21 #define write_num 0x22 #define char_rx 0 #define debug 1 #define trig_enable 0 ;signal defines #define size0 2 #define size1 4 #define size2 5 #define PIC_clk 0 #define clk_sel 1 #define rd_strb 2 #define RST 3 #define OE 4 #define done 5 #define clk_spd0 0 #define clk_spd1 1 #define clk_spd2 2 org 0 goto start org 4 goto ISR ; null terminated strings for use with printf function START_TITLE retlw 0x0d retlw 0x0a retlw 'P' retlw 'C' retlw ' ' retlw 'L' retlw 'A' retlw ' ' retlw '-' retlw ' ' retlw 'V' retlw 'e' retlw 'r' retlw '.' retlw ' ' retlw '0' retlw '.' retlw '1' retlw '1' retlw 0x00 NUMWRITE retlw 0x0d retlw 0x0a retlw 'N' retlw 'u' retlw 'm' retlw ' ' retlw 't' retlw 'o' retlw ' ' retlw 'w' retlw 'r' retlw 'i' retlw 't' retlw 'e' retlw ':' retlw 0x00 DONE retlw 0x0d retlw 0x0a retlw 'd' retlw 'o' retlw 'n' retlw 'e' retlw 0x0d retlw 0x0a retlw 0x00 ENTERSIZE retlw 0x0d retlw 0x0a retlw 'E' retlw 'n' retlw 't' retlw 'e' retlw 'r' retlw ' ' retlw 's' retlw 'i' retlw 'z' retlw 'e' retlw ':' retlw 0x00 SIZEIS retlw 0x0d retlw 0x0a retlw 'S' retlw 'i' retlw 'z' retlw 'e' retlw '=' retlw 0x00 ENTERCLOCK retlw 0x0d retlw 0x0a retlw 'E' retlw 'n' retlw 't' retlw 'e' retlw 'r' retlw ' ' retlw 'c' retlw 'l' retlw 'k' retlw ':' retlw 0x00 CLOCKIS retlw 0x0d retlw 0x0a retlw 'C' retlw 'l' retlw 'k' retlw '=' retlw 0x00 STOREGO retlw 0x0d retlw 0x0a retlw 'S' retlw 't' retlw 'o' retlw 'r' retlw 'i' retlw 'n' retlw 'g' retlw '.' retlw '.' retlw '.' retlw ' ' retlw 0x00 ENTERTRIG retlw 0x0d retlw 0x0a retlw 'E' retlw 'n' retlw 't' retlw 'e' retlw 'r' retlw ' ' retlw 't' retlw 'r' retlw 'i' retlw 'g' retlw ':' retlw 0x00 TRIGDISABLED retlw 0x0d retlw 0x0a retlw 'T' retlw 'r' retlw 'i' retlw 'g' retlw ' ' retlw 'd' retlw 'i' retlw 's' retlw 'a' retlw 'b' retlw 'l' retlw 'e' retlw 'd' retlw '!' retlw 0x00 WAITING retlw 0x0d retlw 0x0a retlw 'W' retlw 'a' retlw 'i' retlw 't' retlw 'i' retlw 'n' retlw 'g' retlw '.' retlw '.' retlw '.' retlw ' ' retlw 0x00 ; macro BANK0 - switches to bank0 BANK0 macro bcf STATUS, RP0 bcf STATUS, RP1 endm ; macro BANK1 - switches to bank1 BANK1 macro bsf STATUS, RP0 bcf STATUS, RP1 endm ; macro BANK2 - switches to bank2 BANK2 macro bcf STATUS, RP0 bsf STATUS, RP1 endm ; macro BANK3 - switches to bank3 BANK3 macro bsf STATUS, RP0 bsf STATUS, RP1 endm start clrf curMode ;clear mode register call init_IO ;call init IOs call init_getChar ;call init_getChar call init_ints ;enable interrupts bcf PORTC, RST ;bring CPLD out of reset movlw START_TITLE ;print the start banner call printf command_wait movlw 0x0d ;send carridge return call sendChar movlw 0x0a call sendChar movlw ':' ;print prompt call sendChar command_loop btfss curMode, char_rx ;has a char been received? goto command_loop ;nope, loop bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 'd' ;is it a 'd'? btfsc STATUS, Z goto dump_mem ;yup, goto dump mem routine movfw ISR_charBuf sublw 'w' ;is it a 'w'? btfsc STATUS, Z goto write_mem ;yup, goto write mem routine movfw ISR_charBuf sublw 's' ;is it a 's'? btfsc STATUS, Z goto set_size ;yup, goto set size routine movfw ISR_charBuf sublw 'c' ;is it a 'c'? btfsc STATUS, Z goto set_clock ;yup, goto set clock routine movfw ISR_charBuf sublw 'r' ;is it a 'r'? btfsc STATUS, Z goto read_mem ;yup, goto read_mem routine movfw ISR_charBuf sublw 't' ;is it a 't'? btfsc STATUS, Z goto store ;yup, goto store routine movfw ISR_charBuf sublw 'l' ;is it an 'l'? btfsc STATUS, Z goto load_trig ;yup, goto load_trigger routine goto command_loop ;no command received, go back ;-------------------------------------------------------------------- ;load trigger routine - loads trigger supplied by serial port into ;global variables trig_byte1, trig_byte2, trig_mask and sets bit ;trig_enable in trig_status load_trig BANK0 MOVLW ENTERTRIG call printf lt_loop0 btfss curMode, char_rx ;has a char been received? goto lt_loop0 ;nope, loop bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 0x1b ;is the character 'ESC'? btfsc STATUS, Z goto command_wait ;yup go back to command_wait movfw ISR_charBuf sublw '0' ;is it a zero? btfsc STATUS, Z goto disable_trig clrf trig_byte1 ;clear trigger variables clrf trig_byte2 clrf trig_mask lt_byte1 movlw ' ' ;print spacer call sendChar movlw 8 movwf temp lt_loop1 btfss curMode, char_rx ;has a char been received? goto lt_loop1 ;nope loop bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 0x1b ;is the character 'ESC'? btfsc STATUS, Z goto disable_trig ;yup disable trigger movfw ISR_charBuf sublw '0' ;is the character '0'? btfsc STATUS, Z goto lt_next_byte1 movfw ISR_charBuf sublw '1' ;is the character '1'? btfsc STATUS, Z goto lt_byte1_1 movfw ISR_charBuf sublw 'x' ;is the character 'x'? btfsc STATUS, Z goto lt_byte1_x goto lt_loop1 lt_byte1_1 bsf trig_byte1, 0 goto lt_next_byte1 lt_byte1_x bsf trig_mask, 0 goto lt_next_byte1 lt_next_byte1 decfsz temp, F goto lt_skip1 goto lt_byte2 lt_skip1 bcf STATUS, C rlf trig_byte1, F bcf STATUS, C rlf trig_mask, F goto lt_loop1 lt_byte2 comf trig_mask, F movlw ' ' ;print spacer call sendChar movlw 8 movwf temp lt_loop2 btfss curMode, char_rx ;has a char been received? goto lt_loop2 ;nope loop bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 0x1b ;is the character 'ESC'? btfsc STATUS, Z goto disable_trig ;yup disable trigger movfw ISR_charBuf sublw '0' ;is the character '0'? btfsc STATUS, Z goto lt_next_byte2 movfw ISR_charBuf sublw '1' ;is the character '1'? btfsc STATUS, Z goto lt_byte2_1 movfw ISR_charBuf goto lt_loop2 lt_byte2_1 bsf trig_byte2, 0 goto lt_next_byte2 lt_next_byte2 decfsz temp, F goto lt_skip2 goto trig_done lt_skip2 bcf STATUS, C rlf trig_byte2, F goto lt_loop2 trig_done bsf trig_status, trig_enable ;enable triggering movlw ':' ;print out byte 1 in hex call sendChar movlw ' ' call sendChar movfw trig_byte1 call print_hex movlw ' ' ;print out byte 2 in hex call sendChar movfw trig_byte2 call print_hex movlw ' ' ;print out mask byte in hex call sendChar movfw trig_mask call print_hex movlw DONE call printf goto command_wait ;all done, go back disable_trig bcf trig_status, trig_enable ;disable triggering movlw TRIGDISABLED call printf ;print trig disabled message goto command_wait ;go back ;-------------------------------------------------------------------- ;store routine - simply sets clk_sel, rd_strb and finally RST to start ;a data capture, waits for done from CPLD store BANK1 movlw B'11111111' movwf TRISB ;set port B for inputs BANK0 bsf PORTC, clk_sel ;set for CPLD clock as source bcf PORTC, rd_strb ;set for store process bcf PORTC, OE ;enable signal input buffers bcf PORTC, RST ;clear reset btfss trig_status, trig_enable ;is triggering enabled? goto store_go store_trig1 movlw WAITING ;print trigger wainting message call printf movlw ' ' call sendChar movlw '1' call sendChar store_trig1_loop btfsc curMode, char_rx ;has a char been received? goto s_trig1_esc ;check if it's an escape movfw PORTB andwf trig_mask, W ;mask off don't cares xorwf trig_byte1, W ;are the lines the same as trigger? btfsc STATUS, Z goto store_trig2 ;yup, goto state 2! bsf PORTC, RST ;nope, try again, reset CPLD bcf PORTC, RST goto store_trig1_loop s_trig1_esc movfw ISR_charBuf bcf curMode, char_rx ;clear char_rx flag sublw D'27' btfss STATUS, Z ;is it an ESC? goto store_trig1_loop ;nope, goto store loop goto store_done ;yup, goto store done store_trig2 movlw ' ' call sendChar movlw '2' call sendChar store_trig2_loop btfsc curMode, char_rx ;has a char been received? goto s_trig2_esc ;check if it's an escape movfw PORTB andwf trig_mask, W ;mask off don't cares xorwf trig_byte2, W ;are the lines the same as trigger? btfsc STATUS, Z goto store_go ;yup, goto storing! bsf PORTC, RST ;nope, try again, reset CPLD bcf PORTC, RST goto store_trig2_loop ;nope, try again s_trig2_esc movfw ISR_charBuf bcf curMode, char_rx ;clear char_rx flag sublw D'27' btfss STATUS, Z ;is it an ESC? goto store_trig1 ;nope, goto store loop goto store_done ;yup, goto store done store_go movlw STOREGO call printf ;print STOREGO message store_loop btfsc curMode, char_rx ;has a char been received? goto store_char ;yup, goto char received btfss PORTC, done ;has done been received yet? goto store_loop ;nope, loop again store_done bsf PORTC, RST ;finished aquiring, return to bsf PORTC, OE ;tri-state signal input buffers bsf PORTC, rd_strb ;steady state mode bcf PORTC, clk_sel movlw DONE call printf ;print DONE message goto command_wait ;go back store_char movfw ISR_charBuf bcf curMode, char_rx ;clear char_rx flag sublw D'27' btfss STATUS, Z ;is it an ESC? goto store_loop ;nope, goto store loop goto store_done ;yup, goto store done ;-------------------------------------------------------------------- ;set_clock routine - prompts user for number between 0-7 and sets the ;clock bits accordingly set_clock BANK0 movlw ENTERCLOCK ;print enter clock message call printf set_clock_loop btfss curMode, char_rx ;has a char been received? goto set_clock_loop ;nope, loop bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 0x1b ;is the character 'ESC'? btfsc STATUS, Z goto command_wait ;yup go back to command_wait movlw '0' subwf ISR_charBuf, W ;convert from ASCII movwf temp ;save result sublw 7 btfss STATUS, C ;is the number more than 7? goto command_wait ;yup, abort movfw temp ;get back number andlw B'00000111' ;mask off upper 5 bits movwf PORTE ;move result to port E movlw CLOCKIS call printf ;print CLOCKIS message movfw ISR_charBuf call sendChar ;prints character pressed goto command_wait ;done, clock speed is now set ;------------------------------------------------------------------------------ ;read_mem routine - reads the memory sending it as full binary, use dump_mem ;for terminal friendly display read_mem bsf PORTC, rd_strb ;set for read process bsf PORTC, RST ;clock through RESET bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bcf PORTC, RST read_loop bsf PORTC, PIC_clk ;keep sending until done is received bcf PORTC, PIC_clk movfw PORTB call sendChar bsf PORTC, PIC_clk bcf PORTC, PIC_clk btfsc curMode, char_rx ;has a char been received? goto read_esc_chk read_done_chk btfss PORTC, done goto read_loop read_done bsf PORTC, RST ;clock through RESET bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bcf PORTC, RST movlw DONE ;print DONE string call printf goto command_wait ;go back read_esc_chk bcf curMode, char_rx ;clear received flag movfw ISR_charBuf sublw 0x1b ;has ESC been received? btfss STATUS, Z goto read_done_chk ;nope, go back goto read_done ;yup, aborting read op ;-------------------------------------------------------------------- ;set_size routine - prompts user for number between 0-7 and sets the ;size bits accordingly set_size BANK0 movlw ENTERSIZE ;print enter size message call printf set_size_loop btfss curMode, char_rx ;has a char been received? goto set_size_loop ;nope, loop bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 0x1b ;is the character 'ESC'? btfsc STATUS, Z goto command_wait ;yup go back to command_wait movfw ISR_charBuf sublw '0' ;is it a zero? btfsc STATUS, Z goto size_0 ;yup, set size for 0 movfw ISR_charBuf sublw '1' ;is it a one? btfsc STATUS, Z goto size_1 ;yup, set size for 1 movfw ISR_charBuf sublw '2' ;is it a two? btfsc STATUS, Z goto size_2 ;yup, set size for 2 movfw ISR_charBuf sublw '3' ;is it a three? btfsc STATUS, Z goto size_3 ;yup, set size for 3 movfw ISR_charBuf sublw '4' ;is it a four? btfsc STATUS, Z goto size_4 ;yup, set size for 4 movfw ISR_charBuf sublw '5' ;is it a five? btfsc STATUS, Z goto size_5 ;yup, set size for 5 movfw ISR_charBuf sublw '6' ;is it a six? btfsc STATUS, Z goto size_6 ;yup, set size for 6 movfw ISR_charBuf sublw '7' ;is it a seven? btfsc STATUS, Z goto size_7 ;yup, set size for 7 goto command_wait ;go back size_0 movlw SIZEIS ;print sizeis message call printf movlw '0' call sendChar movlw B'00000000' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_1 movlw SIZEIS ;print sizeis message call printf movlw '1' call sendChar movlw B'00000100' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_2 movlw SIZEIS ;print sizeis message call printf movlw '2' call sendChar movlw B'00010000' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_3 movlw SIZEIS ;print sizeis message call printf movlw '3' call sendChar movlw B'00010100' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_4 movlw SIZEIS ;print sizeis message call printf movlw '4' call sendChar movlw B'00100000' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_5 movlw SIZEIS ;print sizeis message call printf movlw '5' call sendChar movlw B'00100100' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_6 movlw SIZEIS ;print sizeis message call printf movlw '6' call sendChar movlw B'00110000' movwf PORTA ;set size bits accordingly goto command_wait ;go back size_7 movlw SIZEIS ;print sizeis message call printf movlw '7' call sendChar movlw B'00110100' movwf PORTA ;set size bits accordingly goto command_wait ;go back ;-------------------------------------------------------------------- ;write_mem routine - writes to the memory a pseudo random number, used ;for testing write_mem BANK0 movlw 0x57 ;make write_num pseudo random addwf write_num, F movlw NUMWRITE ;print NUMWRITE string call printf BANK0 movfw write_num call print_hex ;print number to be written movlw 0x0d call sendChar movlw 0x0a call sendChar bcf PORTC, rd_strb ;set for store process bsf PORTC, PIC_clk ;clock through RESET bcf PORTC, PIC_clk bsf PORTC, RST bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bcf PORTC, RST ;reset done BANK1 clrf TRISB ;set port B for output BANK0 movfw write_num ;move number to write to port B movwf PORTB write_loop bsf PORTC, PIC_clk ;keep clocking until done is received bcf PORTC, PIC_clk btfss PORTC, done goto write_loop BANK1 movlw B'11111111' ;we are done writing, set PORT B movwf TRISB ;for input again BANK0 bsf PORTC, rd_strb ;set for read process bsf PORTC, RST ;clock through RESET bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bcf PORTC, RST movlw DONE ;print DONE string call printf goto command_wait ;go back ;------------------------------------------------------------ ;dump_mem routine - prints out the contents of the memory until ;done is received, terminal friendly, use 'r' for quicker ;download (binary download) dump_mem BANK0 movlw D'26' ;set line_count counter movwf line_count movlw D'22' ;set page_count counter movwf page_count movlw 0x0d call sendChar movlw 0x0a call sendChar bsf PORTC, RST ;clock through RESET bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bcf PORTC, RST dump_loop bsf PORTC, PIC_clk ;keep printing until done is received bcf PORTC, PIC_clk movfw PORTB call print_hex movlw ' ' call sendChar decfsz line_count, F ;have we filled this line? goto main_cont ;nope movlw 0x0d ;yup, send caridge return call sendChar movlw 0x0a call sendChar BANK0 movlw D'26' ;reset line counter movwf line_count decfsz page_count, F ;have we printed a whole page? goto main_cont ;nope wait_space btfss curMode, char_rx ;has a char been received? goto wait_space ;nope bcf curMode, char_rx ;yup, clear flag movfw ISR_charBuf sublw 0x1b ;is it the 'ESC' character? btfsc STATUS, Z goto command_wait ;yup, go back movfw ISR_charBuf ;nope, is it a space? sublw ' ' btfss STATUS, Z goto wait_space ;nope, keep waiting movlw D'22' ;reset page counter movwf page_count movlw 0x0d ;send caridge return call sendChar movlw 0x0a call sendChar main_cont bsf PORTC, PIC_clk bcf PORTC, PIC_clk btfss PORTC, done ;has done been received? goto dump_loop ;nope movlw DONE ;yup, print DONE string call printf goto command_wait ;go back ;---------------------------------------------------------------------------- ;print_hex routine - outputs the byte in W in ASCII print_hex BANK0 movwf hex_temp ;save character to be printed andlw B'11110000' ;mask off lower nibble movwf hex_temp2 bcf STATUS, C ;shift upper nibble to lower nibble rrf hex_temp2, F bcf STATUS, C rrf hex_temp2, F bcf STATUS, C rrf hex_temp2, F bcf STATUS, C rrf hex_temp2, F movlw 0x0a ;is it bigger than 0x0a? subwf hex_temp2, W movlw 0x30 ;set to add 0x30 btfsc STATUS, C movlw 0x37 ;yup, set to add 0x37 addwf hex_temp2, W call sendChar ;send upper nibble in ASCII form movlw B'00001111' andwf hex_temp, W ;mask off upper nibble movwf hex_temp2 movlw 0x0a ;is it bigger than 0x0a? subwf hex_temp2, W movlw 0x30 ;set to add 0x30 btfsc STATUS, C movlw 0x37 ;yup, set to add 0x37 addwf hex_temp2, W call sendChar ;send lower nibble in ASCII form return ;return ;--------------------------------------------------------------------------------- ;init_IO routine - sets up the chip with the correct port directions and port settings init_IO BANK1 movlw B'00001011' ;setting port directions movwf TRISA ;port A movlw B'11111111' movwf TRISB ;port B movlw B'11100000' movwf TRISC ;port C movlw B'11111111' movwf TRISD ;port D movlw B'00000000' movwf TRISE ;port E movlw B'00000100' ;set A/D convertor so port E and bits movwf ADCON1 ;2, 4 and 5 of port A are digital BANK0 clrf trig_status ;clear trigger status register movlw B'00000000' movwf PORTE ;set clk_speed<2:0> to 000 movlw B'00000000' movwf PORTA ;set size<2:0> to 000 bsf PORTC, OE ;tri-stat input signal buffers bcf PORTC, clk_sel ;set for PIC_clk as source bsf PORTC, rd_strb ;set for read process bsf PORTC, RST ;set reset bcf PORTC, PIC_clk ;clk the reset in bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk bsf PORTC, PIC_clk bcf PORTC, PIC_clk ;ready to go! return ;------------------------------------------------------------------------------ ; interrupt service routine ISR movwf ISR_w ; save w swapf STATUS, w ; save current status clrf STATUS ; set ram page 1 movwf ISR_status ; save current status call ISR_getChar ; get the character received movwf ISR_charBuf ; save it bcf PIR1, RCIF ; clear interrupt flag bsf curMode, char_rx call sendChar ; send character received call init_getChar ; init to receive another character swapf ISR_status, W ; restore status register movwf STATUS swapf ISR_w, F swapf ISR_w, W ; restore W register retfie ; return from interrupt ;--------------------------------------------------------------------------------- ;inits the USART for character reception for getChar routine init_getChar BANK1 movlw 0x0c ;set baud rate generator for 19200bps movwf SPBRG ;with 4MHz crystal bsf TXSTA, BRGH ;set high speed mode bcf TXSTA, SYNC ;clear SYNC bit BANK0 bsf RCSTA, SPEN ;set SPEN bit bsf RCSTA, CREN ;enable reception BANK1 bsf PIE1, RCIE ; enable receive interrupts BANK0 return ;now ready to receive, return ;--------------------------------------------------------------------------------- ;getChar ISR ISR_getChar btfss PIR1, RCIF ;is RCIF set (byte received)? goto ISR_getChar ;nope, go back movfw RCREG ;move the result into W bcf RCSTA, CREN ;clear this bit in case an error occured return ;we're done, return from call ;--------------------------------------------------------------------------------- ;subroutine init_ints - enables interrupts init_ints BANK1 ;bsf PIE1, RCIE ; enable receive interrupts bsf INTCON, PEIE ; enable peripheral interrupts bsf INTCON, GIE ; enable interrupts BANK0 return ;--------------------------------------------------------------------------------- ; subroutine printf - outputs the null terminated string at address ; in W to the serial port printf movwf printf_temp ; load character counter printf_loop call table_lookup ; get a character from the table movwf charBuf ; necessary to effect Z movfw charBuf ; necessary to effect Z btfss STATUS, Z ; is character a null? goto printf_go_on ; nope, go on return ; yup, hit a null, return printf_go_on call sendChar ; we have a non zero number, send it incf printf_temp, F ; increment counter goto printf_loop ; start loop again table_lookup movfw printf_temp ; move address of chr to get into w movwf PCL ; set PCL to that address ;--------------------------------------------------------------------------------- ;should only be called if the ISR flags a char received ; subroutine getChar - gets a character from the serial port getChar BANK1 movlw 0x0c ;set baud rate generator for 19200bps movwf SPBRG ;with 4MHz crystal bsf TXSTA, BRGH ;set high speed mode bcf TXSTA, SYNC ;clear SYNC bit BANK0 bsf RCSTA, SPEN ;set SPEN bit bsf RCSTA, CREN ;enable reception getChar_loop btfss PIR1, RCIF ;is RCIF set (byte received? goto getChar_loop ;nope, go back movfw RCREG ;move the result into W bcf RCSTA, CREN ;clear this bit in case an error occured return ;we're done, return from call ;--------------------------------------------------------------------------------- ; subroutine sendChar - sends the character in W to the serial port sendChar BANK1 sendChar_loop btfss TXSTA, TRMT ;is TSR empty goto sendChar_loop movwf charBuf ;save char to be sent movlw 0x0c ;set baud rate generator for 19200bps movwf SPBRG ;with 4MHz crystal bsf TXSTA, BRGH ;set high speed mode bcf TXSTA, SYNC ;clear SYNC bit BANK0 bsf RCSTA, SPEN ;set SPEN bit BANK1 bsf TXSTA, TXEN ;enable transmission movfw charBuf ;get char to be sent BANK0 movwf TXREG ;move char to be sent into tx reg return END