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

PIC18F2550 not being recognized by PC

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



Joined: 03 Dec 2013
Posts: 6

View user's profile Send private message

PIC18F2550 not being recognized by PC
PostPosted: Tue Dec 03, 2013 2:15 pm     Reply with quote

Hello, first time here, and I'm novice.

I'm trying to make a program to work.
Code:
 #include <18F2550.h>
 #device ADC=10
 
 #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
 #use delay(clock=48000000)
 #include <usb_cdc.h>
 
 #define GREEN_LED PIN_B4
 #define USB_CON_SENSE_PIN PIN_B7
 
 void main(void) {
         int16 q,q1;
         float p;
         q1=0;
         setup_adc_ports(AN0|VSS_VREF);
         setup_adc(adc_clock_internal);
         set_adc_channel(0);
 
         OUTPUT_HIGH(GREEN_LED);
 
         usb_cdc_init();
         usb_init();
         do {
                 usb_task();
                 if(usb_enumerated()) {
                            OUTPUT_LOW(GREEN_LED);
                            q=read_adc();
                            if (q!=q1) {
                                      p=5.0*q/1024.0;
                                      printf(usb_cdc_putc, "\n\r Voltage = %01.2fV", p);
                            }
                            q1=q;
                            delay_ms(500);
                  }
         }while (true);
 }

Which successfully compiled and generated hex file, so I went programming the pic. However when connecting usb to the PC it does nothing at all. Windows (7 Professional 64 bit) doesn't even recognize any new devices, and green led remains lit, effectively meaning there's no enumeration, nor any attempt to it. Since there's no "New hardware found" popout I can't use the cdc custom inf file...

These are my connections: pin1 +5V, pin2 should be the analog reading channel, pin4 ground, pin5 +5V, pin8 ground, pin9/10 20 mhz oscillator with 22pF capacitors, pin14 ground, pin15 D-, pin16 D+, pin19 ground, pin20 +5V, pin25 led with resistor. The rest of pins no connection. Usb connector's ground and power pins also connected to ground and +5V respectively, so circuit is power supplied by PC's usb port (or so it seems, already tried with an external power supply and same results...)

What's happening?
Thanks beforehand.
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 3:47 pm     Reply with quote

You have 'connection_sense_pin', set to PIN_B7 (this should be set _before_ loading the USB code - it'll actually use B0 I think by default....). This pin needs to be pulled up when the USB power is present. Circuit is in the .h file for the USB (as text art).
Also as a comment, ADC_CLOCK_INTERNAL, is not legal above 1Mhz. Will degrade the accuracy.

Best Wishes
leba2



Joined: 03 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Tue Dec 03, 2013 6:26 pm     Reply with quote

Sorry for the question, but does that have to do with the pic not being recognized at all by the Windows PC? I can't even use the inf file for the driver...
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 2:07 am     Reply with quote

Yes.
Unless connection sense is 'true', the chip will not even attempt to start USB.
leba2



Joined: 03 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 1:28 pm     Reply with quote

But I thought "USB_CON_SENSE_PIN" was just a label, just like "GREEN_LED" is, which only meaning was identifying the pins by other name instead of the pin name itself...
Labels should not have meaning themselves for the compiler....

But, do you mean I should define pins *before* including usb_cdc.h?

And regarding setup_adc, how can I fix it legal way then?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Dec 04, 2013 2:15 pm     Reply with quote

leba2 wrote:
But, do you mean I should define pins *before* including usb_cdc.h?
YES!!!!
This is part of the basic understanding for the C language. The compiler goes through the source code text in several runs to convert the text into machine understandable code. One of the first runs is a simple text replacement, all the values you have #defined will be searched in the code from top to bottom and replaced by the defined value. This way you can also switch options in the source code on and off.
The place where you locate your #define is important as only from that point in the code the value will be known.

Now, the USB driver includes pic18_usb.h where you find the following fragment:
Code:

#ifndef USB_CON_SENSE_PIN
#define USB_CON_SENSE_PIN  0
#endif

...

/******************************************************************************
/* usb_attached()
/*
/* Summary: Returns TRUE if the device is attached to a USB cable
/*
/*****************************************************************************/
#if USB_CON_SENSE_PIN
 #define usb_attached() input(USB_CON_SENSE_PIN)
#else
 #define usb_attached() TRUE
#endif
So, when the #define USB_CON_SENSE_PIN is missing, then it will be defined as 0.
In the second part you see that when USB_CON_SENSE_PIN is defined as 0 then the function usb_attached() will always return TRUE.

So yes, the #define should be located before the #include and this is wrong. But... since the default behaviour is now to _always_ return a usb_attached state the USB peripheral is always initialized.
Conclusion: you have more errors.

First:
Simplify your program by stripping all not needed code until you have a tiny program that is just blinking a LED, that is, no USB stuff yet.
This will confirm you can program the chip and that at least 80% of the electronics is connected correctly.

Second:
Don't try to re-invent the wheel. Use one of the many USB sample programs supplied by CCS.
leba2



Joined: 03 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Thu Dec 05, 2013 2:43 pm     Reply with quote

OK, tried with a very simple program:
Code:
#include <18F2550.h>

#fuses XTPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN

#use delay(clock=48000000)

#define green_led PIN_B4
#define USB_CON_SENSE_PIN PIN_B7

#include <usb_cdc.h>

void main(void) {
        do {
                output_high(green_led);
                delay_ms(500);
                output_low(green_led);
                delay_ms(500);
        }while(true);
}

Using a 4 MHz crystal oscillator and left connections just as I told in my first post. Seemed to work. Led blinked every half a second and master clear pin also worked. I'll have to continue tomorrow since the pic programmer is not mine...

The example I was trying I didn't invent it, I took it directly from a CCS book as one of the solved examples. But now that I check it, certainly the pin definition was prior to including usb cdc, so I copied it wrong. I wanted to leave this usb_con_sense as an indicator of being enumerated and existence of data streaming. But I guess I should better use usb_enumerated()...

Also, for programming the pic I'm using SP580U superpro for Windows. I noticed when adding the hex file the config word gets automatically updated. With this simple led program it was like this:
Code:
FOSC1=1
USBDIV=1
VREGEN=1
WDPS0=1
WDPS1=1
WDPS2=1
WDPS3=1
CCP2MX=1
enabled IESO=1
VBOR set to 2.0V
PWRT enabled
FCMEN=1 enabled
WDT disabled

The first 2 pages. Are they correct, or should I change something?
Ttelmah



Joined: 11 Mar 2010
Posts: 19358

View user's profile Send private message

PostPosted: Thu Dec 05, 2013 3:56 pm     Reply with quote

Big question. Is the device going to be powered from the USB, or have it's own supply in the final version?.

Key to understand, is that if the device has it's own power, then the 'connection sense' input is _required_, not optional. You can only run without it when powered from the USB bus (since then the device is only powered when attached). Unless you use connection sense, the device won't re-enumerate, when disconnected, and then reconnected.

Best Wishes
leba2



Joined: 03 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 12:21 pm     Reply with quote

Well, I'm using supply from USB.
After the simple led test, I retried the program in my first post but with these changes before the main program:
Code:
#include <18F2550.h>
#device ADC=10
#fuses XTPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN
#use delay(clock=48000000)
#define green_led PIN_B4
#include <usb_cdc.h>

Connections are the same as mentioned in first post as well, but I'm using a 4 MHz crystal oscillator, since the 20 MHz one wasn't mine.
Still not working, same results. Also I can't test the CCS examples since I don't even understand them.
Any help?
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 2:15 pm     Reply with quote

Honestly, you should get the CCS examples to run first ! Most people here have used them as the basis of getting USB running, so they are familiar with how the code works.
I did that with the '4550 to PROVE things were correct(like D-,D+) after that I went the USB<>TTL module approach for several good reasons.

I have no idea what "SP580U superpro " is or how it'll affect C code construction..
I run CCS Compiler through the MPLAB IDE as it works for me since V2.540


hth
jay
leba2



Joined: 03 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Fri Dec 06, 2013 5:19 pm     Reply with quote

SP580U superpro is the programmer I use to physically program the PIC. I really thought people here knew what it is... The programmer trademark is SuperPro.
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