PIC18F4550 USB to UART

Thread Starter

corsair

Joined Mar 6, 2010
51
Hi guys,

Currently, I have my PIC connected to my USB and I am able to "echo" characters back through a terminal window (RealTerm/FlashMagic) with a loopback jumper connected on the RX/TX pins.

However, my goal is to be able to send commands to my PIC (via terminal) and have it respond when it recognizes a certain string.

For example, in the terminal:
Rich (BB code):
Input: 
"get data"

Output:
variable 1: x
variable 2: y
Except, obviously, I would have it spit me back numbers that I have stored in the PIC.

So, all-in-all, here's what I need help with:
1) Recognizing a string input from the terminal window
2) Giving an (ASCII?) output on the terminal window or just perhaps reading the data on the PIC from the USB

I appreciate any help, thanks!
 

rjenkins

Joined Nov 6, 2005
1,013
In principle, store each character in a buffer (character array) as you receive it.

Once you receive an 'enter' character, start comparing the buffer string to possible commands.

For output, build the data you want to send in another character array.

If you are programming in C, the compiler may allow printf to be used with a comms port. As long as it supports sprintf, you can still print a formatted string to a charater array & then copy it a character at a time to the port.
 

Thread Starter

corsair

Joined Mar 6, 2010
51
ok so i wrote a small function to see if i had any output on the terminal window, but it didn't work. this is the code i wrote:

Rich (BB code):
void print_data(void) {
	char buff[50];
	int ret, hex = 4325, oct = 5626;

	sprintf(buff, "%x is %d in hex and %o is %d in octal",hex,hex,oct,oct);
	//ret = sprintf(buff, "%x is %d in hex and %o is %d in octal",hex,hex,oct,oct);
	//printf("%s, %d chars long", buff, ret);
	debugstatus++;

	return 0;
}
debugstatus is just a counter variable in which is displayed on my 7-segment display. everytime i would type a character, it would increment. that way, if the counter was incrementing, i know this function was being called. have any idea?

here's my code in where it's being called:
Rich (BB code):
usb:

	call USBTasks         		 	;USB tasks are done once per main loop -- must do at > 1 kHz
	
	;check whether USB is (still) connected
	movlw CONFIGURED_STATE 			;get code indicating USB is 'connected' 
	movlb usb_device_state			;are we connected?
	cpfslt usb_device_state,BANKED	; ..
	bra USB_connected				;	yes -- go do communication tasks

USB_detached:						;	no --report current state, then continue to monitor connection state
	;build debugstatus display message from usb_device_state and usb_bus_sense
	swapf	usb_device_state,W,BANKED	;debugstatus=16*usb_device_state+usb_bus_sense;
	andlw	0xF0						; ..
	addlw	usb_bus_sense				;!!this would need to be fixed if usb_bus_sense becomes a real signal
;	movlb	debugstatus					; ..
	movwf	debugstatus,ACCESS			; ..
	clrf	debugstatus+1,ACCESS		; ..
	call showhex_debugstatus			;display current state of the connection			
;	movlb	debugstatus					;reset debug status so RX packet count will start at '0' 
	clrf	debugstatus,ACCESS			; ..
	clrf	debugstatus+1,ACCESS		; ..
	bra usb

USB_connected:	
	;here as long as the USB com device is connected to the PC host
usbRx_to_uartTx: ;maintain the usbRx-to-uartTx relay
	movlb uartTxbuf_count				;is the uart Tx buffer empty?
	movf  uartTxbuf_count,W,BANKED		; ..
	bnz	usbRx_to_uartTx_exit			;	no--then not ready to do anything here
	call copy_usbRxbuf_to_uartTxbuf		;	yes--then copy new usbRx data (if any) to the uartTx buffer
	movlb uartTxbuf_count				;was there any new usbRx data? (Does the uart Tx buffer now have something in it?) 
	movf  uartTxbuf_count,W,BANKED		; ..
	bz usbRx_to_uartTx_exit				;	no--then still not ready to do anything here
	addwf	debugstatus+1,F,ACCESS		; 	yes--	record how many bytes
	bsf PIE1bits,TXIE,0					;	  then reenable uart tx  interrupt (it was disabled in the interrupt when the last character was sent from the buffer)
usbRx_to_uartTx_exit: ;update usbRx-to-uart-Tx relay is updated

;	bra uartRx_to_usbTx_exit
uartRx_to_usbTx: ;maintain the uartRx-to-usbTx relay
	bcf PIE1bits,RCIE,0					;disable uart rx interrupt while processing Rxbuf data
	movlb uartRxbuf_count				;is the uart Rx buffer empty?
	movf  uartRxbuf_count,W,BANKED		; ..
	bz	uartRx_to_usbTx_exit			;	yes -- then nothing to do here (compiler does 'sublw 0, bc exit' for this test)
;	movlb	debugstatus					;	no -- data needs to be moved ..
	addwf	debugstatus,F,ACCESS		; 		record how many bytes
	call copy_uartRxbuf_to_usbTxbuf		;		then move the data (data will not be copied if USB is not ready)
	movlb uartRxbuf_count				;		get the count of any data not taken by usb
	movf  uartRxbuf_count,W,BANKED		;	 	..
;	movlb	debugstatus					;	 	and adjust the count of transferred data 
	subwf	debugstatus,F,ACCESS		; 		.. 
	call	print_data

uartRx_to_usbTx_exit:
	bsf PIE1bits,RCIE,0					;	reenable the uart rx interrupt 	
	call showbinary_peekdata			;displays debugstatus if no switches are pressed !!this is potentially a very useful tool for hardware debug (see regpeek.asm for more info)
	bra usb
not even sure if that helps at all, but just trying to provide some additional information. when i type something into the terminal window, it is still only echo'ing back characters. maybe i just have my terminal window set up incorrectly?

edit:
i've been reading http://ww1.microchip.com/downloads/en/devicedoc/mplab_c18_libraries_51297f.pdf (4.10)
i was wondering if i need to use functions such as "getsUSART"
 
Last edited:
Top