CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC18F24J50 and USB

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
a21



Joined: 01 May 2010
Posts: 10

View user's profile Send private message

PIC18F24J50 and USB
PostPosted: Fri Jan 14, 2011 11:33 am     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 14, 2011 12:42 pm     Reply with quote

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 ?).
a21



Joined: 01 May 2010
Posts: 10

View user's profile Send private message

PostPosted: Fri Jan 14, 2011 1:13 pm     Reply with quote

Thanks. I will try it with 4MHz clock.
a21



Joined: 01 May 2010
Posts: 10

View user's profile Send private message

PostPosted: Sun Jan 16, 2011 11:47 am     Reply with quote

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.
a21



Joined: 01 May 2010
Posts: 10

View user's profile Send private message

PostPosted: Mon Jan 17, 2011 2:11 pm     Reply with quote

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.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Jan 17, 2011 3:01 pm     Reply with quote

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
a21



Joined: 01 May 2010
Posts: 10

View user's profile Send private message

PostPosted: Mon Jan 17, 2011 3:06 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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