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

Turn Led on - USB PIC
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 07, 2014 2:01 pm     Reply with quote

I was able to get it to work, after a while. I didn't use your Java
application because the Youtube video is not in English and I didn't
understand it. Because this is USB CDC program, I used TeraTerm
instead.

I took the CCS example file, ex_usb_hid_and_cdc.c, and I stripped it
down and simplified it a lot:
Code:

#include <ex_usb_common.h>
#include <usb_desc_cdc.h>   
#include <usb_cdc.h>

static void my_cdc_demo(void)
{
int8 received_char;
 
if(!usb_cdc_kbhit())
   return;

received_char = usb_cdc_getc();


if(received_char == '1')
  {
   output_high(PIN_B0);   // 1 = Turn on LED
  }

if(received_char == '0')  // 0 = Turn off LED
  {
   output_low(PIN_B0);
  }
 
// All characters other than '0' or '1' are ignored.

}
//===========================
void main(void)
{
output_low(PIN_B0);

usb_init_cs();

while(TRUE)
  {
   usb_task();

   if(usb_enumerated())
     {
      my_cdc_demo();
      delay_ms(1);
     }
  }
}


I selected PIC18F4550 in the MPLAB (vs. 8.92) Configure/Select Device
menu. My board has a 20 MHz crystal connected to the 18F4550.
The CCS USB programs are setup for this configuration.
I'm using CCS vs. 5.026.

At first, it wouldn't enumerate. After a while, I remembered that the
PicDem2-Plus that I'm using has both built-in circuits and other circuits that
I have added, connected to the USB pins. So I removed all those external
circuits, and I disconnected the 4.7K pull-up resistor on Pin C4 that is
built-in to the PicDem2-Plus. Then it enumerated.

Next problem. I'm using Windows XP. When I plug in the USB cable
to my PicDem2-Plus board (running the CDC program above), it wants
a driver. So I first pointed Windows to this directory:
Quote:
c:\Program Files\PICC\Drivers\NT,2000,XP,VISTA,7

But, Windows XP SP3 didn't like that driver directory. It wouldn't take it.
Then I tried this directory:
Quote:
c:\Program Files\PICC\Drivers\OLD_NT,2000,XP,VISTA,7

Then Windows liked it and installed the driver.

I then right-clicked on My Computer, went to Properties, Hardware,
Device Manager, Ports (COM & LPT) and then I could see that it has
the PicDem2-Plus with 18F4550 USB listed as "USB to UART (COM4)".
That's good. It sees the board, and it's on COM4.

Then I started up the TeraTerm terminal program. It defaults to 9600
baud which is the baudrate that the CCS USB programs expect. In the
TeraTerm menu, I went to Setup/ Serial Port, and then picked COM4 from
the drop-down box.

Then, using the TeraTerm program, I pressed the '1' key on my PC
keyboard and the LED on pin B0 on the PicDem2-Plus board turned on.
When I pressed '0', it turned off. It's working

I had to struggle a lot to get it to do this. Any little problem at all, will
make it fail.
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

Blink LED
PostPosted: Thu Aug 07, 2014 9:12 pm     Reply with quote

I tried the app you suggested, and I noticed that it ran slower than what was specified in the delay, so I had to change the clock speed to 1/4, so that it would run as it should.
I did this with the following code:
#use delay(clock=5000000)
I'm using the pic with 5V.
I just don't know the answers to the other questions.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 07, 2014 9:22 pm     Reply with quote

This photo of the PIC DIP40 MINI KIT board shows a 4 MHz crystal:
http://100mhz.com/u_file/product/13_11_05/f8f6ae7c8e.jpg

What is the frequency of the crystal on your board ?
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

Crystal Frequency
PostPosted: Fri Aug 08, 2014 12:03 am     Reply with quote

I'm using a 20 MHZ crystal, because the seller told me I should use that crystal for the usb connection to work.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 08, 2014 12:23 am     Reply with quote

I installed vs. 5.011 and I tried to recompile my project, but I got this error:
Code:
*** Error 111 "C:\Program Files\PICC\Projects\usb_cdc_led\ex_usb_common.h" Line 117(10,73): Unknown keyword in #FUSES   "VREGEN"
      1 Errors,  0 Warnings.

It doesn't like the VREGEN fuse. Did you get that error ? I'm curious
how you could compile the file.
Ttelmah



Joined: 11 Mar 2010
Posts: 19255

View user's profile Send private message

PostPosted: Fri Aug 08, 2014 1:46 am     Reply with quote

As 'info', PCM_programmer, just tried it with 5.009, 5.010, and 5.012, and all compiled happily, and accepted VREGEN for me.
I started from 'scratch', launched 5.010, just created a single file containing your posted code, and compiled this.
Annoyingly '011', is the only version at this time, I haven't got.
Maybe this is why I didn't use it!....
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

error
PostPosted: Fri Aug 08, 2014 6:42 am     Reply with quote

I compiled without getting an error.
I really don't know which version would you recommend me to use.
Thanks in advance!
temtronic



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

View user's profile Send private message

PostPosted: Fri Aug 08, 2014 7:03 am     Reply with quote

OK, though it's been 2-3 years since I last worked with the 4550, I'm pretty sure you can use a 4MHz xtal,then set the PLL prescaler to /1 which runs the PLL at 4MHz(it HAS to). From there you can divide the PLL output to run the CPU at 4 different speeds.
Perhaps someone that uses the PIC will comment.I have a lot of 4MHz xtals from the 16C84 days and am trying to use them up!

cheers
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19255

View user's profile Send private message

PostPosted: Fri Aug 08, 2014 8:51 am     Reply with quote

You are right.
You can run full speed USB with the 4550, from a crystal at:

4, 8, 12, 16, 20 or 24MHz.

Then CPU at

48, 32, 24, or 16MHz
from any of these.

or at crystal /1, /2, /3 or /4 for the CPU from all of these as well.

So from 20MHz, CPU at 20, 10, 6.66, or 5MHz as well. (HS operation, rather than HSPLL)

However generally CPU rates below 8MHz, can start to become 'problematic' for USB operation, so for the slower crystals, the 'direct' divisions are best avoided.

With an external oscillator (as opposed to the crystal), you can also work off 40Mhz, and 48Mhz.

4MHz, is fine for USB, but you may find you need a series resistor for reliable operation (the PLL is slightly 'more fussy' off the 4MHz crystal - it is vital that the PLL is fed off a symmetrical 50:50 waveform - the prescalers other than /1 ensure this).

Key is that the USB PLL still runs if XT, HS, EC, or ECIO are selected, but the CPU then clocks off the crystal, not the PLL. The PLL still runs for USB though, unless USBDIV is turned off.

I use 8MHz, and 12MHz a lot (slightly lower power consumption, than the faster crystals).

Have fun.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 08, 2014 3:08 pm     Reply with quote

Based on Ttelmah's results, I un-installed the compiler and did a clean
install of vs. 5.011. Then it worked. Everything worked. I can press
'1' and turn on the LED, and press '0' and turn it off.

Also, I made an LED blinking program, and it blinked at the correct rate.
It's not running at 1/4 speed. It blinks the LED at 1 Hz with vs. 5.011:
Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,BROWNOUT,PUT,PLL5,CPUDIV1
#use delay(clock=48M)

//=================================
void main()
{

while(1)
  {
   output_toggle(PIN_B0);
   delay_ms(500);
  }

}


----------------------------
Another topic:
I think CCS's multi-versioning system just doesn't work reliably. I had
5.026 installed (command line version) and installed 5.011, and went to
the PIC-C Versions application (provided by CCS), and double-clicked
5.011 to make it active. But then upon compiling, it said it didn't like the
VREGEN fuse.

I didn't realize it yesterday, but in fact it wouldn't take any fuse. It rejects
them all. Somehow the installation gets screwed up. I've seen this before.
It's CCS's multi-versioning system and it just isn't reliable. It used to be,
before vs. 5, I could do a fast, clean install every time and it was 100%
reliable.

One more thing - If you go to Control Panel / Add or Remove Programs
and un-install the compiler from there, you will get CCS's tickbox window
asking you what items you want to installed. (At least, with vs. 5.026).
I discovered that even if you don't select the tickbox to remove your
Projects folder, it will wipe it out anyway. Along with your .CRG
registration files and everything else. Fortunately I had backed up both
those items in My Documents. Otherwise I would have lost years worth of
project files. Be very cautious about using the CCS un-install program.
Back up everything first, and do it some place other than the CCS folder.

I just emailed CCS support about this. Under no circumstances should
the un-installer ever wipe out a person's own project files. If it has to
be done, the person should do it manually.
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

Circuit schematic
PostPosted: Sat Aug 09, 2014 4:53 pm     Reply with quote

Thanks for all the help you've given me.
Could you please share the circuit schematic you used to build the circuit to turn on the led with orders sent from the PC?
I have seen a lot of schematics, but none of them seem to work.
I also think I rather use a 20 MHZ crystal.
could you also share the code you used, the one in CCS and the other in java (If you used java to send the 1 and 0).
Thanks for your help!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 09, 2014 5:35 pm     Reply with quote

The code I used is from my previous post, here:
http://www.ccsinfo.com/forum/viewtopic.php?t=52722&start=8

The Teraterm setup instructions and link to the download for it is here:
http://www.ccsinfo.com/forum/viewtopic.php?t=39388&start=18
TeraTerm is used to send the '1' and '0' characters to the COM port.
Remember to look in Windows Device Manager to see what COM port
has been assigned to the USB board. You have to select that same
COM port (ie., COM4 or COM6 or whatever it is) in TeraTerm.

I'm using the older non-Rohs version of the PicDem2-Plus. The schematic
is in the back of this PDF:
http://ww1.microchip.com/downloads/en/DeviceDoc/PICDEM%202Plus%20Old%20Version%2051275b.pdf

On the Vusb pin of the 18F4550, I have a 4.7uF Tantalum capacitor.
I know Ttelmah says that's wrong, the value is too high, and I basically
agree, but I also know it works on my board and I didn't have a 470 nF
capacitor so I left it in there.

As I said in my post that I linked to above, the PicDem2-Plus has a 4.7K
pullup on pin C4 that had to be removed so it wouldn't interfere with the
USB pins.

I have a USB connector mounted on the board. I connected the ground
to the board's ground, and the D+ and D- pins to the 18F4550. I carefully
verified the pinout of the USB connector to make sure I had D+ and D-
going to the correct pins.

Here's a good schematic:
http://320volt.com/en/usb-uygulamalari-ccs-pic18f4550-pic18f2550/
The 1K on the MCLR is too low. Microchip wants it to be 10K for the ICD3
and CCS wants it to be 47K for their ICD-U64. I don't use the 1 Meg
resistor across the crystal. I've never found it necessary with the PICs
or crystals (ECS brand) that I use.

I also have the USB connection sense circuit installed. It consists of two
100K resistors. It was described in a ASCII Art schematic in an older
version of the CCS compiler example files. I can't find an example
right now. I'll post that later.
Ttelmah



Joined: 11 Mar 2010
Posts: 19255

View user's profile Send private message

PostPosted: Sun Aug 10, 2014 1:10 am     Reply with quote

As a comment PCM_programmer, I've kept on using the 'old' way of working with CCS. I found very early on that the automatic updating, and version switching, was a classic 'not working properly' bit of CCS code (why can't they concentrate on the compiler, not the frills?). The version switching, doesn't seem to always correctly change the include files used properly, and if the devices.dat is from a much older compiler version, it doesn't get handled correctly (I think this is what caused your problem).

I delete the automatic update utility (ccsload), when I install a new version, and just install each new version separately as I always used to do. This way it never tells me about 'new versions' (I can just check every few weeks, or if I see a comment here about something now working). It is far easier/safer than letting CCS stick their 'nose' into the update side of things!....

Though the reliability of the code has improved markedly in the last few years, I'm still a believer in not trusting any new release, and being 'sure' I know which release of everything, I'm actually using.

On the 4.7uF, the 'big' problem, is people fitting things like 4.7uF aluminium electrolytic capacitors thinking 'bigger is better' here, which then have poor HF performance. The better performance of tantalum means this is not a problem. The other problem is the same one as most regulators, that if the main supply rail drops faster than the USB rail, the regulator gets reverse biased. With something really large like 100uF, I'd get 'worried' by that, but at 4.7uF I'd be quite happy. Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 10, 2014 1:28 pm     Reply with quote

I don't have a ccsload file with the command line compiler.
In c:\Program Files\PICC, the closest thing I have is ccscon.exe.
Deleting that file did not get rid of the multi-versioning system.

I'm going to make a batch file that deletes the DLL directory, Devices,
Drivers, and all the files in c:\Program Files\PICC.

CCS also has ccsc.ini and pcw.ini in this directory (on XP):
c:\Documents and Settings\<user name>\Application Data\PICC
I'm going to delete those too.

Edit:
This is the batch file I made. It seems to work. I get a clean install
of the desired version, without any old versions left in the PICC directory.

This was only tested on Windows XP, and wouldn't work with other
versions of Windows because they don't have deltree. But a work-around
could be done with del and rd. This batch file is only for removal of
the command line versions of the compiler. Also, it doesn't do anything
to the registry settings. It leaves them unchanged. MPLAB has to be
closed when running this batch file, or you will get an "access denied"
on at least one .DLL file.
Code:

@echo off
cd "c:\Program Files\PICC"
deltree /y DLL
deltree /y Examples
deltree /y Devices
deltree /y Drivers
deltree /y ico
del UNWISE.EXE
del Default.mcp
del Suite_CCSPic.dll
del CCSCON.exe
del Readme.txt
del Ccsc.chm
del Ccsc.exe
del ccsuninstall.exe
del install.log
del Checkgdiplus.exe
cd "c:\Documents and Settings\<user name>\Application Data\PICC"
del pcw.ini
del ccsc.ini
cd "c:\Program Files\PICC"
dir
pause

It's not perfect. It doesn't check for the existence of files or directories
before attempting to delete them.

Edit:
Actually, XP doesn't come with deltree.exe. You have to download it from
the web. For example, this site has it:
https://www.raymond.cc/blog/download/did/1707/
Put it in the c:\Windows directory. Then the above batch file will work.


Last edited by PCM programmer on Tue Aug 19, 2014 1:52 pm; edited 1 time in total
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

CCS & MPLab
PostPosted: Tue Aug 12, 2014 10:36 am     Reply with quote

Do I have to install mplab in order to use this code you provided?
From what I read, you used the mplab plugin.
I tried to compile the code only using ccs, but it gave me an error.
I also need to make this work for windows 7, 8, 8.1,etc.
Please tell me how to do this.
Thanks in advance!
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, 3, 4  Next
Page 1 of 4

 
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