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++);
}
----------------------------------------------------------------------------------------------------------------------------------------------
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
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
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).
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
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.
ASSEMBLY CODE
U EQU 31
L EQU 32
PORT EQU P1
RS EQU P2.0
RW EQU P2.1
EN EQU P2.2
ORG 0000H
CLR RW
ACALL INIT
MOV A, #' '
ACALL LCD_DATA
MOV A, #'E'
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 $
MOV U, A
;return
;make en high
ACALL DELAY
CLR EN
;clear en
ACALL DELAY
RET
;short delay
;return
MOV A, U
;copy u to a
;copy l to a
;return
;copy u to a
;copy l to a
;return
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
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
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
White lead
(End point 2 of
Phase2)
0
0
0
1
Ste
p
1
2
3
4
5
6
7
8
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
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
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:
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.
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).
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
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
PORT DECLERATION
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%
SDA
EQU
P3.5 ;SDA=PIN5
SCL
EQU
P3.4
;SCL=PIN6
DS1307W EQU
TO WRITE
0D0H
DS1307R EQU
READ
0D1H
; **********************************************************
; Main Program
; **********************************************************
CALL OSC_CONTROL
REPEAT:
CALL READ_CLOCK
AJMP REPEAT
; **********************************************************
; SUB SETS THE DS1307 OSCILLATOR
; **********************************************************
OSC_CONTROL:
ACALL
MOV
ACALL
MOV
A,#00H
BIT
ACALL
LEAVE
SETB
ACALL
ACALL
MOV
ACALL
SEND_BYTE ;
ACALL
BIT
CLR
ACC.7
OSC_SET:
PUSH
ACC
ACALL
SEND_STOP ;
ACALL
SEND_START ;
MOV
ACALL
SEND_BYTE ;
MOV
A,#00H
ACALL
SEND_BYTE ;
POP
ACC
; SAVE ON STACK
ACALL
SEND_BYTE ; SEND IT
ACALL
SEND_STOP
RET
; **********************************************************
; THIS SUB CONTROLS THE SQW OUTPUT 1HZ
; **********************************************************
SQW_CONTROL_1HZ:
LCALL SEND_START
MOV A,#DS1307W
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
MOV
A,#00H
SETB
SDA
READ_BITS:
SCL_HIGH
MOV
C,SDA
RLC
CLR
SCL
DJNZ
BITCNT,READ_BITS
; LOOP FOR 8-BITS
JB
LASTREAD,ACKN
; CHECK TO SEE IF THIS IS
; THE LAST READ
CLR
SDA
ACKN:
SCL_HIGH
ACKNOWLEDGE
CLR
SCL
RET
; **********************************************************
; SUB SENDS START CONDITION
; **********************************************************
SEND_START:
SETB
CLR
ACKS
; OPERATION IS IN PROGRESS
CLR
BUS_FLT
JNB
SCL,FAULT
JNB
SDA,FAULT
SETB
SDA
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
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
SB_LOOP:
JNB
SETB
JMP
SDA
ONE
NOTONE:
CLR
SDA
ONE:
SCL_HIGH
RL
CLR
SCL
DJNZ
SETB
SDA
SCL_HIGH
; ACKNOWLEDGE PULSE
CLR
ACKS
JNB
SETB
ACKS
; NOT ACK
SB_EX:
CALL
DEELAY
CLR
SCL
CALL
DEELAY
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
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
MOV
A,BYTECNT
CJNE
A,#0AH,READ_LOOP
CALL
SEND_STOP
RET
;
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
;
;
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
STORE_RTC:
LCALL SEND_START
CONDITION
MOV A,#DS1307W
COMMAND
LCALL SEND_BYTE
MOV A,#01H
TO BEGINNING
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
RET
;$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$