|
|
View previous topic :: View next topic |
Author |
Message |
rald85
Joined: 28 Sep 2010 Posts: 6
|
pic18f2450 problems with the USB in HID and CDC codes |
Posted: Tue Sep 28, 2010 11:17 pm |
|
|
Hello experts
I hope than somebody can help me with this issues because I have like 6 months working on this
description problem:
CDC: When I configure the PIC as CDC device the PC recognize the PIC as comm port in hyperteminal. I can receive character or lines saying "hello world", but when I tried to send character like "1" or "2" or letters the PIC doesn't respond. For example when I send 1 the led turn on or when and send 2 the led turn off. But this never happen when I use usb_cdc_kbhit(). I tell the pic send me "character received" and I see it in hyperterminal but the led didn't respond, so I don't know what should be the problem.
HID: After try CDC, I tried with HID. When I plug in the pic to the USB port the PC recognize the pic as HID. But after 3 seg the PC show me blue screen with some letter on it but I can't read, then the computer restart.
Please I hope somebody have the solution or can give an idea how fix it. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 29, 2010 2:07 am |
|
|
Not really.
Both the CDC, and HID examples, do work. Obvious things would be something really basic, like not remembering that the character '1', is not the number '1', in your test code. I suspect this is your first problem...
The blue screen, possibly suggests a hardware issue. Poor smoothing on Vusb. Poor smoothing on the PIC supply. Overlong tracks between the USB connector and the PIC. Frequency problems with the crystal you are using (possibly the wrong capacitors on this - remember USB allows just +/- 0.01% on the timings, so your PCB layout, load capacitors and crystal need to be carefully selected). Depending on the block size being used for your HID example, this code may be fractionally less forgiving than CDC here.
Best Wishes |
|
|
rald85
Joined: 28 Sep 2010 Posts: 6
|
|
Posted: Wed Sep 29, 2010 2:31 am |
|
|
thank you Ttelmah for your quick reply
I am using 8MHz osillator with 20pF capacitor maybe this is the problem I'll change it. also I am adding my code for you can check it and tell me if something is wrong.
Code: | #include <18F2450.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HSPLL //Crystal/Resonator with PLL enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES VREGEN //USB voltage regulator enabled
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protecte
#FUSES MCLR //Master Clear pin enabled
#FUSES PLL2 //Divide By 2(8MHz oscillator input)
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES CPUDIV2 //System Clock by 4
#use delay(clock= 48000000)
#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
// librerias requeridas------------------------------------------------------------------------------
#include <usb_cdc.h>
#include <usb_desc_cdc.h>
#define on output_high
#define off output_low
#define led (pin_a0)
void main(void) {
char x;
on(led);
delay_ms(1000);
off(led);
usb_init();
usb_cdc_init();
while(true){
usb_task();
if (usb_cdc_kbhit()){
usb_cdc_putc("character received\n\r");
x = usb_cdc_getc();
usb_cdc_putc(x); //should be sending back the received character
}
}
}
|
I am using the same code for HID and CDC only changing the commands
the hardware is 220pF on the Vusb and I am not sure if I am using the internal pull up resistor or I need to install an external resistor. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 29, 2010 3:56 am |
|
|
220pF, on Vusb, is _far_ too small.
220nF, is the data sheet recommended capacitor, and many people have found that slightly larger than this may be better (470nF).
This may be 99% of your problem.
Then your clock setting is wrong.
This is one of the unfortunate 'must read the data sheet' bits for these chips. The fuse pattern CPUDIV2, gives division by two, if being fed from the external oscillator, but division by three, if being fed from the USB PLL. If you look at Figure 2-1 in the data sheet, you will see that divisions are:
Code: |
Name From external From USB
CPUDIV1 /1 /2
CPUDIV2 /2 /3
CPUDIV3 /3 /4
CPUDIV4 /4 /6
|
The CCS names, are the divisions when working from the external clock, and have to be translated by you, when working from the USB clock
So, with 'CPUDIV2', your 'clock' statement, needs to select 32MHz, not 48MHz. Use CPUDIV1, to get 48MHz.
Your RS232 won't work till this is fixed, or anything else using timings.
Best Wishes |
|
|
rald85
Joined: 28 Sep 2010 Posts: 6
|
|
Posted: Wed Sep 29, 2010 9:52 am |
|
|
Hi Ttelmah
I am sorry the capacitor value is 220nF no pF, I tried with 220nF and also with 470nF but the same results in CDC I can send characters from the PIC to the PC but not receive from the PC. Also I tried using an external pull up resistor on the D+ like the PDF says, and actually don't give me any problem, I guess this means the pic is sending the info in full speed and it's fine.
I changed the CPUDIV from 2 to 1 as you suggested but it is the same.
I am thinking if would be possible there are a bug on the library and which one library?, my version is 4.068 what do you think?
thanks, regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Sep 29, 2010 2:56 pm |
|
|
Not in the library, but the compiler....
The V4 compilers, basically started to work reasonably 'right', at around the early 4.07x area. Some code worked OK before this, and some complex code still gave occasional problems after, but as a 'guide', 4.070, is the earliest V4 compiler I have kept (I keep every compiler where a project was built and shipped, together with a note in the project saying what compiler it was built with), so before this point I had not got any of my code to actually work completely correctly with the V4 compilers. I have 3.249, and several earlier V3 compilers (the earliest V3 compiler I kept, was I think 3.094, again marking where this version started to work....).
Up till about 4.09x, I was still tending to 'double check' with 3.249, that problems were mine, and not the compiler....
Best Wishes |
|
|
rald85
Joined: 28 Sep 2010 Posts: 6
|
|
Posted: Wed Sep 29, 2010 4:07 pm |
|
|
Ttelmah
I just tried with another version of CCS V4.084 and not change, I don't know what would be the problem this time I am attching the code that am using to test the pic.
Code: | #include <18F2450.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HSPLL //Crystal/Resonator with PLL enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES VREGEN //USB voltage regulator enabled
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protecte
#FUSES MCLR //Master Clear pin enabled
#FUSES PLL2 //Divide By 2(8MHz oscillator input)
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES CPUDIV1 //
#use delay(clock= 48000000)
// librarys ------------------------------------------------------------------------------
#include <usb_cdc.h>
#include <usb_desc_cdc.h>
#define on output_high
#define off output_low
#define led (pin_a0)
void main(void) {
char x;
on(led);
delay_ms(1000);
off(led);
usb_init();
usb_cdc_init();
while(true){
usb_task();
if (usb_cdc_kbhit()){
usb_cdc_putc("\n\rcharacter received\n\r");
x = usb_cdc_getc();
usb_cdc_putc(x);
if (x == "1"){
on(led);
}
else if (x == "2"){
off(led);
}
}
}
}
|
So any other idea?... do you know if I need to configure something on the hyperteminal to send the characters?
regards, |
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Wed Sep 29, 2010 6:20 pm |
|
|
Code: |
if (x == "1"){
on(led);
}
else if (x == "2"){
off(led);
|
change the above to this
Code: |
if (x == '1'){
on(led);
}
else if (x == '2'){
off(led);
|
|
|
|
rald85
Joined: 28 Sep 2010 Posts: 6
|
|
Posted: Wed Sep 29, 2010 7:35 pm |
|
|
Hello MikeP
I tried your suggestion but did work, also using a variable but didn't too. |
|
|
rald85
Joined: 28 Sep 2010 Posts: 6
|
|
Posted: Wed Sep 29, 2010 10:59 pm |
|
|
Hello everyone,
I just got a solution for one of the problems the HID configuration, the problem was that I was not sending information to the PC, then this "information missing" makes the PC buffer crash, and make an error, that's why the PC showed me the blue screen and restarted, after configure the pic to keep sending the word "hello" to the PC, this never crash and looks good, until now!!!....
this is the code
Code: | #include <18F2450.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HSPLL //Crystal/Resonator with PLL enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES VREGEN //USB voltage regulator enabled
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protecte
#FUSES MCLR //Master Clear pin enabled
#FUSES PLL2 //Divide By 2(8MHz oscillator input)
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES CPUDIV1 //
#use delay(clock= 48000000)
// librerias requeridas HID------------------------------------------------------------------------------
#DEFINE USB_HID_DEVICE TRUE
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_TX_SIZE 8
#include <pic18_usb.h>
#include <usb_desc_hid.h>
#include <usb.c>
//-------------------------------------------------------------------------------------------------------
#define on output_high
#define off output_low
#define led (pin_a0)
void main(void) {
char x, valor1;
valor1 = "hello";
on(led);
delay_ms(1000);
off(led);
usb_init_cs();
while(true){
usb_task();
if (usb_enumerated()){
on(led);
delay_ms(1000);
off(led);
while(true){
usb_put_packet(1,valor1,4,USB_DTS_TOGGLE);
on(led);
delay_ms(500);
off(led);
delay_ms(500);
}
}
}
}
|
I am going to keep reading about the HID configuration, I hope finally start send character to the pic....
Does anyone knows how configure HID devices in visual basic?
regards |
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Wed Sep 29, 2010 11:20 pm |
|
|
I compiled this your code as posted with my changes in my last post it works fine here. I just had to make changes for the fuses because my clock is 20mhz on my demo board.
I can't test with hyperterminal as I do not have it installed and I refuse to use it. It is a POS and probably your problem now.
I would drop hyperterminal and try Teraterm you can get it here
http://en.sourceforge.jp/projects/ttssh2/downloads/48772/teraterm-4.67.exe/
Edit: fixed url |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|