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

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



Joined: 10 Jun 2012
Posts: 5

View user's profile Send private message

USB problems...
PostPosted: Sun Jun 10, 2012 4:23 am     Reply with quote

Hello everyone,

I am trying to leave the old (but lovely) 232 for the new (but ugly) USB. The main reason for this is that it’s almost impossible to see a PC with classical serial ports nowadays. Unfortunately, any attempt I had tried to make the USB stack work has failed. I have copied the code from an example but as I didn’t have all the components I had to adapt it to ones I had. My PIC is a 18F4455 and I’m using a 16 MHz crystal with a couple of 33 pF capacitors. I have used a couple of 95K resistors for the connection sense pin (which is B2), and I have attached a led with a serial resistance to B7 just to check that the core is running at the right speed (the idea is to see a blink for a second) and also, to see it bright continuously when the USB is working. Here’s the code:

Code:

#include <18F4455.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //Crystal osc > 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL4                     //PLL PreScaler divided by 4
#FUSES CPUDIV1                  //No System Clock Postscaler
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled
#FUSES ICPRT                    //ICPRT enabled

#use delay(clock=16000000)


The code is supposed to transfer through USB the readings from the ADC channel 0..

Code:

#include "C:\Trabajos\Proyectos PIC\Prueba_USB\main.h"
  #include <assert.h>
  #include <ctype.h>
  #include <errno.h>
  #include <float.h>
  #include <limits.h>
  #include <locale.h>
  #include <math.h>
  #include <setjmp.h>
  #include <stdio.h>
  #include <stddef.h>
  #include <stdlib.h>
  #include <stdlibm.h>
  #include <string.h>

#define USB_CON_SENSE_PIN PIN_B2
#include <usb_cdc.h>


void main()
{

   int16 q, q1;
   float p;
   q1=0;

   setup_adc_ports(AN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   
   set_adc_channel(0);
   set_tris_b(0x0F);
   
   //Blink...
   output_low(PIN_B7);
   delay_ms(500);
   output_high(PIN_B7);
   delay_ms(1000);
   output_low(PIN_B7);
     
     
   usb_cdc_init();
   usb_init();
   do{
   usb_task();
   if(usb_enumerated()){
   output_high(PIN_B7);
   q = read_adc();
   if(q!=q1){
   p = 5.0 * q/1024.0;
   printf(usb_cdc_putc, "\n\r Voltaje = %01.2fV",p);
 
   }
   q1 = q;
   delay_ms(500);
   }
   }while(TRUE);
     
}


When I turn on the PIC, I can see that the B7 led blinks for a second.. so then I connect it to the PC (Windows 7) and the OS shows me that it is an “unknown device” (code 43)..

Please If you have any ideas, would be very welcome
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Jun 10, 2012 5:35 am     Reply with quote

I case you didn't know you need a cap on the vusb pin it isn't that critical for the value 0.22uf is recommended but even 1uf will work.
Next check the wiring, win7 I believe will report a 43 error if the D- and D+ are reversed.
My fuses are
Code:

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL4,CPUDIV2,VREGEN

Below is some documentation for a 2455 usb to can bus project
Code:
///////                            18F2455
///                               +-----------+
////                         mclr_|1        28|_B7 PCD
//                                |           |
//                             A0_|2        27|_B6 PGC
//                                |           |                 
///        Green   Led         A1_|3        26|_B5
//                                |           |
//                             A2_|4        25|_B4---- 100k-->5v
//                                |           |
////      Red LED              A3_|5        24|_B3 Debugger
//                                |           |
//                             A4_|6        23|_B2 INT of MCP2515 Pin 12
//                                |           |                 
///      MCP2515 Reset  <----  A5_|7        22|_B1 SCK -->SPIclk to MCP2515 Pin13
//                                |           |                     
//                      Vss   Gnd_|8        21|_B0 SDI--->SO MCP2515 Pin15
//                                |           |
////   +---|15pf|--+-____--- Osc1_|9        20|_5v Vdd
// Gnd-|          16Mhz Xtal      |           |
//     +---|15pf|--+-------- Osc2_|10       19|_Gnd Vss
//                                |           |                 
///                            C0_|11       18|_C7 SDO--->SI MCP2515 Pin14
//                                |           |
//                             C1_|12       17|_C6
//                                |           |
///         CS to MCP2515 <----C2_|13       16|_C5 D+ USB green
//                                |           |
//    Gnd--------|0.47uf|----Vusb_|14       15|_C4 D- USB white
//    data sheet is 0.22uf        |           |
//    use polyester               +-----------+
//   Note we are using 1uf polyester
/////////////////////////////////////////////
///
///     5v  ----/\/\/\--+--__\__---- GND
///              100k   | NO switch
///                     |
///     B4 -------------
///               
///     Vusb ------| |----- GND
////             
////
/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
///
///                  usb connector
///                 ___________      _______
///                 | 4 3 2 1 |     /       \
///                 |=========|     | 1   2 |
///                 |_________|     | 4   3 |
//                                  |_______|
///
///              1 Red wire VCC 5v
///              2 White        D-
///              3 Green        D+
///              4 Black        Gnd
//////////////////////////////////////////////////////////////////////////
AlphaParticle



Joined: 10 Jun 2012
Posts: 5

View user's profile Send private message

PostPosted: Sun Jun 10, 2012 7:09 am     Reply with quote

Hey Douglas! Thank you SOOOOOOOOOOOOOOOOO MUCH!! Very Happy It was the capacitor!! I simply can’t believe I have spent all these days just for a capacitor! Shocked It has worked perfectly as it was supposed to work! Thanks!! Very Happy
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