PIC Processor and DC Motor Control Reference

Nothing on this page is warranted for correctness, accuracy, suitability, or fitness for a purpose.

Glenn Neidermeier April '04

PWM Speed Control of DC Motor with Analog Circuit

Some light reading on Electronic Speed controllers. Interesting designs and theory.

Several electronic circuits for model aircraft can be found on Stefan's Electric R/C Web Site, including several options for building electronic speed controllers based on commonly-available comparators and op-amps. I made some notes on a couple of these circuit designs.

ESC with BEC based on 393 comparator IC

I implemented this circuit using IRF510 MOSFETs (often available from Radio Shack). These are non-logic level MOSFETs; the IRL2203 or comparable part was not immediately available. I found that the 339 quad-comparator (usually available from Radio Shack) seems to work OK in this circuit, if the 393 is not available. However, 339 is a 16-pin part, whereas only two comparators are required for this circuit.

ESC no BEC

Non-logic level MOSFETs are used in this Speed Controller with brake. The MOSFET gates are driven by a 1K/1M voltage divider between the rails. The circuit also illustrates how to use more than one MOSFET in parallel configuration. The comparator based ESC works on a fairly well-proven principle. That is, the first comparator is an integrator. It uses the 1-2ms servo demand pulse from the receiver servo signal to produce a nearly DC signal with a level that varies in accordance with the length of the servo demand signal. The varying DC level is than used as an input to one of the comparator circuits on the IC. The other comparator is used as a sawtooth generator with a frequency which will be the frequency of the final PWM output that drives the MOSFET gate(s). The PWM output frequency is usually around 2-3khz. By using the sawtooth and the DC level as inputs to a comparator, a PWM signal is produced.

Refer to Behr's R/C page for a good collection of information on electronic speed controls, notably the comparator based ESC designs by Ulrich Dallman and Milan Lulic may be instructive. The circuits described above appear to be closely related to Milan Lulic designs.

A listing of NTE logic-level N-CHANNEL Enhancement Mode MOSFETs can be found on NTE website.

The ESC implemented with comparator is a sensible solution if it is not practical to implement something with a PIC. However, getting a response from the ESC over the entire range of the transmitter pulse can be problematic. This design worked (somewhat) with Futaba Attack-4 transmitter (which is almost exactly 20ms frames with 1-2ms demand pulse) but did not work with JR Quattro (have not had a chance to profile the JR signal).

PIC 16F84 References, Programmer, Tools

A vast collection of PIC reference material is available at David Tait's PIC Archive. The site is no longer updated, but is still an invaluable source of information for getting started in PIC development.

You can find the original design for the so-called "TAIT" style programmer (pp) at the site. The pp archive includes reference schematics, source code for DOS, hardware designs for either PC Serial Port or PC Parallel port, as well as other useful information.

There are many variants of the pp programmer design and most of them seem to support the in-circuit serial programming (ICSP) function. ICSP is a feature found on the later Microchip controllers whereby the part can be programmed even after having been installed in the target-circuit. OTP parts can obviously only be programmed once. The programming arrangement uses a serial command and data protocol which typically requires the participation of only a few pins on the microcontroller. David Tait provides a good summary of ICSP in the document In-circuit 16F84 Programmers.

David Tate provides a simple in-circuit PIC programmer (spp01) with source code for DOS, based on the pic84v05. spp01 is basically all the same code from pic84v05 but uses the files SPP.C and SPP.H (instead of PPHW.C and PPHW.H) to implement the programming algorithm for the spp specific hardware. The spp01 archive contains variations of the programmer hardware for serial and parallel ports, and also suggested design of a target PIC circuit which supports ICSP.

Referring again to the pic84v05 software (pp), this was adapted for Linux by Chris Wilson. The project apparently does not have it's own website, but the linuxpp software is distributed from David Tait's PIC Archive with complete source code (as well as a patchfile against the original pic84v05).

I added the SPP.C and SPP.H files from the David Tate spp01 project in order to create a spp programmer for Linux (note I called the files spphw.c and spphw.h for consistency). I am also providing a board layout for the parallel port version of the spp programmer. The layout file was created in CadSoft Eagle Layout Editor and is designed to fit under the hood of a large DB25 connector.

In general, the leads from the parallel port to the programming connector on the PIC board should be kept as short as possible in order to ensure error-free operation. However, you may want to review a termination scheme which seems to help things work a bit better. Apparently serial-programming clock signal on RB7 benefits greatly from the application of an RC bypass circuit. I had good results taking RB7 to ground through 270ohm and .001uF.

Having constructed the programmer hardware, David Tait provides some very simple test circuits and software (look in the text document for pre-compiled hex files).

After verifying that the programmer with "canned" hex files, the gpasm assembler can be found in the gputils package, along with other useful development tools.

A simulator is also a very useful tool; the 'F84 has no support for on-chip debugging. However, gpsim can be used to show how the PIC memory and registers are affected by the running code.

You may want to refer to the Application Note 30277c from Microchip which covers in-circuit programming of midrange PIC controller. Search for 30277c on Microchip site. Refer to Section 3 "Programming Specification", and particularly "In-Circuit Serial Programming for 16F8x Flash MCU".

16F84 Electronic Speed Controller

An easy to build electronic speed controller circuit using the 16F84 can be found on Wings-n-Things website. It is based on a design for the 12c50x OTP device as used on some commercial ESC offerings. The original 12c50x circuit and software originated from Mike Norton's Hobbies website - that site is no longer available but all files are conveniently mirrored on Wings-n-Things website. The 16F84 is a larger part with more I/O pins, but being a flash part, has the advantage for development purposes of being reprogrammable. Refer to the original documentation for the 12c50x circuit for a thorough explanation of how it all works. The code is not terribly hard to follow, but does rely on precise instruction timing to accurately measure the incoming servo signal from the receiver, and "bit-banging" the output pin to produce the PWM signal to the motor - the software does not rely on any interrupt, timer or dedicated PWM feature such as found on the mid-range and high-end PIC controllers.

General Purpose PIC Board

I created a layout for a circuit board to carry the 18-pin PIC microcontroller, along with a crystal oscillator, 2940 5-volt regulator, and all I/O pins taken out to pin-headers. The board also has the 5-pin connector which matches the TAIT-SPP programmer so the PIC can be programmed in-circuit. The board has connections to 5V and "12V" (system voltage). The 7805 is shown in the circuit, it only needs 7.5V to operate. A 2940 is another good choice, is pin compatible, and allows even smaller Vin (6.25V).

Board layout for the PIC processor board.

Any peripheral module designed for attachment to the microprocessor board can be powered from the onboard regulated 5V, as long as all attachments do not draw more than ~1A. 2940 (or 7805) is supplied by the system-supply connection (marked 12V on the schematic).

The layout of the power-pins are not entirely satisfactory. If I build another PIC board, I will revise the layout. The board is sized about 1.25x2.0 inches (press-on transfer technique was used for the layout, it could be made much smaller using laser-toner process). The pin layout is a little odd, but groups the RA pins together, and the RB pins. The arrangement had to be such to accommodate a single-sided board layout which is easier to produce using homebrew techniques. RB6 and RB7 could be used in the application as long as the programmer circuit is not attached. The programmer circuit must have the capability to supply 12V to MCLR pin in order to support ICSP. The parpic programmer described above is made with a external connector to 12V supply which it in turn uses to set the programming reference voltage on MCLR. If your PIC project board is supplied with 12V, you could also design it to make the 12V available to your programmer hardware.

The MCLR pin is connected to VPP (5V) because it must be at logic-hi in order to put the PIC in run mode. However, the programmer circuit takes control of MCLR during programming, that is, it pulls MCLR low to reset the PIC during execution of the programming algorithm. Otherwise, the programmer circuit enables PIC programming mode by switching MCLR to ~12V. Therefore, programmer circuit connection to MCLR is isolated from VPP by using the 1N4148.