PIC Motor Control and Serial Port Example

From Mech
Jump to navigationJump to search

Introduction

In this application, we will use a PIC 18F4431 microcontroller to implement a closed-loop lead-lag compensator that controls the velocity of a motor with an encoder. This example will cover the use of:

  • interrupts
  • reading encoders
  • pulse-width modulation
  • USART (Universal Synchronous Asynchronous Receiver/Transmitter)

Every 20 milli-seconds, the PIC will sample the encoder and run the feedback control loop. The output from the control loop is then sent to the motor to try to match the motor's speed with the target speed. To set the target speed, commands are sent from a PC's serial port to the USART on the PIC. The PIC will also print the target speed and actual speed of the motor to the terminal window of the PC.

Building the System

To build this system, you will need the following:

  • PIC18F4431 microcontroller
  • ICD2 programmer
  • ST-232 RS232 voltage shifter
  • 7414 Hex inverting Schmitt trigger
  • L293D H-bridge
  • Maxon DC motor with encoder
  • DE9F connector (also known as a DB9F connector)
  • PC 9-pin serial cable (one end should be male, the other end female)
  • PC with a serial port, and has MPLAB, C18, and HyperTerminal installed. MPLAB and C18 are available on Microchip's website; HyperTerminal usually comes with Windows.
  1. Wire up the various components as shown in the wiring diagram below. An example breadboard layout is provided.
  2. Plug one end of the serial cable into the PC's serial port, and the other end into the DE9F connector in the breadboard circuit.
  3. Download and open the project, and compile the code.
  4. Load the program onto the PIC18F4431.
  5. Start HyperTerminal and configure it as shown in the HyperTerminal section.
  6. In MPLAB, click the Release from reset button or disconnect the ICD2 from the circuit.

In HyperTerminal, you should see the following message:

C18 tutorial welcome message.png

Press 'a' to accelerate the motor, 'd' to accelerate the motor in the opposite direction, and 's' to stop the motor. The target speed is the speed (in encoder counts per period, 20ms in our case), and the actual speed is the number of encoder counts that the motor spun during the last sampling period.

Components Datasheets

Project Files

Download the MPLAB project here.

This was compiled using MPLAB 7.6 and C18 3.12 (student version). If MPLAB has problems finding files or does not compile, it may be because the directories on your computer are set up differently. In this case, create a new project following these instructions, and copy in the source files.

Wiring Diagram

Wiring Diagram for the motor velocity controller (Image)

Wiring Diagram for the motor velocity controller (PowerPoint)

DE9 Female Connector Wiring (image)

Breadboard Layout

Example Breadboard Layout (Image)

HyperTerminal

Open HyperTerminal by going to Start>All Programs>Accessories>Communications>HyperTerminal. When it prompts you for a name for the connector, you can give it any arbitrary name.