Search This Blog

Sunday, November 20, 2011

8051 interfacing with stepper motor

8051 Interfacing with stepper motor
fig shows interfacing connection
-------------------------------
; 4 PINS OF STEPPER MOTOR CONTROLLED BY 4 BITS OF 8051 P1.0 - P1.3
; IT IS INTERFACED USING DARLINGTON ARRAYS SUCH AS ULN2003 AS
; 8051 LACKS CURRENT TO RUN THE MOTOR. SO ULN2003 IS USED FOR ENERGIZING STATOR.
; PROGRAM BELOW SHOWS JUST ROTATING THE MOTOR STEP WISE
; BUT STEP WIDTH IS UNKNOWN

MOV A, #66H    ; LOAD THE STEP SEQUENCE

BACK :
MOV P1, A    ; LOAD SEQUENCE TO PORT
RR A        ; CHANGE SEQUENCE ROTATE CLOCKWISE
ACALL DELAY    : WAIT FOR IT
SJMP BACK    ; NOW KEEP GOING

DELAY :
MOV R2, #100

H1 :
MOV R3, #255

H2 :
DJNZ R3, H2
DJNZ R2, H1
RET

END

; THIS OTHER PROGRAM CODE HOW IN SAME INTERFACING TO ROTATE A STEPPER MOTOR
; 64 DEGREES IN CLOCKWISE DIRECTION USING 4 STEP SEQUENCE
; IT TAKES SOME CALCULATIONS
; CONSIDER A MOTOR WITH STEP ANGLE 2 DEGREE
; SO STEPS PER REVOLUTION = 180
; NO OF ROTOR TEETH = 45
; MOVEMENT PER 4 STEP SEQUENCE IS 8 DEGREES
; FOR 64 DEGREE MOVEMENT WE NEED TO SEND 8 CONSECUTIVE 4 STEP SEQUENCE
; THAT IS IT WILL COVER 32 STEPS

ORG 0000H
MOV A, #66H
MOV R1, #32H    ; 32 STEPS TO BE TAKEN

BACK :
RR A        ; ROTATE CLOCKWISE
MOV P1, A    ;
ACALL DELAY    ;
DJNZ R1, BACK    ;
END
-----------------

Now few question arises
q1) why did we took 66h as step value??
Ans))

A normal 4 step sequence is like below.
Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
1
2
1
1
0
0
3
0
1
1
0
4
0
0
1
1
Going from step 1 to 4 we rotate motor clockwise
Going from step 4 to 1 we rotate motor counter clockwise

The stepper motor discussed here has total 6 leads. 4 leads representing 4 windings and 2 commons for centre tapped leads. Here we have used 2 phase 4 step sequence. It must also be noted we can start from any step.  Be it step 1,2,3 or 4. But we must continue in proper order for proper rotation.
In programs on blog I have mostly begun from step 3.
 other step sequences are as below

Half Step 8 Step Sequence
Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
1
2
1
0
0
0
3
1
1
0
0
4
0
1
0
0
5
0
1
1
0
6
0
0
1
0
7
0
0
1
1
8
0
0
0
1



Wave drive 4 step sequence

Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
0
2
0
1
0
0
3
0
0
1
0
4
0
0
0
1





 q2) how much movement is associated with a step angle??

Step angle
Steps per revolution
.72
500
1.8
200
2
180
2.5
144
5
72
7.5
48
15
24


 ------------------

Edit - 1: Changed the Line DJNZ R0 to R1 as it was a mistake in typing.

8051 interfacing with DC motor

8051 interfacing with DC motor
fig shows interfacing connection.Below is the basic program

; P1.0 CONNECTED TO ENABLE
; P1.1 CONNECTED TO INPUT 1
; P1.2 CONNECTED TO INPUT 2
; ASSUME P2.1 BEING CONNECTED TO SWITCH SW
; THIS PROGRAM WILL ROTATE DC MOTOR CLOCKWISE SW = 0
; AND COUNTER CLOCKWISE WILL BE ROTATION OF DC MOTOR SW = 1

ORG 0000H        ;

MAIN :
CLR P1.0        ;
CLR P1.1        ;
CLR P1.2        ;
SETB P2.1        ;

MONITOR :
SETB P1.0        ; ENABLE CHIP
JNB P2.1, CLOCKWISE    ; JUMP WHEN SW = 0

COUNTERCLOCKWISE :
CLR P1.1        ;
SETB P1.2        ; TURN MOTOR COUNTERCLOCKWISE

CLOCKWISE :
SETB P1.1        ;
CLR P1.2        ; TURN MOTOR CLOCKWISE
SJMP MONITOR        ;
END            ;




Monday, November 14, 2011

SCON (serial control) Register

Its used to program the start bit, the stop bit and the data bits of data framing among other things.
8 bit register from MSB ---- to ----- LSB
SM0
SM1
SM2
REN
TB8
RB8
T1
R1

SM0
SM1
These 2 bits determine the framing of data by specifying number of bits per character and start and stop bits. they take following combo.
SM0
SM1
MODE
0
0
Serial mode 0
0
1
Serial mode 1, 8 bit data, 1 stop bit, 1 start bit.
1
0
Serial mode 2
1
1
Serial mode 3
SM2
This enables multiprocessing capabilities of 8051. Usually set to 0
REN
Also referred to as SCON.4 as SCON is a bit addressable register. This is receive enable. When high or 1 it allows 8051 to receive data from RxD pin.  Used or access as SET SCON.4 and CLR SCON.4. very useful in blocking external serial reception.
TB8
Transfer bit 8. Used for serial mode 2 and 3 not generally used so set it always to 0
RB8
Receive bit 8. Again used for serial mode 2 and 3 not used so set it to 0
T1
Transmit interrupt. Important flag bit in SCON register. When 8051 finishes transfer of 8 bit character, it raises the T1 flag to indicate that it is ready to transfer another byte. Is used at beginning of stop bit.
R1
Receive interrupt. Another important flag bit in SCON register. When 8051 finishes receiving data i.e when data is successfully stored in SBUF it raises R1 flag to indicate byte is received and to be picked before it gets lost.

SBUF register

Its an 8 bit register used solely for serial communication in 8051. For a byte of data to be transferred via TxD line, it muse be placed in SBUF register. Similarly SBUF register holds the serially inputed data received by TxD line of 8051.

Accessed as:

MOV SBUF,#’S’ ;load ascii

MOV SBUF,A  ;

MOV A, SBUF  ;

The moment the byte is written in SBUF, it is framed with start and stop bits and transferred serially through TxD line.
 
Similarly when bits are received serially via RxD it is deframed by eliminating stop and start bits and a byte of data is received and placed in SBUF.