Search This Blog

Sunday, November 20, 2011

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.

TMOD register

As we know there are 2 timer registers in 8051. Timer 0 and timer 1. Both of these registers use the same register called TMOD to set various timer operation modes.

TMOD is an 8 bit register, in which lower 4 bits are for Timer 0 and upper 4 bits are for Timer 1.

See table below. MSB ---- LSB
GATE
C/T
M1
M0
GATE
C/T
M1
M0
TIMER 1
TIMER 0

GATE
Gating control when set. The timer/counter is enabled only while the INTx pin is high and TRx control pin is set. When cleared, the timer is enabled whenever TRx control pin is set.
C/T
Timer or counter; 0 or  clear for timer operation.(connected to input from internal system clock). 1 or set for counter operation(connected to input from Tx input pin).
M1
Mode bit 1
M0
Mode bit 0

What the heck is M1 and M0??

M0 and M1 select the timer mode in TMOD. 4 modes as mode 0, mode 1, mode 2, mode 3. See table.
M1
M0
MODE
OPERATING MODE
0
0
MODE 0
13 bit timer mode
8-bit timer/counter THx with TLx as 5 bit prescaler
0
1
MODE 1
16 bit timer mode.
16 bit timer/counter THx and TLx are cascaded with no prescaler.
1
0
MODE 2
8 bit auto reload.
8 bit auto reload timer/counter  THx holds a value that’s to be reloaded in TLx each time it overflows.
1
1
MODE 3
Split timer mode

C/T (clock/timer) in TMOD
  •  This bit in TMOD register is used to know whether it is used as a delay generator or as an event counter. 
  •  C/T = 0, then it is used as a timer for time delay generation. The clock source for the time delay is the crystal frequency of 8051. Timer gets pulses from the crystal.
  • C/T = 1, then it is used as a counter and gets its pulses from outside the 8051. That is from pins T0 and T1 belonging to port 3. In case of timer 0, when C/T = 1 pin 3.4 provides the clock pulse and counter counts up for each clock pulse coming from that pin. Similarly for timer 1, when C/T = 1 pin 3.5 provides the clock pulse and counter counts up for each clock pulse coming from that pin.

GATE – IN-- TMOD
  • One of the bit of TMOD is GATE bit. What does this GATE thingy do or whats its purpose on this heavenly earth and in our pretty little microcontroller??
  • Every timer has means of starting and stopping. This can be achieved either by software or by hardware or by both. 8051 gives us facility of both.
  • 8051 the start and stop of timer is controlled by way of software by TR(timer start) bits TR0 and TR1, using instructions :
  • SETB TR1
  • CLR TR1
  • For timer 1 and for timer 0:
  • SETB TR0
  • CLR TR0.
  • The software start and stop is achieved when GATE = 0 in TMOD register.
  • When GATE = 1 in TMOD register it means hardware control.  Its preferable to use GATE = 0 for most of the time.

Values of TMOD to operate as timers in following mode.
Mode 1 timer 1  0001 0000 = 10h
Mode 2 timer 0, mode 2 timer 1 0010 0010 = 22h
 
Note whatever the frequency of crystal is timer will always function at 1/12 th of the crystal frequency.