PDA

View Full Version : Help -USBIn with 18F14K50 receives only one byte



barak
24th May 2012, 01:07
I need a virtual COM-port to exchange data with a PIC18F14K50 from a PC. I never did anything with USB on a PIC. I started based on the USB18 Com.bas example. Made very few changes: changed Device from 18F4550 to 18F14K50 and changed to work with my SPI LCD.
I used the the IDE's Serial Com utility to send receive ASCII strings.
The output to the terminal works exactly as expected but I'm confused by the input. After receiving one char the Carry_Flag is cleared and the code continues to print that one char.Since the examples uses a 20 byte string array (In_Buffer) for the input, I assumed the Carry_Flag does not clear until the entire string from the terminal is loaded to In_Buffer.

Is this a problem in my code or is it the way it works with the serial terminal?

For example: the terminal sends "123"

Repeat
USBIn 3, In_Buffer, Auto
Until Carry_Flag = 0

Print At 1,1,"USB Buffer"
Print At 2,1,In_Buffer ' prints only one char
The display first print only "1" then on the next iteration of the loop shows only "2", etc.
I tried adding at the end
Repeat : Until TRNIF = 1
But this gets even more confusing, the loop stops one char before the last but the last is still in the buffer and prints the next time I send a string from the terminal???

The entire string from the terminal is loaded correctly to In_Buffer if I change the code to keep reading single chars (just like reading from a serial port) until a CR:

While 1 = 1
Repeat
USBIn 3, usb_byte, Auto
Until Carry_Flag = 0
If usb_byte = 13 Then Break ' exit the loop on CR
In_Buffer = In_Buffer + usb_byte ' load char to string
Wend
Print At 1,1,"USB Buffer"
Print At 2,1,In_Buffer ' prints OK "123"
Version 3.5.4.2 - complete code attached

johngb
24th May 2012, 08:28
My understanding is that the carry flag will indicate that at least one byte has been received, you need to monitor TRNIF to be sure that all data has been received.

Kapitan
24th May 2012, 11:33
The other gotcha to watch for..... ;)

Right click on the TX window, in the terminal app, and look at the options..

;---------------------------------------------------------------------

barak
24th May 2012, 13:26
My understanding is that the carry flag will indicate that at least one byte has been received, you need to monitor TRNIF to be sure that all data has been received. I tried it as per example in manual (see in original post) - the only difference it that the loop stops one char before the last but the last is still in the buffer and prints the next time I send a string from the terminal. I verified TRNIF is set after the first byte.

barak
24th May 2012, 13:32
The other gotcha to watch for..... ;)

Right click on the TX window, in the terminal app, and look at the options..

;--------------------------------------------------------------------- I tried many different options on the Serial com TX window, it only affects the last char (i.e., if a CR, null, etc.) still getting one char at a time.

Kapitan
24th May 2012, 13:37
I tried many different options on the Serial com TX window, it only affects the last char (i.e., if a CR, null, etc.) still getting one char at a time.

It was just a thought, it caught me out several times Sir.

Joe

barak
24th May 2012, 14:19
Hey Joe,
Being the USB guru that you are, can you confirm that the code in the USB18 Com example is supposed to load the entire “block” of data to In_Buffer?
If so, then is the problem coming from use of 18F14K50 instead of 18F4550?
The code with fuse settings, etc. is attached to original post
As I wrote in the original post, the USB output works perfect except that after each char sent from the terminal all the output sequence is repeated:
For example: the Transmit window of the terminal has "12" when transmitted the Receive window will have the output strings repeated two times:
----------------
First for the "1"
----------------
USB Buffer
Array Buffer
String Buffer
Code Memory Buffer
Quoted String
Code Memory
VAR1 = 72
'---------------
And again for the "2"
'---------------
USB Buffer
Array Buffer
String Buffer
Code Memory Buffer
Quoted String
Code Memory
VAR1 = 73

Kapitan
25th May 2012, 07:39
Hi Barak, thanks for your confidence lol ;), but it is misplaced as I have forgotton most of what I did a few years back, and none of that was with cdc.

Can you clarify, in your first post the second example you say works ? is that correct, as in works as expected ?
in my limited understanding, you have the option in the tx window setting to hold the code until cr is pressed, or, if unchecked, send 1 character at a time.
depending on the setting you choose, is is then up to you what you do at the other end.
Unfortunately I have not got easy acces to the dev board at the moment or I would do a test on the 4550.
But it seems logical that if you choose the terminal prog to send instantly on key press (not waiting for cr) that the byte is going to be transmitted and received, but what do you then do with it ? . you either accumulate it, or send it to the display one byte at a time.
I hope that makes sense :) .
It would be far easier to have the board in front of me and experiment, and as said, i never really looked at cdc at all.

barak
25th May 2012, 15:04
Thanks Joe,
I completely understand about not remembering stuff. Before I turned 50, I could remember everything for years. Now, a couple of months after developing something, I look at my code/schematics as if made by someone else.
The second example simply takes only one byte at a time until the byte value is 13 (CR). This is a typical solution for serial terminal but my understanding is that USBIn should load up to 64 bytes directly from the dual port RAM (__USBIN_BUFFER) to an array (In_Buffer in the example).
By mirroring the Carry_Flag to an I/O, I did some timing tests with a scope. It takes almost 1mS between “reading” each byte. See timing explained in comments below

While 1 = 1
'1. hanging here for almost 1000uS !!!
'----------------------------------
Repeat
USBIn 3, usb_byte, Auto
Until Carry_Flag = 0
' 2. from 1 to 2 takes 1.6uS
'---------------------------------
If usb_byte = 13 Then Break
In_Buffer = In_Buffer + usb_byte
' 3. measured separately: from 2 to 3 takes 5uS
'--------------------------------
Wend
This long delay may be the reason USBIn only takes one byte.
The question is weather it is the terminal that is slow (I tried several baud rates but it made no difference)? or is something in the code or 18F14K22 vs. 18F4550 that slows it?

barak
25th May 2012, 15:39
Code is OK, USBIn is OK. The problem must be with the IDE's Serial com utility.
I just wrote a simple terminal program in VB and an entire sting loads to the array as it supposed to. Looks very promising.

Kapitan
26th May 2012, 05:43
Hi Barak, try terra term
or, if you have a windows xp machine about, just copy the old hyperterminal over if you are on win7, works great on win 7/64
I like the old hyperterminal I must confess. I never got on well with the pds one.
re age, yes I hit 55 next month and I am sure I will forget even more.. I even look back on fully commented code from a month ago and often it takes me several hours to get my head around it ;)
Anyway, looks like you may have a solution ! fingers crossed.
Joe

barak
26th May 2012, 12:43
Thanks Joe,
I am not far behind you at 54. The good thing about fading memory is you can watch the same movies or read the same books again and again.
The USB CDC works very nice both XP and WIN7. I took a VB 2008 project with serial com and got it to work with minimal modifications. With USB being rather complicated its amazing what PDS can do with just three commands.
Next mission is USB boot loader.

Maverick
26th May 2012, 17:44
Don't worry about the memory thing I am 38 and forget what i did yesterday half the time.

Joe I am using Terraterm atm but must also confess a preference to good ol hyper-terminal. I tried to drop it on here (WIN764) but it doesn't play nice with 64 and some virtual coms I found.
Anything I might be missing to make it behave itself ?

billyminor
27th May 2012, 01:15
Maverick why don't you try Realterm , here's a link
http://realterm.sourceforge.net/

Kapitan
27th May 2012, 05:33
Don't worry about the memory thing I am 38 and forget what i did yesterday half the time.

Joe I am using Terraterm atm but must also confess a preference to good ol hyper-terminal. I tried to drop it on here (WIN764) but it doesn't play nice with 64 and some virtual coms I found.
Anything I might be missing to make it behave itself ?

Hi Mav
Two files to copy hypertrm.exe and hypertrm.dll
I have them both in a single folder, and runs great with a shortcut to the exe
Win7 64 home preminum

Maverick
27th May 2012, 08:59
Thanks Joe thats how I did it also , so looks like my system is to blame ,, never mind back to terra term.

Thanks Brian , I could have sworn i tried real term and dismissed it for some reason ,, but like i said earlier I forget , hehe
I will download it and give it a bash

Kapitan
27th May 2012, 09:09
Thanks Joe thats how I did it also , so looks like my system is to blame ,, never mind back to terra term.

Thanks Brian , I could have sworn i tried real term and dismissed it for some reason ,, but like i said earlier I forget , hehe
I will download it and give it a bash

Hi Mav
Strange, it works great here with what I use it for, like the rs232 /423 to USB converters, CDC PDS Pic direct, FT232R USB to uP etc..
What types of things are giving you grief with it ?
Joe

ps re

Don't worry about the memory thing I am 38 and forget what i did yesterday half the time.

Which half do you forget most ?, the half you are asleep, or the half you are awake lol :) ... for me, it is both of them nowadays haha..

Maverick
27th May 2012, 14:21
pretty much forget everything ; lol

As for what gives me grief , it seems like a buffer thing ,, when I am running at say 921600 baud it hangs (only have virtual ports as my PC is void of real ones) Terra term works fine tho any baud.
Also hangs at odd middle bauds ,, like its fine at 115200 but burps at 56600 as I am debugging at 921600 i switched to terra term in the end.

Kapitan
28th May 2012, 05:41
Ah. understood.
I must confess although all my serial ports are virtual, I tend to run at lower rates (115.2 Max) for what I need. Good to know that TT is ok there though in case I need it, I have both installed, but tend to just use HT at the mo.

joesaliba
2nd March 2013, 16:59
Code is OK, USBIn is OK. The problem must be with the IDE's Serial com utility.
I just wrote a simple terminal program in VB and an entire sting loads to the array as it supposed to. Looks very promising.

Barak,

Old post, but.....

I am trying to learn VB and I need some hints how to send a string. Using VB2010. I did a text box and then try to send out the data but all is coming up is garbage. Trying PDS serial communicator all I am getting is a single byte each time.

Thank you

barak
2nd March 2013, 18:27
Joseph, I had to put this project on hold but I'll try dig out the VB 2008 code and send it to you.

joesaliba
2nd March 2013, 18:36
Thank you very much!