Mechatronics Project Kit - Getting Started Manual

Similar documents
MicroToys Guide: Motors N. Pinckney April 2005

ELCT 912: Advanced Embedded Systems

GCE A level 1145/01 ELECTRONICS ET5. P.M. THURSDAY, 31 May hours. Centre Number. Candidate Number. Surname. Other Names

Controlling DC Brush Motor using MD10B or MD30B. Version 1.2. Aug Cytron Technologies Sdn. Bhd.

GCE A level 1145/01 ELECTRONICS ET5

;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cblock Bank0RAM ;Temporary storage for STATUS during interrupts

GCE A level 1145/01 ELECTRONICS ET5

IST TSic Temperature Sensor IC Application Notes ZACwire Digital Output

Embedded Systems. Interfacing PIC with external devices Analog to digital Converter. Eng. Anis Nazer Second Semester

Hashemite University Faculty of Engineering Mechatronics Engineering Department. Microprocessors and Microcontrollers Laboratory

Sensor Interface Using PIC12CXXX as a Sensor Interface for Metal Detection

Designing with a Microcontroller (v6)

Physics 335 Lab 7 - Microcontroller PWM Waveform Generation

EASTERN MEDITERRANEAN UNIVERSITY FACULTY OF ENGINEERING Electrical and Electronics Engineering Department

Triple Stage Incubator

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

2015 Technological Studies. Advanced Higher. Finalised Marking Instructions

Simple Bridge Stand Alone H-Bridge Data Sheet Revision 1 August 2005

Introduction to Using the PIC16F877 Justin Rice IMDL Spring 2002

Laboratory Exercise 1 Microcontroller Board with Driver Board

PROCESS. Object. Block diagram of our design. DISPLAY THE DISTANCE (7 segment display) PIC 16F873

PIC ADC to PWM and Mosfet Low-Side Driver

Microcontroller Based Inductance Capacitance Meter

Lesson 19 In-Circuit Programming

1. ASSEMBLING THE PCB 2. FLASH THE ZIP LEDs 3. BUILDING THE WHEELS

νµθωερτψυιοπασδφγηϕκλζξχϖβνµθωερτ ψυιοπασδφγηϕκλζξχϖβνµθωερτψυιοπα σδφγηϕκλζξχϖβνµθωερτψυιοπασδφγηϕκ χϖβνµθωερτψυιοπασδφγηϕκλζξχϖβνµθ

Pulse Width Modulation

PIC Functionality. General I/O Dedicated Interrupt Change State Interrupt Input Capture Output Compare PWM ADC RS232

MICROPROCESSORS A (17.383) Fall Lecture Outline

Final Project Report E3390 Electronic Circuits Design Lab. RFID Access Control System. Jeffrey Mok Joseph Kim

UNIVERSITY OF VICTORIA FACULTY OF ENGINEERING. SENG 466 Software for Embedded and Mechatronic Systems. Project 1 Report. May 25, 2006.

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 1.

Binary Outputs: LEDs

PIC Analog Voltage to PWM Duty Cycle

LINE MAZE SOLVING ROBOT

Development of a Low Cost MPPT Circuit for Solar Panel

EXPERIMENT 6: Advanced I/O Programming

Stepper Motors & Look Up Table

ABM International, Inc. Navigator Assembly Manual

LEG CURL IP-S1315 INSTALLATION INSTRUCTIONS

OWNERS MANUAL FOR MEC 306XP/408XP WOBBLE

Wednesday 7 June 2017 Afternoon Time allowed: 1 hour 30 minutes

Design of Low Cost Embedded Power Plant Relay Testing Unit

Disclaimers. Important Notice

Three-Stage Coil Gun

Sten-Bot Robot Kit Stensat Group LLC, Copyright 2013

FitWork Walkstation Series 7 AdjusTables

Installing the 3 Indexer: PRS Standard Tools

ORTOP Modular Robot v3.0 Arm Assembly

The University of Texas at Arlington Lecture 10 ADC and DAC

DIODE / TRANSISTOR TESTER KIT

Laboratory 11. Pulse-Width-Modulation Motor Speed Control with a PIC

HOMANN DESIGNS. DigiSpeed. Instruction manual. Version 1.0. Copyright 2004 Homann Designs.

Legacy Woodworking Machinery a division of Phantom Engineering. The Legacy CNC. Assembly Manual

Ocean Controls KT-5198 Dual Bidirectional DC Motor Speed Controller

Jass.Performance Low Profiles Installation Manual

Bill of Materials: PWM Stepper Motor Driver PART NO

STAGE PAGE. 6 The left front door interior The right front wheel The right front seat back The right front wheel 37

EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 11 Motor Control

STOP. V00029AC Rev. 04 READ ALL OF THE FOLLOWING INSTRUCTIONS BEFORE REMOVING CABINET FROM SKID TOOL LIST. NET-ACCESS S-Type Network Cabinets

ECE 203 LAB 2 CONTROL FUNDAMENTALS AND MAGNETIC LEVITATION

E4-WM5-Y525A00 MOUNTING INSTRUCTION

Pulse-Width-Modulation Motor Speed Control with a PIC (modified from lab text by Alciatore)

Tin Lizzie 18 Assembly Instructions

Budget Robotics Octabot Assembly Instructions

Gared Pro-S Portable Backstop

Jaguar Motor Controller (Stellaris Brushed DC Motor Control Module with CAN)

Process Components. Process component

Discrete Logic Replacement Garage Door Indicator

DigiSpeed-SL DC-04. Isolated Control Voltage Generator User s Guide. DigiSpeed PCB Ver:1.0 Firmware Ver: 1.0 Mach3 Ver: Updated: 5.

InterBOARD TM 12 Channel Transmitter and Receiver Evaluation Board User Guide

M4 Foot Operated Underpinner Instruction Manual

The Useless Machine. Parts Only - Build Guide v0001

Astro-Physics Inc. 400QMD Lubrication/Maintenance Guide

Brushed DC Motor Control. Module with CAN (MDL-BDC24)

The Portable Open Source 3D Printer

T6+ Analog I/O Section. Installation booklet for part numbers: 5/4-80A-115 5/4-90A-115 5/4-80A /4-90A-1224

EE 308 Spring S12 SUBSYSTEMS: PULSE WIDTH MODULATION, A/D CONVERTER, AND SYNCHRONOUS SERIAN INTERFACE

PAPER SHREDDER MERCURY RDS2050,RDX1750,RDS2270,RDX1970 ILLUSTRATED PARTS LIST

Data Acquisition Modules/ Distributed IO Modules

Sole Fitness E95 Elliptical Trainer TurnKey Delivery and Setup Training

Autonomous Robot Control Circuit

ECE 511: FINAL PROJECT REPORT GROUP 7 MSP430 TANK

Analogue Servo - Fundamentals Trainer

User guide. Revision 1 January MegaPoints Controllers

DigiSpeed DC-03. Isolated Control Voltage Generator User s Guide. PCB: DC-03 V3.0 Firmware: Ver: 3.0 Mach3: Ver: 1.84

Department of Mechanical and Industrial Engineering MECH 471 MICROCONTROLLERS FOR MECHATRONICS. Laboratory Specialist

V4 Premium Kit. Prusa i3 Build Guide

RPS. XR Front Drive Updates CORPORATION

EE 314 Spring 2003 Microprocessor Systems

USB Multifunction Arbitrary Waveform Generator AWG2300. User Guide

EPPA2-KIT DUAL MONITOR ARM CONVERSION

Series 7 - Adjustable Height Tables

Signature Choral Riser Side Rail

Abstract. 1. Introduction

Blue Point Engineering

installation guide

7. Operating instructions: EFL 300

MGL Avionics Autopilot. Servo. Specifications & Installation Manual. Last Update: 20 October Disclaimer:

Application Note. I C s f o r M o t o r C o n t r o l. Evaluation board for the TDA5143/TDA5144. Report No: EIE/AN R. Galema

Transcription:

Mechatronics Project Kit - Getting Started Manual 40-100-1

Mechatronics Project Kit Getting Started Manual 40-100-1 Feedback Feedback Instruments Ltd, Park Road, Crowborough, E. Sussex, TN6 2QR, UK. Telephone: +44 (0) 1892 653322, Fax: +44 (0) 1892 663719. email: feedback@fdbk.co.uk website: http://www.fbk.com Manual: 40-100-1 Ed03 072001 Printed in England by Fl Ltd, Crowborough Feedback Part No. 1160 401001

Notes

Preface 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 information regarding 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 in accordance 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 be pointed 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 whatsoever about 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 the user. These have been indicated in the specified way by yellow labels with black printing, the meaning of any labels that may be fixed to the instrument are shown below: CAUTION - RISK OF DANGER Refer to accompanying documents CAUTION - RISK OF ELECTRIC SHOCK CAUTION - ELECTROSTATIC SENSITIVE DEVICE 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 of printing. However, some product changes which do not affect the instructional capability of the equipment, may not be included until it is necessary 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 supply potential or higher, these must be replaced with components of equal international safety approval in order to maintain full equipment safety. In order to maintain compliance with international directives, all replacement components should be identical to those originally supplied. 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 performance either to published specification or compliance with international standards. 40-100-1 i

Preface 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 then Feedback Instruments Ltd hereby states that conformity with the protection requirements of the European Community Electromagnetic Compatibility 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 its immediate 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 any means, 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. ii 40-100-1

Contents 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 40-100-1 TOC 1

Contents 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 TOC 2 40-100-1

Contents 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 40-100-1 TOC 3

Contents Notes TOC 4 40-100-1

Chapter 1 Introduction 1 Introduction Mechatronics allows the integration of mechanics, electronics and computer technologies to enhance the performance of products, systems and processes. Typical products that use the principles of mechatronics are camcorders, computer disk drives, industrial robots and automobiles. The Mechatronics Project Kit, shown in Figure 1-1, provides the means for students to design and build a self-guided vehicle (buggy) from a set of modules, including two drive modules, two steering and one jockey wheel solutions and a microcontroller (PIC). When completed, the autonomous buggy is able to follow a track using infra red sensors. There are a number of different constructions possible with the components supplied in the kit but, if required, the hole pitch and all spacing is Meccano compatible so the kit can be expanded with any Meccano kit. Figure 1-1: Mechatronics Kit 40-100-1 1-1

CHAPTER 1 Introduction This project has been designed to combine all aspects of engineering, including mechanical, electrical, electronic, communications and software programming, into one development product. The buggy will be able to follow a track of insulation tape that has been laid on the floor; the type of tape that is required depends on the type of floor covering. The floor needs to be of a single colour, which is either infrared reflective or non-reflective; with a reflective floor, a non-reflective tape is required and vice-versa for a non-reflective floor. This is a fundamental guide to getting started on the mechatronics buggy project and does not cover every aspect of the design that is needed to complete the project as there is many different outcomes possible. 1.1 Modules The kit comprises of a number of different modules that can be used to construct a wheeled vehicle that will be capable of following a predetermined circuit on the laboratory floor. These parts are supplied unassembled in order for the students to define their particular 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 bytes of data memory, 256 8 bytes of EEPROM data memory and 8K 14 bytes of FLASH program memory. Programming of the PIC is achieved through the Microchips own programming environment called MPLAB (see Microchip Resources). High performance RISC CPU with a 35 single-word instruction set and an interrupt capability of up to 14 sources. On-board low dropout voltage regulation allows the unit to be powered from an unregulated 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-way D-type connector, which can also be used as a stand-alone serial communications port. Synchronous serial port (SSP) with I 2 C (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. 1-2 40-100-1

Chapter 1 Introduction 1.1.2 DC Motor Drive Circuitry This circuitry is capable of driving an interchangeable single or dual motor arrangement and 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 is pointing 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 to experiment 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 40-100-1 1-3

CHAPTER 1 Introduction 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 final option can be either front or rear wheel drive. Base1 Base2 Base3 Choice of three base plates with the PIC micro controller on Choice of two drive systems µ Processor Sensor feedback from each module Single DC Motor with Differential Dual DC Motor Direct Steering Choice of two steering systems Choice of three steering systems Ackermann Steering Stepper motor controlled pivot Wheel Ackermann Steering Stepper motor controlled pivot Wheel Jockey Wheel Figure 1-2: Mechatronics Kit Main Choices 1-4 40-100-1

Chapter 1 Introduction 1.3 Battery Charging The battery supplied in the kit is a rechargeable nickel-cadmium battery which can be charged up using the 12 V dc power supply and charge cable provided as shown in Figure 1-3. The method of charging used in this application is that of a constant current source delivering 1 A until the battery is nearly charged then the charging current starts to reduce. The charge-time that the battery requires depends on the amount of discharge during use; when discharged to a level that will not power the motors, the battery takes between 1.5 and 2 hours to charge. Note: The charge-time should not be exceeded, as permanent damage to the battery will occur. 12 V dc power supply connected to the mains supply In-line current limiter Battery Figure 1-3: Battery Charging Connections 40-100-1 1-5

CHAPTER 1 Introduction Notes 1-6 40-100-1

Chapter 2 Microchip Resources 2 Microchip Resources The PIC assembler used is Microchip s own MPLAB with the latest versions freely downloadable from their website (www.microchip.com). Also at this site are the latest revisions of the MPlab User Guide, PIC REFERENCE MANUAL and the PIC 16F87X Databook, all free to download in pdf format. Another area of the site is the Knowledge Base/Frequently Asked Questions page that can provide helpful information on various problems encountered. Microchip also offer a wide range of application notes for many different tasks that the PIC can be programmed to do. 40-100-1 2-1

Chapter 2 Introduction Notes 2-2 40-100-1

Chapter 3 Construction of the Mardave Components 3 Mechanical Components 3.1 Chassis There are three different chassis to choose from and each gives the system a different characteristic; 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, if you can design any other method of steering from the parts provided which give a better performance, 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 are connected to a plate attached to a stepper shaft as shown in Figure 3-2. The way Ackermann steering works is that the inside wheel has a greater turning angle for a tighter radius of the corner, thus allowing the wheels to have a differential cornering speed. This arrangement 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 as follows: 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 the two track rods to the steering link plate. 3. Bolt the stepper motor to the Z bracket and slide on the brass boss, then tighten the grub 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 40-100-1 3-1

Chapter 3 Construction of the Mardave Components suit your requirements. For correct operation of the Ackermann steering, the shaft of the stepper motor needs to be facing backwards so that the inner wheel has a greater turning angle. Plastic ball socket King pin Brass boss Steering link Zplate Steering arm Stub axle Track rod Base plate Triangular arms Ball screw 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. This arrangement has a full 360 controllable rotation, which gives a fast accurate response with 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 3-2 40-100-1

Chapter 3 Construction of the Mardave Components replaces the stepper motor with a caster block that allows the shaft to freely rotate. This arrangement is used when there is a different method of steering, e.g. twin motor driving the two driving wheels independently. 3.3 Drive Motor There are two options for driving the buggy, both using the same control board, which has the 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 tyre over 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 two halves 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 the rear, 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 for alignment of the two gears, so that the wheel spins freely. Differential spacer Axle Differential axle gear Optical sensing tape on drive block Optical sensing tape on differential block Motor mounts 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 block and the wheel onto the hex drive; then loosely tighten nut. Repeat similarly on other 40-100-1 3-3

Chapter 3 Construction of the Mardave Components side. 7. The motor mounts are then screwed to the motor drive board using flanged head selftap screws and the magnetic sensor is screwed up so that there is a 1-2 mm gap between the gear and the sensor. 3.3.2 Dual motor This system uses two motors running from two sets of control signals for independent drive and direction control. The method of constructing this arrangement is described below: 1. Attach the motors to the mounting brackets with two screws and lightly tighten, attach motor 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 slide on 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 the gears mesh together evenly) and align the magnetic sensor with the centre of the gear. Axle gear Optical sensing tape on drive block Motor gear Motor mount Axle Figure 3.5: Dual Motor Arrangements Axle gear Optical sensing tape on drive block 3-4 40-100-1

Chapter 4 Description of the Electronic Circuitry 4 Description of the Electronic Circuitry The pots on the motor drive board have been factory pre-set to specific values and altering these will change the feedback characteristics and result in inaccurate measurements. The pots on the optical sensor boards are for adjustment of the sensitivity of the optical switch 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: PWM B Fwd/Rv D Bi/Uni C A Figure 4-1: GAL Logic Figure 4-1 controls the firing sequence of the MOSFET H-bridge for the dc motor, which can be seen in Figure 4-2. 40-100-1 4-1

B MECHATRONICS PROJECT KIT Chapter 4 Description of the Electronic Circuitry B IC3B 5 1 4 4049 A IC3A 3 1 2 4049 IC3C 7 1 6 4049 R20 4K7 TR1 2SJ174 IRLZ24N TR2 R21 4K7 D 7.2V D3 UF4002 UF4002 D2 IC3D 9 1 10 4049 C IC3F 14 1 15 M1 MOTOR 4049 + C9 470u 35V IC3E 11 1 12 4049 D4 UF4002 D5 UF4002 TR3 2SJ174 0V R22 4K7 TR4 IRLZ24N R25 4K7 7.2V 0V 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 the optical sensor output passes the transistor threshold. Adjusting the variable resistor moves this threshold so that the transistor switches and therefore tweaks the sensitivity. +5V IC11 OPTO REFLECTOR [ ISTS708 ] R50 220R A RV5 100K R51 2K2 K C D10 BAX13 R52 1K0 TR9 ZTX108C C22 100n E 0V Figure 4-3: Optical Wheel Rotation Sensor Circuit 4-2 40-100-1

Chapter 4 Description of the Electronic Circuitry 4.3 Magnetic Speed Sensors The magnetic sensor circuit (Figure 4-4) has a filter on the input to reduce the affect of any noise that might be picked up from the motor; this is then ac coupled and biased up to 2.5 V (half the supply) and amplified with a gain of 52. The signal is then passed into a comparator with the other input being 2.5 V. The output of this circuit will either be high or low. +5V R2 100K R3 100K +5V MAGNETIC SENSOR AV1 AV2 0V R1 10K C1 10n 0V C2 100n R4 1M0 5 6 R5 10K IC1B LMC660CN R6 0V 510K 7 R8 10K R7 10K 3 2 C4 10n +5V IC1A LMC660CN 1 4 11 0V C3 100n 0V 0V Figure 4-4: Magnet Sensor Circuit 40-100-1 4-3

Chapter 4 Description of the Electronic Circuitry 4.4 Temperature Sensor The temperature sensor circuit is shown in Figure 4-5. The maximum voltage out of the temperature sensor is 1.75 V with a temperature of 125 C, the motor will not reach this temperature as the output signal is amplified by a factor of four. This means that at the limit 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: X = decimalnumber of binaryconvertion Vout = X amp gain o Vout = (10mV/ C Temp voltage out of o C) + 500mV 5 1024 the sensor voltage into the PIC +5V IC6 LM50BIM3 TEMPERATURE SENSOR R30 3K3 3 2 +5V 4 IC4A LMC660CN 1 11 1 +VS GND VO 2 3 0V R31 0V RV2 20K 0V 3K3 Figure 4-5: Temperature Sensor Circuit 4-4 40-100-1

Chapter 4 Description of the Electronic Circuitry 4.5 Back EMF Sensing The back EMF is only available under certain conditions, these being that the bi/uni signal needs to be set to bi and the back EMF available signal is low. R23 10K R24 10K R26 10K SW2A SW MAX323 2 1 7 C10 100n 0V 10 9 IC4C LMC660CN 8 12 13 IC4D LMC660CN 14 RV1 20K 0V R27 2K2 0V Figure 4-6: Back EMF Sensing Circuit The circuit above samples the back EMF signal during the pwm off period via an analogue switch, which is controlled by the back EMF available signal. The capacitor then holds the average value, which is amplified to give a range between 0 and 5 volts for the PIC to read during pwm on period. The back emf signal has a dc offset to take into account when the PIC converts the analogue signal. This motor offset is 0.5 volts. 40-100-1 4-5

Chapter 4 Description of the Electronic Circuitry 4.6 Stepper Motor Drive Board As shown in Figure 4-7, the driving signals for the stepper motor are buffered into MOSFETS that provide the switching for the coils in the motor. The inputs to this circuit need to have 100k ohms pull-down resistors otherwise they float high and cause an incorrect stepping sequence. IC1C 4050 7 6 TR1 IRLD014 IC1D 4050 9 10 TR2 IRLD014 Signals from microprocessor IC1E 4050 11 12 IC1F 4050 14 15 TR3 IRLD014 TR4 IRLD014 5V on board Signals to Stepper Motor R1 100K R2 100K R3 100K R4 100K 0V 0V Figure 4-7: Stepper Motor Drive Circuit 4.7 Optical Sensors The individual reflective sensor boards use the same basic circuit as the optical wheel rotation sensor. The two changes are the addition of an LED to have a visual representation of which sensor the tape is underneath, and resistor R50 has been reduced 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 a better coupling and therefore only two other components are needed to operate the device. This can be used for the detection of the steering pointing straight ahead on the Ackermann steering. 4-6 40-100-1

Chapter 5 Programming Guidelines 5 Programming Guidelines A manual is provided by Abitec on the PIC board layout and pic877 software for downloading 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 set up. To set up the environment mode; open MPlab, select project and new project, enter name 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 open and, in that window, make sure that MPLAB-SIM simulator is selected and the processor selected 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 link the necessary source files for the particular processor used. All the instruction set and detailed methods of programming are contained in the PIC manual. 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 their bit name (e.g. STATUS,Z), both these instructions look at the zero flag, z. When a destination field is required after the operand, the codes zero or one used to determine accumulator or file can be replaced with the letters w (accumulator) or f (file). 40-100-1 5-1

Chapter 5 Programming Guidelines 5.1 Registers The STATUS register holds key information about which memory bank the program is looking at and what happened to the accumulator in the previous instruction, (carry and zero flags). The INTCON register contains various interrupt and enable bits and flag bits for the external interrupts and portb interrupt on change. The PIE1 register contains the enable bits for the peripheral interrupts, with the corresponding flag bits in the PIR1 register. Other useful registers include ADCONO, ADCON1 for analogue to digital conversion control 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 and TCON2. Other useful registers include TRIS* (* = A, B, C, D or E) data direction and some control for the ports. A list of all registers and their memory locations can be found in the PIC Manual, Chapter 2. Specific control bits for each individual register can also be found in the relevant chapter of the PIC manual. 5.2 Timers Timer 2, which is used for the PWM timing, needs to be set up with the maximum amount of prescale and a maximum value in the timer register in order to scale down the control frequency to the motors, as the motor has a low optimum control frequency. The prescale is set in the T2CON register bits 1 and 2 and the period is set in PR2 register. The control of the mark to space ratio is achieved by a 10 bit binary number that can vary from 0-100%, the lower two bits can be found in CCP1CON bits 5 and 4 and the upper 8 bits are 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 long programs with different tasks running sequentially (some tasks could miss a vital piece of input data required). Because interrupts can occur at any time, the program might be in the middle of a calculation. Therefore, the w register and the STATUS register need to be saved first so that when the ISR is finished, the program registers can be returned to their original state. More details can be found in Chapter 12.10 of the PIC16F87X Reference Manual. 5-2 40-100-1

Chapter 5 Programming Guidelines 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 M F E D A B C A A B B 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 to either the steering mechanism or to the motor. 40-100-1 5-3

Chapter 5 Programming Guidelines 5.5 PIC Microprocessor Pin-out The PIC microprocessor pin-out is shown in Figure 5-2. Figure 5-2: PIC Microprocessor Pin-out 5-4 40-100-1

Chapter 5 Programming Guidelines 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 it was connected to. The status bar at the bottom of the window details the state of the connection. 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 button should be selected, the amount of memory is automatically set for the required size of program. 9. Select the Program menu and the Program command; the red and green LED s are lit. 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. 40-100-1 5-5

Chapter 5 Programming Guidelines Notes. 5-6 40-100-1

Chapter 6 Sample Programs for each Module 6 Sample Programs for each Module The following programmes were written for a buggy with the single-motor mechanical differential drive, Ackermann steering and the three reflective sensors mounted from the front 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 2 LED s, the basic circuit for these that can be built on is O/P from 330R 2. Another useful board to make would be an 8-pin DIL switch package to simulate inputs of 0 V and 5 V, for debugging purposes. 40-100-1 6-1

Chapter 6 Sample Programs for each Module 6.1 Pulse Width Modulation (PWM) The PWM frequency that is required for optimal motor response is very low, and with this system 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 about 4 ; 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 include p = 16f877 <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' 6-2 40-100-1

Chapter 6 Sample Programs for each Module ;*************************************** ;vector settings ;*************************************** ORG ORG H'00' start 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 40-100-1 6-3

Chapter 6 Sample Programs for each Module ;************************************** ;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 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 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 clear ; if clear 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 6-4 40-100-1

Chapter 6 Sample Programs for each Module 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 include p = 16f877 <p16f877.inc> ;***************************************** ;allocate memory locations ;***************************************** big equ H'30' delay_hi equ H'31' delay_lo equ H'32' 40-100-1 6-5

Chapter 6 Sample Programs for each Module tmp equ H'33' tmp2 equ H'34' count equ H'35' count2 equ H'36' ;**************************************** ;vector settings ;**************************************** ORG ORG H'00' start 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 ;************************************** 6-6 40-100-1

Chapter 6 Sample Programs for each Module 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 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 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 drive3 ; no then repeat drive4 call call delay step_right 40-100-1 6-7

Chapter 6 Sample Programs for each Module 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 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 drive ;************************************ ;subroutines ;************************************ find call ldelay call right step btfsc PORTB,0 ; test if centre inc ; no, then inc call found ; yes, then found return inc call step_left call ldelay 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 6-8 40-100-1

Chapter 6 Sample Programs for each Module call movlw movwf call decfsz movlw movwf return delay b'00000011' PORTD delay count,1 right H'09' count 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 40-100-1 6-9

Chapter 6 Sample Programs for each Module call ldelay ; long delay call ldelay call ldelay call ldelay return delay movlw movwf movlw movwf H'00' delay_lo H'70' delay_hi outer inner incfsz delay_lo,1 inner incfsz return delay_hi,1 outer ldelay movlw movwf movlw movwf movlw movwf H'00' delay_lo H'00' delay_hi H'FA' big extra outer2 inner2 incfsz delay_lo,1 inner2 incfsz delay_hi,1 6-10 40-100-1

Chapter 6 Sample Programs for each Module outer2 incfsz return big,1 extra 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 include p = 16f877 <p16f877.inc> 40-100-1 6-11

Chapter 6 Sample Programs for each Module ;****************************************** ;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 ORG H'00' start 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 6-12 40-100-1

Chapter 6 Sample Programs for each Module 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 main ; yes repeat btfss PORTD,7 ; test left sensor left1 ; yes left1 btfss PORTD,5 ; test right sensor right1 ; yes right1 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 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 under1 ; yes 40-100-1 6-13

Chapter 6 Sample Programs for each Module bcf TMP,0 ; clear bit 0 of tmp movf TMP,0 ; move tmp to w reg movwf PORTD ; output stepper control signal call delay 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 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 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 over1 ; yes movwf PORTD ; output stepper control signal call delay 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 main steer ; routine to find the centre position by counting ; half the maximum number of steps from one limit ; to the other limit 6-14 40-100-1

Chapter 6 Sample Programs for each Module 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 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 left_st movlw H'0A' movwf lstcount ; re-setup loop count 40-100-1 6-15

Chapter 6 Sample Programs for each Module 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 right_st2 movlw H'05' movwf rstcount re- setup loop count return delay movlw movwf movlw movwf H'00' delay_lo H'00' delay_hi outer inner incfsz delay_lo,1 inner incfsz delay_hi,1 outer return END 6-16 40-100-1

Chapter 6 Sample Programs for each Module 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 of 256 ; ;file name pwm&opt.asm ;date last modified 04/02/2000 ;written by Martyn Langfield ; ;*************************************** ;directive statement ;*************************************** list include p = 16f877 <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' 40-100-1 6-17

Chapter 6 Sample Programs for each Module status_temp equ H'38' ;*************************************** ;vector settings ;*************************************** ORG ORG H'00' start 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 per ; yes btfsc INTCON,RBIF ; is interrupt portb port_t ; yes per bcf PIR1,1 ; clear tmr2 interrupt Restore port_t btfss PORTB,4 ; test if positive edge 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 6-18 40-100-1

Chapter 6 Sample Programs for each Module 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 40-100-1 6-19

Chapter 6 Sample Programs for each Module 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 main ;************************************ ;subroutines ;************************************ toggle btfsc PORTC,3 ; test direction bit 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 6-20 40-100-1

Chapter 6 Sample Programs for each Module 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 ORG H'00' start H'23' 40-100-1 6-21

Chapter 6 Sample Programs for each Module ;**************************************** ;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 6-22 40-100-1

Chapter 6 Sample Programs for each Module done btfss ADCON0,2 ; poll for done bit display 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 main ;************************************ ; subroutines ;************************************ delay movlw H'00' movwf delay_hi movlw H'00' movwf delay_lo outer inner incfsz delay_lo,1 inner incfsz delay_hi,1 outer return sdelay 40-100-1 6-23

Chapter 6 Sample Programs for each Module movlw movwf H'64' delay_lo lo incfsz delay_lo,1 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 6-24 40-100-1

Chapter 6 Sample Programs for each Module ;******************************************* 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 ORG H'00' start 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 per ; yes 40-100-1 6-25

Chapter 6 Sample Programs for each Module btfsc INTCON,RBIF ; test if portb interrupt port_t restore per call increment bcf PIR1,1 ; clear tmr2 interrupt restore port_t btfsc PORTB,7 ; test if zero on pin 7 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 6-26 40-100-1

Chapter 6 Sample Programs for each Module 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 40-100-1 6-27

Chapter 6 Sample Programs for each Module 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 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 6-28 40-100-1

Chapter 6 Sample Programs for each Module 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 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 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 40-100-1 6-29

Chapter 6 Sample Programs for each Module movwf PORTB ; output higher 2 bits of result return sdelay movlw movwf H'96' delay_lo lo incfsz delay_lo,1 lo return END 6-30 40-100-1

Chapter 7 Solutions 7 Solutions 7.1 3D Models/Photos The following are three typical examples of the major components of the buggy kit. Single motor mechanical differential with single-wheel controlled steering. Single-motor mechanical differential with Ackermann steering. Dual-motor drive, giving the driving wheels independent control capable of steering, with caster wheel at other end. 40-100-1 7-1