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

USB with PIC18F4550

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



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

USB with PIC18F4550
PostPosted: Sun May 01, 2011 12:12 pm     Reply with quote

Hello,

I will try the ex_usb_serial.c example.
I need an extern 5V power supply because the 500mA from USB are too little.

So I have connected the pic18f4550 on my 5V power supply. My question, must I connect the VUSB from the USB connector to the pic VUSB?

I have never found a sample for this.

Thanks Volker
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun May 01, 2011 2:58 pm     Reply with quote

No.
Vusb, is _not_ 5v. Attaching 5v to this pin, will stop USB working.
Vusb, is around 3.3v, used for the USB pull up resistors and drivers. Look at the data sheet. Figure 17-1.The PIC itself can generate Vusb, provided it is fed with 5v, _or_ you can provide an external voltage regulator. Normally use the internal regulator (VREGEN fuse), and an external capacitor on this pin (low ESR, 0.22 to 1uF). It is also often recommended that a resistor to ground of about 150 to 220KR is attached to this pin.
It is _easier_ to detect that the USB connection is attached, if you attach the USB 5v, _via a resistor divider_, to pin B2 of the PIC (this is selectable in software look at the USB files).

Best Wishes
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Mon May 02, 2011 5:03 am     Reply with quote

Hello,

so I have attached a 220nF from vusb to gnd.
Then I have create a project with pic18F4550 and put the example code ex_usb_serial in my project.

But the pc is not find the pic.

Then I have create a new project with the same fuses like the example and a led should blink with every 200ms. The led is blinking, but every 3 seconds!?

Then I have try this code (example code with led blink), but no led is blinking:
Code:
/////////////////////////////////////////////////////////////////////////
////                                                                 ////
////                        ex_usb_serial.c                          ////
////                                                                 ////
//// A demonstration of the USB CDC API that is provided by CCS.     ////
//// The USB CDC API that CCS provides will create a virtual UART    ////
//// port.  USB CDC drivers are included with most versions of       ////
//// Microsoft Windows, and when properly loaded will create a COMx  ////
//// port from which you can write and read to your PIC device       ////
//// like any serial device that has a COMx port.                    ////
////                                                                 ////
//// This example creates a USB<->UART converter.  Open              ////
//// Hyperterminal to COM1 (or whatever COM port is your usual RS232 ////
//// serial COM port).  Plug the PIC to USB.  Open Hypertimernal to  ////
//// the new COM port that is the USB<->UART COM port (for this      ////
//// example say it is COM2).  Typing a character in COM1 will cause ////
//// it to be sent out COM2, and vice-versa.                         ////
////                                                                 ////
//// See usb_cdc.h for API documentation.                            ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
////                                                                 ////
//// VERSION HISTORY                                                 ////
////                                                                 ////
//// July 1st, 2005: Initial Release.                                ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2005 Custom Computer Services         ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////

//set to 1 to use a PIC's internal USB Peripheral
//set to 0 to use a National USBN960x peripheral
#define __USB_PIC_PERIF__ 1

#if !defined(__PCH__)
 #error USB CDC Library requires PIC18
#endif


#if __USB_PIC_PERIF__
 #DEFINE LED1  PIN_A5
 #include <18F4550.h>
 #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
 #use delay(clock=48000000)
#else //use the National USBN960x peripheral
 #DEFINE LED1  PIN_B3
 #include <18F452.h>
 #fuses HS,NOWDT,NOPROTECT,NOLVP
 #use delay(clock=20000000)
#endif   //endif check to see which peripheral to use

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

/////////////////////////////////////////////////////////////////////////////
//
// If you are using a USB connection sense pin, define it here.  If you are
// not using connection sense, comment out this line.  Without connection
// sense you will not know if the device gets disconnected.
//       (connection sense should look like this:
//                             100k
//            VBUS-----+----/\/\/\/\/\----- (I/O PIN ON PIC)
//                     |
//                     +----/\/\/\/\/\-----GND
//                             100k
//        (where VBUS is pin1 of the USB connector)
//
/////////////////////////////////////////////////////////////////////////////
///only the 18F4550 development kit has this pin
#if __USB_PIC_PERIF__ && defined(__PCH__)
 #define USB_CON_SENSE_PIN PIN_B2
#endif


#include <usb_cdc.h>

/////////////////////////////////////////////////////////////////////////////
//
// Configure the demonstration I/O
//
/////////////////////////////////////////////////////////////////////////////
#define LED2 PIN_B4
#define LED3 PIN_B5
#DEFINE BUTTON PIN_A4
#define LED_ON output_low
#define LED_OFF output_high


/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging information over serial
// to display enumeration and connection states.  Also lights LED1 based upon
// enumeration and status.
//
/////////////////////////////////////////////////////////////////////////////
void usb_debug_task(void) {
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;
   static int8 last_cdc;
   int8 new_cdc;

   new_connected=usb_attached();
   new_enumerated=usb_enumerated();
   new_cdc=usb_cdc_connected();

   if (new_enumerated)
      LED_ON(LED1);
   else
      LED_OFF(LED1);

   if (new_cdc)
      LED_ON(LED2);
   else
      LED_OFF(LED2);

   if (usb_cdc_carrier.dte_present)
      LED_ON(LED3);
   else
      LED_OFF(LED3);

   if (new_connected && !last_connected)
      printf("USB connected, waiting for enumaration...\r\n\n");
   if (!new_connected && last_connected)
      printf("USB disconnected, waiting for connection...\r\n\n");
   if (new_enumerated && !last_enumerated)
      printf("USB enumerated by PC/HOST\r\n\n");
   if (!new_enumerated && last_enumerated)
      printf("USB unenumerated by PC/HOST, waiting for enumeration...\r\n\n");
   if (new_cdc && !last_cdc) {
      printf("Serial program initiated on USB<->UART COM Port\r\n\n");
      printf(usb_cdc_putc, "\r\n\nCCS CDC (Virtual RS232) Example\r\n\n");
   }

   last_connected=new_connected;
   last_enumerated=new_enumerated;
   last_cdc=new_cdc;
}

void main(void) {
   char c;

   LED_OFF(LED1);
   LED_OFF(LED2);
   LED_OFF(LED3);
   
   //->>>
   while(1){
   LED_ON(LED1);
   LED_ON(LED2);
   LED_ON(LED3);
   delay_ms(200);
   LED_OFF(LED1);
   LED_OFF(LED2);
   LED_OFF(LED3);
   delay_ms(200);
   }
   //<<<-

   printf("\r\n\nCCS CDC (Virtual RS232) Example\r\n");

  #ifdef __PCH__
   printf("PCH: v");
   printf(__PCH__);
  #else
   printf("PCM: v");
   printf(__PCM__);
  #endif
   printf("\r\n");

   usb_init_cs();

  #if !(__USB_PIC_PERIF__)
   printf("USBN: 0x%X", usbn_get_version());
   printf("\r\n\n");
  #endif


   while (TRUE) {
      usb_task();
      usb_debug_task();

      if (kbhit()) {
         c=getc();
         if (c=='\n') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         if (c=='\r') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         else {usb_cdc_putc(c);}
      }
      if (usb_cdc_kbhit()) {
         c=usb_cdc_getc();
         if (c=='\n') {putc('\r'); putc('\n');}
         if (c=='\r') {putc('\r'); putc('\n');}
         else {putc(c);}
      }
   }
}




Can anybody tell me the right fuses?
Why are in the sample the delay_clock are 48.000.000?

[/code]
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon May 02, 2011 6:02 am     Reply with quote

If you're using MPLAB, be sure to set the 'build configuration' to 'release' AND recompile(F10) THEN download the code into your PIC.
The default build configuration is set to 'debug' and will really slow down the execution speed of code in the PIC.
There is a patch to change the default build configuration for MPLAB v8.66.
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Mon May 02, 2011 7:11 am     Reply with quote

I am not using MPLAB...

The PC don't find the pic. Has anybody an idea, tipp?

Best regards
Volker
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon May 02, 2011 9:59 am     Reply with quote

You don't mention connection sense. Have you got the connection sense wired to pin B2?.
Section around line 62 in your file. Won't affect the initial flash, but will stop USB from working.

With _two_ 200mSec rate, the flash rate should be about 0.4 second.
You have got a 20MHz crystal?. If you had a 4MHz crystal, I'd expect about a 2 second flash rate....

Best Wishes
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Tue May 03, 2011 9:28 am     Reply with quote

Hello,

@Ttelmah this was the problem.
Thank you Very Happy
I tryed for many hours to run the USB!

But where is this signal used?

Best regards
Volker
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue May 03, 2011 2:32 pm     Reply with quote

This is used if you _don't_ want the code to hang for ever if usb is not attached.
You can turn it off in the setup files.
If it is defined, you can call usb_init_cs (connection sense), instead of usb_init, and the code will return immediately, _without trying to initialise the USB_, if the cable is not attached. The USB won't actually initialise, till the connection is seen.
Look at the remarks at the top of usb.h, where the use of the pin, and how it affects the behaviour is described. The default for the examples, is to have this pin connected.

Best Wishes
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Wed May 04, 2011 2:16 pm     Reply with quote

Hello,

the usb is not easy Shocked

I have running the ex_usb_serial sample, it works fine.

But I have extended the code:
Code:
void usb_debug_task(void) {
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;
   static int8 last_cdc;
   int8 new_cdc;

   new_connected=usb_attached();
   new_enumerated=usb_enumerated();
   new_cdc=usb_cdc_connected();

   if (new_enumerated)
      LED_ON(LED1);
   else
      LED_OFF(LED1);

   if (new_cdc)
      LED_ON(LED2);
   else
      LED_OFF(LED2);

   if (usb_cdc_carrier.dte_present)
      LED_ON(LED3);
   else
      LED_OFF(LED3);

   if (new_connected && !last_connected)
      printf("USB connected, waiting for enumaration...\r\n\n");
   if (!new_connected && last_connected)
      printf("USB disconnected, waiting for connection...\r\n\n");
   if (new_enumerated && !last_enumerated)
      printf("USB enumerated by PC/HOST\r\n\n");
   if (!new_enumerated && last_enumerated)
      printf("USB unenumerated by PC/HOST, waiting for enumeration...\r\n\n");
   if (new_cdc && !last_cdc) {
      printf("Serial program initiated on USB<->UART COM Port\r\n\n");
      printf(usb_cdc_putc, "\r\n\Debug Information\r\n\n");
   }

   last_connected=new_connected;
   last_enumerated=new_enumerated;
   last_cdc=new_cdc;
}

void main(void) {
   /////////////////////////////////////////////////////////////////////////////
   // Variable declaartion
   /////////////////////////////////////////////////////////////////////////////
   int i = 0;                   // enumerate char from interface
   int j = 0;
   char c;                      // char from interface
   char cCommand[32];
   char bOpen=0;
   
   /////////////////////////////////////////////////////////////////////////////
   // Setup and inistialisation
   /////////////////////////////////////////////////////////////////////////////
   kbd_init();
   
   
   LED_OFF(LED1);
   LED_OFF(LED2);
   LED_OFF(LED3);
   
 
   printf("\r\n\nWelcome \r\n");
 
   printf("PCH: v");
   printf(__PCH__);
   printf("\r\n");

   usb_init_cs();

  #if !(__USB_PIC_PERIF__)
   printf("USBN: 0x%X", usbn_get_version());
   printf("\r\n\n");
  #endif
 

   while (TRUE) {
      /*-----------------------------------------------------------
        - USB
        -----------------------------------------------------------*/
      usb_task();
      usb_debug_task();

      if (kbhit()) {
         c=getc();
         if (c=='\n') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         if (c=='\r') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         else {usb_cdc_putc(c);}
      }
      if (usb_cdc_kbhit()) {
         c=usb_cdc_getc();
         if (c=='\n') {putc('\r'); putc('\n');}
         if (c=='\r') {putc('\r'); putc('\n');}
         else {putc(c);}
      }
     
      /*-----------------------------------------------------------
        - USB Port commands
        -----------------------------------------------------------*/
      i=0;
      printf("Command---: ");
      while(i<=32){
        c = kbd_getc();
        if(c!='\0'){
          cCommand[i]=c;
          if(c=='#'){
            putc('\n');
            putc('\r');
            break;
          }
          if(c=='*'){
            for(j=0; j<i; j++){
              putc(cCommand[j]);
            }
            putc('\n');
            putc('\r');
            if(bOpen==0){
              srv_min();
              bOpen=1;
              printf("\n\r--open--\n\r");
            }else{
              srv_max();
              bOpen=0;
              printf("\n\r--close--\n\r");
            }
             
            break;
          }
          i++;
        }
      }
   }


The usb device was not correct identified.
srv_max, srv_min are simle lopps to move a servo.

When I comment out this:
Code:
i=0;
      printf("Command---: ");
      while(i<=32){
        c = kbd_getc();
        if(c!='\0'){
          cCommand[i]=c;
          if(c=='#'){
            putc('\n');
            putc('\r');
            break;
          }
          if(c=='*'){
            for(j=0; j<i; j++){
              putc(cCommand[j]);
            }
            putc('\n');
            putc('\r');
            if(bOpen==0){
              srv_min();
              bOpen=1;
              printf("\n\r--open--\n\r");
            }else{
              srv_max();
              bOpen=0;
              printf("\n\r--close--\n\r");
            }
             
            break;
          }
          i++;
        }
      }

... the usb device was correct identified!

It's the same if I make a usb_cdc_putc(...). The pic was hanging up.

Best regards
Volker
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