I I PR
TE T
SI IS
H
VE
TI
NS
UN I
SI S
UNIV
I EN
E RS
T IN
I
AS
T
IS H
ST U
DIO RUM PR
Mechatronical Project
The development of the course in the frame of Master Studies Development Program,
is financed by the Austrian Development Cooperation through WUS-Austria, and co-
financed by the University of Prishtina
EXEMPLARS GRATIS
Implemented by
PRISHTINA
2011
2 Master Studies on Mechatronics - Course: Mechatronical Project
Master Studies on Mechatronics - Course: Mechatronical Project 3
________
Contents
________
Preface
Acknowledgments
Week 2:
Electronic Control Unit (ECU) 26
Microcomputer Systems 28
8051 Microcontroller Development Board 30
Software Development and IDE 31
Hello World 32
Week 3:
8051 Hardware Summary 35
Instruction Types 45
Addressing Modes 49
MIDE Integrated Development Environment (IDE) 50
Editor 51
Assembler 52
Simulator 55
SDCC compiler 56
Week 4:
Internal hardware resources of 8051 microcontroller 58
Timer Operation 63
Serial Port Operation 71
Interrupts 75
Multiprogramming 81
Operating-Systems 82
Process concept 85
Scheduling 87
Interprocess communications 89
Process coordination 90
Events, states and state-diagrams. 91
Week 7:
Position/speed proportionality control with logical outputs 108
Interfacing to Step-Motors 109
Unipolar Drivers 110
Bipolar Drivers 111
Full- and Half-Step Drive 113
Interfacing Scanner Step-Motor and its Slotted Opto-Coupler 117
Week 8:
Analog Inputs and Analog Outputs 120
Analog/Digital and Digital/Analog Converters 122
Voltage references 126
Communication Waveforms and Bit-Banging I/O Bits and/or Ports 127
Analog Position and Speed Sensor Interfacing 128
Command Signal Generation 131
Week 9:
Unipolar and Bipolar Analog Signals 134
Number Representation 134
Introduction to QET (Quanser Engineer Training) Control Plant 135
Interfacing Parallel D/A of QET to 8051 Board 137
Linear Power Amplifiers and DC-Motor Drive 140
Master Studies on Mechatronics - Course: Mechatronical Project 5
Week 10:
Interfacing Absolute and Incremental Encoders 142
Software Processing of Incremental Encoder Pulses 143
Special Dedicated Integrated Circuits for Counting Incremental
Encoder Pulses 150
Week 11:
Accepting/presenting Data from/to User 151
Human-Machine-Interface 153
Keyboard Interfacing and Programming 155
7-Segment Display Interfacing and Programming 156
LCD Interfacing and Programming 157
Control Performance of QET and PC Interface 158
Week 13:
More Advanced Control Algorithms 164
Mathematical Calculations on Microcontroller Systems 164
Proportional-Integral (PI) Controllers 165
Proportional-Derivative (PD) Controllers 165
PID Controllers 166
Digital Controllers and Digital Filtering 166
Real-time Control with PC 166
Real-Time-Operating System (RTOS) 167
Hardware-in-the-Loop (HIL) 167
Rapid Prototyping 167
MATLAB/Simulink/WinCon System 167
Week 14:
Pulse Drives (Switch-Mode) 168
Pulse-Width-Modulation (PWM) 168
Thyristors, Triacs and Phase-Control Drives 169
6 Master Studies on Mechatronics - Course: Mechatronical Project
Appendices
A1: Ink-jet printer internals with part description 186
A2: PLC Example 187
A3: Closed-Loop A/D - K=2 - D/A with analog reference
(from on-board potentiometer or external input) 188
A4: MODBUS Slave for 8051 197
. 209
Master Studies on Mechatronics - Course: Mechatronical Project 7
8 Master Studies on Mechatronics - Course: Mechatronical Project
MECHAnics + elecTRONICS
Definitions:
Yasakawa (1969):
The word, mechatronics, is compsed of mecha from mechanics and
the tronics from electronics. In other words, technologies and
developed products will be incorporating electronics more and more into
mechanisms, intimately and organically, and making it impossible to tell
where one ends and the other begins.
Bolton (1999):
A mechatronic system is not just a marriage of electrical and mechanical
systems and is more than just a control system; it is a complete
integration of all of them.
Master Studies on Mechatronics - Course: Mechatronical Project 9
- mechanical systems
- mechanical elements
- machines
- precision mechanics
- electronic systems
- microelectronics
- power electronics
- sensor and actuator technology
- information technology
- systems theory
- automation
- software engineering
- artificial intelligence
Master Studies on Mechatronics - Course: Mechatronical Project 13
Military - UAV
16 Master Studies on Mechatronics - Course: Mechatronical Project
Machines - CNC
Service - Robot-VacuumCleaner
Master Studies on Mechatronics - Course: Mechatronical Project 17
Xerox
And some with their internal parts that you can use to experiment
with:
HardDiskDrive-Exploded-Cutaway
20 Master Studies on Mechatronics - Course: Mechatronical Project
Floppy-Disk-Drives
CD-ROM Drive
Master Studies on Mechatronics - Course: Mechatronical Project 21
Flatbad Scanner
Laboratory Plants:
24 Master Studies on Mechatronics - Course: Mechatronical Project
Examination:
Homework 10 points
Assignments
Literature:
Assignment 1:
During two weeks students are expected to explore for different mechatronic
projects, and each student will submit one project idea individually that must
include following issues
- Sensing
- Actuating
- Real-time control
- Mechanical design
Due: Week 3
26 Master Studies on Mechatronics - Course: Mechatronical Project
ECU Implementation
- Dedicated Solution
- Single Board Computer
- PC Based
- PLC
28 Master Studies on Mechatronics - Course: Mechatronical Project
- Data-Bus
- Address-Bus
- Control-Lines
- Additional Peripherals
Master Studies on Mechatronics - Course: Mechatronical Project 29
Microcomputer Architecture
Step 1:
Download interated environment MIDE from http://www.opcube.com/
and install. MIDE-51 icon will be created.
Step 2:
Installing Driver for USB programmer (CD of Easy8051A with
Drivers). 8051FLASH icon will be created.
Step 3:
Open 8051FLASH and under "Device" select AT89S8253, under
"Device Frequency [MHz]" select 8.0.
Master Studies on Mechatronics - Course: Mechatronical Project 33
Step 4:
Connect Easy8051A board to USB port. Actions normal to connecting
new device to USB will happen and device should be recognised. When first
plugged green LED (POWER) should go on, after drivers are loaded yellow
LED (USB LINK) should go on too, signalling that communication has been
established.
Step 5:
Open MIDE-51 and start New document. Write following lines of
code:
Lines of code will be black. They will get colored after you save file with
known extension, *.a51.
Step 6:
Assemble code with Build->Build (or other equivalent methods). If
there are no errors, *.hex and *.lst files will be created.
Step 7:
Load *.hex on 8051FLASH, button "Load" under "FLASH".
Step 8:
Board programming is done with "Write" button of 8051FLASH. After
programmin board automatically goes in Run mode and program is executed.
Program writes binary value 10101010, or AA in hex, to Port 0. Result
should be seen on 8 LEDs of Port 0, with LED on if value of corresponding bit
is 0 and with LED off if value is 1:
Step 9:
Experimenting with different vales (#00000000B - #11111111B) and
different ports (P0 - P3).
Note:
DIP switches 1-4 of SW2 must be ON (PORTx LEDs On, x=0, 1, 2, 3).
Master Studies on Mechatronics - Course: Mechatronical Project 35
Program Memory
- 64 K byte space
- Read Only
- Accessed with PSEN_ - when External
- EA_=1 uses available internal Program Memory
- EA_=0 uses external Program Memory
Internal Data Memory
- 128 bytes on 8051
- 128 bytes of Special Function Registers space
- some of them non-existent
- may be used by 8051 derivatives
- additional 128 bytes on 8052 compatibles
External Data Memory
- 64 K byte space
- Read or Write
- Accessed with RD_ (Read) and WR_ (Write) signals
Master Studies on Mechatronics - Course: Mechatronical Project 41
- Oscillator components
- Reset circuit
- Latch for demultiplexing A7-A0 and D7-D0
- Serial RS232 level-translators
Master Studies on Mechatronics - Course: Mechatronical Project 43
- External RAM
- Program Memory
Arithmetic Instructions
46 Master Studies on Mechatronics - Course: Mechatronical Project
Master Studies on Mechatronics - Course: Mechatronical Project 47
Logical Instructions
48 Master Studies on Mechatronics - Course: Mechatronical Project
Boolean Instructions
Jump Instructions
- Unconditional Instructions
- Conditional Instructions
Master Studies on Mechatronics - Course: Mechatronical Project 49
Feature on MIDE-51
Syntax highlighter on ASEM-51 reserved word & addition register on
selected device (devices listed on ASEM51/MCU folder)
Syntax highlighter on SDCC reserved word & MCS-51 standard
register
Support multi document workspace
Support standard editor feature and shortcut key such as Cut , Copy,
Paste, Find, Replace and Windows tile & cascade
Editor font style and size selectable
Save recent file(s) opened in list
Shortcut to ASEM-51 html manual
Shortcut to SDCC html & PDF manual (search file on SDCC/DOC)
Report assembler & compiler message
Support drag and drop file from explorer.
Automatic save last windows position.
Support wheel mouse
Bookmark code position upto 10
Show/Hide line number on editor
Master Studies on Mechatronics - Course: Mechatronical Project 51
03.4.1 Editor
Step 1:
Step 2:
Step 3:
Save file with *.a51 (or *.asm), and lines of code will be colored when
syntax is recognized. Different color for each field of Assembly language
program.
52 Master Studies on Mechatronics - Course: Mechatronical Project
03.4.2 Assembler
Label Field
- label: - represents an address, ends with colon (:)
- symbol - may be addresses, data constants, etc.
- must begin with letter, question mark (?), or undersore (_)
- must follow by letter, number, question mark (?), or undersore (_)
- reserved words (mnemonics, operators, etc.) may not be used
Mnemonic Field
- assembler directive - e.g., ORG, EQU, DB, etc.
- instruction mnemonic - e.g., MOV, ADD, DIV, INC, etc.
Operand Field
- follows the mnemonic field
- must begin on same line as the mnemonic field
- some operations have no operand (e.g., the RET instruction)
- multiple operands are separated by coma
Master Studies on Mechatronics - Course: Mechatronical Project 53
Comment Field
- begins with semicolon (;)
- Remarks to clarify the program
ORG 0000H
*
*
*
program lines
*
*
*
END
54 Master Studies on Mechatronics - Course: Mechatronical Project
If there are no errors, the message "no errors" is printed at Output message
window. It also prints that corresponding *.hex file is generated.
This file will be used for programming the microcontroller. Additional file
generated is the one with extension lst, *.lst.
*.hex file
*.lst file
Master Studies on Mechatronics - Course: Mechatronical Project 55
03.4.3 Simulator
During development phase, programs can be run and tested, even without
hardware, on simulator. Simulator is very practical, since it is possible to see
all internal data and how they change. Programs can be executed instruction-
by-instruction (Single-Step) and check program flow.
Simulator can be started from MIDE with BuildBuild and Sim
Simulator Screen
When Edited file is saved with *.C extension, coloring is done ccording to C
syntax. Building procedure is same as was with assembler.
MIDE with C source, and Output message that *.hex had been generated after
program Build.
One can always check assembler source generated after compilation, *.asm,
and corresponding *.lst file.
Assignment 2:
Due: Week 5
Due: Week 4
58 Master Studies on Mechatronics - Course: Mechatronical Project
TMOD Register
TCON Register
Master Studies on Mechatronics - Course: Mechatronical Project 65
Programming example:
68 Master Studies on Mechatronics - Course: Mechatronical Project
Programming example:
Note:
70 Master Studies on Mechatronics - Course: Mechatronical Project
Counter
Programming example:
Master Studies on Mechatronics - Course: Mechatronical Project 71
8051 to PC Communication
SCON Register
Master Studies on Mechatronics - Course: Mechatronical Project 73
04.4 Interrupts
Interrupt Sources
76 Master Studies on Mechatronics - Course: Mechatronical Project
Interrupt Vectors
Sructured programming
- technique for organizing and coding programs
- reduces complexity
- improves clearity
- fascilitates debugging and modifying
- all programs may be written using only three structures
- statements
- loops
- choices
- each structure has one entry point and one exit point
Decision block
Process block
Input block
Output block
Program terminator
Off-page connector
Pre-defined process
(subroutine)
80 Master Studies on Mechatronics - Course: Mechatronical Project
05.1.2 Multiprogramming
Implementations of concurrency
- shared memory
- multiprogramming (concurrency is simulated)
- time-sharing (or multitasking)
- multiprocessing (true parallelism as in distributed proc.)
- distributed processing
82 Master Studies on Mechatronics - Course: Mechatronical Project
Definition:
What is a Process?
Process States
86 Master Studies on Mechatronics - Course: Mechatronical Project
05.2.2 Scheduling
- Measures for response time and turn around are user centered
parameters.
- independent processes
- cannot effect other processes
- cannot be effected by other processes
- does not share data with any process
- cooperating processes
- can effect other processes
- cant be effected by other processes
- shares data with other processes
- Information sharing
- Computation speedup
- Modularity
- Convenience
Race condition
- several processes access and manipulate the same data concurrently
- guard against race condition is needed
- requirement for process synchronization
- Peterson's Solution
- Synchronization Hardware
- lock
-Semaphores
- wait( )
-signal( )
- Monitors
- Atomic Transactions
- Deadlocks
Master Studies on Mechatronics - Course: Mechatronical Project 91
Event-driven application
- Statless
- Stateful
- States
Actions
- Entry action
- During action
- Exit action
Transitions
- Events
- Conditions
Triggering state-diagrams
- Sampling
- Events
- One state-diagram to drive activity of another
92 Master Studies on Mechatronics - Course: Mechatronical Project
S00 / Inic
S01 S02
P0.2="L" / f_P1.1=2Hz
Each state has its row on tabproc1, 3-bytes for ljmp p1_Sxx and one for nop.
94 Master Studies on Mechatronics - Course: Mechatronical Project
Every call of StateScan will switch to start address of current state, p1_S.
Transition Conditions i/j will be tested, one by one.
First one fulfilled will continue with Transition Actions:
- Exit Action i
- Entry Action j
State processing ends.
This is usual connection for using port pins as current sink. Sinking is done by
internal transistor, and can sink several mA.
Source confiuration is not approprite for connectin loads, since internal pull-up
resistence tens to hundreds of kilooms.
For driving more powerful load, drivers are needed. We may use transistors or
Interated Circuits.
Voltage regulator +5V (7805) for logic circuit and power-on RESET generator
(TL7705A).
06.3.2 Scan-Cycle
Graphical languages
- Ladder Diagrams (LD)
- Function Block Diagram (FBD)
Text-based languages
- Instruction List (IL)
- Structured Text (ST)
When signals are filtered with appropriate low-pass filter, we will have
proportional analog signal. Not always we have to use filter, because most
processes used to control by nature have low-pass filtering properties.
Special types of actuators are step-motors that are controlled by logical signals
and give as output proportional position.
Low-Pass
Filter
Master Studies on Mechatronics - Course: Mechatronical Project 109
Types of Step-motors
Different wirings
110 Master Studies on Mechatronics - Course: Mechatronical Project
Half-step mode
Wave-drive
STK6713BMK4 Drive
Master Studies on Mechatronics - Course: Mechatronical Project 117
Four control signals are connected to P1.7, P1.6, P1.5 and P1.4.
Sensor state is read at P1.0.
org 0
mov p1,#00000001b
loop:
mov p0,p1
setb sma
lcall delay
setb smb
lcall delay
clr sma
lcall delay
setb smac
lcall delay
clr smb
lcall delay
setb smbc
lcall delay
clr smac
lcall delay
setb sma
lcall delay
clr smbc
sjmp loop
Master Studies on Mechatronics - Course: Mechatronical Project 119
end
120 Master Studies on Mechatronics - Course: Mechatronical Project
DIP switches SW1 must be set according to the figure in oder to use analog
inputs. They connect control lines of analog-to-digital converter (A/D) with
I/O port lines of microcontroller.
Master Studies on Mechatronics - Course: Mechatronical Project 121
Signals SPI-SCK and SPI-MOSI are the same for A/D and D/A.
Pin configuration
124 Master Studies on Mechatronics - Course: Mechatronical Project
Pin configuration
Master Studies on Mechatronics - Course: Mechatronical Project 125
Write command
126 Master Studies on Mechatronics - Course: Mechatronical Project
CLR P1.0
CLR P1.2
SETB P1.1
CLR P1.1
SETB P1.2
SETB P1.1
CLR P1.1
VTG
TG
GND
Loop:
clr cs
clr clk
setb clk
clr clk
mov p2.4,c
setb clk
clr clk
setb cs
nop
nop
nop
nop
LJMP Loop
end
Master Studies on Mechatronics - Course: Mechatronical Project 131
Loop:
mov DAh,#01111000b
mov DAl,#00000000b
clr cs
clr clk
mov a,DAh
mov a,DAl
setb cs
SJMP Loop
end
134 Master Studies on Mechatronics - Course: Mechatronical Project
The maximum (Vmax) and minimum (Vmin) voltages are a function of the
control hardware. Some of standard ranges are:
0V to 5V
0V to 10V
-2.5V to 2.5V
-5V to 5V
-10V to 10V
Computer represent all numbers, other than integers and some fractions
with imprecision.
Numbers are stored in some approximation which can be represented
by a fixed number of bits or bytes.
There are different types of representations or data types.
Data types vary in number of bits used (word length) and
representation - fixed point (int or long) or floating point (float or
double) format.
External connections
136 Master Studies on Mechatronics - Course: Mechatronical Project
DCMCT schematic
The experiments:
- Modeling
- Speed control
- Robustness
- Position control
- Haptic interaction
Master Studies on Mechatronics - Course: Mechatronical Project 137
A0 equ p1.4
A1 equ p1.5
A0_139 equ p1.6
A1_139 equ p1.7
QET:
mov 50h,#0
QETloop:
mov r5,#50
Qloop5: mov r6,#100
Qloop6: mov r7,#100
Qloop7: djnz r7,Qloop7
djnz r6,Qloop6
djnz r5,Qloop5
cpl p3.4
;clr switch
inc 50h
ljmp QETloop
ret
140 Master Studies on Mechatronics - Course: Mechatronical Project
org 0
ljmp main
org 000bh
ljmp t0isr
144 Master Studies on Mechatronics - Course: Mechatronical Project
reti
mov p1_gj,#0
mov p2_gj,#0
mov p2old,#0
mov p2new,#0
clr p2.7
loop:
mov a,encB0
lcall n_to_7seg
mov 30h,a
mov a,encB1
lcall n_to_7seg
mov 31h,a
mov a,encB2
lcall n_to_7seg
mov 32h,a
mov a,encB3
lcall n_to_7seg
mov 33h,a
sjmp loop
Master Studies on Mechatronics - Course: Mechatronical Project 145
delay25:
mov r5,#1
loop5: mov r6,#4
loop6: mov r7,#250
loop7: djnz r7,loop7
djnz r6,loop6
djnz r5,loop5
ret
dispscan:
mov a,p1_gj
rl a
rl a
mov dptr,#tabproc1
jmp @a+dptr
tabproc1:
ljmp p1_gj00
nop
ljmp p1_gj01
nop
ljmp p1_gj02
nop
ljmp p1_gj03
nop
p1_gj00:
anl p1,#11110000b
mov p0,30h ;#00111111b ;0 ne disp0
setb p1.0
mov p1_gj,#1
ljmp p1_end
p1_gj01:
anl p1,#11110000b
mov p0,31h ;#00000110b ;1 ne disp1
setb p1.1
mov p1_gj,#2
ljmp p1_end
p1_gj02:
anl p1,#11110000b
mov p0,32h ;#01011011b ;2 ne disp2
setb p1.2
mov p1_gj,#3
ljmp p1_end
p1_gj03:
anl p1,#11110000b
mov p0,33h ;#01001111b ;3 ne disp3
setb p1.3
mov p1_gj,#0
ljmp p1_end
146 Master Studies on Mechatronics - Course: Mechatronical Project
p1_end:
ret
encscan:
;ENC AB Filter
mov 20h,p2
mov c,20h.1 ;encA
mov a,ADB
rrc a
mov ADB,a
anl a,#01110000b
cjne a,#01110000b,anot1
;A=1
setb 21h.0
sjmp aend
anot1: cjne a,#00000000b,anot0
;A=0
clr 21h.0
anot0:
aend:
mov c,20h.2 ;encB
mov a,BDB
rrc a
mov BDB,a
anl a,#01110000b
cjne a,#01110000b,bnot1
;B=1
setb 21h.1
sjmp bend
bnot1: cjne a,#00000000b,bnot0
;B=0
clr 21h.1
bnot0:
bend:
;mov a,p2
;anl a,#00000110b
;rr a
mov a,21h
anl a,#00000011b
mov p2new,a
mov a,p2old
rl a
rl a
orl a,p2new
mov p2old,p2new
; mov a,#0
;mov a,p2_gj
rl a
rl a
Master Studies on Mechatronics - Course: Mechatronical Project 147
mov dptr,#tabproc2
jmp @a+dptr
tabproc2:
ljmp p2_gj00
nop
ljmp p2_gj01
nop
ljmp p2_gj02
nop
ljmp p2_gj03
nop
ljmp p2_gj04
nop
ljmp p2_gj05
nop
ljmp p2_gj06
nop
ljmp p2_gj07
nop
ljmp p2_gj08
nop
ljmp p2_gj09
nop
ljmp p2_gj10
nop
ljmp p2_gj11
nop
ljmp p2_gj12
nop
ljmp p2_gj13
nop
ljmp p2_gj14
nop
ljmp p2_gj15
nop
p2_gj00:
ljmp p2_end
p2_gj01:
;dec p3
lcall encdec
ljmp p2_end
p2_gj02:
;inc p3
lcall encinc
ljmp p2_end
p2_gj03:
cpl p1.7
ljmp p2_end
p2_gj04:
148 Master Studies on Mechatronics - Course: Mechatronical Project
;inc p3
lcall encinc
ljmp p2_end
p2_gj05:
ljmp p2_end
p2_gj06:
cpl p1.7
ljmp p2_end
p2_gj07:
;dec p3
lcall encdec
ljmp p2_end
p2_gj08:
;dec p3
lcall encdec
ljmp p2_end
p2_gj09:
cpl p1.7
ljmp p2_end
p2_gj10:
ljmp p2_end
p2_gj11:
;inc p3
lcall encinc
ljmp p2_end
p2_gj12:
cpl p1.7
ljmp p2_end
p2_gj13:
;inc p3
lcall encinc
ljmp p2_end
p2_gj14:
;dec p3
lcall encdec
ljmp p2_end
p2_gj15:
ljmp p2_end
p2_end:
ret
Master Studies on Mechatronics - Course: Mechatronical Project 149
encinc:
inc encB0
mov a,encB0
cjne a,#10,eiend
mov encB0,#0
inc encB1
mov a,encB1
cjne a,#10,eiend
mov encB1,#0
inc encB2
mov a,encB2
cjne a,#10,eiend
mov encB2,#0
inc encB3
mov a,encB3
cjne a,#10,eiend
mov encB3,#0
eiend:
ret
encdec:
dec encB0
mov a,encB0
cjne a,#0ffH,edend
mov encB0,#9
dec encB1
mov a,encB1
cjne a,#0ffH,edend
mov encB1,#9
dec encB2
mov a,encB2
cjne a,#0ffH,edend
mov encB2,#9
dec encB3
mov a,encB3
cjne a,#0ffH,edend
mov encB3,#9
edend:
ret
n_to_7seg:
inc a
movc a,@a+pc
ret
n7segtab:
db 00111111b ;0
db 00000110b ;1
db 01011011b ;2
db 01001111b ;3
db 01100110b ;4
db 01101101b ;5
db 01111100b ;6
db 00000111b ;7
150 Master Studies on Mechatronics - Course: Mechatronical Project
db 01111111b ;8
db 01100111b ;9
sjmp $
end
Pushbutton switches
Port LEDs
To control the HMI from a PLC the user inputs set bits in the PLC memory,
and other bits in the PLC memory can be set to turn on/off items on the HMI
screen.
154 Master Studies on Mechatronics - Course: Mechatronical Project
loop:
anl p1,#11110000b
mov p0,#00111111b ;0 ne disp0
setb p1.0
lcall delay25
anl p1,#11110000b
mov p0,#00000110b ;1 ne disp1
setb p1.1
lcall delay25
Master Studies on Mechatronics - Course: Mechatronical Project 157
anl p1,#11110000b
mov p0,#01011011b ;2 ne disp2
setb p1.2
lcall delay25
anl p1,#11110000b
mov p0,#01001111b ;3 ne disp3
setb p1.3
lcall delay25
sjmp loop
delay25:
mov r5,#1
loop5: mov r6,#4
loop6: mov r7,#250
loop7: djnz r7,loop7
djnz r6,loop6
djnz r5,loop5
ret
end
org 0
ljmp main
org 000bh
ljmp t0isr
160 Master Studies on Mechatronics - Course: Mechatronical Project
end
1
G C s
1 sRC
Master Studies on Mechatronics - Course: Mechatronical Project 161
U(z) = K E(z)
MOV R7,04H ; R7 = R4
MOV R6,05H ; R6 = R5
1
ess
1 K
Assignment 3:
Select one system and write short paper about hardware construction, types of
step-motors, elekctronics for step-motor drive, and for control software. What
types of data format if can accept?
Due: Week 13
164 Master Studies on Mechatronics - Course: Mechatronical Project
- software implementation
- hardware implementation (DSP, pentium)
Master Studies on Mechatronics - Course: Mechatronical Project 165
t
1
u t K P et et dt
Ti 0
det
u t K P et Td
dt
166 Master Studies on Mechatronics - Course: Mechatronical Project
t
1 det
u t K P et et dt Td
Ti 0 dt
K PT
u kT u kT T K P ekT ekT T ekT
TI
K P TD
ekT 2ekT T ekT 2T
T
13.6 Digital Controllers and Digital Filtering
- DOS
- QuaRC
- Bipolar transistors
- MOSFETs
- IGBTs
- Thyristors
- Triacs
- State on (saturation)
- State off
Current limiting
Stepping motors are often run at voltages higher than their rated voltage.
Although this is not necessarily the case for very small stepper motors, high
torque stepper motors need to run at higher voltages in order for the motor to
reach its full potential. Increasing the voltage supplied to a motor increases the
rate at which current rises in the windings of the motor. The more responsive
current in the windings, the greater the torque and speed characteristics of the
motor.
Chopper Control
Chopper control is a way to limit the current in the winding of a stepping
motor when using a high voltage supply (a voltage higher than a motors rated
voltage). This method is very suitable when using a microcontroller for
stepping motor control because limited additional microcontroller resources
are required. The basic idea behind chopper control is to use a high voltage
source to bring the current in the winding of a stepping motor up to IMAX
very quickly. When IMAX is reached the voltage is chopped or switched off.
CHOPPER CIRCUIT
Low performance stepper motors are purposefully built with a high internal
resistance so that they can be run on a typical voltage source, usually 5V to
12V. These motors do not produce much torque for their size, nor do they spin
very fast. They can be run without current limiting considerations as long as
they are driven at their rated voltage. However, higher motor performance
(speed and torque) can be achieved by running these motors at higher voltages.
High torque stepping motors are a different story all together. These motors
have very small internal resistances and run at high currents. An example of a
high performance motor would be a motor with the specifications shown in
Table. The statement of holding torque in Table is typical of many motor
manufacturers, using kilograms as a measure of force. This must be converted
to dyne-centimeters or newtonmeters before doing any physical calculations.
174 Master Studies on Mechatronics - Course: Mechatronical Project
windings will not be the perfect arithmetic sum of the torques with just one or
the other winding powered.
The desired motion of a stepper motor is linear. This means the steps should
be equal in size with no noticeable acceleration or deceleration of the shaft as
the motor turns. Good microstepping implementations strive to get as near to
this linear motion as possible.
Star topology
Ring topology
Fieldbus network
MODBUS - Master/Slave
- RTU (Remote Terminal Unit)
- ASCII (American Standard Code for Information Interchange)
184 Master Studies on Mechatronics - Course: Mechatronical Project
Appendices
ORG 0030H
MAIN: MOV 24H,#00H ;PII
MOV 25H,#00H ;
CycleBeg:
;Read Inputs -> PII
CLR P3.4
MOV 24H,P1
SETB P3.4
CLR P3.5
MOV 25H,P1
SETB P3.5
;Program Scan
MOV C,24H.0
ORL C,28H.0
ANL C,/24H.1
ANL C,/24H.4
ORL C,24H.2
MOV 28H.0,C
;System functions
LJMP CycleBeg
END
188 Master Studies on Mechatronics - Course: Mechatronical Project
A3: Closed Loop A/D - K=2 - D/A with analog reference (from
on-board potentiometer or external input)
org 0
Loop:
;mov REFh,#00001000b
;mov REFl,#00000000b
mov REFh,ADh2
mov REFl,ADl2
clr c
mov a,REFl
subb a,ADl3
mov DAl,a
mov a,REFh
subb a,ADh3
mov DAh,a
jnc anneg
mov DAh,#00000000b
mov DAl,#00000000b
sjmp apos
anneg: clr c
mov a,DAl
rlc a
mov DAl,a
mov a,DAh
rlc a
mov DAh,a
mov a,DAh
Master Studies on Mechatronics - Course: Mechatronical Project 189
anl a,#11110000b
jz apos
mov DAh,#00001111b
mov DAl,#11111111b
apos:
;mov DAh,#01111000b
;mov DAl,#00100000b
mov a,DAh
cpl a
mov p0,a
mov a,DAl
cpl a
mov p2,a
mov a,DAh
anl a,#00001111b
orl a,#01110000b
clr csDA
clr clk
;mov a,DAh
mov a,DAl
setb csDA
nop
nop
nop
nop
clr cs
clr clk
setb clk
clr clk
setb clk
clr clk
mov a,#0
mov ADh3,a
rlc a
setb clk
clr clk
mov ADl3,A
setb cs
clr cs
clr clk
194 Master Studies on Mechatronics - Course: Mechatronical Project
setb clk
clr clk
mov a,#0
clr clk
mov ADh2,a
;mov p2.2,c
rlc a
setb clk
clr clk
mov ADl2,A
setb cs
; mov a,ADh
; cpl a
; mov p0,a
; mov a,ADl
; cpl a
; mov p2,a
nop
nop
nop
nop
; mov r7,#10
;del07: mov r6,#100
del06: mov r5,#100
del05: djnz r5,del05
; djnz r6,del06
; djnz r7,del07
LJMP Loop
end
Master Studies on Mechatronics - Course: Mechatronical Project 197
ORG 0
LJMP INIT
ORG 0BH
TMR0 EQU $ ;vektori i tajmerit 0
LJMP TMR0VEC
ORG 23H
SERINT EQU $ ;vektori i interaptit serik I/O
LJMP SERVEC
ORG 30H
INIT:
MOV SP,#30H
MOV R3,#01H ;regjistri i gjendjes
MOV R4,#00H ;fshihet numruesi i bafferit te
memories eksterne per shkrim
MOV SCON,#50H ;TI=1, RI=0, REN=1
MOV TMOD,#21H ;TIMER1=MOD2-AUTORELOAD,TIMER0=MOD1-
16BIT
MOV PCON,#00H ;SINGLE BAUD RATE
MOV TH1,#0FDH ;9600 BAUD, #0xF3=>1200 BAUD (12 MHz)
SETB TR1 ;startohet timeri per portin serik
MOV TH0,#HIGH t35
MOV TL0,#LOW t35
SETB TR0
MOV IE,#92H ;lejohet interapti i tajmerit0 dhe i
portit serik
MOV 20H,#00H
SJMP $
;********************************
;INTERAPTI I TAJMERIT
198 Master Studies on Mechatronics - Course: Mechatronical Project
TMR0VEC:
CLR TR0 ;ndalet tajmeri
CJNE R3,#01H,TOJO1
MOV R3,#02H
RETI
TOJO1:
TOJO4: RETI
;********************************
;RX INTERAPTI
SERVEC: JB RI,RX
MOV A,R4 ;rutina per dergim
CJNE A,02H,NEXTCH ;r2=02h
MOV R3,#02H
MOV R4,#00H
CLR TI
RETI
NEXTCH:
MOV DPH,#40H
MOV DPL,R2
MOVX A,@DPTR
MOV SBUF,A
INC R2
CLR TI
RETI
RX: CLR RI
CJNE R3,#01H,RXJO1
CLR TR0
MOV TH0,#HIGH t35
MOV TL0,#LOW t35
SETB TR0
RETI
;fshirja e bafferit
; MOV R4,#00H
; RETI
RXJO4: RETI
;*******************************
;ANALIZA
ANALIZA:
MOV A,R4
MOV R7,A ;R7=R4 por ndyshon ne 0 gjate kalkulimit
teCRC
DEC R7 ;zvoglojme per 2(e hjekim crc nga
mesazhi)
DEC R7
CALL CALC_CRC
;krahsimi i crc
MOV DPH,#40H
MOV DPL,R7
MOVX A,@DPTR
CJNE A,CRC_ACCUM_LOW,ANALEND ;shkon ne analend nese crc
gabim
INC DPTR
MOVX A,@DPTR
CJNE A,CRC_ACCUM_HI,ANALEND ;shkon ne analend nese crc
gabim
;fundi i krahasimit (CRC-OK)
;krahasimi i adreses
MOV DPH,#40H
MOV DPL,#00H
MOVX A,@DPTR
CJNE A,#01H,ANALEND
JMP FUNKS
ANALEND:
200 Master Studies on Mechatronics - Course: Mechatronical Project
JMP ANALEND2
;fundi i krahasimit(AdresaOK)
;*******************************
;FUNKSIONI
FUNKS: MOV DPL,#01H
MOVX A,@DPTR
CJNE A,#1,JOFO1
FO1:
INC DPTR
MOVX A,@DPTR
JZ FO1_01
JMP STADDERR
FO1_01: INC DPTR
MOVX A,@DPTR
MOV R6,A
CLR C
SUBB A,#COILNUM ;numri total i coilave=coilnum
JC FO1_02
JMP STADDERR
FO1_02: INC DPTR
MOVX A,@DPTR
JZ FO1_03
JMP VALERR
FO1_03: INC DPTR
MOVX A,@DPTR
MOV R5,A
DEC A
ADD A,R6
CLR C
SUBB A,#COILNUM ;numri total i coilave=coilnum
JC FO1_04
JMP VALERR
FO1_04: MOV R4,#02H
CALL READCOIL
JMP SENDMSG
MOVX A,@DPTR
JZ FO3_03
JMP VALERR
FO3_03: INC DPTR
MOVX A,@DPTR
MOV R5,A
DEC A
ADD A,R6
CLR C
SUBB A,#HREGNUM ;numri total i regjistrave
JC FO3_04
JMP VALERR
FO3_04: MOV R4,#02H
CALL READREG
JMP SENDMSG
MOVX A,@DPTR
MOV R6,A
CLR C
SUBB A,#HREGNUM ;numri total i regjistrave=hregnum
JNC FO6_E1
INC DPTR
CALL PRESREG
JMP SENDMSG1
FO6_E1: JMP STADDERR
INC DPTR
MOVX A,@DPTR
JZ FO16_01
JMP STADDERR
FO16_01:INC DPTR
MOVX A,@DPTR
MOV R6,A
CLR C
SUBB A,#HREGNUM ;numri total i regjistrave
JC FO16_02
JMP STADDERR
Master Studies on Mechatronics - Course: Mechatronical Project 203
FO16_02:INC DPTR
MOVX A,@DPTR
JZ FO16_03
JMP VALERR
FO16_03:INC DPTR
MOVX A,@DPTR
MOV R5,A
DEC A
ADD A,R6
CLR C
SUBB A,#HREGNUM ;numri total i regjistrave
JC FO16_04
JMP VALERR
FO16_04:MOV R4,#06H
CALL PRESMREG
JMP SENDMSG
JOFO16:
FUNCERR:
MOV DPTR,#4001H
MOVX A,@DPTR
SETB ACC.7
MOVX @DPTR,A
INC DPTR
MOV A,#01H
MOVX @DPTR,A
MOV R4,#03H
JMP SENDMSG
ANALEND2:
VALERR:
MOV DPTR,#4001H
MOVX A,@DPTR
SETB ACC.7
MOVX @DPTR,A
INC DPTR
MOV A,#03H
MOVX @DPTR,A
MOV R4,#03H
JMP SENDMSG
STADDERR:
MOV DPTR,#4001H
MOVX A,@DPTR
SETB ACC.7
MOVX @DPTR,A
INC DPTR
MOV A,#02H
MOVX @DPTR,A
MOV R4,#03H
204 Master Studies on Mechatronics - Course: Mechatronical Project
JMP SENDMSG
SENDMSG:MOV DPH,#40H
MOV DPL,R4
MOV 07H,R4
CALL CALC_CRC
MOV A,0DH
MOVX @DPTR,A
INC DPTR
MOV A,0EH
MOVX @DPTR,A
INC R4
INC R4
SENDMSG1: ;reply i njejte tx=rx pa llogaritje
MOV R2,#0 ;te CRC-se
SETB TI
RET
CALC_CRC:
;kalkulon crc ne karakteret e shkruar ne memorie extprej4000h dhe
rezulratin
;e vendose ne 0D,0E praR5,R6banka1
;perdore R4=numri char te pranuar,R7=R4=DPL pastaj behet 0 dhe R6=8bit
ne bajt
MOV CRC_ACCUM_HI,A
CC30:
DJNZ R6,CC20 ;perseritet tete here
INC DPTR ;gati per bajtin e ardhshem te
mesazhit
DJNZ R7,CC10 ;nje bajt me pak per te
llogarite
pop 07H
RET
;***************************************
;Set coil rutina
;Coili qe setohet ne R6
SETCOIL:
MOV A,R6
ANL A,#07H
INC A
MOV R1,A
MOV A,#00H
SETB C
SC_00: RLC A
DJNZ R1,SC_00
MOV R1,A
MOV A,R6
ANL A,#0F8H
RR A
RR A
RR A
ADD A,#20H
MOV R0,A
MOV A,@R0
ORL A,R1
MOV @R0,A
MOV A,20H ;Bartja e coilave ne portin P1
; CPL A ;Percaktimi i logjikes 1/0,
on/off
MOV P1,A
RET
RESCOIL:
MOV A,R6
ANL A,#07H
INC A
MOV R1,A
MOV A,#00H
SETB C
RC_00: RLC A
DJNZ R1,RC_00
CPL A
MOV R1,A
MOV A,R6
ANL A,#0F8H
206 Master Studies on Mechatronics - Course: Mechatronical Project
RR A
RR A
RR A
ADD A,#20H
MOV R0,A
MOV A,@R0
ANL A,R1
MOV @R0,A
MOV A,20H ;Bartja e coilave ne portin P1
; CPL A ;Percaktimi i logjikes 1/0,
on/off
MOV P1,A
RET
;Testbit:
;gjendja e bitit qe testohet bartet ne carry C,
;R6 permban adresen e bitit qe testohet(0-127)
TESTBIT:
MOV DPTR,#3F00H
MOV A,#72H ;kodi i ORL C,BIT
MOVX @DPTR,A
INC DPL
MOV A,R6 ;parametri BIT i ORL me lart
MOVX @DPTR,A
INC DPL
MOV A,#22H ;kodi i RET
MOVX @DPTR,A
CLR C
JMP 3F00H
READCOIL:
MOV A,R5
DEC A
ANL A,#0F8H
RR A
RR A
RR A
INC A
MOV DPH,#40H
MOV DPL,R4
MOVX @DPTR,A
INC R4
CLR C
MOV A,R5
TB_03: SUBB A,#9
JNC TB_01
MOV A,#00H
PUSH ACC
SJMP TB_02
TB_01: INC A
PUSH ACC
MOV R5,#8
TB_02: MOV R7,#00H
MOV A,R5
ADD A,R6
MOV R6,A
PUSH 06H
TB_00: DEC R6
CALL TESTBIT
MOV A,R7
RLC A
MOV R7,A
DJNZ R5,TB_00
POP 06H
MOV DPH,#40H
MOV DPL,R4
MOV A,R7
MOVX @DPTR,A
INC R4
POP ACC
JNZ TB_03
RET
;readreg
;leximi i regjistrave
;R6 start adress,R5 count
READREG:
MOV 20H,P1
MOV A,R5
RL A
MOV DPH,#40H
MOV DPL,R4
MOVX @DPTR,A
INC DPL
INC R4
208 Master Studies on Mechatronics - Course: Mechatronical Project
MOV A,R6
ADD A,#20H
MOV R0,A
RR_00: INC 00H
MOV A,@R0
MOVX @DPTR,A
INC DPL
INC R4
DEC 00H
MOV A,@R0
MOVX @DPTR,A
INC DPL
INC R4
INC 00H
INC 00H
DJNZ R5,RR_00
RET
;presreg=preset register
;shkruarja e regjistrave
;R6 start adress,R5 count
PRESMREG:
MOV DPH,#40H
MOV DPL,R4
INC DPL
MOV A,R6
ADD A,#20H
MOV R0,A
PMR_00: INC 00H
MOVX A,@DPTR
MOV @R0,A
INC DPL
DEC 00H
MOVX A,@DPTR
MOV @R0,A
INC DPL
INC 00H
INC 00H
DJNZ R5,PMR_00
MOV A,20H
; CPL A ;Percaktimi i logjikes 1/0,
on/off
MOV P1,A ;Bartja e regjistrit ne portin
P1
RET
;Forcebit:
;carry C bartet ne bitin e adresuar
;R6 permban adresen e bitit qe vendoset(0-127)
FORCEBIT:
JC FB_00
MOV DPTR,#3F00H
MOV A,#0C2H ;kodi i CLR BIT
MOVX @DPTR,A
Master Studies on Mechatronics - Course: Mechatronical Project 209
INC DPL
MOV A,R6 ;parametri BIT i ORL me lart
MOVX @DPTR,A
INC DPL
MOV A,#22H ;kodi i RET
MOVX @DPTR,A
JMP 3F00H
FB_00: MOV DPTR,#3F00H
MOV A,#0D2H ;kodi i SET BIT
MOVX @DPTR,A
INC DPL
MOV A,R6 ;parametri BIT i ORL me lart
MOVX @DPTR,A
INC DPL
MOV A,#22H ;kodi i RET
MOVX @DPTR,A
JMP 3F00H
FOMCOIL:
MOV A,R5
FMC_03: SUBB A,#9
JNC FMC_01
MOV A,#00H
PUSH ACC
SJMP FMC_02
FMC_01: INC A
PUSH ACC
MOV R5,#8
FMC_02: INC R4
MOV DPH,#40H
MOV DPL,R4
MOVX A,@DPTR
MOV R7,A
FMC_00: MOV A,R7
RRC A
MOV R7,A
CALL FORCEBIT
INC R6
DJNZ R5,FMC_00
POP ACC
JNZ FMC_03
MOV A,20H
; CPL A ;Percaktimi i logjikes 1/0, on/off
MOV P1,A ;Bartja e coilave ne portin P1
RET
END