mechatronics project kit - getting started manual …mechatronics project kit getting started manual...
TRANSCRIPT
Mec
hatr
onic
s P
roje
ct K
it - G
ettin
g S
tart
ed M
anua
l
40-1
00-1
Mechatronics Project Kit
Getting Started Manual
40-100-1
FeedbackFeedback Instruments Ltd, Park Road, Crowborough, E. Sussex, TN6 2QR, UK.
Telephone: +44 (0) 1892 653322, Fax: +44 (0) 1892 663719.email: [email protected] website: http://www.fbk.com
Manual: 40-100-1 Ed03 072001 Printed in England by Fl Ltd, CrowboroughFeedback Part No. 1160–401001
Notes
MECHATRONICS PROJECT KIT Preface
40-100-1 i
THE HEALTH AND SAFETY AT WORK ACT 1974
We are required under the Health and Safety at Work Act 1974, to make available to users of this equipment certain informationregarding its safe use.
The equipment, when used in normal or prescribed applications within the parameters set for its mechanical and electrical performance,should not cause any danger or hazard to health or safety if normal engineering practices are observed and they are used inaccordance with the instructions supplied.
If, in specific cases, circumstances exist in which a potential hazard may be brought about by careless or improper use, these will bepointed out and the necessary precautions emphasised.
While we provide the fullest possible user information relating to the proper use of this equipment, if there is any doubt whatsoeverabout any aspect, the user should contact the Product Safety Officer at Feedback Instruments Limited, Crowborough.
This equipment should not be used by inexperienced users unless they are under supervision.
We are required by European Directives to indicate on our equipment panels certain areas and warnings that require attention by theuser. These have been indicated in the specified way by yellow labels with black printing, the meaning of any labels that may be fixed tothe instrument are shown below:
CAUTION -RISK OFDANGER
CAUTION -RISK OF
ELECTRIC SHOCK
CAUTION -ELECTROSTATIC
SENSITIVE DEVICE
Refer to accompanying documents
PRODUCT IMPROVEMENTS
We maintain a policy of continuous product improvement by incorporating the latest developments and components into our equipment,even up to the time of dispatch.
All major changes are incorporated into up-dated editions of our manuals and this manual was believed to be correct at the time ofprinting. However, some product changes which do not affect the instructional capability of the equipment, may not be included until it isnecessary to incorporate other significant changes.
COMPONENT REPLACEMENT
Where components are of a ‘Safety Critical’ nature, i.e. all components involved with the supply or carrying of voltages at supplypotential or higher, these must be replaced with components of equal international safety approval in order to maintain full equipmentsafety.
In order to maintain compliance with international directives, all replacement components should be identical to those originallysupplied.
Any component may be ordered direct from Feedback or its agents by quoting the following information:
1. Equipment type
3. Component reference
2. Component value
4. Equipment serial number
Components can often be replaced by alternatives available locally, however we cannot therefore guarantee continued performanceeither to published specification or compliance with international standards.
MECHATRONICS PROJECT KIT Preface
ii 40-100-1
DECLARATION CONCERNING ELECTROMAGNETIC COMPATIBILITY
Should this equipment be used outside the classroom, laboratory study area or similar such place for which it is designed and sold thenFeedback Instruments Ltd hereby states that conformity with the protection requirements of the European Community ElectromagneticCompatibility Directive (89/336/EEC) may be invalidated and could lead to prosecution.
This equipment, when operated in accordance with the supplied documentation, does not cause electromagnetic disturbance outside itsimmediate electromagnetic environment.
COPYRIGHT NOTICE
© Feedback Instruments Limited
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by anymeans, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of Feedback Instruments Limited.
ACKNOWLEDGEMENTS
Feedback Instruments Ltd acknowledge all trademarks.
IBM, IBM - PC are registered trademarks of International Business Machines.
MICROSOFT, WINDOWS 95, WINDOWS 3.1 are registered trademarks of Microsoft Corporation.
MPLAB and PIC are registered trademarks of Microchip Technologies Inc.
Meccano is a registered trademark of Meccano SA.
MECHATRONICS PROJECT KIT Contents
40-100-1 TOC 1
TABLE OF CONTENTS
1 Introduction 1-1
1.1 Modules 1-2
1.2 Equipment Required to Complete Project 1-4
1.3 Battery Charging 1-5
2 Microchip Resources 2-1
3 Mechanical Components 3-1
3.1 Chassis 3-1
3.2 Steering 3-1
3.2.1 Ackermann Steering 3-1
3.2.2 Controlled Single Wheel 3-2
3.2.3 Castor Wheel 3-2
3.3 Drive Motor 3-3
3.3.1 Single Motor 3-3
3.3.2 Dual motor 3-4
4 Description of the Electronic Circuitry 4-1
4.1 Motor Drive 4-1
4.2 Optical Wheel Rotation Sensors 4-2
4.3 Magnetic Speed Sensors 4-3
4.4 Temperature Sensor 4-4
4.5 Back EMF Sensing 4-5
4.6 Stepper Motor Drive Board 4-6
MECHATRONICS PROJECT KIT Contents
TOC 2 40-100-1
4.7 Optical Sensors 4-6
5 Programming Guidelines 5-1
5.1 Registers 5-2
5.2 Timers 5-2
5.3 Interrupts and the ISR 5-2
5.4 Stepper Motor Configuration 5-3
5.5 PIC Microprocessor Pin-out 5-4
5.6 Downloading and Running a Program 5-5
6 Sample Programs for each Module 6-1
6.1 Pulse Width Modulation (PWM) 6-2
6.1.1 Wiring Required 6-2
6.1.2 Program 6-2
6.2 Stepper Control 6-5
6.2.1 Wiring Required 6-5
6.2.2 Program 6-5
6.3 Line Sensors 6-11
6.3.1 Wiring Required 6-11
6.3.2 Program 6-11
6.4 Optical/Magnetic Speed Sensing 6-17
6.4.1 Wiring Required 6-17
6.4.2 Program 6-17
6.5 Temperature Sensing 6-21
6.5.1 Wiring Requirements 6-21
6.5.2 Program 6-21
6.6 Back EMF Sensing 6-24
6.6.1 Wiring Requirements 6-24
6.6.2 Program 6-24
MECHATRONICS PROJECT KIT Contents
40-100-1 TOC 3
7 Solutions 7-1
7.1 3D Models/Photos 7-1
7.2 Assembly 7-2
7.3 Trouble-Shooting 7-3
7.4 The Track 7-3
7.5 Program 7-4
MECHATRONICS PROJECT KIT Contents
TOC 4 40-100-1
Notes
Chapter 1MECHATRONICS PROJECT KIT Introduction
40-100-1 1-1
1 Introduction
Mechatronics allows the integration of mechanics, electronics and computer technologiesto enhance the performance of products, systems and processes. Typical products thatuse the principles of mechatronics are camcorders, computer disk drives, industrial robotsand automobiles.
The Mechatronics Project Kit, shown in Figure 1-1, provides the means for students todesign and build a self-guided vehicle (buggy) from a set of modules, including two drivemodules, two steering and one jockey wheel solutions and a microcontroller (PIC). Whencompleted, the autonomous buggy is able to follow a track using infra red sensors. Thereare a number of different constructions possible with the components supplied in the kitbut, if required, the hole pitch and all spacing is Meccano compatible so the kit can beexpanded with any Meccano kit.
Figure 1-1: Mechatronics Kit
CHAPTER 1MECHATRONICS PROJECT KIT Introduction
1-2 40-100-1
This project has been designed to combine all aspects of engineering, includingmechanical, electrical, electronic, communications and software programming, into onedevelopment product. The buggy will be able to follow a track of insulation tape that hasbeen laid on the floor; the type of tape that is required depends on the type of floorcovering. The floor needs to be of a single colour, which is either infrared reflective ornon-reflective; with a reflective floor, a non-reflective tape is required and vice-versa for anon-reflective floor.
This is a fundamental guide to getting started on the mechatronics buggy project anddoes not cover every aspect of the design that is needed to complete the project as thereis many different outcomes possible.
1.1 Modules
The kit comprises of a number of different modules that can be used to construct awheeled vehicle that will be capable of following a predetermined circuit on the laboratoryfloor. These parts are supplied unassembled in order for the students to define theirparticular requirements and assemble the necessary components. The modules are:
1.1.1 Microprocessor Control Board
All inputs and outputs are available via screw terminal blocks and the board includes:
• Powerful Microchip PIC16F877 controller running at 10 MHz, with 368´8 bytesof data memory, 256´8 bytes of EEPROM data memory and 8K´14 bytes ofFLASH program memory. Programming of the PIC is achieved through theMicrochips own programming environment called MPLAB (see MicrochipResources).
• High performance RISC CPU with a 35 single-word instruction set and aninterrupt capability of up to 14 sources.
• On-board low dropout voltage regulation allows the unit to be powered from anunregulated 5.5V to 18V dc, via a 2.1 mm power inlet.
• Regulated +5V dc can be sourced from several screw terminal connectors.
• RS-232C serial port for downloading program to the on-board PIC, via a 9-wayD-type connector, which can also be used as a stand-alone serialcommunications port.
• Synchronous serial port (SSP) with I2C (master/slave).
• Up to 8 analogue input channels with a 10-bit analogue-to-digital converter.
• All inputs and outputs are available via plug-able screw terminal blocks.
• Digital I/O ports are also available via 26-way and 40-way IDC.
Chapter 1MECHATRONICS PROJECT KIT Introduction
40-100-1 1-3
1.1.2 DC Motor Drive Circuitry
This circuitry is capable of driving an interchangeable single or dual motor arrangementand has the following sensors:
• Wheel speed via optical or magnetic sensors.
• Temperature.
• Motor speed through back emf.
1.1.3 Stepper Motor Drive Board
This board controls either the:
• Ackermann steering module.
• Single wheel steering module.
The board also has circuitry for an optical sensor to detect when the stepper motor ispointing the steering in a straight line.
1.1.4 Trolley Wheel
The trolley wheel uses the same mechanism as the single wheel but with no motor.
1.1.5 Optical Sensor Boards
There are six individual infrared reflective sensor boards that the students need toexperiment with to find the optimum sensing configuration for following a 19 mm tape.
1.1.6 Three Types of Chassis
There are three types of chassis available as follows:
• Short rectangle
• Long rectangle
• Long rectangle chassis tapered at one end.
1.1.7 Ni-Cd 7.2V 1800mAh battery.
1.1.8 A selection of brackets
CHAPTER 1MECHATRONICS PROJECT KIT Introduction
1-4 40-100-1
1.2 Equipment Required to Complete Project
• Small flat blade screwdriver
• Small Philips screwdriver
• 4 mm, 5.5 mm and 7 mm spanner or nutdriver
• Multiple coloured wire
• Wire cutter
• Wire stripper
The Figure 1-2 shows the main configurations that are possible with the kit. The finaloption can be either front or rear wheel drive.
Figure 1-2: Mechatronics Kit Main Choices
Choice of twosteering systems
Choice of twodrive systems
Choice of three baseplates with the PICmicro controller on µµµµ Processor
Single DC Motorwith Differential
Dual DC MotorDirect Steering
AckermannSteering
Stepper motorcontrolled pivot
Wheel
JockeyWheel
Base1 Base2 Base3
Stepper motorcontrolled pivot
Wheel
AckermannSteering
Choice of threesteering systems
Sensor feedbackfrom each module
Chapter 1MECHATRONICS PROJECT KIT Introduction
40-100-1 1-5
1.3 Battery Charging
The battery supplied in the kit is a rechargeable nickel-cadmium battery which can becharged up using the 12 V dc power supply and charge cable provided as shown inFigure 1-3.
The method of charging used in this application is that of a constant current sourcedelivering 1 A until the battery is nearly charged then the charging current starts toreduce.
The charge-time that the battery requires depends on the amount of discharge duringuse; when discharged to a level that will not power the motors, the battery takes between1.5 and 2 hours to charge.
Note:
The charge-time should not be exceeded, as permanentdamage to the battery will occur.
Figure 1-3: Battery Charging Connections
12 V dc powersupply connected
to the mains supplyBatteryIn-line
currentlimiter
CHAPTER 1MECHATRONICS PROJECT KIT Introduction
1-6 40-100-1
Notes
Chapter 2MECHATRONICS PROJECT KIT Microchip Resources
40-100-1 2-1
2 Microchip Resources
The PIC assembler used is Microchip’s own MPLAB with the latest versions freelydownloadable from their website (www.microchip.com). Also at this site are the latestrevisions of the MPlab User Guide, PIC REFERENCE MANUAL and the PIC 16F87XDatabook, all free to download in pdf format. Another area of the site is the KnowledgeBase/Frequently Asked Questions page that can provide helpful information on variousproblems encountered. Microchip also offer a wide range of application notes for manydifferent tasks that the PIC can be programmed to do.
Chapter 2MECHATRONICS PROJECT KIT Introduction
2-2 40-100-1
Notes
Chapter 3Construction of the
MECHATRONICS PROJECT KIT Mardave Components
40-100-1 3-1
3 Mechanical Components
3.1 Chassis
There are three different chassis to choose from and each gives the system a differentcharacteristic; the alternatives are shown in Figure 3-1.
Figure 3-1: Chassis
3.2 Steering
There are three standard options for the steering, which are described below. However, ifyou can design any other method of steering from the parts provided which give a betterperformance, this will show good initiative and design skills.
3.2.1 Ackermann Steering
This is a system that uses two wheels to steer with, linked together by two arms that areconnected to a plate attached to a stepper shaft as shown in Figure 3-2. The wayAckermann steering works is that the inside wheel has a greater turning angle for a tighterradius of the corner, thus allowing the wheels to have a differential cornering speed. Thisarrangement allows for a lot of grip whilst cornering but the turning circle is the smallest.
To assemble the Ackermann steering mechanism to the stepper motor, proceed asfollows:
1. Bolt the base plate to the Z bracket.
2. Using an M2.5 screw, attach the brass boss to the steering link and then attach thetwo track rods to the steering link plate.
3. Bolt the stepper motor to the Z bracket and slide on the brass boss, then tighten thegrub screw so that the steering linkage plate is vertical
4. If the wheels are not correctly aligned, remove track rod and either tighten or loosen to
Chapter 3Construction of the
MECHATRONICS PROJECT KIT Mardave Components
3-2 40-100-1
suit your requirements. For correct operation of the Ackermann steering, the shaft ofthe stepper motor needs to be facing backwards so that the inner wheel has a greaterturning angle.
Figure 3-2:: Ackermann Steering Assembly
3.2.2 Controlled Single Wheel
As shown in Figure 3-3, this set up uses a single wheel that is mounted on a bracket,which can be fastened to the shaft of the stepper motor via a grub screw. Thisarrangement has a full 360° controllable rotation, which gives a fast accurate responsewith a smaller turning circle, but at the cost of some speed.
Figure 3-3: Single Wheel Kit
3.2.3 Castor Wheel
This utilises the same wheel and bracket arrangement as the controlled single wheel, but
Triangular arms
King pin
Steering arm
Ball screw
Base plate
Brass boss
Steering link
Track rod
Plastic ballsocket
Z plateStub axle
Chapter 3Construction of the
MECHATRONICS PROJECT KIT Mardave Components
40-100-1 3-3
replaces the stepper motor with a caster block that allows the shaft to freely rotate. Thisarrangement is used when there is a different method of steering, e.g. twin motor drivingthe two driving wheels independently.
3.3 Drive Motor
There are two options for driving the buggy, both using the same control board, which hasthe capability of driving two dc motors independently with Pulse Width Modulation (PWM)signals. Both options attach motors onto the underside of the control board.
The tyres are fitted to the wheels by first turning the them inside out then rolling the tyreover the rest of the wheel.
3.3.1 Single Motor
This system uses a single motor and a mechanical slip differential as shown in Figure 3-4.The method of constructing this arrangement is described below:
1. Join the two motor mount halves together using a 16 mm self-tap screw. Hold the twohalves down on a flat surface when tightening to ensure correct alignment.
2. Press fit the two plastic rear axle bushes into the motor mounts.
3. Slide the rear axle with differential through the bushes so that the motor will be at therear, and slide the drive spacer onto the other end.
4. Attach the motor to the mounting brackets with two M3x12 screws and lightly tighten,attach motor gear to shaft and tighten grub screw. Then adjust the motor position foralignment of the two gears, so that the wheel spins freely.
Figure 3-4: Single Motor Arrangements
5. Using the M3x25 screws, screw the drive block on to the differential block.
6. Insert the black plastic hex wheel drive onto the two locating pins on the drive blockand the wheel onto the hex drive; then loosely tighten nut. Repeat similarly on other
Optical sensing tapeon differential block
Optical sensing tapeon drive block
Differentialaxle gear
Motor mounts
Differentialspacer
Axle
Chapter 3Construction of the
MECHATRONICS PROJECT KIT Mardave Components
3-4 40-100-1
side.
7. The motor mounts are then screwed to the motor drive board using flanged head self-tap screws and the magnetic sensor is screwed up so that there is a 1-2 mm gapbetween the gear and the sensor.
3.3.2 Dual motor
This system uses two motors running from two sets of control signals for independentdrive and direction control. The method of constructing this arrangement is describedbelow:
1. Attach the motors to the mounting brackets with two screws and lightly tighten, attachmotor gear to shaft and tighten grub screw.
2. Fit the axle bushes to the motor bracket and slide the axle through.
3. Fit two washers to each side then slide the collar on.
4. Using the M3x20 and a nut, join together the drive block and the Axle gear then slideon the axle. The wheels are fitted in the same way as for the differential drive.
5. Attach the brackets to the board (the motor position will need adjusting so that thegears mesh together evenly) and align the magnetic sensor with the centre of thegear.
Figure 3.5: Dual Motor Arrangements
Axle gear
Motor gear
Optical sensing tape on drive block
Motor mount
Axle
Axle gear Optical sensing tape on drive block
Chapter 4MECHATRONICS PROJECT KIT Description of the Electronic Circuitry
40-100-1 4-1
4 Description of the Electronic Circuitry
The pots on the motor drive board have been factory pre-set to specific values andaltering these will change the feedback characteristics and result in inaccuratemeasurements.
The pots on the optical sensor boards are for adjustment of the sensitivity of the opticalswitch for different heights.
4.1 Motor Drive
Three signals are required from the microcontroller to drive the motor circuit, PWM,direction and bi/uni. These are injected into a GAL with the following circuit programmed:
Figure 4-1: GAL Logic
Figure 4-1 controls the firing sequence of the MOSFET H-bridge for the dc motor, whichcan be seen in Figure 4-2.
Bi/Uni
Fwd/Rv
PWM
C
A
B
D
Chapter 4MECHATRONICS PROJECT KIT Description of the Electronic Circuitry
4-2 40-100-1
Figure 4-2: Motor Drive Circuit
4.2 Optical Wheel Rotation Sensors
As shown in Figure 4-3, this is a simple circuit that switches the transistor on when theoptical sensor output passes the transistor threshold. Adjusting the variable resistormoves this threshold so that the transistor switches and therefore tweaks the sensitivity.
Figure 4-3: Optical Wheel Rotation Sensor Circuit
AK
CE
IC11OPTOREFLECTOR
C22100n
RV5100K
R50220R R51
2K2
R521K0
0V
+5V
[ ISTS708 ]
TR9ZTX108C
D10
BAX13
B
TR2IRLZ24N
TR12SJ174
M1
MOTOR
R21
4K7
D3UF4002
R204K7
D2UF4002
+ C9470u
TR4IRLZ24N
TR32SJ174
D4UF4002
D5UF4002
R22
4K7
R25
4K7
7.2V
7.2V
0V
35V
3 21
IC3A
4049
7 61
IC3C
4049
5 41
IC3B
4049
14 151
IC3F
4049
9 101
IC3D
4049
11 121
IC3E
40490V
C
A
B
D
Chapter 4MECHATRONICS PROJECT KIT Description of the Electronic Circuitry
40-100-1 4-3
4.3 Magnetic Speed Sensors
The magnetic sensor circuit (Figure 4-4) has a filter on the input to reduce the affect ofany noise that might be picked up from the motor; this is then ac coupled and biased up to2.5 V (half the supply) and amplified with a gain of 52. The signal is then passed into acomparator with the other input being 2.5 V. The output of this circuit will either be high orlow.
Figure 4-4: Magnet Sensor Circuit
R510K
R6
510K
3
21
411
IC1ALMC660CN5
67
IC1BLMC660CN
AV1
AV2
R110K
R41M0
0V
R710K
R3100K
MAGNETICSENSOR
R810K
R2100K
C3100n
C2100n
C110n
C410n0V
0V
+5V
+5V
0V
0V
+5V
0V
Chapter 4MECHATRONICS PROJECT KIT Description of the Electronic Circuitry
4-4 40-100-1
4.4 Temperature Sensor
The temperature sensor circuit is shown in Figure 4-5. The maximum voltage out of thetemperature sensor is 1.75 V with a temperature of 125°C, the motor will not reach thistemperature as the output signal is amplified by a factor of four. This means that at thelimit of the analogue signal (5 V), the temperature of the case of the motor would be 75°C,which is higher than the case of the motor will go.
The formula for calculating the temperature from the 10-bit conversion is:
500mVC)TempC(10mV/Vout
sensortheofoutevoltaggainamp
XVout
PICtheintoltagevo1024
5convertionbinaryofnumberdecimalX
oo +×=
=
×=
Figure 4-5: Temperature Sensor Circuit
3
21
411
IC4ALMC660CNR30
3K3
R31
3K3
+5V
0V
0V
+VS
1
VO 2
GN
D3
IC6LM50BIM3
0V
+5V
TEMPERATURESENSOR
RV220K
Chapter 4MECHATRONICS PROJECT KIT Description of the Electronic Circuitry
40-100-1 4-5
4.5 Back EMF Sensing
The back EMF is only available under certain conditions, these being that the bi/uni signalneeds to be set to bi and the back EMF available signal is low.
Figure 4-6: Back EMF Sensing Circuit
The circuit above samples the back EMF signal during the pwm off period via ananalogue switch, which is controlled by the back EMF available signal. The capacitor thenholds the average value, which is amplified to give a range between 0 and 5 volts for thePIC to read during pwm on period. The back emf signal has a dc offset to take intoaccount when the PIC converts the analogue signal. This motor offset is 0.5 volts.
10
98
IC4CLMC660CN
12
1314
IC4DLMC660CN
C10100n
R2610K 0V
R272K2
0V
R2310K
R24
10K
RV120K
0V
2 1
7SW2ASW MAX323
Chapter 4MECHATRONICS PROJECT KIT Description of the Electronic Circuitry
4-6 40-100-1
4.6 Stepper Motor Drive Board
As shown in Figure 4-7, the driving signals for the stepper motor are buffered intoMOSFETS that provide the switching for the coils in the motor. The inputs to this circuitneed to have 100k ohms pull-down resistors otherwise they float high and cause anincorrect stepping sequence.
Figure 4-7: Stepper Motor Drive Circuit
4.7 Optical Sensors
The individual reflective sensor boards use the same basic circuit as the optical wheelrotation sensor. The two changes are the addition of an LED to have a visualrepresentation of which sensor the tape is underneath, and resistor R50 has beenreduced to 68 ohms, this will increase the drive current to the infra red LED.
There is also a board that contains a slotted sensor, as this type of optical switch has abetter coupling and therefore only two other components are needed to operate thedevice. This can be used for the detection of the steering pointing straight ahead on theAckermann steering.
R4100K
R3100K
R1100K
R2100K
TR1IRLD014
TR2IRLD014
TR3IRLD014
TR4IRLD014
7 6IC1C 4050
9 10IC1D 4050
11 12IC1E 4050
14 15IC1F 4050
0V
0V
Signals toStepper MotorSignals from
microprocessor
5V on board
Chapter 5MECHATRONICS PROJECT KIT Programming Guidelines
40-100-1 5-1
5 Programming Guidelines
A manual is provided by Abitec on the PIC board layout and pic877 software fordownloading your program, which includes a full explanation and an example.
The programming environment has its own simulator and for this a project needs to be setup. To set up the environment mode; open MPlab, select project and new project, entername and select OK, under project files select the file and then select node properties(needs to be set up first by selecting options and development mode). A window will openand, in that window, make sure that MPLAB-SIM simulator is selected and the processorselected is the PIC16F877.
The Figure 5-1 shows a typical workstation for programming the buggy.
Figure 5-1: Mechatronics Kit Programming Environment
The beginning of any program needs a directive statement so that the compiler can linkthe necessary source files for the particular processor used.
All the instruction set and detailed methods of programming are contained in the PICmanual. In this chapter, the main considerations that need to be known are highlighted.
Register bits can be accessed through their bit number (e.g. STATUS,2) or through theirbit name (e.g. STATUS,Z), both these instructions look at the zero flag, z. When adestination field is required after the operand, the codes zero or one used to determineaccumulator or file can be replaced with the letters w (accumulator) or f (file).
Chapter 5MECHATRONICS PROJECT KIT Programming Guidelines
5-2 40-100-1
5.1 Registers
The STATUS register holds key information about which memory bank the program islooking at and what happened to the accumulator in the previous instruction, (carry andzero flags). The INTCON register contains various interrupt and enable bits and flag bitsfor the external interrupts and portb interrupt on change. The PIE1 register contains theenable bits for the peripheral interrupts, with the corresponding flag bits in the PIR1register.
Other useful registers include ADCONO, ADCON1 – for analogue to digital conversioncontrol and analogue pin select. ADRESH, ADRESL for the ADC result.
The CCP registers are used for the control of the pulse width modulation and interrupts,with timer 2 being used for the PWM frequency and associated registers being PR2 andTCON2.
Other useful registers include TRIS* (* = A, B, C, D or E) data direction and some controlfor the ports.
A list of all registers and their memory locations can be found in the PIC Manual, Chapter2. Specific control bits for each individual register can also be found in the relevantchapter of the PIC manual.
5.2 Timers
Timer 2, which is used for the PWM timing, needs to be set up with the maximum amountof prescale and a maximum value in the timer register in order to scale down the controlfrequency to the motors, as the motor has a low optimum control frequency. The prescaleis set in the T2CON register bits 1 and 2 and the period is set in PR2 register. The controlof the mark to space ratio is achieved by a 10 bit binary number that can vary from0-100%, the lower two bits can be found in CCP1CON bits 5 and 4 and the upper 8 bitsare stored in the CCPR1L register.
Full explanations of each timer can be found in the PIC Manual, Chapters 5, 6 and 7.
5.3 Interrupts and the ISR
The use of interrupts is a personal choice as they are not necessary but useful in longprograms with different tasks running sequentially (some tasks could miss a vital piece ofinput data required). Because interrupts can occur at any time, the program might be inthe middle of a calculation. Therefore, the w register and the STATUS register need to besaved first so that when the ISR is finished, the program registers can be returned to theiroriginal state. More details can be found in Chapter 12.10 of the PIC16F87X ReferenceManual.
Chapter 5MECHATRONICS PROJECT KIT Programming Guidelines
40-100-1 5-3
5.4 Stepper Motor Configuration
Stepper motor colour code for the wires to each phase should be:
A B C D E F
440-420 White Brown Red Yellow Brown Blue
Step Sequence
Step A B A| B|
1 b a
2 a a
3 a a
4 a a
This step sequence is required for continuous rotation of the shaft.
Note:When using the Ackermann steering at its maximum angle,
no further steps should be made as this could cause damage toeither the steering mechanism or to the motor.
F E D
C
B
AM A
A|
B
B|
Chapter 5MECHATRONICS PROJECT KIT Programming Guidelines
5-4 40-100-1
5.5 PIC Microprocessor Pin-out
The PIC microprocessor pin-out is shown in Figure 5-2.
Figure 5-2: PIC Microprocessor Pin-out
Chapter 5MECHATRONICS PROJECT KIT Programming Guidelines
40-100-1 5-5
5.6 Downloading and Running a Program
Proceed as follows:
1. Connect the cable to a free serial port on the PC and the port on the PIC board.
2. Set the Prg/Run switch to the Prg position.
3. Apply power to the board; the yellow LED is lit.
4. Start the software. This should automatically detect the PIC board on the serial port itwas connected to. The status bar at the bottom of the window details the state of theconnection.
5. If a program is in the PIC, select ‘Program’ menu and ‘Erase All’.
6. Select the ‘file’ menu and ‘load’.
7. Locate and select the .HEX file you wish to program.
8. Select ‘Prog. and Configure’ radio button. The ‘Program Selected Range’ radio buttonshould be selected, the amount of memory is automatically set for the required size ofprogram.
9. Select the ‘Program’ menu and the ‘Program’ command; the red and green LED’s arelit. The bar along the bottom of the screen shows the progress of the download.
10.When the download is complete, switch the Prg/Run switch to Run.
11.Press the Start/Stop button to start the program; the green LED is lit.
12.Press the Start/Stop button to stop the program.
13.For reprogramming, set the Prg/Run switch to the Prg position and return to step 5.
Chapter 5MECHATRONICS PROJECT KIT Programming Guidelines
5-6 40-100-1
Notes.
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-1
6 Sample Programs for each Module
The following programmes were written for a buggy with the single-motor mechanicaldifferential drive, Ackermann steering and the three reflective sensors mounted from thefront at a distance of 14 mm apart and 8 mm off the floor.
Student Tips
1. For some of these programs you will need to construct a bank of 8 and a bank of 2LED’s, the basic circuit for these that can be built on is
2. Another useful board to make would be an 8-pin DIL switch package to simulate inputsof 0 V and 5 V, for debugging purposes.
O/P from330R
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-2 40-100-1
6.1 Pulse Width Modulation (PWM)
The PWM frequency that is required for optimal motor response is very low, and with thissystem the lowest pwm frequency possible is 600 Hz.
6.1.1 Wiring Required
PIC board RC2 → motor board PWM 1
PIC board RC3 → motor board FD/RV 1
PIC board RC4 → motor board BI/UNI 1
6.1.2 Program
; Mechatronics project
; PIC used 16f877
; this program turns the wheels at a constant speed in one direction for about4 ; seconds and then in the other direction for about 4 seconds using the PWM; timer
;
;
; file name pwm.asm
; date last modified 04/02/2000
; written by Martyn Langfield
;
;***************************************
; directive statement
;***************************************
list p = 16f877
include <p16f877.inc>
;***************************************
; allocate memory locations
;***************************************
delay_hi equ H'31'
delay_lo equ H'32'
TMP2 equ H'33'
count equ H'34'
count2 equ H'35'
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-3
;***************************************
;vector settings
;***************************************
ORG H'00'
goto start
ORG H'23'
;****************************************
;initialisation
;****************************************
start
movlw H'FF'
BSF STATUS,RP0 ; select bank1
movwf PR2 ; pwm period = FF
movlw H'20' ; value for duty cycle
BCF STATUS,RP0 ; bank 0
movwf CCPR1L ; duty cycle location
bsf CCP1CON,5
bcf CCP1CON,4 ; lsb of the 10bit duty cycle
BSF STATUS,RP0 ; select bank1
bcf INTCON,7 ; disable global interrupts
movlw H'00'
movwf TRISC ; portc output
bsf PIE1,1 ; TMR2 to PR2 Match Interrupt Enable bit
BCF STATUS,RP0 ; bank 0
bsf T2CON,1 ; timer 2 prescale to 16
bsf T2CON,2 ; TIMER 2 ON
bsf CCP1CON,3
bsf CCP1CON,2 ; SETS BITS FOR PWM MODE
movlw H'FF'
movwf count ; set up count with value FF hex
movlw H'05'
movwf count2 ; set up hi count with value 05 hex
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-4 40-100-1
;**************************************
;main program
;**************************************
main
bcf PORTC,4 ; set control method bi/uni
movf TMR2,0 ; READ TIMER2
movwf TMP2
btfsc PIR1,1 ; CHECK IF INTERRUPT FLAG SET
call increment ; yes call subroutine
goto main ; no
;************************************
;subroutines
;************************************
increment
bcf PIR1,1 ; clear interrupt flag
decfsz count,1 ; decrement count and skip when zero is reached
return
decfsz count2,1 ; decrement count2 and skip when zero is reached
return
goto toggle
return
toggle
movlw H'FF'
movwf count ; load the value of ff to the register count
movlw H'05'
movwf count2 ; load the value of 05 to the register count 2
btfsc PORTC,3 ; test bit 3 portc
goto clear ; if clear goto clear
bsf PORTC,3 ; set bit 3 portc
movlw H'20'
movwf CCPR1L ; set pwm period
return
clear
bcf PORTC,3 ; clear it 3 portc
movlw H'20'
movwf CCPR1L ; set pwm period
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-5
return
END
6.2 Stepper Control
6.2.1 Wiring Required
PIC board RD0 → stepper board B′
PIC board RD1 → stepper board A′
PIC board RD2 → stepper board B
PIC board RD3 → stepper board A
PIC board RB0 → slotted sensor board
6.2.2 Program
;Mechatronics project
;PIC 16f877
;this program moves the steering to one limit then finds the centre
;then moves to either extreme and back to centre position and checks if centre;position reached
;
;file name centre.asm
;date last amended 22/03/2000
;written by Martyn Langfield
;
;*****************************************
;directive statement
;*****************************************
list p = 16f877
include <p16f877.inc>
;*****************************************
;allocate memory locations
;*****************************************
big equ H'30'
delay_hi equ H'31'
delay_lo equ H'32'
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-6 40-100-1
tmp equ H'33'
tmp2 equ H'34'
count equ H'35'
count2 equ H'36'
;****************************************
;vector settings
;****************************************
ORG H'00'
goto start
ORG H'23'
;****************************************
;initialisation
;****************************************
start
clrf PORTA
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
movlw H'00'
BSF STATUS,RP0 ; select bank1
movwf TRISD ; setup o/p ports
movwf TRISC
movlw H'FF'
movwf TRISB ; setup i/p port
BCF STATUS,RP0 ; bank 0
movlw H'05'
movwf count ; setup count with 05 hex
clrf count2 ; setup count2 with 00 hex
clrf tmp
clrf tmp2
;**************************************
;main program
;**************************************
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-7
main
btfsc PORTB,0 ; check if centre sensor is ‘1’
call find ; if not call find
drive
call delay
call step_right
movf count2,w ; move count2
movwf tmp ; to tmp register
movlw B'00100000' ; load w reg with value
subwf tmp,f ; sub value of w reg from tmp
btfss STATUS,Z ; test for zero flag
goto drive ; no then repeat
drive2
call delay
call step_left
movf count2,w ; move count2
movwf tmp ; to tmp register
movlw B'00010000' ; load w reg with value
subwf tmp,f ; sub value of w reg from tmp
btfss STATUS,Z ; test for zero flag
goto drive2 ; no then repeat
call delay
btfsc PORTB,0 ; test for centre flag on input
call find ; no flag then find
bsf PORTC,5 ; centre found set bit 5 portc
call ldelay
drive3
call delay
call step_left
movf count2,w ; move count2
movwf tmp ; to tmp register
btfss STATUS,Z ; test for zero flag
goto drive3 ; no then repeat
drive4
call delay
call step_right
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-8 40-100-1
movf count2,w ; move count2
movwf tmp ; to tmp register
movlw B'00010000' ; load w reg with value
subwf tmp,f ; sub value of w reg from tmp
btfss STATUS,Z ; test for zero flag
goto drive4 ; no then repeat
call delay
btfsc PORTB,0 ; test for centre flag on input
call find ; no flag then find
bsf PORTC,5 ; centre found set bit 5 portc
call ldelay
goto drive
;************************************
;subroutines
;************************************
find
call ldelay
call right
step
btfsc PORTB,0 ; test if centre
goto inc ; no, then goto inc
call found ; yes, then goto found
return
inc
call step_left
call ldelay
goto step
right
movlw b'00001001' ; step steering to limit
movwf PORTD
call delay
movlw b'00001100'
movwf PORTD
call delay
movlw b'00000110'
movwf PORTD
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-9
call delay
movlw b'00000011'
movwf PORTD
call delay
decfsz count,1
goto right
movlw H'09'
movwf count
return
step_right
bcf PORTC,5 ; clear steering straight flag
movf PORTD,W ; reed portd to w reg
movwf tmp2 ; move w reg to tmp2
rrf tmp2,F ; rotate right tmp2
btfsc STATUS,C ; test for overflow
bsf tmp2,3 ; if yes set bit 3 in tmp2
movf tmp2,w ; move tmp2 to w reg
movwf PORTD ; move w reg to portd
incf count2,f ; increment count2
return
step_left
bcf PORTC,5 ; clear steering straight flag
bcf STATUS,C ; clear carry flag
movf PORTD,W ; reed portd to w reg
movwf tmp2 ; move w reg to tmp2
rlf tmp2,F ; rotate left tmp2
btfsc tmp2,4 ; test for overflow in tmp2
bsf tmp2,0 ; if yes set bit 0
bcf tmp2,4 ; clear bit 4
movf tmp2,w ; move tmp2 to w reg
movwf PORTD ; move w reg to portd
decf count2,f ; decrement count2
return
found
movlw B'00010000'
movwf count2 ; load count2 with centre count
bsf PORTC,5 ; output 1 on portc bit5 to signal centre located
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-10 40-100-1
call ldelay ; long delay
call ldelay
call ldelay
call ldelay
return
delay
movlw H'00'
movwf delay_lo
movlw H'70'
movwf delay_hi
outer
inner
incfsz delay_lo,1
goto inner
incfsz delay_hi,1
goto outer
return
ldelay
movlw H'00'
movwf delay_lo
movlw H'00'
movwf delay_hi
movlw H'FA'
movwf big
extra
outer2
inner2
incfsz delay_lo,1
goto inner2
incfsz delay_hi,1
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-11
goto outer2
incfsz big,1
goto extra
return
END
6.3 Line Sensors
6.3.1 Wiring Required
PIC board RD7 → sensor board 3
PIC board RD6 → sensor board 2
PIC board RD5 → sensor board 1
PIC board RD0 → stepper board B′
PIC board RD1 → stepper board A′
PIC board RD2 → stepper board B
PIC board RD3 → stepper board A
6.3.2 Program
;Mechatronics project
;PIC 16f877
;To look at a line and move the steering accordingly
;
;file name line3s.asm
;date last amended 04/02/2000
;written by Martyn Langfield
;
;******************************************
;directive statement
;******************************************
list p = 16f877
include <p16f877.inc>
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-12 40-100-1
;******************************************
;allocate memory locations
;******************************************
delay_hi equ H'31'
delay_lo equ H'32'
TMP equ H'33'
count equ H'34'
rstcount equ H'35'
lstcount equ H'36'
;******************************************
;vector settings
;******************************************
ORG H'00'
goto start
ORG H'23'
;****************************************
;initialisation
;****************************************
start
BCF STATUS,RP0 ; bank 0
clrf PORTA ; clear ports
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
BSF STATUS,RP0 ; select bank1
movlw H'00'
movwf TRISB ; PORTB OUTPUT
movwf TRISC ; portc output
movwf TRISE ; porte output
movwf TRISA ; porta output
movlw B'11110000'
movwf TRISD ; portd 0-3 O/P 4-7 I/P
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-13
BCF STATUS, RP0 ; bank 0
movlw B'00010100'
movwf count ; setup count for maximum steering travel
movlw H'05'
movwf rstcount ; initial steering count right
movlw H'0A'
movwf lstcount ; initial steering count left
call steer ; call subroutine to straighten steering
;**************************************
;main program
;**************************************
main
btfss PORTD,6 ; test centre line sensor
goto main ; yes repeat
btfss PORTD,7 ; test left sensor
goto left1 ; yes goto left1
btfss PORTD,5 ; test right sensor
goto right1 ; yes goto right1
goto main ; no sensor on begin test again
;************************************
;subroutines
;************************************
right1
movf count,w ; move count to w reg
btfsc STATUS,Z ; test if zero flag set
goto main ; if yes go to read sensors
decf count,f ; decrement count
rlf PORTD,w ; shift left portd and put in w reg
movwf TMP ; move w reg to tmp
btfsc TMP,4 ; test for over flow
goto under1 ; yes
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-14 40-100-1
bcf TMP,0 ; clear bit 0 of tmp
movf TMP,0 ; move tmp to w reg
movwf PORTD ; output stepper control signal
call delay
goto main
under1
bcf TMP,4 ; clear bit 4 in tmp
bsf TMP,0 ; set bit 0 in tmp
movf TMP,0 ; move tmp to w reg
movwf PORTD ; output stepper control signal
call delay
goto main
left1
movf count,w
addlw B'11011000' ; add a number to the w reg so that when the; steering is at its limit a zero condition is met
btfsc STATUS,Z ; test zero flag
goto main ; yes
incf count,1 ; increment steering counter
rrf PORTD,0 ; rotate contents of portd right and place in w reg
movwf TMP
btfsc STATUS,C ; check for overflow
goto over1 ; yes
movwf PORTD ; output stepper control signal
call delay
goto main
over1
bsf TMP,3 ; over flowed bit needs to be moved to other end of; the o/p nibble
movf TMP,0 ; move tmp to the w reg
movwf PORTD ; output stepper control signal
call delay
goto main
steer ; routine to find the centre position by counting; half the maximum number of steps from one limit; to the other limit
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-15
right_ST
movlw H'00'
movwf CCPR1L ; set 0 pwm signal (pwm off)
movlw b'00000011' ; stepper motor sequence to right limit
movwf PORTD
call delay
movlw b'00000110'
movwf PORTD
call delay
movlw b'00001100'
movwf PORTD
call delay
movlw b'00001001'
movwf PORTD
call delay
decfsz rstcount,1 ; reduce loop count by one
goto right_ST
movlw H'05'
movwf rstcount ; re-setup loop count
left_ST
movlw b'00001001' ; stepper sequence to left limit
movwf PORTD
call delay
movlw b'00001100'
movwf PORTD
call delay
movlw b'00000110'
movwf PORTD
call delay
movlw b'00000011'
movwf PORTD
call delay
decfsz lstcount,1
goto left_ST
movlw H'0A'
movwf lstcount ; re-setup loop count
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-16 40-100-1
right_ST2
movlw b'00000011' ; step 5 loop counts to centre the steering
movwf PORTD
call delay
movlw b'00000110'
movwf PORTD
call delay
movlw b'00001100'
movwf PORTD
call delay
movlw b'00001001'
movwf PORTD
call delay
decfsz rstcount,1
goto right_ST2
movlw H'05'
movwf rstcount re- setup loop count
return
delay
movlw H'00'
movwf delay_lo
movlw H'00'
movwf delay_hi
outer
inner
incfsz delay_lo,1
goto inner
incfsz delay_hi,1
goto outer
return
END
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-17
6.4 Optical/Magnetic Speed Sensing
6.4.1 Wiring Required
PIC board RB5 → Motor board Opt/Mag1
PIC board RC2 → motor board PWM 1
PIC board RC3 → motor board FD/RV 1
PIC board RC4 → motor board BI/UNI 1
LED’s were connected to port d to indicate the counted pulses from the motor board.
6.4.2 Program
;Mechatronics project
;PIC used 16f877
;this program uses the PWM timer to turn the wheels at a constant speed in one
;direction for a count of 256 and then reverses the direction for a count of256
;
;file name pwm&opt.asm
;date last modified 04/02/2000
;written by Martyn Langfield
;
;***************************************
;directive statement
;***************************************
list p = 16f877
include <p16f877.inc>
;***************************************
;allocate memory locations
;***************************************
delay_hi equ H'31'
delay_lo equ H'32'
TMP2 equ H'33'
pulse equ H'36'
w_temp equ H'37'
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-18 40-100-1
status_temp equ H'38'
;***************************************
;vector settings
;***************************************
ORG H'00'
goto start
ORG H'23'
;*****************************************
;isr
;*****************************************
ORG 0x004 ; interrupt vector location
Movwf w_temp ; save off current W register contents
Movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
btfsc PIR1,1 ; is interrupt tmr2
goto per ; yes
btfsc INTCON,RBIF ; is interrupt portb
goto port_t ; yes
per
bcf PIR1,1 ; clear tmr2 interrupt
goto Restore
port_t
btfss PORTB,4 ; test if positive edge
goto Restore ; no
incf pulse,1 ; add 1 to register
btfsc STATUS,Z ; is zero flag set
call toggle ; yes
bcf INTCON,RBIF ; clear portb interrupt
Restore
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-19
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;****************************************
;initialisation
;****************************************
start
movlw H'FF' ; value for pwm period
BSF STATUS,RP0 ; select bank1
movwf PR2
movlw H'0E' ; value for duty cycle
BCF STATUS,RP0 ; bank 0
movwf CCPR1L ; duty cycle location upper 8 bits
bcf CCP1CON,5 ; bit 1 of duty cycle
bcf CCP1CON,4 ; lsb of the 10bit duty cycle
clrf PORTC ; clear data registers
clrf PORTD
bcf PIR1,1 ; clear tmr2 interrupt
bcf INTCON,T0IF
bcf INTCON,RBIF ; clear portb interrupt flag
BSF STATUS,RP0 ; select bank1
movlw H'00'
movwf TRISC ; portc output
movwf TRISD
bsf PIE1,1 ; TMR2 to PR2 Match Interrupt Enable bit
movlw H'FF'
movwf TRISA ; porta i/p
movwf TRISB
BCF STATUS,RP0 ; bank 0
bsf T2CON,1 ; timer 2 prescale to 16
bsf T2CON,2 ; TIMER 2 ON
bsf CCP1CON,3 ; set for pwm mode
bsf CCP1CON,2 ; SETS BITS FOR PWM MODE
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-20 40-100-1
clrf pulse ; clear pulse
bcf PORTC,4 ; bi/uni set to bi
bsf INTCON,PEIE ; ENABLE peripheral interrupts
bsf INTCON,RBIE ; enable port b interrupt on change
bsf INTCON,GIE ; enable global interrupts
;**************************************
;main program
;**************************************
main
movf pulse,w
movwf PORTD ; display counted pulses
goto main
;************************************
;subroutines
;************************************
toggle
btfsc PORTC,3 ; test direction bit
goto clear
bsf PORTC,3 ; set direction bit
movlw H'11' ; set speed for direction
movwf CCPR1L
return
clear
bcf PORTC,3 ; clear direction bit
movlw H'11' ; set speed for direction
movwf CCPR1L
return
END
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-21
6.5 Temperature Sensing
6.5.1 Wiring Requirements
PIC board RE2 → Temp1
LED’s connected to ports D and C4 and C5 to display the 10-bit conversion.
6.5.2 Program
;Mechatronics project
;PIC 16f877
;an analogue input with binary value displayed on LED's
;
;file name analo.asm
;date last amended 04/02/2000
;written by Martyn Langfield
;
;********************************************
;directive statement
;********************************************
list p = 16f877
include <p16f877.inc>
;********************************************
;allocate memory locations
;********************************************
delay_hi equ H'31'
delay_lo equ H'32'
;********************************************
;vector settings
;********************************************
ORG H'00'
goto start
ORG H'23'
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-22 40-100-1
;****************************************
;initialisation
;****************************************
start
BCF STATUS,RP0 ; bank 0
clrf PORTA ; clear port o/p buffers
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
BSF STATUS,RP0 ; select bank1
movlw H'00'
movwf TRISB ; PORTB o/p
movwf TRISC ; portc output
movwf TRISD ; portd o/p
movlw H'07'
movwf TRISE ; Porte i/p
movlw H'FF'
movwf TRISA ; porta i/p
;**************************************
; main program
;**************************************
main
BSF STATUS,RP0 ; select bank1
movlw B'10000000'
movwf ADCON1 ; SETUP PORT A & E FOR analogue AND RIGHT JUSTIFIED
bcf STATUS,RP0 ; select bank0
movlw B'10111001'
movwf ADCON0 ; SET TO CONVERT AN7 (PORTE,2), clocked at 32Tosc; and select AD on
call sdelay ; delay for acquisition time
bsf ADCON0,2 ; set conversion go
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-23
done
btfss ADCON0,2 ; poll for done bit
goto display
goto done
display
bsf STATUS,RP0 ; BANK 1
movf ADRESL,w
bcf STATUS,RP0 ; BANK 0
movwf PORTD ; output value of conversion
swapf ADRESH,w ; swap nibbles to w reg so that the two high bits; are in locations 4 and 5
movwf PORTC ; output higher 2 bits of result on portc bits 4&5
call delay
call delay
call delay
goto main
;************************************
; subroutines
;************************************
delay movlw H'00'
movwf delay_hi
movlw H'00'
movwf delay_lo
outer
inner
incfsz delay_lo,1
goto inner
incfsz delay_hi,1
goto outer
return
sdelay
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-24 40-100-1
movlw H'64'
movwf delay_lo
lo
incfsz delay_lo,1
goto lo
return
END
6.6 Back EMF Sensing
6.6.1 Wiring Requirements
PIC board RE2 → back emf1
PIC board RB7 → emf av.1
PIC board RC2 → motor board PWM 1
PIC board RC3 → motor board FD/RV 1
PIC board RC4 → motor board BI/UNI 1
LED’s connected to port D and port B0&1 to display the conversion.
6.6.2 Program
;Mechatronics project
;PIC 16f877
;PWM and the 10 bit digital conversion of the motor back emf displayed on LED's
;this program is solely interrupt driven, so will do nothing until an interrupt;occurs
;
;file name pwm&bemf.asm
;date last amended 04/02/2000
;written by Martyn Langfield
;
;*******************************************
;directive statement
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-25
;*******************************************
list p = 16f877
include <p16f877.inc>
;*************************************
;allocate memory locations
;*************************************
delay_hi equ H'31'
delay_lo equ H'32'
TMP2 equ H'33'
count equ H'34'
count2 equ H'35'
tmp equ H'36'
status_temp equ H'37'
w_temp equ H'38'
display_av equ H'39'
average_lo equ H'40'
average_hi equ H'41'
;**************************************
;vector settings
;**************************************
ORG H'00'
goto start
ORG H'23'
;*****************************************
;isr
;*****************************************
ORG 0x004 ; interrupt vector location
Movwf w_temp ; save off current W register contents
Movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
btfsc PIR1,1 ; test if pwm interrupt
goto per ; yes
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-26 40-100-1
btfsc INTCON,RBIF ; test if portb interrupt
goto port_t
goto restore
per
call increment
bcf PIR1,1 ; clear tmr2 interrupt
goto restore
port_t
btfsc PORTB,7 ; test if zero on pin 7
goto clr ; yes
call adc ; call analogue to digital conversion
clr bcf INTCON,RBIF ; clear portb interrupt
restore
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;****************************************
; initialisation
;****************************************
start
BCF STATUS,RP0 ; bank 0
clrf PORTA ; clear port o/p buffers
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
BSF STATUS,RP0 ; select bank1
movlw H'00'
movwf TRISD ; portd o/p
movwf TRISA ; porta i/p
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-27
movlw H'07'
movwf TRISE ; Porte i/p
movlw B'00000010'
movwf TRISC ; portc output except pin 1 i/p
movlw B'10000000'
movwf TRISB ; PORTB o/p except pin 7
movlw H'FF'
movwf PR2 ; value for pwm period 'FF'
movlw H'77' ; value for duty cycle
BCF STATUS,RP0 ; bank 0
movwf CCPR1L ; duty cycle location
bcf CCP1CON,5
bcf CCP1CON,4 ; lsb of the 10bit duty cycle
bcf INTCON,RBIF ; clear portb interrupt flag
BSF STATUS,RP0 ; select bank1
bsf PIE1,1 ; TMR2 to PR2 Match Interrupt Enable bit
movlw B'10000000'
movwf ADCON1 ; SET-UP PORT A & E FOR analogue and right; justified
BCF STATUS,RP0 ; bank 0
bsf CCP1CON,3
bsf CCP1CON,2 ; SETS BITS FOR PWM MODE
movlw H'FF'
movwf count ; set up count with value 10
movlw H'02'
movwf count2 ; set up hi count2 with value 01
bsf PORTC,4 ; bi/uni
movlw H'FF'
movwf display_av ; set number of conversions to average
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-28 40-100-1
clrf average_lo ; clear average registers
clrf average_hi
bsf T2CON,1 ; timer 2 prescale to 16
bsf T2CON,2 ; TIMER 2 ON
bsf INTCON,PEIE ; ENABLE peripheral interrupts
bsf INTCON,RBIE ; enable port b interrupt on change
bsf INTCON,7 ; enable global interrupts
;**************************************
;main program
;**************************************
main
nop
goto main
;************************************
; subroutines
;************************************
increment
decfsz count,1 ; counts a set number of pwm frequency cycles
return
decfsz count2,1
return
call toggle
return
toggle
movlw H'FF' ; then resets the count
movwf count
movlw H'02'
movwf count2
incf CCPR1L,1 ; and increments the mark to space ratio
return
adc
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
40-100-1 6-29
call sdelay
movlw B'10111001'
movwf ADCON0 ; SET TO CONVERT AN7 (PORTE,2), clocked at 32Tosc; and select AD on
call sdelay ; delay to allow the holding capacitor on the PIC
call sdelay ; to charge
bsf ADCON0,2 ; set conversion go
done
btfsc ADCON0,2 ; poll for done bit
goto done
bsf STATUS,RP0 ; BANK 1
movf ADRESL,w ; read lower 8 bits
bcf STATUS,RP0 ; BANK 0
addwf average_lo,f ; add to low average
btfss STATUS,C ; test for carry
goto upper ; no
incf average_hi,f ; yes
upper
movf ADRESH,w ; read upper 2 bits
addwf average_hi,f ; add to hi average
rrf average_hi,f ; divide by 2
rrf average_lo,f ; divide by 2
decfsz display_av,f ; average 255 conversions
return
movlw H'FF'
movwf discount
display
movf average_lo,w
movwf PORTD ; output value of conversion
movf average_hi,w
Chapter 6MECHATRONICS PROJECT KIT Sample Programs for each Module
6-30 40-100-1
movwf PORTB ; output higher 2 bits of result
return
sdelay
movlw H'96'
movwf delay_lo
lo
incfsz delay_lo,1
goto lo
return
END
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-1
7 Solutions
7.1 3D Models/Photos
The following are three typical examples of the major components of the buggy kit.
Single motor mechanicaldifferential with single-wheelcontrolled steering.
Single-motormechanical differentialwith Ackermannsteering.
Dual-motor drive, giving thedriving wheels independentcontrol capable of steering,with caster wheel at otherend.
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-2 40-100-1
7.2 Assembly
Using the single-motor solution and Ackermann steering on the long rectangular chassiswith the tapered end, the sensor boards were hung at the front at a height of 8 mm fromthe floor and at a distance of 14 mm apart from centre of sensor to centre of sensor. ThePIC board was mounted across the back with the battery in front with its terminals facingto the right .
This solution controls the speed of a dc motor, varying the speed in discrete steps withthe amount of turning angle on the stepper motor. The system also reads the line via fiveoptical sensors and adjusts the angle on the Ackermann steering according to how muchthe buggy is off the centre of the line.
Figure 7-1: Wiring for Five Sensor Line Following Solution
6 wayconnecto
Sensor Boards
Stepper Boardµprocessor Board
Steppermotor
Motor Board
+5VGNDRA0
+5V0VO/p
+5VGNDRC2RC3RC4
+5V0VPWM 1FD/RVBI/UNI
Power from battery
Power to PIC board
PIC board Power
+5VGNDRA1
+5V0VO/p
+5V
RA2
+5V0VO/p
+5VGNDRA3
+5V0VO/p
+5VGNDRA4
+5V0VO/p
+7.2V unreg+5VGNDRD0RD1RD2RD3
+5V0VB′A′BA
UNREG
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-3
4.5m 2m
3.25m2.5m
1.5m
2.4m
The wiring is best started on the peripheral boards when they are off the chassis with longwires, which can be cut to the required length later. Then the boards can be fixed to thechassis and the wires tied to the chassis or tied together in suitable places. The wires canbe cut to the required length and connected to the terminal blocks on the PIC board. Thepower plug from the motor board needs to be plugged in to the power in on the PIC boardand the battery needs to be connected to the flying lead on motor board.
7.3 Trouble-Shooting
If the progpic program does not automatically set up the communications link with the PICboard, check that all connectors are fully engaged, the power is connected and the run/prog switch is in the prog position; then select the “Comms” menu and “detect”.
Whilst running the program the motors hum but do not turn, first check that the battery isfully charged. If it is then increase the PWM on period until the motors turn.
If the steering wheel(s) turn the opposite way to the line, check the wiring is the correctorientation from the sensor boards and to the stepper board.
The LED’s on the sensor board will light when the when there is no reflected signal.
7.4 The Track
The track that was used for testing was set out as follows:
Figure 7-2: Diagram of Test Track
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-4 40-100-1
Using the program listed below, the following results were obtained for the test trackshown in Figure 7-2. The circuit was completed in about 20 seconds with a full batterypack, and the buggy kept going for 12.5 circuits, each circuit was slower then the previousone until there was not enough power left in the battery to drive the motor. The conditionof the floor can make a considerable amount of difference to the overall performance ofthe buggy. If the floor has ridges, the buggy will tend to jump at the front end. Addingweight to the front end can improve the stability.
7.5 Program
;Mechatronics project
;PIC 16f877
;to drive the motor and to read the 5 line sensors
;and move the steering accordingly
;
;file name: follow6.asm
;last modified 11/06/01
;written by Martyn Langfield
;
;directive statement
list p = 16f877
include <p16f877.inc>
;
;project definitions
;
;allocate memory locations
delay_hi equ H'31'
delay_lo equ H'32'
delay2_hi equ H'33'
delay2_lo equ H'34'
TMP equ H'35'
count equ H'36'
rstcount equ H'37'
lstcount equ H'38'
;vector settings
ORG H'00'
goto start
ORG H'23'
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-5
;****************************************
;initialization
;****************************************
start
BCF STATUS, RP0 ; bank 0
clrf PORTA ; clear ports
clrf PORTB
clrf PORTC
clrf PORTD
clrf PORTE
movlw H'FF' ; load w reg with FF
BSF STATUS, RP0 ; select bank1
movwf PR2 ; load value for pwm frequency
movlw H'00'
movwf TRISB ; PORTB OUTPUT
movwf TRISE ; porte output
movlw B'0000010'
movwf TRISC ; portc pin 1 i/p rest o/p
movlw B'00000000'
movwf TRISD ; portd 0-3 O/P 4-7 I/P
movlw H'FF'
movwf TRISA ; porta INPUT
movlw H'07'
movwf ADCON1 ; SETUP PORT A FOR DIGITAL I/O
movlw H'00' ; value for duty cycle
BCF STATUS, RP0 ; bank 0
movwf CCPR1L ; duty cycle location
bcf CCP1CON,5
bcf CCP1CON,4 ; lsb of the 10bit duty cycle
BSF STATUS, RP0 ; select bank1
bsf INTCON,7 ; enable globle interupts
bsf PIE1,1 ; TMR2 to PR2 Match Interrupt Enable bit
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-6 40-100-1
BCF STATUS, RP0 ; bank 0
bsf T2CON,1 ; timer 2 prescale to 16
bsf T2CON,2 ; TIMER 2 ON
bsf CCP1CON,3
bsf CCP1CON,2 ; SETS BITS FOR PWM MODE
movlw B'00010010'
movwf count ; setup count for maximum steeing traval
movlw H'05'
movwf rstcount ; initial steering count right
movlw H'0A'
movwf lstcount ; initial steering count left
call steer ; call subroutine to straighten steering
bsf PORTC,2 ; bi/uni set 1
;**************************************
;main program
;**************************************
main
btfss PORTA,4 ; and goto the relevant dirrection
goto center ; routine
btfss PORTA,3
goto left1
btfss PORTA,2
goto far_left
btfss PORTA,5
goto right1
btfss PORTC,1
goto far_right
movlw H'30' ; set speed
movwf CCPR1L
goto main
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-7
;************************************
;subroutines
;************************************
center
btfss PORTA,3 ; and goto the relevant dirrection
goto center_left ; routine
btfss PORTA,5
goto center_right
movf count,w
addlw B'11101110'
btfss STATUS,Z
goto test_c
movlw H'46' ; set speed
movwf CCPR1L
goto main
test_c
btfss STATUS,C
goto center_left
goto center_right
center_left
movlw H'43' ; set speed
movwf CCPR1L
movf count,w
addlw B'11101100' ; check if steering is at limit
btfsc STATUS,Z
goto main
btfsc STATUS,C
goto center_right
incf count,f ; increment steering counter
rrf PORTD,w ; rotate contents of portd right and place in w reg
movwf TMP
btfsc STATUS,C ; check for overflow
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-8 40-100-1
goto over
movwf PORTD
call delay
goto main
over
bsf TMP,3 ; bit needs to be carried to other end of the o/p; nibble
movf TMP,0
movwf PORTD
call delay
goto main
center_right
movlw H'43' ; set speed
movwf CCPR1L
movf count,w
sublw B'00010000'
btfsc STATUS,Z
goto main
btfsc STATUS,C
goto center_left
decf count,f ; same as for left but shifting left
rlf PORTD,w
movwf TMP
btfsc TMP,4
goto under
bcf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-9
under
bcf TMP,4
bsf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
left1
btfss PORTA,2 ; and goto the relevant dirrection
goto left_left ; routine
left
movlw H'40' ; old 35 30, 40 A0 45
movwf CCPR1L
movf count,w
addlw B'11101000' ; check if steering is at limit
btfsc STATUS,Z
goto main
btfsc STATUS,C
goto right
incf count,f ; increment steering counter
rrf PORTD,w ; rotate contents of portd right and place in w reg
movwf TMP
btfsc STATUS,C ; check for overflow
goto over1
movwf PORTD
call delay
goto main
over1
bsf TMP,3 ; bit needs to be carried to other end of the o/p; nibble
movf TMP,0
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-10 40-100-1
movwf PORTD
call delay
goto main
left_left
movlw H'3C' ; set speed
movwf CCPR1L
movf count,w
addlw B'11100010' ; check if steering is at limit
btfsc STATUS,Z
goto main
btfsc STATUS,C
goto right_right
incf count,f ; increment steering counter
rrf PORTD,w ; rotate contents of portd right and place in w reg
movwf TMP
btfsc STATUS,C ; check for overflow
goto over2
movwf PORTD
call delay
goto main
over2
bsf TMP,3 ; bit needs to be carried to other end of the o/p; nibble
movf TMP,0
movwf PORTD
call delay
goto main
right1
btfss PORTC,1 ; and goto the relevant dirrection
goto right_right ; routine
right
movlw H'40' ; set speed
movwf CCPR1L
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-11
movf count,w
sublw B'00001100'
btfsc STATUS,Z
goto main
btfsc STATUS,C
goto left
decf count,f ; same as for left but shifting left
rlf PORTD,w
movwf TMP
btfsc TMP,4
goto under1
bcf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
under1
bcf TMP,4
bsf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
right_right
movlw H'3C' ; set speed
movwf CCPR1L
movf count,w
sublw B'00000110'
btfsc STATUS,Z
goto main
btfsc STATUS,C
goto left_left
decf count,f ; same as for left but shifting left
rlf PORTD,w
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-12 40-100-1
movwf TMP
btfsc TMP,4
goto under2
bcf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
under2
bcf TMP,4
bsf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
far_right
movlw H'35' ; set speed
movwf CCPR1L
movf count,w
btfsc STATUS,Z
goto main
decf count,f ; same as for left but shifting left
rlf PORTD,w
movwf TMP
btfsc TMP,4
goto under3
bcf TMP,0
movf TMP,0
movwf PORTD
call delay
goto main
under3
bcf TMP,4
bsf TMP,0
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-13
movf TMP,0
movwf PORTD
call delay
goto main
far_left
movlw H'35' ; set speed
movwf CCPR1L
movf count,w
addlw B'11011100' ; check if steering is at limit
btfsc STATUS,Z
goto main
incf count,f ; increment steering counter
rrf PORTD,w ; rotate contents of portd right and place in w reg
movwf TMP
btfsc STATUS,C ; check for overflow
goto over3
movwf PORTD
call delay
goto main
over3
bsf TMP,3 ; bit needs to be carried to other end of the o/p; nibble
movf TMP,0
movwf PORTD
call delay
goto main
steer
right_ST
movlw H'00'
movwf CCPR1L
movlw b'00000011' ; stepper motor sequence
movwf PORTD
call delay2
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-14 40-100-1
movlw b'00000110'
movwf PORTD
call delay2
movlw b'00001100'
movwf PORTD
call delay2
movlw b'00001001'
movwf PORTD
call delay2
decfsz rstcount,f
goto right_ST
movlw H'05'
movwf rstcount
left_ST
movlw b'00001001'
movwf PORTD
call delay2
movlw b'00001100'
movwf PORTD
call delay2
movlw b'00000110'
movwf PORTD
call delay2
movlw b'00000011'
movwf PORTD
call delay2
decfsz lstcount,f
goto left_ST
movlw H'0A'
movwf lstcount
right_ST2
movlw b'00000011'
movwf PORTD
call delay2
movlw b'00000110'
movwf PORTD
call delay2
Chapter 7MECHATRONICS PROJECT KIT Solutions
40-100-1 7-15
movlw b'00001100'
movwf PORTD
call delay2
movlw b'00001001'
movwf PORTD
call delay2
decfsz rstcount,f
goto right_ST2
movlw H'05'
movwf rstcount
movlw b'00001100'
movwf PORTD
call delay2
movlw H'50'
movwf CCPR1L
return
delay
movlw H'00'
movwf delay_lo
movlw H'DF'
movwf delay_hi
outer
inner
incfsz delay_lo,f
goto inner
incfsz delay_hi,f
goto outer
return
delay2
Chapter 7MECHATRONICS PROJECT KIT Solutions
7-16 40-100-1
movlw H'00'
movwf delay2_lo
movlw H'B0'
movwf delay2_hi
outer2
inner2
incfsz delay2_lo,f
goto inner2
incfsz delay2_hi,f
goto outer2
return
END