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 support@ccsinfo.com

USB CDC problem

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



Joined: 08 Feb 2011
Posts: 18

View user's profile Send private message

USB CDC problem
PostPosted: Tue Mar 22, 2011 3:39 pm     Reply with quote

Hello! I had successfully implemented an USB CDC with 18F4550 to retrieve data from a PIC24FJ by using a SPI interface, however, between each operation, at least 100us delay must be added to ensure the data sent by PIC24FJ does not get corrupted....

I think the problem lies with the use of #use delay() because for PIC18F I'm using a 20MHz crystal to Operate at 48MHz, in PIC24FJ however I set #use delay() to 8MHz....
Code:

  #include <18F4550.h>

  //configure a 20MHz crystal to operate at 48MHz
  #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
  //#fuses   USBDIV, PLL1, CPUDIV1, PROTECT, NOCPD, noBROWNOUT,HSPLL,NOWDT,nolvp, VREGEN
  #use delay(clock=48000000)


/////////////////////////////////////////////////////////////////////////////
//
// 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

// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>

#rom int 0xf00000={1,2,3,4}

void main() {
   int i;
   int8 value0[1024], value1[1024],value2[1024];
   setup_spi(spi_slave | spi_ss_disabled);
//   setup_spi (SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4);
   usb_cdc_init();
   usb_init();
   
   while(!usb_cdc_connected()) {}

   do {
      usb_task();
      if (usb_enumerated()) {
//         printf(usb_cdc_putc, "\r\n\nData read from ADC:\r\n");              // Display contents of the first 64
      if (spi_data_is_in())
      {
//    while(!spi_data_is_in());    // wait for SPI activity from master
       // Read byre from SPI interface
     for (i=0;i<1024;i++)
       {
      value0[i] = spi_read();
      value1[i] = spi_read();
      value2[i] = spi_read();
      }
//   for (i=0;i<256;i++)
//   {
//   printf(usb_cdc_putc, "%2x ", i);
//   }
       
     for (j=0;i<1024;j++)
       {
      // Write byte to USB
       printf(usb_cdc_putc, "%X%X%X\r",value0[i],value1[i],value2[i]);
      }
     }
   }

      }while (TRUE);
   }



Code:
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOCOE                    //Device will reset into operational mode
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES PR                       //Primary Oscillator
//#FUSES PR_PLL                   //Primary Oscillator with PLL
#FUSES CKSNOFSM                 //Clock Switching is enabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES XT
#use delay(clock=8000000)
#use rs232(UART1,baud=9600,parity=N,bits=8)
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