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
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

Turn Led on - USB PIC
PostPosted: Tue Aug 05, 2014 2:59 pm     Reply with quote

Can someone please tell me how to fix my code?
Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#USE DELAY(CLOCK=20000000)

#include <usb_cdc.h>

const int8 lenbuf = 32;
int8 recbuf[lenbuf];
char version[] = "v.1.0";

void main(){
   delay_ms(500);
    usb_init();
    usb_task();
    usb_wait_for_enumeration();   
    enable_interrupts(global);
    while(TRUE){
      if(usb_enumerated()) {
          output_low(PIN_B0);
          output_low(PIN_B1);
           
         if (usb_kbhit(1)) {
            usb_get_packet(1,recbuf,lenbuf);
             
            if(recbuf[0] == 127){
                output_high(PIN_B0);
            }else{
                output_high(PIN_B1);
            }
            delay_ms(500);           
         }
      }
   }
}

What I'm trying to do is turn on a led throgh a command sent by java to the usb port. When the button is pressed, the value of 127, should turn on the led.
Thanks in advance!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

hints
PostPosted: Tue Aug 05, 2014 6:38 pm     Reply with quote

what is the INITIAL state of pin_B0 and B1
before the (repeated needlessly?) usb_enumerated() test?

as written you can't even tell if enumeration was successful
let alone if your magic character received.......

also a #define lenbuf 32
ought to be fine - no need for a const var
as all the litfing can be done at compile time

you are a long way from a happy meal here.
Sad Sad
Ttelmah



Joined: 11 Mar 2010
Posts: 19255

View user's profile Send private message

PostPosted: Tue Aug 05, 2014 11:03 pm     Reply with quote

Other thing is clock settings are wrong.

HSPLL says feed the CPU from the USB PLL.

20MHz crystal (assumed). PLL5, says feed the USB PLL, from the crystal/5. 4MHz. At this point the USB PLL is generating 96MHz.
CPUDIV1, then says run the CPU off the USB PLL/2.

So the CPU is running at 48MHz, not 20MHz.

To run the CPU off 20MHz, needs:
Code:

#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#USE DELAY(CLOCK=20000000)


HS, not HSPLL. The PLL still runs, but only feeds the USB.
temtronic



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

View user's profile Send private message

PostPosted: Wed Aug 06, 2014 5:58 am     Reply with quote

I suggest using the working example that CCS supplies in the 'examples' folder as a basis of your program.
I know they work......

jay
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

new code
PostPosted: Wed Aug 06, 2014 8:33 pm     Reply with quote

Code:

#include <18F4550.h>
//#include <pic_18usb.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#USE DELAY(CLOCK=20000000)

#include <usb_cdc.h>

const int8 lenbuf = 32;
int8 recbuf[lenbuf];
char version[] = "v.1.0";

void main(void) {
  delay_ms(500);
  usb_init();
  usb_task();
  usb_wait_for_enumeration();
  enable_interrupts(global);
  while (TRUE){
    if(usb_enumerated()){
      if (usb_kbhit(1)){
        usb_get_packet(1, recbuf, Lenbuf);
       
        if(recbuf[0]==127){
           output_high(PIN_B0);
        }
        if(recbuf[0]==126){
          output_low(PIN_B0);
        }
   
      }
    }
  }
}

Can someone please tell me what do I have to fix in this new code, so I can make the led blink?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 06, 2014 8:39 pm     Reply with quote

Well, describe your Java application.

Describe your hardware. Did you buy the board or build it yourself ?
Post a link to the schematic or to the website for the board.

Do you have the USB connection sense circuit installed ?
When you plug in the USB cable between your board and the PC,
do you hear Windows make the "USB connection" sound ?

Or are you testing this project in Proteus ?

And what is your CCS compiler version ?
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

description
PostPosted: Thu Aug 07, 2014 7:16 am     Reply with quote

My java application uses the jpicusb, to send the byte with a value of 127 to turn on the led, and a value of 126 to turn off the led.
I purchased the board, and I just programmed the board.
This is the schematic of the board:
https://www.mediafire.com/?y0l042t2l2rka13
I can hear the sound of a new device connected to the PC, when I connect the board.
I'm not testing the project in proteus, because the usb port doesn't work in 64 bit systems.
The CCS version is 5.011.
I used the same java code as the one used in this video:
https://www.youtube.com/watch?v=icFvEidPOmk
temtronic



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

View user's profile Send private message

PostPosted: Thu Aug 07, 2014 9:36 am     Reply with quote

OK silly questions
1) Have you gotten the LEDs to flash with the classic 1Hz LED program ?
2) Do you have the required cap on the USB pin of the 4550?
3) Does the CCS supplied USB program function correctly?
4) are you running the 4550 on 5V or 3V


???
jay
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!
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