# PIC programming for computer to PIC

#### ErnieM

Joined Apr 24, 2011
7,991
I see the question as, and I quote, "a project in which you control your lights via a computer.", so I offered an alternative. Did someone die and leave you the post police?
(With apologies to the OP) When one answers a questions not asked it's basically a thread hijack. It should be everyone's job to help push a thread back on topic.

#### Brownout

Joined Jan 10, 2012
2,364
(With apologies to the OP) When one answers a questions not asked it's basically a thread hijack. It should be everyone's job to help push a thread back on topic.
The question was for help using a computer to control lights. I consider my responses on topic. I consider it my obligation to offer alternative solutions that accomplish the objective, especially if the solution is simpler to implement. Being the OP, by his own admission, had no experience with USB, he could benefit from the suggestion of alternative solutions. It's the OP's call and not yours.

I've love to continue playing your game, but you continue to take it further off-topic. In the future, I suggest if you think someone's answer is improper, you report it and let the mods sort it out. Pursuing other contributors like this only makes it harder to stay on topic.

Last edited:

#### takao21203

Joined Apr 28, 2012
3,682
in some sectors of the software business offtrack creativity and playing around with solutions is exactly what is asked for. More than never, you'd be the one who implements something new, without a book, maybe some stuff from years ago but that does not sell so much anymore.

For engineering, there could be a superior who has some idea and who in case of doubt will insist (even if it annoys customers).

As it looks OP wants to do a private project, and question was somehow answered. Maybe we can read some results soon.

#### spinnaker

Joined Oct 29, 2009
7,815
The question was for help using a computer to control lights. I consider my responses on topic. I consider it my obligation to offer alternative solutions that accomplish the objective, especially if the solution is simpler to implement. Being the OP, by his own admission, had no experience with USB, he could benefit from the suggestion of alternative solutions. It's the OP's call and not yours.

I've love to continue playing your game, but you continue to take it further off-topic. In the future, I suggest if you think someone's answer is improper, you report it and let the mods sort it out. Pursuing other contributors like this only makes it harder to stay on topic.
Don't worry you were 100% right in offering the alternative. The OP might not know such a solution existed and assumed USB was the only alternative. The only way I would change your suggestion would be to tell the OP to look at the MAX232. It really is not hard to interface to RS232 with that chip.

#### Brownout

Joined Jan 10, 2012
2,364
Don't worry you were 100% right in offering the alternative. The OP might not know such a solution existed and assumed USB was the only alternative. The only way I would change your suggestion would be to tell the OP to look at the MAX232. It really is not hard to interface to RS232 with that chip.
Thanks! I also used MAX232 with my PIC projects. Problem is, new computers don't have RS232 ports, and so I have to use the converter. If you use the mini-module, the outputs are 3.3v CMOS and you don't need the MAX chip, though I leave it on my board becuase I know I'll need it again in the future.

#### spinnaker

Joined Oct 29, 2009
7,815
Thanks! I also used MAX232 with my PIC projects. Problem is, new computers don't have RS232 ports, and so I have to use the converter. If you use the mini-module, the outputs are 3.3v CMOS and you don't need the MAX chip, though I leave it on my board becuase I know I'll need it again in the future.

So that is the module you mentioned before? An interface between the PCs USB port (making it look like a serial port) and the SDI to the Pic?

If so that is an excellent choice. I might look ar one of those. I did not know that existed. Much more simple than the Max232.

#### THE_RB

Joined Feb 11, 2008
5,430
(With apologies to the OP) When one answers a questions not asked it's basically a thread hijack. It should be everyone's job to help push a thread back on topic.
It's not a hijack.

When the OP is struggling to do something the hard way, it is good advise to suggest doing it the easy way.

#### takao21203

Joined Apr 28, 2012
3,682
Why do you insist USB is so difficult?

I got it going within a day, even adapting it for a not supported PIC.

Just browse the source as many times as necessary, and *compile* it.

If you have a chip that's supported it should compile straight away.

it does not matter if it is for a demo board with loads of stuff on it.

Simply comment out all activity except the enumeration (until the Windows program can make contact to the device).

OK one thing is you need Visual Studio, and you need to have some clue of Windows C++, but also the full source is supplied.

There are plenty of example designs supplied- as I said, just comment out (deactivate) functionality that needs components on the demo board and which is not present in your circuit.

You can enumerate a blank PIC, with two LEDs or so to show activity.

For 18F you also need the correct crystal.

That costed me hours, finally I tried a 12MHz canned oscillator.

RS232 is a nice effort to, have push button to cycle the baud rate + a small 7seg display (just a suggestion). And it is a bit detail work to set up the USART so you don't get a garbled preamble in the Windows control (for displaying). It is also quite an exercize to make it display a hex table etc.

OP did not say a word about the Windows software which should be good for controlling the lights and how this is done.

Anyone who can create such a software on Windows can make use of USB easily- and I doubt Windows programmers are just totally unaware of serial ports (maybe some irony here?)

#### Brownout

Joined Jan 10, 2012
2,364
RS232 is a nice effort to, have push button to cycle the baud rate + a small 7seg display (just a suggestion). And it is a bit detail work to set up the USART so you don't get a garbled preamble in the Windows control (for displaying). It is also quite an exercize to make it display a hex table etc.
Yeah, it's a whole lotta effort. Here is the code it takes to setup the UART:

Rich (BB code):
OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 34);
That's it. No more setup required.

Last edited:

#### takao21203

Joined Apr 28, 2012
3,682
OK I see, I remember doing it in PIC MPASM was more difficult, cycling baud rates from a table.

Sorry for the confusion caused.

#### ErnieM

Joined Apr 24, 2011
7,991
Oh, it's now a code contest? Well, here's the code I wrote to enable the USB module:
Rich (BB code):
Obviously a reduction in code from the "keep adding chips till it's simple enough" approach.

Yes, USB takes more code then this. No, you don't have to write it, copy i from an example program.

If the OP comes back and asks something specific I'll help him there. I doubt he will, if my first question resulted in half the answers being "do something different" I too may never return.

#### thisdabest

Joined Feb 16, 2014
4
Im having trouble with finding a solution and I believe its because I was vague on what I needed. The basics of the project is that from your phone you will send an email. Visual basics on your computer will be scanning the subject lines of the e-mails which will say whether a light needs to be turned on or off. Visual basics will then send these commands to the PIC via the USB of the computer to the USB input of the PIC. What I need is the code that shall be programmed into the PIC that shall allow it to use the USB input to control 4 outputs/lights.

#### Brownout

Joined Jan 10, 2012
2,364
Welll, there have been two suggestions to move your project forward: 1) Find libraries and applications the use the PIC USB and modify them for your PIC's USB or 2) Use a USB to serial converter and connect to your PIC's serial pins. Either will work. Chose one and run with it.

#### ErnieM

Joined Apr 24, 2011
7,991
What I need is the code that shall be programmed into the PIC that shall allow it to use the USB input to control 4 outputs/lights.
To give you a head start pop open one of the CDC serial device demo projects (no workspace, just a project: save it as your own workspace). Look thru the main.c for the ProcessIO() routine: that's where the program handles all the USB and serial stuff. Cut out what you don't need, and just read the incoming USB buffer, validate the data, and set the LEDs you wish.

#### THE_RB

Joined Feb 11, 2008
5,430
... send these commands to the PIC via the USB of the computer to the USB input of the PIC. What I need is the code that shall be programmed into the PIC that shall allow it to use the USB input to control 4 outputs/lights.
The easiest way to do this if you have no PIC USB experience is to use the cheap external USB->TTL serial module as Brownout originally suggested.

That way your PIC code only needs to receive a single byte into its simple USART, to control the 4 lights.

There's no shame in buying a \$3 module and making your code really simple and easy to get working.