View previous topic :: View next topic |
Author |
Message |
benni
Joined: 28 Jun 2012 Posts: 28
|
Switch LED on/off with 18F4550 |
Posted: Sun Dec 23, 2012 12:42 pm |
|
|
Hello,
I would really like to know how i can switch Leds on/off with my computer
like on this example just on the 18F4550:
http://www.sprut.de/electronic/pic/8bit/18f/programm/usb2550/usb2550.htm and his example and schematic
Is there already a tutorial in the Forum?
Would this code work:
Code: | #include <main.h>
// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>
#use rs232(baud=9600, UART1, errors)
/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging information over serial
// to display enumeration and connection states. Also lights LED2 and LED3
// based upon enumeration and connection status.
//
/////////////////////////////////////////////////////////////////////////////
void usb_debug_task(void)
{
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
new_connected=usb_attached();
new_enumerated=usb_enumerated();
if (new_connected)
output_high(PIN_B1);
else
output_low(PIN_B1);
if (new_enumerated)
output_high(PIN_B2);
else
output_low(PIN_B2);
if (new_connected && !last_connected)
printf("\r\n\nUSB connected, waiting for enumaration...");
if (!new_connected && last_connected)
printf("\r\n\nUSB disconnected, waiting for connection...");
if (new_enumerated && !last_enumerated)
printf("\r\n\nUSB enumerated by PC/HOST");
if (!new_enumerated && last_enumerated)
printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");
last_connected=new_connected;
last_enumerated=new_enumerated;
}
void main(void)
{
BYTE i;
output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);
output_high(PIN_B0);
usb_init_cs();
for(;;)
{
usb_task();
usb_debug_task();
if (usb_enumerated())
{
if (usb_cdc_kbhit())
{
i = usb_cdc_getc();
if (i == 0x80)
{
output_high(PIN_B2);
}
}
}
}
}
|
when i send with a C# Program this:
Code: | public bool TurnLedOn()
{
// Command 0x80 - Turn LED on
// Declare our output buffer
Byte[] outputBuffer = new Byte[65];
// Byte 0 must be set to 0
outputBuffer[0] = 0;
// Byte 1 must be set to our command
outputBuffer[1] = 0x80;
// Perform the write command
bool success;
success = writeRawReportToDevice(outputBuffer);
// We can't tell if the device received the data ok, we are
// only indicating that the write was error free.
return success;
}
|
Thanks for help
Regards Benni
Last edited by benni on Sun Dec 23, 2012 4:12 pm; edited 1 time in total |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sun Dec 23, 2012 2:54 pm |
|
|
Hi,
It's a trivial modification to the CDC USB example that CCS provides in the
\Examples folder. Have a look, and see what you come up with....
John |
|
|
benni
Joined: 28 Jun 2012 Posts: 28
|
|
Posted: Sun Dec 23, 2012 4:13 pm |
|
|
Hello,
I've Updated the topic, please recheck it.
Regards Benni |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Sun Dec 23, 2012 5:57 pm |
|
|
Is this meant to be a quiz? Are you asking us to guess what output you received from the PIC? _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
benni
Joined: 28 Jun 2012 Posts: 28
|
|
Posted: Mon Dec 24, 2012 3:08 am |
|
|
No, i just want to know if it should work. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Dec 24, 2012 6:14 am |
|
|
Benni,
The point is that posting random snippets of code like you did here, and with
your recent LCD thread, and then asking "will this work?" is a really poor way
to learn programming, and is not the way the forum is supposed
to work. Determining if code will work is YOUR job - you need to compile it,
load it onto your PIC, and actually test it and debug it yourself! We are here
to help you after you do all that, and still have a problem! If you aren't
willing to do it this way, you are never going to learn any worthwhile
programming and troubleshooting techniques, and the people here that might
help you will tire of your questions and start to ignore you!
Good Luck!
John |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Mon Dec 24, 2012 6:52 am |
|
|
comments...
as for the 4550 code..it 'should' work, though only once.you don't have a 'turn off LED 'command...
as for the c# code..I don't use it.Too darn top heavy for 100% of the PC code I cut.
overall..the best teacher is a breadboard,PIC and parts, I KNOW the CCS CDC 4550 works,as i did your 'project' 2-3 years ago.you have to start small(get their program running), then slowly expand to include your desired 'functions'.
to test PIC<->PC serial communications, use Hyperterminal,RealTerm or something similar unless you KNOW your C# code runs flawlessly!
be sure to keep incremental backups.start with a small program.make a change but save as a new version,test,repeat this proceedure until the project is completed.Don't worry about having several(30,40,50+) 'versions' on your PC.when done, simple delete all but the last 4 versions.never change orginal source code for drivers or CCS examples only modify COPIES !
hth
jay |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Dec 24, 2012 7:40 am |
|
|
Hi Jay,
He should be using something simple like Hyperterminal or Realterm to send
data to his PIC. Something like 'A' to turn the LED On and 'B' to turn the LED
Off. Once that is working, sure try something more advanced, but take it in
an incremental fashion.... The point is that he'll learn far more by "doing",
than he will by "asking"....
The ironic part of this is that the entire thing could have been done in the
time it took to create the thread, and ask the question!
John |
|
|
benni
Joined: 28 Jun 2012 Posts: 28
|
|
Posted: Tue Dec 25, 2012 9:40 am |
|
|
Hey,
I just wanted to be sure that it will work. Nothing else.
I just need to wire it right and to test it.
The other thing is how to make the driver or is there a general driver for it?
Regards Benni |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Tue Dec 25, 2012 2:03 pm |
|
|
Benni
You've got to get a breadboard out! No amount of typing on a PC will give you the hands on experience of real world testing (aka R&D).
CCS supplies the drivers for both PIC and PC in the examples folder and I know they work, well did, 2 years ago.Use their CDC example...get it up and running, then copy and make the easy modifications to the COPY, compiler and see what happens.
From what I recall the USB drivers for the PIC use up about 1/3 of the PIC4550 codespace. Something to consider once you get into serious programming....I now use $3 TTL-USB adapters for several reasons.
hth
jay |
|
|
benni
Joined: 28 Jun 2012 Posts: 28
|
|
Posted: Tue Dec 25, 2012 3:40 pm |
|
|
I've ordered the parts to do this. I think that i will get it work because i googled and found some interestings things. When it work i will post screenshots here.
Regards Benni |
|
|
benni
Joined: 28 Jun 2012 Posts: 28
|
|
Posted: Thu Dec 27, 2012 8:47 am |
|
|
Hey,
Will it also work without defining the USB_CON_SENSE_PIN ?
I just saw that usb_attached is just a definition for input of the USB_CON_SENSE_PIN pin.
Normally it should be attached when it gets current, because the current is from the PC. So why is there a definition for it?
Regards Benni |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Dec 28, 2012 3:08 am |
|
|
Remember USB devices can be powered from their own supplies, _or_ from the PC.
The USB spec, _requires_ that a device attached to a bus that is not supplying power, disables it's transceivers. Hence you use the connection sense pin, and the code can then do this. If you are running powered from the USB bus, then this is not needed, and you can disable this option.
Quite a few people (incorrectly) run without using this pin, with a device that has it's own power. Some 'get away' with this, while others have problems (depends on the exact details of the chipset in the PC, any hubs in the bus, etc. etc...
Best Wishes |
|
|
benni
Joined: 28 Jun 2012 Posts: 28
|
|
Posted: Sat Dec 29, 2012 4:04 pm |
|
|
Ok, I got it successfully working.
-Receiving and sending data bytes.
Picture:
Thanks everyone for help.
Regards Benni
Last edited by benni on Sat Dec 29, 2012 6:50 pm; edited 1 time in total |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Sat Dec 29, 2012 4:25 pm |
|
|
Hi Benni,
You received useful help and advice from the forum, right? Then why not post your code, and help the next person that has a similar question?
John |
|
|
|