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 interfacing from pic to PC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nidhimittalhada



Joined: 06 Sep 2010
Posts: 21

View user's profile Send private message

usb interfacing from pic to PC
PostPosted: Sat Oct 02, 2010 4:53 am     Reply with quote

I have pic development board

http://www.rhydolabz.com/index.php?main_page=product_info&cPath=99_101&products_id=297
in which I give power to board through usb port from PC.

Now I want to learn interfacing PIC18f4550 through usb to PC.

How should I start?

I have no knowledge about USB interface /protocol/device.
Please guide me from start...

Nidhi
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 02, 2010 11:57 am     Reply with quote

The CCS compiler has many examples of using an 18F4550 with USB:
Code:

c:\program files\picc\examples\ex_usb_hid.c
c:\program files\picc\examples\ex_usb_scope.c
c:\program files\picc\examples\ex_usb_mouse.c
c:\program files\picc\examples\ex_usb_kbmouse2.c
c:\program files\picc\examples\ex_usb_kbmouse.c
c:\program files\picc\examples\ex_usb_bootloader.c
c:\program files\picc\examples\ex_usb_serial2.c
c:\program files\picc\examples\ex_usb_serial.c
c:\program files\picc\examples\ex_usb_loadmouse.c

I think most people on this board would say to start with the USB CDC
examples (serial).

If you are a beginner with C and with hardware, using the CCS examples
is going to be difficult. They have large numbers of #ifdef statements
that will be hard to understand.

I only did one experiment with their examples, for the ex_usb_hid.c file.
In the thread below, I posted a "walk-through" for modifying the file to
work with my board (PicDem2-Plus).
http://www.ccsinfo.com/forum/viewtopic.php?t=38897
Maybe someone else has a similar thread for the CDC examples. I don't
know. Also, I don't want to do this project for you. I'm just helping you
to get started.
nidhimittalhada



Joined: 06 Sep 2010
Posts: 21

View user's profile Send private message

PostPosted: Sun Oct 03, 2010 2:30 am     Reply with quote

#include "18F4550.h"

#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN

#use delay(clock=48000000)

#include <usb.c>
void main()
{
int8 new_enumerated;

usb_init();
set_tris_b(0x00);
while(1)
{
new_enumerated=usb_enumerated();
if (new_enumerated)
{
output_high(PIN_B1);
delay_ms(2000);
}
else
output_high(PIN_B3);
}
}

This is my code , just starting code for testing device enumeration.
But it on compilation gives error

Error 23 "C:\Program Files\PICC\devices\18F4550.h" Line 2(8,9): Can not change device type this far into the code

I have tried removing include 18F4550 line from my main file also , still this error persists .

pl help , what can be cause of this error!!!

thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19458

View user's profile Send private message

PostPosted: Sun Oct 03, 2010 4:13 am     Reply with quote

You have something else 'in front' of what you have posted.
Basically, this error occurs, if you have any code that changes the device type _after_ something else that sets or uses the device type. What you have posted, compiles _without_ this error. So you have something (an include, a device statement, almost anything!), in front of what you have posted.
Are you compiling this in CCS, or using another environment like MPLAB?. If the latter, you could also get the error if something is being included by the development environment, before this code.

Comment, use the code buttons. Makes code much easier to read!....

Best Wishes
nidhimittalhada



Joined: 06 Sep 2010
Posts: 21

View user's profile Send private message

Undefined identifier-usb_enumerated even when usb.c included
PostPosted: Tue Oct 05, 2010 11:13 am     Reply with quote

Hi
I have resolved that problem of header file inclusion in proper sequence.
But now the other problem is

When I build all files in project -- it says BUILD FAILED. Error is
Undefined identifier -- usb_enumerated
but I have included usb.c as shown in file tree I am printing below.

I am using MPLAB 8.53 and My file tree shows
Source Files
usb.c
usb_main.c

Header File
PIC18F4550.h
usb_desc_hid.h
usb.h
pic18_usb.h
usb_main.h


Object Files

other file
usb.osym
usb_main.sym
usb_main.sym


I am showing you the file structure where in usb.c is standard library and usb_main.c is my program.
Code:

#include "18F4550.h"
#include <pic18_usb.h>
#include <usb_desc_hid.h>   //USB Configuration and Device descriptors for this UBS device
#include <usb.h>
#include <usb_main.h>

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

void main()
{
      int8 out_data[20];
      int8 in_data[2];
   int8 new_connected;
    int8 new_enumerated;
      int8 send_timer=0;
   usb_init();
   set_tris_b(0x00);
   while(1)
   {
       new_connected=usb_attached();
      if(new_connected)
       output_high(PIN_B1);
      delay_ms(2000);   
            new_enumerated=usb_enumerated();
         if (new_enumerated)
      {
           output_high(PIN_B1);
         delay_ms(2000);   
      }
      else
         output_high(PIN_B3);
   }
}

Please guide why its not getting usb_enumerated variable recognized when its present in usb.c file, which is present in my file tree.

Now my guess is:
Build process is not giving error related to usb_init which is a function in usb.c file.
But build process is giving error related to usb_enumerated which is not a function usb.c but a #define constant is usb.c


Code snippet from usb.c
Code:

/* usb_enumerated()
/*
/* Input: Global variable USB_Curr_Config
/* Returns: Returns a 1 if device is configured / enumerated,
/*          Returns a 0 if device is un-configured / not enumerated.
/*
/* Summary: Use this to see if device is configured / enumerated.
/***************************************************************/
#define usb_enumerated() (USB_stack_status.curr_config)

Please guide us.
sturnfie



Joined: 26 Apr 2010
Posts: 17
Location: Palatine, IL

View user's profile Send private message Visit poster's website

Re: Undefined identifier-usb_enumerated even when usb.c incl
PostPosted: Wed Oct 06, 2010 12:44 pm     Reply with quote

nidhimittalhada wrote:
Hi
I have resolved that problem of header file inclusion in proper sequence.
But now the other problem is

When I build all files in project -- it says BUILD FAILED. Error is
Undefined identifier -- usb_enumerated
but I have included usb.c as shown in file tree I am printing below.
Please guide us.


The code base for USB is relatively dense. I'd recommend pulling the files into the project as in-line includes (see usb_cdc.h as an example of how to pull in usb.c and usb_desc_cdc.h). This will guarantee each file in built in the required order. Directly adding them to the project, especially in MPLAB, doesn't guarantee the build order without some extra compiler configuration. This results in those annoying "undefined identifier" errors.

If you go with a CDC based test, you can do something like this (stripped down version of ex_usb_serial2.c):

Code:

#include "18F4550.h"

#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, NODEBUG, USBDIV, PLL5, CPUDIV1, VREGEN

#use delay(clock=48000000)

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


void main(void)
{
   BYTE i;
   usb_init_cs();

   do
   {
      usb_task();
      if (usb_enumerated())
      {
         if (usb_cdc_kbhit())
         {
            i = toupper(usb_cdc_getc());
           
            if (i == 'R')
            {
               printf(usb_cdc_putc, "Sending a R Back");
            }
           
            if (i == 'W')
            {
               printf(usb_cdc_putc, "Sending a W Back);
            }
         }
      }
   } while (TRUE);
}




Lucas
_________________
Lucas Sturnfield
Blog - http://www.sturntech.com/blog
nidhimittalhada



Joined: 06 Sep 2010
Posts: 21

View user's profile Send private message

PostPosted: Fri Oct 08, 2010 3:02 am     Reply with quote

we copied pasted your code and in header files included
18F4550.h
usb_cdc.h
nothing else
Now it gives familiar error
Cannot define Device so far in the code --- error points to 18f4550 line #DEVICE

which we were also getting in our case too...then we tweaked inclusion of files in usb.c to remove this error.
nidhimittalhada



Joined: 06 Sep 2010
Posts: 21

View user's profile Send private message

errors in given program
PostPosted: Tue Oct 12, 2010 10:41 am     Reply with quote

Source Files
try_usb.c -----------> this program given by you
Header Files
usb_cdc.h


Now
i do project build all in MP LAB
and error comes is


*** Error 23 "C:\Program Files\PICC\devices\18F4550.h" Line 2(8,9): Can not change device type this far into the code
*** Error 48 "C:\Program Files\PICC\devices\18F4550.h" Line 190(2,6): Expecting a (
*** Error 48 "C:\Program Files\PICC\devices\18F4550.h" Line 190(18,23): Expecting a (
*** Error 43 "C:\Program Files\PICC\devices\18F4550.h" Line 190(0,1): Expecting a declaration
*** Error 51 "try_usb.c" Line 39(37,38): A numeric expression must appear here
*** Error 79 "try_usb.c" Line 39(37,38): Expect }
*** Error 79 "try_usb.c" Line 39(37,38): Expect }
*** Error 79 "try_usb.c" Line 39(37,38): Expect }
*** Error 79 "try_usb.c" Line 39(37,38): Expect }
*** Error 75 "try_usb.c" Line 39(37,38): Expect WHILE
*** Error 79 "try_usb.c" Line 39(37,38): Expect }
11 Errors, 1 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Sun Sep 19 22:09:30 2010
sturnfie



Joined: 26 Apr 2010
Posts: 17
Location: Palatine, IL

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 12, 2010 10:56 am     Reply with quote

Don't add usb_cdc.h directly to the project. Pull it in-line as per the example. Only add this "try_usb.c" to the project. Then build the project. The code I posted does have a typo, it is missing a closing ". Double click the error to find the line, and change the code to match this:

Code:
           
if (i == 'W')
 {
               printf(usb_cdc_putc, "Sending a W Back");
 }



If you want to add additional files to the project, you're going to have to specify a build order, or add a conditional check to determine if the #DEVICE has already been specified. An easy way to do this is to use a header file. In this header file (let's call it main.h), define a variable:
Code:
#define _MAIN_H_


In each file you add to your project, put this at the top:
Code:

#ifndef _MAIN_H_
#include "main.h"
#endif


In main.h, you want to pull in the device file:
Code:
#include "18F14K50.h"


This is a method of ensuring header files are only compiled once.
_________________
Lucas Sturnfield
Blog - http://www.sturntech.com/blog
sturnfie



Joined: 26 Apr 2010
Posts: 17
Location: Palatine, IL

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 12, 2010 11:08 am     Reply with quote

Btw, you'll get this when the build succeeds. Use the search feature on this forum to learn why you get these warnings (they can be safely ignored).

Code:

>>> Warning 216 "try_usb.c" Line 39(2,3): Interrupts disabled during call to prevent re-entrancy:  (usb_token_reset)
>>> Warning 216 "try_usb.c" Line 39(2,3): Interrupts disabled during call to prevent re-entrancy:  (usb_cdc_get_discard)
>>> Warning 216 "try_usb.c" Line 39(2,3): Interrupts disabled during call to prevent re-entrancy:  (usb_tbe)
>>> Warning 216 "try_usb.c" Line 39(2,3): Interrupts disabled during call to prevent re-entrancy:  (usb_cdc_flush_out_buffer)
      Memory usage:   ROM=14%      RAM=20% - 23%
      0 Errors,  5 Warnings.
BUILD SUCCEEDED: Tue Oct 12 12:06:15 2010

_________________
Lucas Sturnfield
Blog - http://www.sturntech.com/blog
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Wed Oct 13, 2010 6:25 am     Reply with quote

Well if you are new to the PIC and new to usb this will prove to be very challenging. Most start simple then go complex in this case complex is the usb interface. Usb has a sequenced set of commands and strict timing any one of which just slightly off will result in failure. The PIC code itself can be complex partly because it addresses the complexity of usb and partly because it is written so as to be portable to different pic devices via includes and defines. Doing something simple like blinking leds and timers will start getting you acquainted with the includes and defines found in main and the xxxx.h files.
With skill gained with the compiler issues then move onto usb. If a compiler message shows up chances are you are already familiar with it.
asdf85



Joined: 03 Jan 2011
Posts: 34

View user's profile Send private message

PostPosted: Wed Jan 11, 2012 11:46 pm     Reply with quote

I've been trying to get the PC to recognize the USB connection with my pic, but all the examples I try doesn't work.

I'm using a pic18f25j50 with a 20Mhz crystal, trying to connect to windows 7. My compiler version is 4.093.

I tried Sturnfie code here also and it doesn't work. Everytime I plug the USB cable into the pc, there's a notification saying 'USB device not recognized'. What am I missing? I did download the 'Microchip Application Libraries V2011-12-05' thinking there are drivers there, but I can't seem to get any recognized.

This is Sturnfie's code which I adjusted slightly due to the different PIC used:
Code:

#include "18F25j50.h"

#fuses HS, NOWDT, NOPROTECT,NODEBUG, PLL5

#use delay(clock=20000000)
//#define USB_CON_SENSE_PIN PIN_B2
// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>


void main(void)
{
   BYTE i;
   usb_init_cs();

   while(TRUE)
   {
      usb_task();
      if (usb_enumerated())
      {
         if (usb_cdc_kbhit())
         {
            i = toupper(usb_cdc_getc());
           
            if (i == 'R')
            {
               printf(usb_cdc_putc, "Sending a R Back");
            }
           
            if (i == 'W')
            {
               printf(usb_cdc_putc, "Sending a W Back");
            }
         }
      }
   } 
}

temtronic



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

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 6:26 am     Reply with quote

CCS supplies the required 'Windows stuff' needed for it to recognize and 'connect' to their examples.
The CDC example does work fine as written,I'm using an 18F4550.
USB is a real 'challenge' compared to 'old school' Comports! The USB overhead takes up 1/3 of the memory space of an 18F4550 so I'm really leaning towards using Vinculum-II 'modules' to recover the memory,have BOTH Host and Slave ports and get the product out the door faster.
The nice thing it sames TONS of time and money but for home use the onboard USB does work. Get the 'serial CDC ' example running first, then carry on..
sturnfie



Joined: 26 Apr 2010
Posts: 17
Location: Palatine, IL

View user's profile Send private message Visit poster's website

PostPosted: Thu Jan 12, 2012 8:52 am     Reply with quote

asdf85 wrote:
I've been trying to get the PC to recognize the USB connection with my pic, but all the examples I try doesn't work.

Im using a pic18f25j50 with a 20Mhz crystal, trying to connect to windows 7. My compiler version is 4.093.

I tried Sturnfie code here also and it doesn't work. Everytime I plug the USB cable into the pc, there's a notification saying 'USB device not recognized'. What am I missing? I did download the 'Microchip Application Libraries V2011-12-05' thinking there are drivers there, but I can't seem to get any recognized.

This is Sturnfie's code which I adjusted slightly due to the different PIC used.


Hi,

I'm guessing you have a USB driver issue on the Windows 7 side. I'd suggest getting USBView (from http://www.ftdichip.com/Support/Utilities.htm) and looking at the USB Device Descriptors.

If the device is enumerating properly, you will be able to read the idVendor and idProduct values. If you don't have the Windows 7 USB driver to match with the Vendor and Product descriptors, the device will not be recognized by Windows 7.

A forum search turns up this:
http://www.ccsinfo.com/forum/viewtopic.php?t=42852

Lucas
_________________
Lucas Sturnfield
Blog - http://www.sturntech.com/blog
asdf85



Joined: 03 Jan 2011
Posts: 34

View user's profile Send private message

PostPosted: Thu Jan 12, 2012 9:35 pm     Reply with quote

When I try updating the driver with the 'CDC_NTXPVista.inf' once windows states ' usb device not recognized', it does show 'USB to rs232' in the update driver screen. However when i click next to install, windows will state 'Windows encountered a problem installing the driver software for your device. Btw I am using a 32bit windows 7.

I would also like to know, if i make a simple program with just 'usb_init_cs(); ' followed by a 'usb_task();' , is that enough to get the usb device recognized or enumerated by windows in most cases?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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