View previous topic :: View next topic |
Author |
Message |
a21
Joined: 01 May 2010 Posts: 10
|
PIC18F24J50 and USB |
Posted: Fri Jan 14, 2011 11:33 am |
|
|
Hi,
I want to connect PIC18F24J50 to the pc by USB. The datasheet says that it must be a capacitor on Vusb pin (ex. 0.1uF). I put 1uF SMD. When I plug it in the USB the Windows says that it can not recognize the device. Here is my code:
main.h:
Code: |
#include <18F24J50.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOCPUDIV
#FUSES NOPROTECT //Code not protected from reading
#FUSES HSPLL //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PLL5
#FUSES T1DIG
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES DSWDTOSC_INT
#FUSES RTCOSC_T1
#FUSES NODSBOR
#FUSES NODSWDT
#FUSES DSWDT2
#FUSES NOIOL1WAY //Allows multiple reconfigurations of peripheral pins
#FUSES MSSPMSK7
#FUSES NOWPFP
#FUSES WPBEG
#FUSES NOWPCFG
#FUSES WPDIS
#use delay(clock=48000000)
|
main.c:
Code: |
#include <main.h>
#include <pic18_usb.h>
#include <descriptor.h>
#include <usb.c>
void main()
{
setup_oscillator(OSC_PLL_ON);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
int8 out_data[USB_CONFIG_HID_TX_SIZE];
int8 in_data[USB_CONFIG_HID_RX_SIZE];
memset(in_data, 0x00, USB_CONFIG_HID_RX_SIZE);
memset(out_data, 0x00, USB_CONFIG_HID_TX_SIZE);
//output_b(0x00);
//set_tris_b(0x00);
usb_init_cs();
usb_task();
output_high(PIN_B3);
while (1)
{
if (usb_enumerated())
{
output_low(PIN_B3);
if (usb_kbhit(1))
{
usb_get_packet(1, in_data, USB_CONFIG_HID_RX_SIZE);
//out_data=in_data;
usb_put_packet(1, in_data, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
}
}
}
}
|
I forgot ... I am using 22.1184MHz crystal with 22pF capacitors (there wasn't 20MHz in the shop) and compiler v4.114 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 14, 2011 12:42 pm |
|
|
I am using 22.1184MHz crystal (there wasn't 20MHz in the shop).
Read this thread on USB oscillator frequency accuracy requirements.
http://www.ccsinfo.com/forum/viewtopic.php?p=127450
(i.e., how far is 22 MHz from 20 MHz ?). |
|
![](templates/subSilver/images/spacer.gif) |
a21
Joined: 01 May 2010 Posts: 10
|
|
Posted: Fri Jan 14, 2011 1:13 pm |
|
|
Thanks. I will try it with 4MHz clock. |
|
![](templates/subSilver/images/spacer.gif) |
a21
Joined: 01 May 2010 Posts: 10
|
|
Posted: Sun Jan 16, 2011 11:47 am |
|
|
It is ok now and Windows enumerates it. But have another problem. I get the HID code for C# from Microchip. It detects when the device is plug/unplug by its VID&PID, but when I try to write to the USB I get error 1784 (ERROR_INVALID_USER_BUFFER). I know that this forum is for CCS not C#, but I hope that here is someone who knows C# and can help me. |
|
![](templates/subSilver/images/spacer.gif) |
a21
Joined: 01 May 2010 Posts: 10
|
|
Posted: Mon Jan 17, 2011 2:11 pm |
|
|
Ok, I did it. But I have another question (... again). How can I use the interrupt from usb? I don't want to have something like:
Code: |
while(1)
{
if (usb_kbhit(1))
{
do something ...
}
}
|
I tried
Code: |
void usb_isr()
{
if (usb_kbhit(1))
{do something ...
}
}
|
... but it doesn't work. I get many errors:
"Identifier already used in this scope" and "Expecting a declaration" and so on. I suppose that this is because the ccs usb driver uses this interrupt. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Mon Jan 17, 2011 3:01 pm |
|
|
There is no 'simple' interrupt from USB. There are dozens of transactions going on all the time, which have to be handled or the interface doesn't work. This is what the USB code does. If you wanted to handle the interrupts yourself, you would have to write the entire driver. Look at the size of the CCS code, and you will get an idea of what is needed.
Date is being received for you, and sent for you by the driver all the time. You really _need_ to write your code like your "I don't want to do this" example.....
You _can_ add a small amount of code to the existing USB interrupt handler, to trigger something to happen when a legitimate character has 'arrived', this is what 'usb_kbhit' already does. If you start adding anything taking significant time to the handler, you _will_ stop USB working (there are response time limits to USB transactions, and if you slow the handler too much, these _will_ be broken).
You do not get a simple 'here is a character' type interrupt from USB. Data is being received all the time.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
a21
Joined: 01 May 2010 Posts: 10
|
|
Posted: Mon Jan 17, 2011 3:06 pm |
|
|
Ttelmah wrote: | There is no 'simple' interrupt from USB. There are dozens of transactions going on all the time, which have to be handled or the interface doesn't work. This is what the USB code does. If you wanted to handle the interrupts yourself, you would have to write the entire driver. Look at the size of the CCS code, and you will get an idea of what is needed.
Date is being received for you, and sent for you by the driver all the time. You really _need_ to write your code like your "I don't want to do this" example.....
You _can_ add a small amount of code to the existing USB interrupt handler, to trigger something to happen when a legitimate character has 'arrived', this is what 'usb_kbhit' already does. If you start adding anything taking significant time to the handler, you _will_ stop USB working (there are response time limits to USB transactions, and if you slow the handler too much, these _will_ be broken).
You do not get a simple 'here is a character' type interrupt from USB. Data is being received all the time.
Best Wishes |
Thanks, man. USB is new for me, but I am trying to learn it and you were very helpful. |
|
![](templates/subSilver/images/spacer.gif) |
|