PIC18F14K50 and USBin


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    10

    Default PIC18F14K50 and USBin

    Hi!

    I'm trying to use an internal USB-port (PIC18F14K50) in CDC-mode (USB-COM emulation). It is strange but the USBIN command reads only the first byte sent from the host. The other bytes are I don't know where from.
    I tried to write to the buffer initial meanings. It is seen that the buffer changes. But the result is very strange.

    Of course I could send byte-after-byte but I wonder why the USB-port functions not properly, not as described in the PBPpro manual.

    The part of the program:

    Variant 1
    -----------
    inloop:
    cnt = 1
    USBService
    USBIn 3, buffer, cnt, inloop

    outloop:
    USBService
    USBOut 3, buffer, 1, outloop
    Goto inloop

    I send "ABC" via USB-COM-port and receive "ABC" back.

    Variant 2
    -----------
    inloop:
    cnt = 3
    USBService
    USBIn 3, buffer, cnt, inloop

    outloop:
    USBService
    USBOut 3, buffer, 3, outloop
    Goto inloop

    I send "ABC" via USB-COM-port and receive "AÆBÆCÆ" back. It can be seen that every right symbol accompanied with some others. It looks like USBIN receives all the rest symbols in the buffer after the first byte is sent from the host.


    Does this apply only to the PIC18F14K50? I looked through the forum and didn't find anything like my problem.
    -------------------
    Everybody wants to have a job but nobody wants to work.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    The USBIN command returns the number of bytes received in the cnt variable.

    You should use that variable for the number of bytes to be sent by USBOUT.
    Code:
    USBOut 3, buffer, cnt, outloop
    Otherwise, if it only receives 1 byte, and sends 3 bytes, 2 of them will be garbage characters left over in the buffer.
    DT

  3. #3
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    10


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    I seem to explain badly.

    The command USBIN doesn't write bytes to the buffer one after another.

    I send 3 bytes and hope that all of them would be sent into the buffer (array of 3). But really after the first received byte the program writes something to the other 2 buffer bytes and begins receiving next byte. Then adds to it 2 its own etc...

    One fact is clear: all those nowhere bytes are the same every time they are written. So I think there is some cause why those strange bytes appeared.
    -------------------
    Everybody wants to have a job but nobody wants to work.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    Quote Originally Posted by ybosco View Post
    I send 3 bytes and hope that all of them would be sent into the buffer (array of 3). But really after the first received byte the program writes something to the other 2 buffer bytes and begins receiving next byte. Then adds to it 2 its own etc...
    With CDC, you have no control over how many bytes are received at a time. That depends on the operating system of the PC.
    You may get 3 bytes, or you may only get 1, you have to account for the number of bytes received.

    Your belief that USBIN is writing characters that it didn't receive, is completely incorrect.
    Those characters are already in the buffer array.
    If you clear the array before doing the USBIN you will see the difference.

    Read Post #2 again, which will fix your sample code.
    Last edited by Darrel Taylor; - 3rd November 2013 at 19:38.
    DT

  5. #5
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    Hi Derrel,
    I would like to use the USB connection to the PC for the first time since I do not have the serial port available to me anymore.
    I am asking if there is a complete sample program that shows me everything on the USB usage to exchange data to and from the PC.
    Are there some hardware advices ? or just use d+ and d- to the PIC ?
    The pic is the 184550 and the compiler is PBP 2.50.
    Thanks in advance for the assistance on the matter.
    Best regards,
    Ambrogio IW2FVO

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    To replace a COM port with USB, you'll want to use the CDC protocol.

    Start with this thread ...

    USB CDC Communications for Dummies!
    http://www.picbasic.co.uk/forum/showthread.php?t=5806
    DT

  7. #7
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    Thanks Derrel,
    I red a lot of posts as indicated.
    I am asking if there is a "final summary "available in which the final hardware and software configuration is described.
    In my personal case I shall use the 18f4455 device and the crystal oscillator that could be 20 Mhz or 4 MHz. No bus power will be used.
    What will be the configuration set up ? ( include, define ... etc )
    What is the program to callup every cycle in my PBP program to comunicate and to receive USB data?
    I am sorry but reading all the above posts I still not have a clear understanding.
    Thanks for any help.
    Best regards,
    Ambrogio
    iw2fvo

  8. #8
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    10


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    Quote Originally Posted by Darrel Taylor View Post
    Start with this thread ...

    USB CDC Communications for Dummies!
    http://www.picbasic.co.uk/forum/showthread.php?t=5806
    I have to note that that thread doesn't write anything useful about clock settings. The line "DEFINE OSC 48" looks very naively. :-) It is not enough.

    By the way I managed partly to solve my problem. I found that if I set the system clock as high as 48 MHz then the CPU functions unstable. I got some other problems with another type of operations besides the USB interface.

    Now my setting are the following:
    • clock frequency- 12 Mhz;
    • 4xPLL -enabled;
    • CPUDIV=10;

    That is the system clock is 16 MHz and USB clock is 48 MHz.
    But I had no time to check USB operations at these conditions. I'll check and will report lately.
    -------------------
    Everybody wants to have a job but nobody wants to work.

  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    I have to note that that thread doesn't write anything useful about clock settings. The line "DEFINE OSC 48" looks very naively. :-) It is not enough.
    Maybe it would help if you read the whole thread, or at least to post #14
    http://www.picbasic.co.uk/forum/show...8359#post68359

    And the data sheet is VERY helpful too.
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    10


    Did you find this post helpful? Yes | No

    Default Re: PIC18F14K50 and USBin

    Quote Originally Posted by mackrackit View Post
    And the data sheet is VERY helpful too.
    Of course, it is very helpful!

    Still not checked USB with new clock settings... It might happen tonight.
    -------------------
    Everybody wants to have a job but nobody wants to work.

Similar Threads

  1. Pic18f14k50
    By JoeyWallice in forum mel PIC BASIC
    Replies: 2
    Last Post: - 23rd October 2012, 12:13
  2. Pic18f14k50
    By JoeyWallice in forum mel PIC BASIC
    Replies: 1
    Last Post: - 17th October 2012, 03:16
  3. Pic18F14K50 when?
    By Free_Flow in forum PBP Wish List
    Replies: 16
    Last Post: - 17th August 2009, 03:36
  4. Usbin and hserin
    By mpardinho in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th October 2007, 16:26
  5. Need help with USBIN
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th June 2007, 19:51

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts