PIC18F4550 USB to UART

Discussion in 'Embedded Systems and Microcontrollers' started by corsair, May 3, 2010.

  1. corsair

    Thread Starter Member

    Mar 6, 2010
    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:
    Code ( (Unknown Language)):
    1. Input:
    2. "get data"
    4. Output:
    5. variable 1: x
    6. 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!
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    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.
    Zenmaster and corsair like this.
  3. corsair

    Thread Starter Member

    Mar 6, 2010
    thank you very much, i will give this a go
  4. corsair

    Thread Starter Member

    Mar 6, 2010
    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:

    Code ( (Unknown Language)):
    1. void print_data(void) {
    2.     char buff[50];
    3.     int ret, hex = 4325, oct = 5626;
    5.     sprintf(buff, "%x is %d in hex and %o is %d in octal",hex,hex,oct,oct);
    6.     //ret = sprintf(buff, "%x is %d in hex and %o is %d in octal",hex,hex,oct,oct);
    7.     //printf("%s, %d chars long", buff, ret);
    8.     debugstatus++;
    10.     return 0;
    11. }
    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:
    Code ( (Unknown Language)):
    1. usb:
    3.     call USBTasks                   ;USB tasks are done once per main loop -- must do at > 1 kHz
    5.     ;check whether USB is (still) connected
    6.     movlw CONFIGURED_STATE          ;get code indicating USB is 'connected'
    7.     movlb usb_device_state          ;are we connected?
    8.     cpfslt usb_device_state,BANKED  ; ..
    9.     bra USB_connected               ;   yes -- go do communication tasks
    11. USB_detached:                       ;   no --report current state, then continue to monitor connection state
    12.     ;build debugstatus display message from usb_device_state and usb_bus_sense
    13.     swapf   usb_device_state,W,BANKED   ;debugstatus=16*usb_device_state+usb_bus_sense;
    14.     andlw   0xF0                        ; ..
    15.     addlw   usb_bus_sense               ;!!this would need to be fixed if usb_bus_sense becomes a real signal
    16. ;   movlb   debugstatus                 ; ..
    17.     movwf   debugstatus,ACCESS          ; ..
    18.     clrf    debugstatus+1,ACCESS        ; ..
    19.     call showhex_debugstatus            ;display current state of the connection           
    20. ;   movlb   debugstatus                 ;reset debug status so RX packet count will start at '0'
    21.     clrf    debugstatus,ACCESS          ; ..
    22.     clrf    debugstatus+1,ACCESS        ; ..
    23.     bra usb
    25. USB_connected: 
    26.     ;here as long as the USB com device is connected to the PC host
    27. usbRx_to_uartTx: ;maintain the usbRx-to-uartTx relay
    28.     movlb uartTxbuf_count               ;is the uart Tx buffer empty?
    29.     movf  uartTxbuf_count,W,BANKED      ; ..
    30.     bnz usbRx_to_uartTx_exit            ;   no--then not ready to do anything here
    31.     call copy_usbRxbuf_to_uartTxbuf     ;   yes--then copy new usbRx data (if any) to the uartTx buffer
    32.     movlb uartTxbuf_count               ;was there any new usbRx data? (Does the uart Tx buffer now have something in it?)
    33.     movf  uartTxbuf_count,W,BANKED      ; ..
    34.     bz usbRx_to_uartTx_exit             ;   no--then still not ready to do anything here
    35.     addwf   debugstatus+1,F,ACCESS      ;   yes--   record how many bytes
    36.     bsf PIE1bits,TXIE,0                 ;     then reenable uart tx  interrupt (it was disabled in the interrupt when the last character was sent from the buffer)
    37. usbRx_to_uartTx_exit: ;update usbRx-to-uart-Tx relay is updated
    39. ;   bra uartRx_to_usbTx_exit
    40. uartRx_to_usbTx: ;maintain the uartRx-to-usbTx relay
    41.     bcf PIE1bits,RCIE,0                 ;disable uart rx interrupt while processing Rxbuf data
    42.     movlb uartRxbuf_count               ;is the uart Rx buffer empty?
    43.     movf  uartRxbuf_count,W,BANKED      ; ..
    44.     bz  uartRx_to_usbTx_exit            ;   yes -- then nothing to do here (compiler does 'sublw 0, bc exit' for this test)
    45. ;   movlb   debugstatus                 ;   no -- data needs to be moved ..
    46.     addwf   debugstatus,F,ACCESS        ;       record how many bytes
    47.     call copy_uartRxbuf_to_usbTxbuf     ;       then move the data (data will not be copied if USB is not ready)
    48.     movlb uartRxbuf_count               ;       get the count of any data not taken by usb
    49.     movf  uartRxbuf_count,W,BANKED      ;       ..
    50. ;   movlb   debugstatus                 ;       and adjust the count of transferred data
    51.     subwf   debugstatus,F,ACCESS        ;       ..
    52.     call    print_data
    54. uartRx_to_usbTx_exit:
    55.     bsf PIE1bits,RCIE,0                 ;   reenable the uart rx interrupt  
    56.     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)
    57.     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?

    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: May 3, 2010