69
Microcontroller Based System Design Module 5 Prepared by Emil Raj,Dept. Of ECE,MLMCE

Microcontroller Based System Design - 5

Embed Size (px)

DESCRIPTION

http://www.facebook.com/EmbeddedProjects

Citation preview

Page 1: Microcontroller Based System Design - 5

Microcontroller Based System Design

Module 5

.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 2: Microcontroller Based System Design - 5

Module 5

• Matrix key board interface - AT keyboard – commands – keyboard response codes -

watch dog timers - DS1232 watch dog timer – real time clocks – DS1302 RTC –

interfacing - measurement of frequency - phase angle - power factor – stepper

motor interface - dc motor speed control – L293 motor driver - design of a position

control system - interfacing programs using C and assembly language.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 3: Microcontroller Based System Design - 5

Matrix Keyboard Interfacing

• Organized in a matrix of rows and columns.

• The CPU accesses both rows and columns through ports;

• With two 8-bit ports, an 8 x 8 matrix of keys can be connected to a • With two 8-bit ports, an 8 x 8 matrix of keys can be connected to a

microprocessor.

• When a key is pressed, a row and a column make a contact;

• Otherwise, there is no connection between rows and columns

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 4: Microcontroller Based System Design - 5

Keyboard Interfacing Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 5: Microcontroller Based System Design - 5

Scanning and identifying the key• 4 x 4 matrix connected to two ports.

• Rows are connected to an output port

• Columns are selected to an input port.

• If no key has been pressed, reading the input port will yield 1s for all

columns since they are connected to high (Vcc).columns since they are connected to high (Vcc).

• If all the rows are grounded and a key is pressed, one of the columns will

have 0 since the key pressed provides the path to ground.

• It is the function of the microcontroller to scan the keyboard continuously

to detect and identify the key pressed.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 6: Microcontroller Based System Design - 5

• To detect a pressed key, the microcontroller grounds all rows by providing 0

to the output latch, then it reads the columns.

• If the data read from the columns is D3 – DO = 1111, no key has been

pressed and the process continues until a key press is detected.

Scanning and identifying the key

pressed and the process continues until a key press is detected.

• If one of the column bits has a zero, this means that a key press has

occurred.

• For example, if D3 - DO = 1101, this means that a key in the Dl column has

been pressed.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 7: Microcontroller Based System Design - 5

• After a key press is detected the microcontroller will go through the process of

identifying the key.

• Starting with the top row, the microcontroller grounds it by providing a low to row

D0 only; then it reads the columns.

• If the data read is all 1s, no key in that row is activated and the process is moved to

the next row.

Scanning and identifying the key

the next row.

• It grounds the next row, reads the columns, and checks for any zero.

• This process continues until the row is identified.

• After identification of the row in which the key has been pressed, the next task is to

find out which column the pressed key belongs to.

• This should be easy since the microcontroller knows at any time which row and

column are being accessed.Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 8: Microcontroller Based System Design - 5

Keyboard Interfacing Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 9: Microcontroller Based System Design - 5

Row and Column status for each key press.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 10: Microcontroller Based System Design - 5

PROGRAMORG 00h

MOV P2,#0FFH ;make port 2 input

REPEAT:MOV P1,#0EH ;MAKE ROW 0=0

JNB P2.0,NUM_0 ; check column0 ,then key 0 is pressed

JNB P2.1,NUM_1 ; check column1 ,then key 1 is pressed

JNB P2.2,NUM_2 ; check column2 ,then key 2 is pressed JNB P2.2,NUM_2 ; check column2 ,then key 2 is pressed

JNB P2.3,NUM_3 ; check column3 ,then key 3 is pressed

MOV P1,#0DH ;MAKE ROW 1=0

JNB P2.0,NUM_4 ; check column0 ,then key 4 is pressed

JNB P2.1,NUM_5 ;check column1 ,then key 5 is pressed

JNB P2.2,NUM_6 ; check column2 ,then key 6 is pressed

JNB P2.3,NUM_7 ; check column3 ,then key 7 is pressed

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 11: Microcontroller Based System Design - 5

.

MOV P1,#0BH ;MAKE ROW 2=0

JNB P2.0,NUM_8 ; check column0 ,then key 8 is pressed

JNB P2.1,NUM_9 ;check column1 ,then key 9 is pressed

JNB P2.2,NUM_A ; check column2 ,then key A is pressed

JNB P2.3,NUM_B ; check column3 ,then key B is pressed

MOV P1,#07H ;MAKE ROW 3=0

JNB P2.0,NUM_C ; check column0 ,then key C is pressed

JNB P2.1,NUM_D ;check column1 ,then key D is pressed

JNB P2.2,NUM_E ; check column2 ,then key E is pressed

JNB P2.3,NUM_F ; check column3 ,then key F is pressed

ABOVE: SJMP REPEAT

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 12: Microcontroller Based System Design - 5

NUM_0: JNB P2.0,NUM_0 ;make sure that key has been released

MOV P3,#'0' ;output the ASCII value 0 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_1: JNB P2.1,NUM_1 ;make sure that key has been released

MOV P3,#'1' ;output the ASCII value 0 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_2: JNB P2.2,NUM_2 ;make sure that key has been released

MOV P3,#'2‘ ;output the ASCII value 0 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_3: JNB P2.3,NUM_3 ;make sure that key has been released

MOV P3,#'3' ;output the ASCII value 3 to port 3

SJMP ABOVE ; repeat key scanning processPrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 13: Microcontroller Based System Design - 5

NUM_4: JNB P2.0,NUM_4 ;make sure that key has been released

MOV P3,#'4' ;output the ASCII value 4 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_5: JNB P2.1,NUM_5 ;make sure that key has been released

MOV P3,#'5' ;output the ASCII value 5 to port 3

SJMP ABOVE ; repeat key scanning processSJMP ABOVE ; repeat key scanning process

NUM_6: JNB P2.2,NUM_6 ;make sure that key has been released

MOV P3,#'6' ;output the ASCII value 6 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_7: JNB P2.3,NUM_7 ;make sure that key has been released

MOV P3,#'7' ;output the ASCII value 7 to port 3

SJMP ABOVE ; repeat key scanning processPrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 14: Microcontroller Based System Design - 5

NUM_8:JNB P2.0,NUM_8 ;make sure that key has been released

MOV P3,#'8' ;output the ASCII value 8 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_9:JNB P2.1,NUM_9 ;make sure that key has been released

MOV P3,#'9' ;output the ASCII value 9 to port 3

SJMP ABOVE ; repeat key scanning process

NUM_A:JNB P2.2,NUM_A ;make sure that key has been released NUM_A:JNB P2.2,NUM_A ;make sure that key has been released

MOV P3,#'A' ;output the ASCII value A to port 3

SJMP ABOVE ; repeat key scanning process

NUM_B:JNB P2.3,NUM_B ;make sure that key has been released

MOV P3,#'B' ;output the ASCII value B to port 3

SJMP ABOVE ; repeat key scanning process

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 15: Microcontroller Based System Design - 5

NUM_C:JNB P2.0,NUM_C ;make sure that key has been released

MOV P3,#'C' ;output the ASCII value C to port 3

SJMP ABOVE ; repeat key scanning process

NUM_D:JNB P2.1,NUM_D ;make sure that key has been released

MOV P3,#'D' ;output the ASCII value D to port 3

SJMP ABOVE ; repeat key scanning process

NUM_E:JNB P2.2,NUM_E ;make sure that key has been released NUM_E:JNB P2.2,NUM_E ;make sure that key has been released

MOV P3,#'E' ;output the ASCII value E to port 3

SJMP ABOVE ; repeat key scanning process

NUM_F:JNB P2.3,NUM_F ;make sure that key has been released

MOV P3,#'F' ;output the ASCII value F to port 3

SJMP ABOVE ; repeat key scanning process

ENDPrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 16: Microcontroller Based System Design - 5

MEASURING FREQUENCY OF AN INPUT SIGNAL

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 17: Microcontroller Based System Design - 5

MEASUREMENT OF FREQUENCY

• The 8051 can be easily configured and programmed to display the

frequency of the input signal.

• Here the incoming signal is connected to Pin T0

• The input to T0 is shown as a high frequency signal.

• The method is to count the number of pulses in a fixed interval of 125ms.

• Frequency is calculated as cycles/second and is obtained by

multiplying the pulses counted in 125ms by 8.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 18: Microcontroller Based System Design - 5

Steps for configuring Counter 0 and Timer 1

• Counter 0 is configured as an external event counter (C/T = 1).

• Timer 1 is programmed to give a known delay interval of 125ms.

• Counter 0 is turned ON at the start of the measuring sequence(TR0 =1).

• Timer 1 is started immediately (TR1 = 1).

• Software now polls the TF1 bit to see if Timer 1 has overflowed.• Software now polls the TF1 bit to see if Timer 1 has overflowed.

• When TF1 is set Counter 0 is read (Read TH0:TL0).

• The value read in this counter is the number of pulses received per

125ms.

• This multiplied by 8 will be frequency of the input signal.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 19: Microcontroller Based System Design - 5

Write a program to count the frequency of an input signal that ranges

from 500Hz to 2.0KHz. Assume a clock frequency of 6.00MHz. The input

signal is connected to Pin TO of the 8051.

Solution

• Configure Counter 0 in Mode 1 to count the external pulses

• Configure Timer 1 to overflow after 125ms.

• Timer 1 frequency = Fosc/12 = 6.000MHz/12 = 0.5MHz• Timer 1 frequency = Fosc/12 = 6.000MHz/12 = 0.5MHz

• Each Timer 1 count takes l/0.5MHz = 2 us

• To generate TF1 after 125ms, the registers TH1:TL1 should be loaded with

(65536-125ms/2 us) = 65536 - 62500 = 3036 = 0BDChex

• Load TH1 = 0B; TL1 = DC

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 20: Microcontroller Based System Design - 5

PROGRAMORG 00H

LJMP SetTimers

SetTimers: MOV TMOD, #15H ; Counter 0 in Mode 1 (External Input)

; Timer 1 in Mode 1(Internal Input)

InitTl: MOVTL1,#0DCH ; Lower byte

MOV TH1, #0BH ; Upper byte

SETB TR0 ; Set Counter 0 Run control bit

SETB TR1 ; Set Timer 1 Run control bit

; Now check for Counter/Timer l overflow

ChkT1Ovf: JNB TF1, ChkT1Ovf ; TF1 bit not set - no overflow

; TF bit set - Timer 1 completed 125ms

; Read TH0:TL0

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 21: Microcontroller Based System Design - 5

T1Ovf: CLRTF1 ; Clear TF1 Interrupt flag

CLR TR0 ; Stop Timer 0

CLR TR1 ; Stop Timer 1

MOV R0, TLO ; Low byte of Counter 0

MOV R1, THO ; High byte of Counter 0

; Now R1, R0 contains the 16 bit value of

; number of transitions counted using

; Counter 0 for a period of 125ms; Counter 0 for a period of 125ms

;Multiply this value by 8 to get frequency

;Multiply by 8 can be easily done by shifting

;the number 3 times to the left

MOV R2, #3 ;load R2 with the number of shifts required

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 22: Microcontroller Based System Design - 5

Loop3: MOV A,R0 ;Load LSB

CLR C ;Clear Carry

RLC A ;Shift left by 1 bit C=8th bit

MOV R0,A ;Store result back into LSB

MOV A, R1 ; Get MSB

RLC A ; Shift left with carry from LSB

MOV R1, A ; Store it back

DJNZ R2, Loop3 ; Do 3 times

; R1, RO has the 16-bit value of the; R1, RO has the 16-bit value of the

;frequency input

MOV P0,R0 ; Move the low byte of frequency to PO

MOV P2,R1 ; Move the high byte of frequency to P2

AJMP InitTl ; Next pass

END ; End of program

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 23: Microcontroller Based System Design - 5

WATCHDOG TIMER

• Hardware that can be used to automatically detect software anomalies and

reset the processor if any occur.

• Based on a counter that counts down from some initial value to zero.

The embedded software selects the counter's initial value and periodically • The embedded software selects the counter's initial value and periodically

restarts it.

• The processor (and the software it's running) will be restarted as if a human

operator had cycled the power.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 24: Microcontroller Based System Design - 5

WATCHDOG TIMER

• The watchdog timer is a chip external

to the processor.

• The output from the watchdog timer

is tied directly to the processor's reset

signal.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 25: Microcontroller Based System Design - 5

DS1232 MICROMONITOR CHIP

• Monitors three vital conditions for a microprocessor:

– Power supply,

– Software execution(watch dog timer)

– External override(push button reset)

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 26: Microcontroller Based System Design - 5

FEATURES

• Halts and restarts an out-of-control microprocessor

• Holds microprocessor in check during power transients

• Automatically restarts microprocessor after power failure

• Monitors pushbutton for external override• Monitors pushbutton for external override

• Eliminates the need for discrete components

• Space-saving, 8-pin mini-DIP (Optional 16-pin SOIC surface mount package)

• Available for Industrial temperature range (-40°C to +85°C)

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 27: Microcontroller Based System Design - 5

PIN DIAGRAM

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 28: Microcontroller Based System Design - 5

MICROMONITOR BLOCK DIAGRAM

.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 29: Microcontroller Based System Design - 5

PUSH BUTTON RESET

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 30: Microcontroller Based System Design - 5

AT KEYBOARD

• AT keyboard is the US standard keyboard introduced in 1986 by IBM

• Used with the IBM compatible computer (a keyboard with 84 keys).

• Later replaced with the 101-key Enhanced keyboard.

• One of the most common keyboards used today.

• Uses the AT (Din5) port, PS/2 and USB keyboards.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 31: Microcontroller Based System Design - 5

AT KEYBOARD

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 32: Microcontroller Based System Design - 5

INTERFACE(CONNECTOR)

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 33: Microcontroller Based System Design - 5

AT Keyboard – General Description

• Keyboards consist of a large matrix of keys, all of which are monitored by an

on-board processor (called the "keyboard encoder".)

• The specific processor monitor which key(s) are being pressed/released and

send the appropriate data to the host.

• Motherboard contains a "keyboard controller“ that is in charge of decoding

all of the data received from the keyboard.

• All communication between the host and the keyboard uses an IBM

protocol.Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 34: Microcontroller Based System Design - 5

AT Keyboard - Scan Codes

• If any key is being pressed, released, or held down, the keyboard will send a packet

of information known as a "scan code" to computer.

• There are two different types of scan codes:

"make codes" and "break codes".

• A make code is sent when a key is pressed or held down.

• A break code is sent when a key is released.

• Every key is assigned its own unique make code and break code.

• The set of make and break codes for every key comprises a "scan code set".

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 35: Microcontroller Based System Design - 5

AT KEYBOARD

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 36: Microcontroller Based System Design - 5

SCAN CODES

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 37: Microcontroller Based System Design - 5

REAL-TIME CLOCK

• A real-time clock (RTC) is a computer clock (mostly in the form of an IC) that

keeps track of the current time.

• Present in almost any electronic device which needs to keep accurate time.

Benefits

• Low power consumption.

• Frees the main system for time-critical tasks.

• Sometimes more accurate than other methods.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 38: Microcontroller Based System Design - 5

REAL-TIME CLOCK• Power source

– RTCs often have an alternate source of power.

– Normally a lithium battery in older systems,

– but some newer systems use a supercapacitor.

• 5.5.3 Timing• 5.5.3 Timing

– Most RTCs use a crystal oscillator,

• Examples

– Many integrated circuit manufacturers make RTCs, including Intersil,

Maxim, Philips, Texas Instruments and STMicroelectronics.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 39: Microcontroller Based System Design - 5

DS1302 TRICKLE CHARGE TIMEKEEPING CHIP

• Contains a real time clock/calendar and 31 bytes of static RAM.

• It communicates with a microprocessor via a simple serial interface.

• The real time clock/calendar provides seconds, minutes, hours, day, date,

month, and year information with leap year compensation valid up to 2100.

• The clock operates in either the 24–hour or 12–hour format with an • The clock operates in either the 24–hour or 12–hour format with an

AM/PM indicator.

• The DS1302 has the additional features of dual power pins for primary

(VCC2) and back–up power supplies.

• 2.0–5.5 volt full operation.

• TTL–compatiblePrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 40: Microcontroller Based System Design - 5

DS1302 - Pin Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 41: Microcontroller Based System Design - 5

DS1302 Block Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 42: Microcontroller Based System Design - 5

DC MOTOR INTERFACING

• (DC) motor is a device that translates electrical pulses into mechanical

movement.

• In the DC motor we have only + and - leads.

• Connecting them to a DC voltage source moves the motor in one direction. • Connecting them to a DC voltage source moves the motor in one direction.

• By reversing the polarity, the DC motor will move in the opposite direction.

• For example, small fans used in many motherboards to cool the CPU are run

by DC motors.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 43: Microcontroller Based System Design - 5

DC MOTOR INTERFACING

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 44: Microcontroller Based System Design - 5

BIDIRECTIONAL CONTROL

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 45: Microcontroller Based System Design - 5

BIDIRECTIONAL CONTROL

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 46: Microcontroller Based System Design - 5

Logic configurations for the H-Bridge

design

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 47: Microcontroller Based System Design - 5

A switch is connected to pin P2.7.

a) If SW = 0, the DC motor moves clockwise .

b) If SW = 1, the DC motor moves counterclockwise.ORG 0000H

MATN:

CLR P1.0 ;switch 1

CLR P1.1 ;switch 2

CLR P1.2 ;switch 3

CLR P1.3 ;switch 4

SETB P2.7

MONITOR:

JNB P2.7, CLOCKWISE

CLR P1.0 ;switch 1

SETB P1.1 ;switch 2

SETB P1.2 ;switch 3

CLR P1.3 ;switch 4

SJMP MONITOR

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 48: Microcontroller Based System Design - 5

.

CLOCKWISE:

SETB P1.0 ;switch 1

CLR P1.1 ;switch 2

CLR P1.2 ;switch 3

SETB P1.3 ;switch 4SETB P1.3 ;switch 4

SJMP MONITOR

END

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 49: Microcontroller Based System Design - 5

L293 MOTOR DRIVER

• The L293 is an integrated circuit motor driver that can be used

for simultaneous, bi-directional control of two small motors.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 50: Microcontroller Based System Design - 5

INTERFACING – Bidirectional Motor control

using an L293 chip

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 51: Microcontroller Based System Design - 5

ORG 0000H

• MAIN:

CLR P1.0 ;switch 1

CLR P1.1 ;switch 2

CLR P1.2 ;switch 3

SETB P2.7

• MONITOR:

SETB P1.0

JNB P2.7, CLOCKWISEJNB P2.7, CLOCKWISE

CLR P1.1

SETB P1.2

SJMP MONITOR

• CLOCKWISE:

SETB P1.1 ;

CLR P1.2 ;

SJMP MONITORPrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 52: Microcontroller Based System Design - 5

Pulse width modulation (PWM)

• Method to maintain a steady speed.

• The speed of the motor depends on three factors:

(a) load, (b) voltage, and (c) current.

• By changing (modulating) the width of the pulse applied to the DC

motor we can increase or decrease the amount of power provided to

the motor, thereby increasing or decreasing the motor speed.

• Wider the pulse, the higher the Speed.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 53: Microcontroller Based System Design - 5

PWM comparisons

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 54: Microcontroller Based System Design - 5

DC motor control with optoisolator

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 55: Microcontroller Based System Design - 5

Refer to the DC motor connection of above Figure. To the 8051 of this figure, a switch

SW i s connected to pin 3.2, which is the INTO pin.

Write a program

(a) Normally the motor runs with a 33% duty cycle

(b) When INTO is activated, the motor runs with 10% duty cycle for a short duration.

ORG 0000H

LJMP MAINLJMP MAIN

;------------the ISR for interrupt INTO----------------

ORG 0003H

SJMP FIRST

;------------main program for initialization------------

ORG 0030H

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 56: Microcontroller Based System Design - 5

MAIN: MOV IE,#81H ;enable INT 0

HERE: SETB Pl.0

MOV R0,#33 ;PI.0 set for 33% time

ACALL DELAY

CLR Pl.0

MOV R0,#67 ;PI.0 cleared for 67% time

ACALL DELAY

SJMP HERE

;--------------this is the ISR for INT 0--------------------------------

FIRST: MOV R5,#0FFH ;this is to create a delayFIRST: MOV R5,#0FFH ;this is to create a delay

THERE: SETB PI.0

MOV R0,#10 ;P1.0 set for 10 % time

ACALL DELAY

CLR PI. 0

MOV R0,#90 ;P1.0 cleared for 90% time

ACALL DELAY

DJNZ R5, THERE ; exit from ISR when R5 = 0

RETIPrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 57: Microcontroller Based System Design - 5

.

;--------------subroutine named DELAY------------------

DELAY :

RPT1 : MOV Rl,#20

RPT 2 : MOV R2,#100

RPT 3 : DJNZ R2,RPT3

DJNZ R1,RPT2

DJNZ R0,RPT1DJNZ R0,RPT1

RET

END

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 58: Microcontroller Based System Design - 5

Stepper Motor

• A stepper motor is a widely used device that translates electrical

pulses into mechanical movement.

• Applications such - disk drives, dot matrix printers, and• Applications such - disk drives, dot matrix printers, and

robotics, Position control.

• Stepper motors commonly have a permanent magnet rotor

surrounded by a stator.Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 59: Microcontroller Based System Design - 5

Stepper Motor

• Most common stepper motors have four

stator windings that are paired with a center-

tapped common.

• The center tap allows a change of current

direction in each of two coils thereby resulting

in a polarity change of the stator.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 60: Microcontroller Based System Design - 5

STEPPER MOTOR INTERFACING

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 61: Microcontroller Based System Design - 5

.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 62: Microcontroller Based System Design - 5

Stepper Motor Step Angles

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 63: Microcontroller Based System Design - 5

Program to rotate motor continuously

ORG 00H

MOV A,#66H ;load step sequence

BACK: MOV PI,A ;issue sequence to motor

RR A ; rotate right clockwise

ACALL DELAY ; wait

SJMP BACK ; keep goingSJMP BACK ; keep going

DELAY:

MOV R2,#100

HI: MOV R3,#255

H2: DJNZ R3,H2

DJNZ R2,HI

RET

ENDPrepared by Emil Raj,Dept. Of ECE,MLMCE

Page 64: Microcontroller Based System Design - 5

A switch is connected to pin P2.7. Write a program to monitor the status of SW and

perform the following

(a) I f SW = 0, the stepper motor moves clockwise.

(b) If SW = 1, the stepper motor moves counterclockwise.

ORG OH ; starting address

MAIN: SETB P2.7 ; make an input

MOV A, #66H ; starting phase value

MOV PI, A ; send value to port

TURN:

JNB P2.7, CW ; check switch result

RLA ; rotate left

ACALL DELAY ; call delay

MOV PI,A ; write value to port

SJMP TURN ; repeat

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 65: Microcontroller Based System Design - 5

CW: RR A ; rotate right

ACALL DELAY ; call delay

MOV PI, A ; write value to port

SJMP TURN ; repeat

DELAY: MOV R2, #100

H1: MOV R3, #255

H2: DJNZ R3, H2

DJNZ R2,H1DJNZ R2,H1

RET

END

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 66: Microcontroller Based System Design - 5

Program To rotate a motor 64° in the clockwise direction. The motor

has a step angle of 2°.

Solution:

A motor with a 2° step angle has the following characteristics:

• step angle: 2°• step angle: 2°

• steps per revolution: 180

• movement per 4-step sequence: 8°

• To move the rotor 64°, we have to send eight consecutive 4-step

sequences, i.e., 32 steps.Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 67: Microcontroller Based System Design - 5

.

ORG 0000H

MOV A,#66H

MOV R0,#32

BACK: RR A

MOV PI,A

ACALL DELAYACALL DELAY

DJNZ R0,BACK

END

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 68: Microcontroller Based System Design - 5

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Page 69: Microcontroller Based System Design - 5

POSITION CONTROL SYSTEM

• Give a brief description of each block.

• Give the explanation of stepper motor.

• Draw the interfacing diagram for stepper

motor.

• Write the pgm for rotating it a particular

degree.eg:64 degree

Prepared by Emil Raj,Dept. Of ECE,MLMCE