View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
USB problem |
Posted: Thu Aug 13, 2015 1:31 am |
|
|
Hai, i'm trying to send out data to port D using usb communication. USB can detect the device and i already install the driver, but the problem is, there is no command "Enter 8 digit binary number : " appear when i use teraterm. I have select the correct COM to used the teraterm. What is the problem?
Code: | #include <18F4550.h>
#fuses HSPLL,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
int1 usb_cdc_oldconnected=FALSE;
int8 binary_string[10];
void main()
usb_init_cs();
do {
if (usb_attached()) {
usb_task();
if (usb_enumerated()){
if (usb_cdc_carrier.dte_present) {
if (usb_cdc_oldconnected==FALSE) {
printf(usb_cdc_putc,"Your 'connected' message");
usb_cdc_oldconnected=TRUE;
}
while(true)
{
printf("Enter 8 digit binary number : \n");
get_string(binary_string,9);
output_D((int8)strtoul(binary_string,0,2));
}
}
else
usb_cdc_oldconnected=FALSE;
}
else
usb_cdc_oldconnected=FALSE;
}
} while (TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Thu Aug 13, 2015 1:49 am |
|
|
Your CPU is running at 48MHz, not 20MHz. Serial won't then work.
HSPLL, says to run the CPU off the USB PLL. With a 20Mhz crystal, and CPUDIV5 (correct), the PLL will be generating 96MHz. CPUDIV1 says to divide this by two, and then the CPU runs at 48MHz.
Primary (most obvious) legal settings:
Code: |
#include <18F4550.h>
#fuses HS,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
//get rid of PLL. CPU then runs off the crystal - PLL still runs the USB
#use delay(clock=20000000)
//or
#include <18F4550.h>
#fuses HSPLL,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48MHz)
|
There are lots of others...
Separately you do realise that the prompt for you to input, will go out on the normal serial, _not_ the USB?. These lines:
Code: |
printf("Enter 8 digit binary number : \n");
get_string(binary_string,9);
|
are both talking to the normal serial port, not the USB serial emulation. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Thu Aug 13, 2015 3:24 am |
|
|
What command should i replace this 2 lines? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Thu Aug 13, 2015 3:59 am |
|
|
For the first, you are already using it just a few lines earlier. Tiny syntax difference....
For the second you just have to write your own. Pull the source code for get_string, and just duplicate it using the usb functions instead of getc/putc. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Thu Aug 13, 2015 4:14 am |
|
|
Is this correct?
Code: |
printf(usb_cdc_putc,"Enter 8 digit binary number : \n");
get_string(usb_cdc_binary_string,9);
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Thu Aug 13, 2015 7:01 am |
|
|
No.
The first line is.
The second, as I said _you_ have to load the code for get_string (it is in input.c), and modify _this_ to use USB. It won't 'miraculously' change to using usb, by just changing the name of the target string. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Thu Aug 13, 2015 10:15 pm |
|
|
Hi ttelmah,
I've check in the 'usb_cdc.h' and it stated that can use get_string_usb(char *s,int max), so i change my code:
printf(usb_cdc_putc,"Enter 8 digit binary number : \n");
get_string_usb(binary_string,9);
But still nothing appear on the teraterm. What is the problem? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Fri Aug 14, 2015 12:35 am |
|
|
Have you changed the clock settings?.
Is the device enumerating?.
Do you get the USB_connected message?.
Are you sure that your terminal program enables DTE?.
Have you tried one of the simple demo programs like ex_serial?. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Fri Aug 14, 2015 1:06 am |
|
|
Hi Ttelmah,
Now its functioning, but there is one new problem arise.
It will work only for about 80 second. After 80 second the port disconnect automatically. What cause this problem? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Fri Aug 14, 2015 4:30 am |
|
|
Could be anything.
Commonest cause of erratic behaviour is inadequate smoothing on Vusb. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Fri Aug 14, 2015 6:50 pm |
|
|
Do i need to connect additional 5v supply to the circuit? And what is the best capacitor value that i should connect to Vusb , 470nF or 560nF or other higher value. Right now, I' m using 470 nF connect to Vusb and GND terminal. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Sat Aug 15, 2015 12:53 am |
|
|
470nF ought to be OK. Is this right by the chip?. What type of capacitor is it?. The recommended type is polyester. Vusb, is 3.3v. It is generated by the internal regulator, which like all regulators needs the right amount of smoothing.
How are your USB connections wired?. (the two track need to be equal length). The lines should not be more than a couple of inches long, away from other signals, and close to 90R differential impedance:
<http://www.cvel.clemson.edu/emc/calculators/PCB-TL_Calculator/>
(45R impedance for each trace). |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Sat Aug 15, 2015 1:58 am |
|
|
Yes, it is a polyester type capacitor and I put it very near to the PIC. I'm using a B type USB. The length of the 2 track is not equal about 1cm due to a jumper to switch position between pin 2 and 3 of USB on PCB.
I will change the USB location in order to make the line length equal. Do you know what is the maximum distance of the USB terminal to PIC terminal?
I will inform to you the result once I modified my design. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19542
|
|
Posted: Sat Aug 15, 2015 2:25 pm |
|
|
If the PCB has the wrong impedance, adding series resistors (15R) close to the USB chip will reduce reflections. Length should not be a problem if less than a few inches, and the impedance is a good match. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Wed Aug 19, 2015 2:21 am |
|
|
Hai Ttelmah,
I've modified the usb location on pwb to make pattern equal length, but the result is still the same, the com still disconnect automatically after about 80 second. Is it cause by the driver? I've tried using different pc with os window xp and window 7, but the problem still happen. Or is there is any other code that can be used to solve this problem. Or maybe a similar example that i can study?
Kindly please reply |
|
|
|