Difference between revisions of "PIC Motor Control and Serial Port Example"

From Mech
Jump to navigationJump to search
 
(36 intermediate revisions by the same user not shown)
Line 2: Line 2:


==Introduction==
==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:
In this application, we will use a PIC18F4431 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
*interrupts
Line 10: Line 10:


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.
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.

#Wire up the various components as shown in the [[/PIC_Motor_Control_and_Serial_Port_Example#Wiring_Diagram|wiring diagram]] below. An example breadboard layout is provided.
#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.
#[[PIC_Motor_Control_and_Serial_Port_Example#Project_Files|Download]] and open the project, and compile the code.
#Load the program onto the PIC18F4431.
#Start HyperTerminal and configure it as shown in the [[PIC_Motor_Control_and_Serial_Port_Example#HyperTerminal|HyperTerminal]] section.
#In MPLAB, click the '''Release from reset''' button or disconnect the ICD2 from the circuit.

In HyperTerminal, you should see the following message:

[[Image: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==
[[PIC_Microcontrollers_with_C18_Compiler#PIC18F4431|PIC18F4431]]

[[media:7414_schmitt_trigger_datasheet.pdf|74HC14 Hex inverting Schmitt trigger]]

[[media:L293D.pdf|L293D four half H-bridges]]

[[media:ST232_datasheet.pdf|ST232 RS-232 Voltage Shifter]]


==Project Files==
==Project Files==
Line 16: Line 50:
This was compiled using MPLAB 7.6 and C18 3.12 (student version).
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 [[Writing_Code_with_the_C18_Compiler#Setting_Up_the_Project|these instructions]], and copy in the source files.
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 [[Writing_Code_with_the_C18_Compiler#Setting_Up_the_Project|these instructions]], and copy in the source files.

The source code is split into five files:
*<tt>main.c</tt> contains the code entry point and main control loop.
*<tt>main.h</tt> contains miscellaneous functions and macros used by other functions.
*<tt>Lead_Lag(tutorial).h </tt> contains the code for the motor control algorithm.
*<tt>motor_pwm.h</tt> contains functions for setting up and using PWM.
*<tt>QEI.h</tt> contains functions for setting up and using the Quadrature Encoder Interface.


==Wiring Diagram==
==Wiring Diagram==
'''Warning: Be sure to wire the motor encoder correctly, or the encoder could get fried.'''
[[Image:C18_tutorial_Motor_controller_wiring.png|thumb|Wiring Diagram for the motor velocity controller example]]


[[media:C18_tutorial_Motor_controller_wiring.ppt|Wiring Diagram for the motor velocity controller example(PowerPoint)]]
[[Image:C18_tutorial_Motor_controller_wiring.gif|thumb|center|Wiring Diagram for the motor velocity controller]]


[[media:C18_tutorial_Motor_controller_wiring.ppt|Wiring Diagram for the motor velocity controller (PowerPoint)]]
[[Image:C18_tutorial_Motor_controller_DE9F.jpeg|frame|thumb|left|200px|DE9 Female Connector Wiring]]

[[Image:C18_tutorial_Motor_controller_DE9F.jpeg|thumb|center|DE9 Female Connector Wiring]]


==Breadboard Layout==
==Breadboard Layout==
[[Image:C18_tutorial_motor_controller_breadboard.jpeg|thumb]]
[[Image:C18_tutorial_motor_controller_breadboard.jpeg|thumb|Example Breadboard Layout|center]]


==HyperTerminal==
==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.
Open HyperTerminal by going to '''Start>All Programs>Accessories>Communications>HyperTerminal'''.

[[Image:C18_tutorial_hyperterminal_open.png|center|thumb|400px|Open HyperTerminal]]

When it prompts you for a name for the connector, you can give it any arbitrary name.

[[Image:C18_tutorial_hyperterminal_choose_name.png|center]]

In the '''Connect To''' menu, chose the COM port that you plugged your serial cable into. If your computer has more than one COM port, you will have to find out which one it is plugged into or try them all.

[[Image:C18_tutorial_hyperterminal_choose_port.png|center]]

In the '''Properties/Port Settings''' menu, use the following settings:
*Bits per second: 115200
*Data bits: 8
*Parity: None
*Stop bits: 1
*Flow control: None


[[Image:C18_tutorial_hyperterminal_choose_baud.png|center]]

Now, you should have a blank window with a blinking cursor. Go to '''File>Properties''' and click the '''Setting''' tab. Under '''Emulation''', choose '''ANSI''', and click '''OK'''.

[[Image:C18_tutorial_hyperterminal_choose_emulation.png|center]]

The terminal should now be ready to send and receive data. You can connect or disconnect by going to the '''Call''' menu and selecting '''Call''' or '''Disconnect'''.

Latest revision as of 21:45, 8 September 2007

Introduction

In this application, we will use a PIC18F4431 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

PIC18F4431

74HC14 Hex inverting Schmitt trigger

L293D four half H-bridges

ST232 RS-232 Voltage Shifter

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.

The source code is split into five files:

  • main.c contains the code entry point and main control loop.
  • main.h contains miscellaneous functions and macros used by other functions.
  • Lead_Lag(tutorial).h contains the code for the motor control algorithm.
  • motor_pwm.h contains functions for setting up and using PWM.
  • QEI.h contains functions for setting up and using the Quadrature Encoder Interface.

Wiring Diagram

Warning: Be sure to wire the motor encoder correctly, or the encoder could get fried.

Wiring Diagram for the motor velocity controller

Wiring Diagram for the motor velocity controller (PowerPoint)

DE9 Female Connector Wiring

Breadboard Layout

Example Breadboard Layout

HyperTerminal

Open HyperTerminal by going to Start>All Programs>Accessories>Communications>HyperTerminal.

Open HyperTerminal

When it prompts you for a name for the connector, you can give it any arbitrary name.

C18 tutorial hyperterminal choose name.png

In the Connect To menu, chose the COM port that you plugged your serial cable into. If your computer has more than one COM port, you will have to find out which one it is plugged into or try them all.

C18 tutorial hyperterminal choose port.png

In the Properties/Port Settings menu, use the following settings:

  • Bits per second: 115200
  • Data bits: 8
  • Parity: None
  • Stop bits: 1
  • Flow control: None


C18 tutorial hyperterminal choose baud.png

Now, you should have a blank window with a blinking cursor. Go to File>Properties and click the Setting tab. Under Emulation, choose ANSI, and click OK.

C18 tutorial hyperterminal choose emulation.png

The terminal should now be ready to send and receive data. You can connect or disconnect by going to the Call menu and selecting Call or Disconnect.