Anda di halaman 1dari 49

PROGRAM 1: LED INTERFACE

Description:
LEDs are most commonly used in many applications for indicating the output. They find huge
range of applications as indicators during test to check the validity of results at different stages.
They are very cheap and easily available in a variety of shape, color and size.
The principle of operation of LEDs is very easy. A Simple LEDs also serves as a basic display
devices, it On and OFF state express meaning full information about a device. The common
available LEDs have a 1.7v voltage drop that means when we apply above 1.7V, the diode
conducts. The diode needs 10mA current to glow with full intensity.
The following circuit describes how to glow the LEDs.
LEDs can be interfaced to the microcontroller in either common anode or common cathode
configuration. Here the LEDs are connected in common anode configuration because the common
cathode configuration consumes more power.

R1(1)

R1

C1
1

5.1K

33p

X1

19

CRYSTAL
2

C2

18

C3(1)

XTAL1
XTAL2

33p
9

RST

C3
10u

29
30
31

PSEN
ALE
EA

R2
10K

1
2
3
4
5
6
7
8

P1.0/T2
P1.1/T2EX
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
AT89C52

P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD

39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17

D1
LED-YELLOW

ASSEMBLY CODE
org 0000h
loop: clr p0.2
acall delay
setb p0.2
acall delay
sjmp loop
delay: mov r7,#100
again_delay: djnz r7,again_delay
ret
end
C CODING
#include<reg51.h>
sbit LED= P0^2;
void delay( unsigned int a);
void main()
{
LED=0;
while(1)
{
LED=0;
delay(60);
LED=1;
delay(60);
}
}
void delay (unsigned int b)
{
unsigned int k;
for(k=0;k<b;k++);
}
----------------------------------------------------------------------------------------------------------------------------------------------

PROGRAM 2: SEVENSEGMENT INTERFACE

Description:
A Seven segment display is the most basic electronic display. It consists of eight LEDs which are
associated in a sequence manner so as to display digits from 0 to 9 when proper combinations of
LEDs are switched on. A 7-segment display uses seven LEDs to display digits from 0 to 9 and the
8th LED is used for dot. A typical seven segment looks likes as shown in figure below.
The 7-segment displays are used in a number of systems to display the numeric information.
They can display one digit at a time. Thus the number of segments used depends on the number
of digits to display. Here the digits 0 to 9 are displayed continuously at a predefined time delay.

The 7-segment displays are available in two configurations which are common anode and
common cathode. Here common anode configuration is used because output current of the
microcontroller is not sufficient enough to drive the LEDs. The 7-segment display works on
negative logic, we have to provide logic 0 to the corresponding pin to make on LED glow.

The following table shows hex code to display different values on seven segment

Assembly coding
org 0000h
main:
clr
sbit
sbit
sbit

P2.0
P2.1
P2.2
P2.3

mov a,#00h
Up:
mov 30h,a
mov dptr,#arr
movc a,@a+dptr
mov P0,a
call delay
mov a, 30h
inc a
jmp Up
jmp main
arr:
db
db
db
db
db
db
db
db
db
db

11111100b
01100000b
11011010b
11110010b
01100110b
10110110b
10111110b
11100000b
11111110b
11110110b

delay:

;Enabling Display-1
;Disabling Display-2
;Disabling Display-3
;Disabling Display-4

mov 40h,#0FFh
L1:
mov 41h,#99h
djnz 41h,$
djnz 40h,L1
ret
end

Common cathode interfacing

Assembly program using pc as base address


ORG 000H //initial starting address
START: MOV A,#00001001B // initial value of accumulator
MOV B,A
MOV R0,#0AH //Register R0 initialized as counter which counts from 10 to 0
LABEL: MOV A,B
INC A
MOV B,A
MOVC A,@A+PC // adds the byte in A to the program counters address
MOV P1,A

ACALL DELAY // calls the delay of the timer


DEC R0//Counter R0 decremented by 1
MOV A,R0 // R0 moved to accumulator to check if it is zero in next instruction.
JZ START //Checks accumulator for zero and jumps to START. Done to check if counting has been
finished.

SJMP LABEL
DB 3FH // digit drive pattern for 0
DB 06H // digit drive pattern for 1
DB 5BH // digit drive pattern for 2
DB 4FH // digit drive pattern for 3
DB 66H // digit drive pattern for 4
DB 6DH // digit drive pattern for 5
DB 7DH // digit drive pattern for 6
DB 07H // digit drive pattern for 7
DB 7FH // digit drive pattern for 8
DB 6FH // digit drive pattern for 9
DELAY: MOV R4,#05H // subroutine for delay
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END

About the program.


Instruction MOVC A,@A+PC is the instruction that produces the required digit drive pattern for
the display. Execution of this instruction will add the value in the accumulator A with the content
of the program counter(address of the next instruction) and will move the data present in the
resultant address to A. After this the program resumes from the line after MOVC A,@A+PC.In the
program, initial value in A is 00001001B. Execution of MOVC A,@A+PC will add oooo1001B to
the content in PC ( address of next instruction). The result will be the address of label DB 3FH
(line15) and the data present in this address ie 3FH (digit drive pattern for 0) gets moved into the
accumulator. Moving this pattern in the accumulator to Port 1 will display 0 which is the first
count.At the next count, value in A will advance to 00001010 and after the execution of MOVC
A,@+PC ,the value in A will be 06H which is the digit drive pattern for 1 and this will display 1
which is the next count and this cycle gets repeated for subsequent counts.
The reason why accumulator is loaded with 00001001B (9 in decimal) initially is that the
instructions from line 9 to line 15 consumes 9 bytes in total.The lines 15 to 24 in the program
which starts with label DB can be called as a Look Up Table (LUT). label DB is known as Define
Byte which defines a byte. This table defines the digit drive patterns for 7 segment display as
bytes (in hex format). MOVC operator fetches the byte from this table based on the result of
adding PC and contents in the accumulator.
Register B is used as a temporary storage of the initial value of the accumulator and the
subsequent increments made to accumulator to fetch each digit drive pattern one by one from
the look up table(LUT).

Note:- In line 6, Accumulator is incremented by 1 each time (each loop iteration) to select the
next digit drive pattern. Since MOVC operator uses the value in A to fetch the digit drive pattern
from LUT, value in ACC has to be incremented/manipulated accordingly. The digit drive patterns
are arranged consecutively in LUT.Register R0 is used as a counter which counts from 10 down to
0. This ensures that digits from o to 9 are continuously displayed in the 7 segment LED. You may
note lines 4, 11, 12, and 13 in the above program. Line 4 initializes R0 to 10 (OAh). When the
program counter reaches line 11 for the first time, 7 segment LED has already displayed 0. So we
can reduce one count and that is why we have written DEC Ro. We need to continuously check if
R0 has reached full count (that is 0). In order to do that lines 12 and 13 are used. We move R0 to
accumulator and then use the Jump if Zero (JZ) instruction to check if accumulator has reached
zero. If Acc=0, then we makes the program to jump to START (initial state) and hence we restart
the 7 segment LED to display from 0 to 9 again. If Acc not equal to zero, we continue the program
to display the next digit (check line 14).

Multiplexing 7 segment display to 8051.


Suppose you need a three digit display connected to the 8051. Each 7 segment display have 8
pins and so a total amount of 24 pins are to the connected to the microcontroller and there will be
only 8 pins left with the microcontroller for other input output applications. Also the maximum
number of displays that can be connected to the 8051 is limited to 4 because 8051 has only 4
ports. More over three 3 displays will be ON always and this consumes a considerable amount of
power. All these problems associated with the straight forward method can be solved by
multiplexing .
In multiplexing all displays are connected in parallel to one port and only one display is allowed to
turn ON at a time, for a short period. This cycle is repeated for at a fast rate and due to the
persistence of vision of human eye, all digits seems to glow. The main advantages of this method
are

Fewer number of port pins are required .


Consumes less power.
More number of display units can be interfaced (maximum 24).

The circuit diagram for multiplexing 2 seven segment displays to the 8051 is shown below.

When assembled and powered on, the circuit will display the number 16 and let us see how it is
done. Initially the first display is activated by making P3.0 high and then digit drive pattern for
1 is loaded to the Port 1. This will make the first display to show 1. In the mean time P3.1 will
be low and so do the second display will be OFF. This condition is maintained for around 1ms and
then P3.0 is made low. Now both displays will be OFF. Then the second display is activated by
making P3.1 high and then the digit drive pattern for 6 is loaded to the port 1. This will make
the second display to show 6. In the mean time P3.0 will be low and so the second display will
be OFF. This condition is maintained for another 1ms and then port 3.1 is made low. This cycle is
repeated and due to the persistence of vision you will feel it as 16.
Transistor Q1 drives the first display (D1) and transistor Q2 drives the second display (D2). R11
and R12 are the base current limiting resistors of Q1 and Q2. The purpose of other components
are explained in the first circuit.

Program.
ORG 000H // initial starting address
MOV P1,#00000000B // clears port 1
MOV R6,#1H // stores "1"
MOV R7,#6H // stores "6"
MOV P3,#00000000B // clears port 3
MOV DPTR,#LABEL1 // loads the adress of line 29 to DPTR
MAIN: MOV A,R6 // "1" is moved to accumulator
SETB P3.0 // activates 1st display
ACALL DISPLAY // calls the display sub routine for getting the pattern for "1"
MOV P1,A // moves the pattern for "1" into port 1
ACALL DELAY // calls the 1ms delay
CLR P3.0 // deactivates the 1st display
MOV A,R7 // "2" is moved to accumulator
SETB P3.1 // activates 2nd display
ACALL DISPLAY // calls the display sub routine for getting the pattern for "2"
MOV P1,A // moves the pattern for "2" into port 1
ACALL DELAY // calls the 1ms delay
CLR P3.1 // deactivates the 2nd display
SJMP MAIN // jumps back to main and cycle is repeated
DELAY: MOV R3,#02H
DEL1: MOV R2,#0FAH
DEL2: DJNZ R2,DEL2
DJNZ R3,DEL1
RET
DISPLAY: MOVC A,@A+DPTR // adds the byte in A to the address in DPTR and loads A with data
present in the resultant address
RET
LABEL1:DB 3FH
DB 06H
DB 5BH
DB 4FH
DB 66H
DB 6DH
DB 7DH
DB 07H
DB 7FH
DB 6FH
END

PRACTICAL 3:-LCD Interfacing to Microcontroller


LCD stands for liquid crystal display which can display the characters per line. Here 16
by 2 LCD display can display 16 characters per line and there are 2 lines. In this LCD
each character is displayed in 5*7 pixel matrix.
LCD is very important device which is used for almost all automated devices such as
washing machines, an autonomous robot, power control systems and other devices. This
is achieved by displaying their status on small display modules like 7-seven segment
displays, multi segment LEDs etc. The reasons being, LCDs are reasonably priced, easily
programmable and they have a no limitations of displaying special characters.
It consists of two registers such as command/instruction register and data register.The
command/instruction register stores the command instructions given to the LCD. A
command is an instruction which is given to the LCD that perform a set of predefined
tasks like initializing, clearing the screen, setting the cursor posing, controlling display
etc.The data register stores the data to be displayed on LCD. The data is an ASCII value
of the characters to be displayed on the LCD.Operation of LCD is controlled by two
commands. When RS=0, R/W=1 it reads the data and when RS=1, R/W=0, it writes
(print) the data.
LCD uses following command codes:
162 LCD module.
162 LCD module is a very common type of LCD module that is used in 8051 based
embedded projects. It consists of 16 rows and 2 columns of 57 or 58 LCD dot
matrices. The module were are talking about here is type number JHD162A which is a
very popular one . It is available in a 16 pin package with back light ,contrast adjustment
function and each dot matrix has 58 dot resolution. The pin numbers, their name and
corresponding functions are shown in the table below.

VEE pin is meant for adjusting the contrast of the LCD display and the contrast can be adjusted by
varying the voltage at this pin. This is done by connecting one end of a POT to the Vcc (5V), other
end to the Ground and connecting the center terminal (wiper) of of the POT to the VEE pin. See
the circuit diagram for better understanding.
The JHD162A has two built in registers namely data register and command register. Data register
is for placing the data to be displayed , and the command register is to place the commands. The
162 LCD module has a set of commands each meant for doing a particular job with the display.
We will discuss in detail about the commands later. High logic at the RS pin will select the data
register and Low logic at the RS pin will select the command register. If we make the RS pin high
and the put a data in the 8 bit data line (DB0 to DB7) , the LCD module will recognize it as a data
to be displayed . If we make RS pin low and put a data on the data line, the module will recognize
it as a command.
R/W pin is meant for selecting between read and write modes. High level at this pin enables read
mode and low level at this pin enables write mode.
E pin is for enabling the module. A high to low transition at this pin will enable the module.

DB0 to DB7 are the data pins. The data to be displayed and the command instructions are
placed on these pins.
LED+ is the anode of the back light LED and this pin must be connected to Vcc through a suitable
series current limiting resistor. LED- is the cathode of the back light LED and this pin must be
connected to ground.

162 LCD module commands.


162 LCD module has a set of preset command instructions. Each command will make
the module to do a particular task. The commonly used commands and their function are
given in the table below.

16x2 LCD Interfacing with 8051 in 4-bit Mode:

ASSEMBLY CODE
U EQU 31

;memory location to hold upper nibble

L EQU 32

;memory location to hold lower nibble

PORT EQU P1
RS EQU P2.0

;data port to connect lcd


;rs pin connection

RW EQU P2.1

;rw pin connection

EN EQU P2.2

;en pin connection

ORG 0000H
CLR RW
ACALL INIT
MOV A, #' '
ACALL LCD_DATA
MOV A, #'E'

;print " esrt. "

ACALL LCD_DATA ;in first line of lcd


MOV A, #'S'
ACALL LCD_DATA
MOV A, #'R'
ACALL LCD_DATA
MOV A, #'T'
ACALL LCD_DATA
MOV A, #'.'
ACALL LCD_DATA
MOV A, #' '
ACALL LCD_DATA

MOV A, #0C0H ;switch to 2nd line of lcd


ACALL LCD_CMD
MOV A, #' '
ACALL LCD_DATA
MOV A, #' '

;print " lab "

ACALL LCD_DATA ;in second line of lcd


MOV A, #' '
ACALL LCD_DATA
MOV A, #' '

ACALL LCD_DATA
MOV A, #' '
ACALL LCD_DATA
MOV A, #' '
ACALL LCD_DATA
MOV A, #' '
ACALL LCD_DATA
MOV A, #'L'
ACALL LCD_DATA
MOV A, #'A'
ACALL LCD_DATA
MOV A, #'B'
ACALL LCD_DATA
MOV A, #' '
ACALL LCD_DATA

SJMP $

;infinite long loop

; separator for upper and lower nibble


;++++++++++++++++++++++++++++++++
++++++
SEPARATOR:

MOV U, A

;save a at temp location u

ANL U, #0F0H ;mask it with 0fh (28h &amp; f0h =


20h)
SWAP A

;swap nibble (28h => 82h)

ANL A, #0F0H ;mask it with 0fh (82h &amp; f0h =


80h)
MOV L, A
RET

;save it at temp location l

;return

; move to port sub-routine


MOVE_TO_PORT:
MOV PORT, A
SETB EN

;make en high

ACALL DELAY
CLR EN

;call a short delay routine

;clear en

ACALL DELAY
RET

;put content of a to port

;short delay

;return

; lcd command write sub-routine


LCD_CMD:
CLR RS

;clear rs, going to send command

ACALL SEPARATOR ;separate the command and


save to u and l

MOV A, U

;copy u to a

ACALL MOVE_TO_PORT ;move content of a to port


MOV A, L

;copy l to a

ACALL MOVE_TO_PORT ;move content of a to port


RET

;return

; lcd data write sub-routine


LCD_DATA:
SETB RS

;rs=1, going to send data

ACALL SEPARATOR ;separate the data and save to u


&amp; l
MOV A, U

;copy u to a

ACALL MOVE_TO_PORT ;send it to lcd


MOV A, L

;copy l to a

ACALL MOVE_TO_PORT ;send it to lcd


RET

;return

;lcd initialization sub-routine


INIT:
ACALL DELAY
ACALL DELAY

;some delay to lcd after power on

MOV PORT, #20H ;send 20h to lcd to set 4 bit mode


CLR RS
SETB EN

;after that we can use lcd_cmd


;make en switching

ACALL DELAY
CLR EN
MOV A, #28H
ACALL LCD_CMD
MOV A, #0CH
ACALL LCD_CMD
MOV A, #06H
ACALL LCD_CMD
MOV A, #01H
ACALL LCD_CMD
RET
; a delay sub-routine
DELAY:
MOV R0, #10H
L2: MOV R1, #0FH
L1: DJNZ R1, L1
DJNZ R0, L2

RET
END

Assembly code
; Reset Vector
org 0000h
mov a,#3ch
acall command
mov a,#0eh
acall command
mov a,#06h
acall command
mov a,#01h
acall command
mov a,#87h
acall command
mov a,#'H'
acall datadis
mov a,#'E'
acall datadis

mov a,#'L'
acall datadis
mov a,#'L'
acall datadis
mov a,#'O'
acall datadis
mov a,#'W'
acall datadis
mov a,#'E'
acall datadis
mov a,#'L'
acall datadis
mov a,#'C'
acall datadis
mov a,#'O'
acall datadis
mov a,#'M'
acall datadis
mov a,#'E'
acall datadis
mov a,#'I'
acall datadis
mov a,#5
acall command
mov a,#1
acall command
here1: sjmp here1
command: acall ready
mov p1,a
clr p3.2
clr p3.3
setb p3.4
acall delay
clr p3.4
ret
datadis:acall ready
mov p1,a
setb p3.2
clr p3.3
setb p3.4
clr p3.4
ret
ready: clr p3.4
mov p1,#0ffh
clr p3.2
setb p3.3
wait:
clr p3.4
setb p3.4
jb p1.7 ,wait
clr p3.4
ret
delay:
here2:
here3:
here:

mov r3,#255
mov r4,#255
mov r5,#255
djnz r5,here3
djnz r4,here

djnz r3,here2
ret
end
ONLY DISPLAY AJAY PATIL MESSAGE TO LCD PROGRAM

c1:

org 0000h
mov dptr,#mycomm
clr a
movc a,@a+dptr
acall commwrt
acall delay
jz send_data
inc dptr
sjmp c1

send_data:mov dptr,#mydata
d1:
clr a
movc a,@a+dptr
acall datawrt
acall delay
inc dptr
jz again
sjmp d1
again: sjmp again
commwrt:
mov p1,a
clr P3.2
clr P3.3
setb P3.4
acall delay
clr P3.4
ret
datawrt:
mov p1,a
setb P3.2
clr P3.3
setb P3.4
acall delay
clr P3.4
acall delay
mov dptr,#mycomm1
clr a
movc a,@a+dptr
acall commwrt
acall delay
ret
delay:
mov r3,#250
here2:
mov r4,#255
here:
djnz r4,here
djnz r3,here2
ret
org 300h
mycomm: db 38h,0eh,01,06,84h,0
mydata:
db "AJAY PATIL",0
mycomm1: db 18h

END
-----------------------------------------------------------------------------------------------------------------------------------------------------SCROLLING DISPLAY OF MESSAGE AJAY PATIL
TO LCD PROGRAM
org 0000h
c1:

mov dptr,#mycomm
clr a
movc a,@a+dptr
acall commwrt
acall delay
jz send_data
inc dptr
sjmp c1

send_data:mov dptr,#mydata
d1:
clr a
movc a,@a+dptr
acall datawrt
acall delay
inc dptr
jz again
sjmp d1
CONT:
sjmp cont
again: mov dptr,#mycomm1
clr a
movc a,@a+dptr
acall commwrt
acall delay
sjmp again
commwrt:
mov p1,a
clr P3.2
clr P3.3
setb P3.4
acall delay
clr P3.4
ret
datawrt:
mov p1,a
setb P3.2
clr P3.3
setb P3.4
acall delay
clr P3.4
acall delay
ret
delay:
here2:
here:

org 300h

mov r3,#250
mov r4,#255
djnz r4,here
djnz r3,here2
ret

mycomm: db 38h,0eh,01,06,84H,0
mydata:
db "AJAY PATIL ",0
mycomm1: db 18h
END

PRACTICAL 4:-Relay Interfacing to Microcontroller

Relay Design
There are only four main parts in a relay. They are

Electromagnet
Movable Armature
Switch point contacts
Spring

What is a relay ?
A relay is usually an electromechanical device that is actuated by an electrical current.
The current flowing in one circuit causes the opening or closing of another circuit. Relays
are like remote control switches and are used in many applications because of their
relative simplicity, long life, and proven high reliability. Relays are used in a wide variety
of applications throughout industry, such as in telephone exchanges, digital computers
and automation systems. Highly sophisticated relays are utilized to protect electric
power systems against trouble and power blackouts as well as to regulate and control
the generation and distribution of power. In the home, relays are used in refrigerators,
washing machines and dishwashers, and heating and air-conditioning controls. Although
relays are generally associated with electrical circuitry, there are many other types, such
as pneumatic and hydraulic. Input may be electrical and output directly mechanical, or
vice versa.
How do relays work?
All relays contain a sensing unit, the electric coil, which is powered by AC or DC current.
When the applied current or voltage exceeds a threshold value, the coil activates the
armature, which operates either to close the open contacts or to open the closed
contacts. When a power is supplied to the coil, it generates a magnetic force that
actuates the switch mechanism. The magnetic force is, in effect, relaying the action from
one circuit to another. The first circuit is called the control circuit; the second is called
the load circuit.
There are three basic functions of a relay: On/Off Control, Limit Control and Logic
Operation.
On/Off Control: Example: Air conditioning control, used to limit and control a high
power
load, such as a compressor
Limit Control: Example: Motor Speed Control, used to disconnect a motor if it runs slower
or
faster than the desired speed
Logic Operation: Example: Test Equipment, used to connect the instrument to a number
of
testing points on the device under test
Types of Relays

There are two basic classifications of relays: Electromechanical and Solid State.
Electromechanical relays have moving parts, whereas solid state relays have no moving
parts. Advantages of
Electromechanical relays include lower
cost, no heat sink is required, multiple
poles are available, and they can
switch AC or DC with equal ease.

C-DODE
#include<reg51.h>
void delay(int a);
void main ()
{
P2=0x00;
while(1)
{

P2=0x01;
delay(6000);
P2=0X00;
delay(6000);
}
}
void delay(int a)
{
int i;
for(i=0;i<a;i++); //null statement
}

ASSEMBLY CODE
org 0000h
HERE12: SETB P2.0
acall DELAY
CLR P2.0
acall DELAY
SETB P2.0
SJMP HERE12
DELAY:
mov r3,#250
here2: mov r4,#255
mov r5,#255
here3: djnz r5,here3
here:

djnz r4,here
djnz r3,here2
ret

END

PRACTICAL 5:-STEPPER MOTOR Interfacing to Microcontroller

Stepper motor is one of the commonly used motors for precise angular movement. The
advantage of using a stepper motor is that the angular position of the motor shaft can be
controlled without any feedback mechanism. Stepper motors are widely used in industrial
and commercial applications. They are also commonly used as in drive systems of
autonomous robots.
This
article
explains
the unipolar
stepper
motor interfacing
with AT89C51 microcontroller. The microcontroller is programmed to rotate the stepper in
wave drive and half drive stepping modes. For basic concepts and working of a stepper
motor, refer the article on Stepper Motors.
A Unipolar Stepper Motor is rotated by energizing the stator coils in a sequence. In unipolar
stepper, the direction of current in stator coils is not required to be controlled by the driving
circuit. Just applying the voltage signals across the motor coils or motor leads in a sequence
is sufficient to drive the motor.
A two phase unipolar stepper motor has a total of six wires/leads of which four are end wires
(connected to coils) and two are common wires. The color of common wires in the stepper
motor used here is Green. Each common wire is connected to two end leads thus forming
two phases. The end leads corresponding to each phase have to be identified.

In some cases, when the leads cannot be directly identified in the motor, the identification of
endpoints and common points can be done by measuring the resistance between the leads.
The leads of different phase will show open circuited condition with respect to each other.
This way the leads corresponding to different phase can be separated. The resistance
between any two end points of same phase will be twice the resistance between a common
point and an end point. This way the common and end points of both the phases can be
identified.

To work with the unipolar stepper motor, the common points are connected to either Ground
or Vcc and the end points of both the phases are usually connected through the port pins of
a microcontroller. In present case the common (Green) wires are connected to Vcc. The end
points receive the control signals as per the controller's output in a particular sequence to
drive the motor.
Since the coils related to each phase are arranged in alternate manner, the end points of two
phases are energized in alternate fashion to rotate the motor. This means that the voltage
signal should be applied to first end point of Phase1 and then to the first end point of the
Phase2 and so on.
1. Wave Drive Stepping Mode
The above mentioned sequence is repeated to rotate the motor in Wave Drive Stepping
Mode. (For more details on different Stepping Modes, refer to the article on Stepper Motors)
The direction of rotation can be clockwise or anti clockwise depending upon the selection of
end points.

Ste
p
1
2
3
4

Signal sequence for Wave Drive Stepping Mode


Yellow lead
Blue lead
Red lead
(End point 1 of
(End point 2 of
(End point 1 of
Phase1)
Phase1)
Phase2)
1
0
0
0
0
1
0
1
0
0
0
0

White lead
(End point 2 of
Phase2)
0
0
0
1

2. Full Drive Stepping Mode


The Full Drive Stepping can be achieved by energizing two endpoints of different phases
simultaneously.
Signal sequence for Full Drive Stepping Mode
Yellow lead
Blue lead
Red lead
White lead
Ste
(End point 1 of
(End point 2 of
(End point 1 of
(End point 2 of
p
Phase1)
Phase1)
Phase2)
Phase2)
1
1
0
1
0
2
0
1
1
0
3
0
1
0
1
4
1
0
0
1

3. Half Drive Stepping Mode


The Half Drive Stepping is achieved by combining the steps of Wave and Full Drive Stepping
Modes. This divides the stepping angle by half.

Ste
p
1
2
3
4
5
6
7
8

Signal sequence for Half Drive Stepping Mode


Yellow lead
Blue lead
Red lead
(End point 1 of
(End point 2 of
(End point 1 of
Phase1)
Phase1)
Phase2)
1
0
0
1
0
1
0
0
1
0
1
1
0
1
0
0
1
0
0
0
0
1
0
0

White lead
(End point 2 of
Phase2)
0
0
0
0
0
1
1
1

Note:
The stepping angle depends upon the resolution of the stepper motor. The direction of
rotation and size of steps is directly related to the order and number of input sequence. The
shaft rotation speed depends on the frequency of the input sequence. The torque is
proportional to the number of magnets magnetized at a time.

Program 6: interfacing to lm 35

LM35 is a precision IC temperature sensor with its output proportional to the


temperature (in oC). The sensor circuitry is sealed and therefore it is not
subjected to oxidation and other processes. With LM35, temperature can be
measured more accurately than with a thermistor. It also possess low self heating

and does not cause more than 0.1 oC temperature rise in still air.
The operating temperature range is from -55C to 150C. The output voltage
varies by 10mV in response to every oC rise/fall in ambient temperature, i.e., its
scale factor is 0.01V/ oC.

ASSEMBLY CODE
org 0000h
intr bit p2.7
mydata equ p1
mov p1,#0ffh
setb intr
back: clr p2.6
setb p2.6
here: jb intr,here
clr p2.5
mov a,mydata
acall conversion
acall data_display
setb p2.5
sjmp back
conversion:
mov b,#10
div ab
mov r7,b
mov b,#10
div ab

mov r6,b
mov r5,a
ret
data_display:
mov p0,r7
acall delay
mov p0,r6
acall delay
mov p0,r5
acall delay
ret
delay:
mov r4,#05H ;// subroutine foR delay
WAIT1: mov r3,#00H
WAIT2: mov r2,#00H
WAIT3: DJNZ r2,WAIT3
DJNZ r3,WAIT2
DJNZ r4,WAIT1
RET
END

PROGARM: KEYPAD INTERFACING (HEX)

ASSEMBLY CODE
ORG 0000H ;set origin
MOV P2, #0FFH ;select p2 as output port
LJMP MAIN ;jump to main program

DELAY_1SEC:
;to give delay of 1second using timer0 in mode 1
MOV TMOD, #01H
MOV R1, #14
XY: MOV TH0, #00H
MOV TL0, #00H
SETB TR0
JNB TF0, $
CLR TF0
CLR TR0
DJNZ R1, XY
RET
DELAY:
;to give lcd some time to write
MOV R1, #05H
S2: MOV R0, #0FFH
S1: DJNZ R0, S1
DJNZ R1, S2
RET
WRITE_CMD:
;command subroutine to write command to lcd
CLR P3.7 ;register select bit cleared to choose command register
MOV P1, A ;port 1 acts as input to lcd
SETB P3.6 ;enable pin high to low for write operation
CLR P3.6 ;
LCALL DELAY ;give lcd some time delay
RET
;return to main program
WRITE_CHR:
;data subroutine to write data to lcd
SETB P3.7 ;register select bit set high to choose data register
MOV P1, A ;port 1 acts as input to lcd
SETB P3.6 ;enable pin high to low for write operation
CLR P3.6 ;
LCALL DELAY ;give lcd some time delay
RET
;return to main program
INI_LCD:
;initialise lcd
MOV A, #38H ;two lines 5x8 matrix
LCALL WRITE_CMD ;call command subroutine
MOV A, #0CH ;display on, cursor off
LCALL WRITE_CMD ;call command subroutine
MOV A, 06H ;increment cursor(shift cursor to right)
LCALL WRITE_CMD ;call command subroutine
RET
;return to main program
B1:
;subroutine to type 'e'
MOV A, #'E'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD1
B2:
;subroutine to type '1'
MOV A, #'1'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD2
B3:
;subroutine to type '4'
MOV A, #'4'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD3
B4:
;subroutine to type '7'
MOV A, #'7'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD4
B5:
;subroutine to type '0'
MOV A, #'0'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD5
B6:
;subroutine to type '2'
MOV A, #'2'

LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD6
B7:
;subroutine to type '5'
MOV A, #'5'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD7
B8:
;subroutine to type '8'
MOV A, #'8'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD8
B9:
;subroutine to type 'f'
MOV A, #'F'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD9
B10:
;subroutine to type '3'
MOV A, #'3'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD10
B11:
;subroutine to type '6'
MOV A, #'6'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD11
B12:
;subroutine to type '9'
MOV A, #'9'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD12
B13:
;subroutine to type 'd'
MOV A, #'D'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD13
B14:
;subroutine to type 'c'
MOV A, #'C'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD14
B15:
;subroutine to type 'b'
MOV A, #'B'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD15
B16:
;subroutine to type 'a'
MOV A, #'A'
LCALL WRITE_CHR
LCALL DELAY_1SEC
LJMP KEYPAD16
MAIN:
;main program
LCALL INI_LCD ;call initialise lcd
KEYPAD: CLR P2.0 ;clear 1st column
JNB P2.4, B1_1 ;check if 'e' pressed
KEYPAD1: JNB P2.5, B2_1 ;check if '1' pressed
KEYPAD2: JNB P2.6, B3_1 ;check if '4' pressed
KEYPAD3: JNB P2.7, B4_1 ;check if '7' pressed
KEYPAD4: SETB P2.0 ;if no key is pressed in column 1 rhen set 1st column
CLR P2.1 ;clear 2nd column
JNB P2.4, B5_1 ;check if '0' pressed
KEYPAD5: JNB P2.5, B6_1 ;check if '2' pressed
KEYPAD6: JNB P2.6, B7_1 ;check if '5' pressed
KEYPAD7: JNB P2.7, B8_1 ;check if '8' pressed

KEYPAD8: SETB P2.1 ;if no key is pressed in column 2 rhen set 2nd column
CLR P2.2 ;clear 3rd column
JNB P2.4, B9_1 ;check if 'f' pressed
KEYPAD9: JNB P2.5, B10_1 ;check if '3' pressed
KEYPAD10: JNB P2.6, B11_1 ;check if '6' pressed
KEYPAD11: JNB P2.7, B12_1 ;check if '9' pressed
KEYPAD12: SETB P2.2 ;if no key is pressed in column 3 rhen set 3rd column
CLR P2.3 ;clear 4th column
JNB P2.4, B13_1 ;check if 'd' pressed
KEYPAD13: JNB P2.5, B14_1 ;check if 'c' pressed
KEYPAD14: JNB P2.6, B15_1 ;check if 'b' pressed
KEYPAD15: JNB P2.7, B16_1 ;check if 'a' pressed
KEYPAD16: SETB P2.3 ;if no key is pressed in column 4 rhen set 4th column
LJMP KEYPAD
STOP: SJMP STOP ;
B1_1:
;subroutine to call b1 to display 'e'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD1
LJMP B1
B2_1:
;subroutine to call b2 to display '1'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD2
LJMP B2
B3_1:
;subroutine to call b3 to display '4'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD3
LJMP B3
B4_1:
;subroutine to call b4 to display '7'
ACALL DEBOUNCE_DELAY
JNB P2.7, KEYPAD4
LJMP B4
B5_1:
;subroutine to call b5 to display '0'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD5
LJMP B5
B6_1:
;subroutine to call b6 to display '2'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD6
LJMP B6
B7_1:
;subroutine to call b7 to display '5'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD7
LJMP B7
B8_1:
;subroutine to call b8 to display '8'
ACALL DEBOUNCE_DELAY
JNB P2.7, KEYPAD8
LJMP B8
B9_1:
;subroutine to call b9 to display 'f'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD9
LJMP B9
B10_1:
;subroutine to call b10 to display '3'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD10
LJMP B10
B11_1:
;subroutine to call b11 to display '6'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD11
LJMP B11
B12_1:
;subroutine to call b12 to display '9'
ACALL DEBOUNCE_DELAY
JNB P2.7, KEYPAD12
LJMP B12
B13_1:
;subroutine to call b13 to display 'd'
ACALL DEBOUNCE_DELAY
JNB P2.4, KEYPAD13

LJMP B13
B14_1:
;subroutine to call b14 to display 'c'
ACALL DEBOUNCE_DELAY
JNB P2.5, KEYPAD14
LJMP B14
B15_1:
;subroutine to call b15 to display 'b'
ACALL DEBOUNCE_DELAY
JNB P2.6, KEYPAD15
LJMP B15
B16_1:
;subroutine to call b16 to display 'a'
ACALL DEBOUNCE_DELAY
JNB P2.7, LOOP
LJMP B16
LOOP: LJMP KEYPAD16
DEBOUNCE_DELAY:
; a key debouncing delay algorithm
MOV TMOD, #10H
MOV TH1, #00H
MOV TL1, #00H
MOV R2, #4
AGAIN: SETB TR1
WAIT: JNB TF1, WAIT
CLR TR1
CLR TF1
DJNZ R2, AGAIN
RET
END
;end of main program

PROGRAM:

KEY PAD INTERFACING

Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. In this
section, we first discuss keyboard fundamentals, along with key press and key detection mechanisms, Then we show how a keyboard is
interfaced to an 8051.
Interfacing the Keyboard to the 8051
At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and column through ports;
therefore, with two 8-bit ports, an 8*8 matrix of keys can be connected to a microprocessor. When a key pressed, a row and column make a
connect; otherwise, there is no connection between row and column. In IBM PC keyboards, a single microcontroller (consisting of
microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of software and hardware interfacing of keyboard. In
such systems it is the function of programs stored in the EPROM of microcontroller to scan the keys continuously, identify which one has
been activated, and present it to the motherboard. In this section we look at the mechanism by which the 8051 scans and identifies the key.

Scanning and identifying the key


Figure13.5 shows a 4*4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input
port. If no key has been pressed, reading the input port will yield 1s for all columns since they are all 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. How it is done is explained next.

Grounding rows and reading columns


To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, and then it reads the columns. If the data
read from the columns is D3-D0=1111, no key has been pressed and the process continues until a key press is detected. However, if one of
the column bits has a zero, this means that a key press has occurred. For example, if D3-D0=1101, this means that a key in the D1 column
has been pressed. 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 all1s, no key in that row
is activated and the process is moved to 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.
Assembly language program for detection and identification of key activation is given below. In this program, it is assumed that P1 and P2
are initialized as output and input, respectively. Program13.1 goes through the following four major stages:
1. To make sure that the preceding key has been released, 0s are output to all rows at once, and the columns are read and checked repeatedly
until all the columns are high. When all columns are found to be high, the program waits for a short amount of time before it goes to the
next stage of waiting for a key to be pressed.
2) To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it. Remember that the
output latches connected to rows still have their initial zeros (provided in stage 1), making them grounded. After the key press detection, it
waits 20ms for the bounce and then scans the columns again. This serves two functions: (a) it ensures that the first key press detection was
not an erroneous one due to spike noise, and(b) the 20ms delay prevents the same key press from being interpreted as a multiple key press.
If after the 20-ms delay the key is still pressed, it goes to the next stage to detect which row it belongs to; otherwise, it goes back into the
loop to detect a real key press
3) To detect which row the key press belongs to, it grounds one row at a time, reading the columns each time. If it finds that all columns are
high, this means that the key press cannot belong to that row; therefore, it grounds the next row and continues until it finds the row the key
press belongs to. Upon finding the row that the key press belongs to, it sets up the starting address for the look-up table holding the scan
codes (or the ASCII value) for that row and goes to the next stage to identify the key.
4) To identify the key press, it rotates the column bits, one bit at a time, into the carry flag and checks to see if it is low. Upon finding the
zero, it pulls out the ASCII code for that key from the look-up table; Otherwise, it increments the pointer to point to the next element of the
look-up table.

While the key press detection is standard for all keyboards, the process for determining which key is pressed varies. The look-up table
method shown in program can be modified to work with any matrix up to 8*8.
There are IC chips such as National Semiconductors MM74C923 that incorporate keyboard scanning and decoding all in one chip. Such
chips use combinations of counters and logic gates (No microcontroller).

;Keyboard subroutine. This program sends the ASCII code


;for pressed key to P0.1
;P1.0-P1.3 connected to rows P2.0-P2.3 connected to columns
MOV P2,#0FFH
;make P2 an input port
MOV P1,#0
;ground all rows at once
MOV A,P2
;read all col. (ensure all keys open)
ANL A,00001111B
;masked unused bits
CJNE A,#00001111B,K1 ;check til all keys released
K2: ACALL DELAY
;call 20 msec delay
MOV A,P2
;see if any key is pressed
ANL A,#00001111B
;mask unused bits
CJNE A,#00001111B,OVER ;key pressed, await closure
SJMP K2
;check il key pressed
OVER: ACALL DELAY
;wait 20 msec debounce time
MOV A,P2
;check key closure
ANL A,#00001111B
;mask unused bits
CJNE A,#00001111B,OVER1;key pressed, find row
SJMP K2
;if none, keep polling
OVER1: MOV P1,#11111110B
;ground row 0
MOV A,P2
;read all columns
ANL A,#00001111B
;mask unused bits
CJNE A,#00001111B,ROW_0;key row 0, find the col.
MOV P1,#11111101B
;ground row 1
MOV A,P2
;read all columns
ANL A,#00001111B
;mask unused bits
CJNE A,#00001111B,ROW_1;keyrow 1, find the col.
MOV P1,#11111011B
;ground row 2
MOV A,P2
;read all columns
ANL A,#00001111B
;mask unused bits
CJNE A,#00001111B,ROW_2;key row 2, find the col.
MOV P1,#11110111B
;ground row 3
MOV A,P2
;read all columns
ANL A,#00001111B
;mask unused bits
CJNE A,#00001111B,ROW_3;keyrow 3, find the col.
LJMP K2
;if none, false input, repeat
ROW_0: MOV DPTR,#KCODE0
;set DPTR=start of row 0
SJMP FIND
;find col. key belongs to
ROW_1: MOV DPTR,#KCODE1
;set DPTR=start of row 1
SJMP FIND
;find col. key belongs to
ROW_2: MOV DPTR,#KCODE2
;set DPTR=start of row 2
SJMP FIND
;find col. key belongs to
ROW_3: MOV DPTR,#KCODE3
;set DPTR=start of row 3
FIND: RRC A
;see if any CY bit low
JNC MATCH
;if zero, get the ASCII code
INC DPTR
;point to next col. address
SJMP FIND
;keep searching
MATCH: CLR A
;set A=0 (match is found)
MOVC A,@A+DPTR
;get ASCII code from table
MOV P0,A
;display pressed key
LJMP K1
K1:

;ASCII LOOK-UP TABLE FOR EACH ROW


ORG
KCODE0:
KCODE1:
KCODE2:

300H
DB '0','1','2','3'
DB '4','5','6','7'
DB '8','9','A','B'

;ROW 0
;ROW 1
;ROW 2

KCODE3: DB

'C','D','E','F'

;ROW 3

END

PROGRAM: RTC (DS1307) Interfacing with AT89C2051

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%

PORT DECLERATION

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SDA

EQU

P3.5 ;SDA=PIN5

SCL

EQU

P3.4

;SCL=PIN6

DS1307W EQU
TO WRITE

0D0H

; SLAVE ADDRESS 1101 000 + 0

DS1307R EQU
READ

0D1H

; SLAVE ADDRESS 1101 000 + 1 TO

; **********************************************************
; Main Program
; **********************************************************
CALL OSC_CONTROL
REPEAT:

CALL READ_CLOCK

;Initilize the RTC


;Read Clock

AJMP REPEAT
; **********************************************************
; SUB SETS THE DS1307 OSCILLATOR
; **********************************************************
OSC_CONTROL:
ACALL

SEND_START ; GENERATE START CONDITION

MOV

A,#DS1307W ; 1101 0000 ADDRESS + WRITE-

ACALL

SEND_BYTE ; SEND BYTE TO 1307

MOV

A,#00H

BIT

ACALL
LEAVE

; ADDRESS BYTE TO REGISTER 00H

SEND_BYTE ; SECONDS REGISTER, ALWAYS

SETB

LASTREAD ; REG 00H-BIT #7 = 0 (LOW)

ACALL

SEND_STOP ; IF REG 00H-BIT #7 = 1 CLOCK

ACALL

SEND_START ; OSCILLATOR IS OFF.

MOV

A,#DS1307R ; 1101 0001 ADDRESS + READ-

ACALL

SEND_BYTE ;

ACALL

READ_BYTE ; READ A BYTE FROM THE 1307

BIT

CLR

ACC.7

OSC_SET:

; CLEAR REG 00H-BIT #7 TO ENABLE


; OSCILLATOR.

PUSH

ACC

ACALL

SEND_STOP ;

ACALL

SEND_START ;

MOV

A,#DS1307W ; SETUP TO WRITE

ACALL

SEND_BYTE ;

MOV

A,#00H

ACALL

SEND_BYTE ;

POP

ACC

; SAVE ON STACK

; REGISTER 00H ADDRESS

; GET DATA TO START OSCILLATOR

ACALL

SEND_BYTE ; SEND IT

ACALL

SEND_STOP

RET
; **********************************************************
; THIS SUB CONTROLS THE SQW OUTPUT 1HZ
; **********************************************************
SQW_CONTROL_1HZ:
LCALL SEND_START

; SEND START CONDITION

MOV A,#DS1307W

; SET POINTER TO REG 07H

ON
; DS1307
LCALL SEND_BYTE
MOV A,#07H
LCALL SEND_BYTE
MOV A,#90H
JNB SQW,SQW_SET
MOV A,#80H

; SQW/OUT ON AT 1HZ
; JUMP IF SQW BIT IS ACTIVE
; TURN SQW/OUT OFF OFF HIGH

SQW_SET:
LCALL SEND_BYTE
LCALL SEND_STOP
RET
; **********************************************************
; THIS SUB READS ONE BYTE OF DATA FROM THE DS1307
; **********************************************************
READ_BYTE:
MOV

BITCNT,#08H; SET COUNTER FOR 8-BITS DATA

MOV

A,#00H

SETB

SDA

; SET SDA HIGH TO ENSURE LINE


; FREE

READ_BITS:
SCL_HIGH
MOV

; TRANSITION SCL LOW-TO-HIGH

C,SDA

RLC

CLR

SCL

DJNZ

; MOVE DATA BIT INTO CARRY


; ROTATE CARRY-BIT INTO ACC.0
; TRANSITION SCL HIGH-TO-LOW

BITCNT,READ_BITS
; LOOP FOR 8-BITS

JB

LASTREAD,ACKN
; CHECK TO SEE IF THIS IS
; THE LAST READ

CLR

SDA

; IF NOT LAST READ SEND ACK-BIT

ACKN:
SCL_HIGH
ACKNOWLEDGE
CLR

SCL

; PULSE SCL TO TRANSMIT


; OR NOT ACKNOWLEDGE BIT

RET
; **********************************************************
; SUB SENDS START CONDITION
; **********************************************************

SEND_START:
SETB

_2W_BUSY ; INDICATE THAT 2-WIRE

CLR

ACKS

; OPERATION IS IN PROGRESS

CLR

BUS_FLT

; CLEAR STATUS FLAGS

JNB

SCL,FAULT

JNB

SDA,FAULT

SETB

SDA

; BEGIN START CODITION

SCL_HIGH
CLR

SDA

ACALL
CLR

DEELAY
SCL

RET
FAULT:
SETB

BUS_FLT

RET
; **********************************************************
; SUB SENDS STOP CONDITION
; **********************************************************
SEND_STOP:
CLR

SDA

SCL_HIGH
SETB
CLR
RET

SDA
_2W_BUSY

; **********************************************************
; SUB DELAYS THE BUS
; **********************************************************
DEELAY:
NOP

; DELAY FOR BUS TIMING

RET
; **********************************************************
; THIS SUB SENDS 1 BYTE OF DATA TO THE DS1307
; CALL THIS FOR EACH REGISTER SECONDS TO YEAR
; ACC MUST CONTAIN DATA TO BE SENT TO CLOCK
; **********************************************************
SEND_BYTE:
MOV

BITCNT,#08H; SET COUNTER FOR 8-BITS

SB_LOOP:
JNB

ACC.7,NOTONE; CHECK TO SEE IF BIT-7 OF

SETB
JMP

SDA

; ACC IS A 1, AND SET SDA HIGH

ONE

NOTONE:
CLR

SDA

; CLR SDA LOW

ONE:
SCL_HIGH

; TRANSITION SCL LOW-TO-HIGH

RL

CLR

SCL

DJNZ

; ROTATE ACC LEFT 1-BIT


; TRANSITION SCL LOW-TO-HIGH

BITCNT,SB_LOOP; LOOP FOR 8-BITS

SETB

SDA

; SET SDA HIGH TO LOOK FOR

SCL_HIGH

; ACKNOWLEDGE PULSE

CLR

ACKS

JNB

SDA,SB_EX ; CHECK FOR ACK OR NOT ACK

SETB

ACKS

; SET ACKNOWLEDGE FLAG FOR

; NOT ACK
SB_EX:
CALL

DEELAY

CLR

SCL

CALL

DEELAY

; DELAY FOR AN OPERATION


; TRANSITION SCL HIGH-TO-LOW
; DELAY FOR AN OPERATION

RET
; **********************************************************
; SUB READS THE CLOCK AND WRITES IT TO THE
SCRATCHPAD MEMORY
; ON RETURN FROM HERE DATE & TIME DATA WILL BE
STORED IN THE
; DATE & TIME REGISTERS FROM 24H (SECS) TO 2AH (YEAR)
; ALARM SETTINGS IN REGISTERS 2CH(HRS) AND
2DH(MINUTES).
; **********************************************************
READ_CLOCK:
MOV

R1,#SECS ; SECONDS STORAGE LOCATION

MOV

BYTECNT,#00H

CLR

LASTREAD

CALL

SEND_START

MOV
CALL
MOV

A,#DS1307W
SEND_BYTE
A,#00H

CALL

SEND_BYTE

CALL

SEND_STOP

CALL

SEND_START

MOV
CALL

A,#DS1307R
SEND_BYTE

READ_LOOP:
MOV

A,BYTECNT

CJNE

A,#09H,NOT_LAST

SETB

LASTREAD

NOT_LAST:
CALL

READ_BYTE

MOV

@R1,A

MOV

A,BYTECNT

CJNE

A,#00H,NOT_FIRST

MOV

A,@R1

CLR
MOV

ACC.7
@R1,A

NOT_FIRST:
INC

R1

INC

BYTECNT

; ENSURE OSC BIT=0 (ENABLED)

MOV

A,BYTECNT

CJNE

A,#0AH,READ_LOOP

CALL

SEND_STOP

RET
;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
;

STORE THE TIME TO RTC CHIP

;
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
STORE_RTC:
LCALL SEND_START
CONDITION

; SEND 2WIRE START

MOV A,#DS1307W
COMMAND
LCALL SEND_BYTE
MOV A,#01H
TO BEGINNING

; LOAD DS1307 WRITE


; SEND WRITE COMMAND
; SET DS1307 DATA POINTER

LCALL SEND_BYTE
MOV A,MINS

; OF 00H
; Send min

LCALL SEND_BYTE
MOV A,HRS
SETB ACC.6

;send hr
;12 HR MODE

JNB AMS,YUH
CLR ACC.5

;AM/PM

1=PM,0=AM

AJMP YUH1
YUH: SETB ACC.5
YUH1:

LCALL SEND_BYTE

MOV A,DAY

; Send Day

LCALL SEND_BYTE
MOV A,DATE1

; Send date

LCALL SEND_BYTE
MOV A,MONTH

; Send month

LCALL SEND_BYTE
MOV A,YEAR

; Send yr

LCALL SEND_BYTE
LCALL SEND_STOP
CONTION

; SEND 2WIRE STOP

RET
;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Anda mungkin juga menyukai