Microcontroller Lab Manual With Arduino Programe PDF
Microcontroller Lab Manual With Arduino Programe PDF
MICRO-CONTROLLER (8051)
LAB MANUAL
1
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
2
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-1
AIM: To study the architecture of 8051 µC
THEORY:
Introduction:
The Intel MCS-51 (commonly referred to as 8051) is a
Harvard architecture, single chip microcontroller (µC)
series which was developed by Intel in 1980 for use in
embedded systems. Intel's original versions were popular
in the 1980s and early 1990s. While Intel no longer
manufactures the MCS-51, binary compatible derivatives
remain popular today.
Intel's original MCS-51 family was developed using NMOS technology, but later versions, identified
by a letter C in their name (e.g., 80C51) used CMOS technology and consumed less power than their
NMOS predecessors. This made them more suitable for battery-powered devices.
Features:
8-bit ALU, Accumulator and 8-bit Registers; hence it is an 8-bit microcontroller
8-bit data bus – It can access 8 bits of data in one operation
3
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Pin diagram:
Pins 1-8: Port 1 Each of these pins can be configured as an input or an output.
Pin 9: RS A logic one on this pin disables the microcontroller and clears the contents of most
registers. In other words, the positive voltage on this pin resets the microcontroller. By applying logic
zero to this pin, the program starts execution from the beginning.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general input or output. Besides, all
of them have alternative functions:
Pin 10: RXD Serial asynchronous communication input or Serial synchronous communication
output.
Pin 11: TXD Serial asynchronous communication output or Serial synchronous
communication clock output.
Pin 12: INT0 Interrupt 0 input.
Pin 13: INT1 Interrupt 1 input.
Pin 14: T0 Counter 0 clock input.
Pin 15: T1 Counter 1 clock input.
Pin 16: WR Write to external (additional) RAM.
Pin 17: RD Read from external RAM.
Pin 18, 19: X2, X1 Internal oscillator input and output. A quartz crystal which specifies operating
frequency is usually connected to these pins. Instead of it, miniature ceramics resonators can also be
used for frequency stability. Later versions of microcontrollers operate at a frequency of 0 Hz up to
over 50 Hz.
4
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are configured as
general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses A8-
A15 will appear on this port. Even though memory with capacity of 64Kb is not used, which means
that not all eight port bits are used for its addressing, the rest of them are not available as
inputs/outputs.
Pin 29: PSEN If external ROM is used for storing program then a logic zero (0) appears on it every
time the microcontroller reads a byte from memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller puts the lower address byte
(A0-A7) on P0 and activates the ALE output. After receiving signal from the ALE pin, the external
register (usually 74HCT373 or 74HCT375 add-on chip) memorizes the state of P0 and uses it as a
memory chip address. Immediately after that, the ALU pin is returned its previous logic state and P0 is
now used as a Data Bus. As seen, port data multiplexing is performed by means of only one additional
(and cheap) integrated circuit. In other words, this port is used for both data and address transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and address transmission
with no regard to whether there is internal memory or not. It means that even there is a program
written to the microcontroller, it will not be executed. Instead, the program written to external ROM
will be executed. By applying logic one to the EA pin, the microcontroller will use both memories,
first internal then external (if exists).
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is driven
high (1) or as data output (Data Bus) when the ALE pin is driven low (0).
Memory Organization
The 8051 has two types of memory and these are Program Memory and Data Memory. Program
Memory (ROM) is used to permanently save the program being executed, while Data Memory (RAM)
is used for temporarily storing data and intermediate results created and used during the operation of
the microcontroller. Depending on the model in use (we are still talking about the 8051
microcontroller family in general) at most a few Kb of ROM and 128 or 256 bytes of RAM is used.
However…
5
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
All 8051 microcontrollers have a 16-bit addressing bus and are capable of addressing 64 kb memory.
It is neither a mistake nor a big ambition of engineers who were working on basic core development. It
is a matter of smart memory organization which makes these microcontrollers a real “programmers’
goody“.
EA=0 In this case, the microcontroller completely ignores internal program memory and executes
only the program stored in external memory.
EA=1 In this case, the microcontroller executes first the program from built-in ROM, then the
program stored in external memory.
In both cases, P0 and P2 are not available for use since being used for data and address transmission.
Besides, the ALE and PSEN pins are also used.
6
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Interrupts
There are five interrupt sources for the 8051, which means that they can recognize 5 different events
that can interrupt regular program execution. Each interrupt can be enabled or disabled by setting bits
of the IE register. Likewise, the whole interrupt system can be disabled by clearing the EA bit of the
same register. Refer to figure below.
Now, it is necessary to explain a few details referring to external interrupts- INT0 and INT1. If the IT0
and IT1 bits of the TCON register are set, an interrupt will be generated on high to low transition, i.e.
on the falling pulse edge (only in that moment). If these bits are cleared, an interrupt will be
continuously executed as far as the pins are held low.
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
7
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-2
AIM: Basic Tutorial for Keil Software
APPARTUS:
Minimum PC requirement:
Windows XP SP2, Windows Vista or Windows 7
Mouse or Similar Pointing Device
Available Hard Disk Space
C51: 200Mbytes
C251: 100Mbytes
C166: 100Mbytes
MDK: 1.4GBytes
1GBytes of RAM (2GB recommended)
Pentium Class PC
THEORY:
The sample program included in the tutorial toggles Ports 1 and 2 on the 8051
Basic Keil Tutorial
Open Keil from the Start menu
The Figure below shows the basic names of the windows referred in this document
8
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
9
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
4. Click on File menu and select Save As… .ASM and SAVE it
10
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
8. Expand the Source Group 1 in the Tree menu to ensure that the file was added to the project
11
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
4. Select an Output tab and click on Creating Hex file, with Hex file name and target folder.
(Note: Here make sure to rename Hex file name for different .ASM files for same Project .uv2
file, otherwise same Hex file will be replaced by new rebuild program)
5. Click on Project Menu and select
12
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Edsim Snapshot:
CONCLUSION:
13
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-3
AIM: Introduction to .ASM programming.
Solution: - here one is internal memory location and other is memory external location. so first the
content of ext memory location FF00h is loaded in acc. then the content of int memory location FFh is
saved first and then content of acc is transferred to FFh. now saved content of FFh is loaded in acc and
then it is transferred to FF00h.
Solution: –first we shall get the upper nibble of r7 in r6. Then we swap nibbles of r7 and make OR
operation with r6 so the upper and lower nibbles are duplicated
Solution: - first we shall clear the carry. Then subtract the lower bytes afterward then subtract higher
bytes.
14
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Programme 4: divide the content of r0 by r1. Store the result in r2 (answer) and r3 (reminder). Then
restore the original content of r0.
Solution:-after getting answer to restore original content we have to multiply answer with divider and
then add reminder in that.
Solution: - here we have to transfer 10 data bytes from internal to external RAM. So first, we need
one counter. Then we need two pointers one for source second for destination.
Programme 6: find out how many equal bytes between two memory blocks 10h to 20h and 20h to
30h.
Solution: - here we shall compare each byte one by one from both blocks. Increase the count every
time when equal bytes are found
15
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Ex: To Practice 8051 program based on instructions like arithmetic and logical operation.
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
16
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-4
AIM: Write & run programs using MOV instructions- Direct Register and Immediate addressing
mode and Indirect addressing mode.
THEORY :
The 8051 provide a total of five distinct addressing modes. They are as follows.
1. Immediate addressing mode.
2. Register addressing mode.
3. Direct addressing mode.
4. Register addressing mode.
5. Indirect addressing mode.
In this addressing mode, the source operand is a constant, i.e. immediate data. Note that
the immediate data must be preceded by the pound sing “ # “ .
Example
MOV A,#82H ; load 82H into A.
MOV R4,#100 ; load the decimal value 100 into R4.
MOV P1,#55H ; load 55H directly to port 1.
Example
MOV A,RO ; Copy the contents of R0 into A.
MOV R7,DPL ; Load R7 with contents of DPL,
Example :
MOV R0,40H : save data of RAM location 40H in R0,
MOV 03H,04H : it copies data in R4 to R3.
17
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
The data.
It is symbol is “@”,
Example :
MOV @R1,B : move data of B into RAM location whose
Address is held by R1,
Example :
MOV A,@A+DPTR
MOV A,@A+PC
Program 01 : Place the number 3Bh in internal RAM location 40h to 41h.
Solution :
Result :
RAM Address Data
40h 3Bh
41h 3Bh
42h 3Bh
Solution :
Program 03 : Copy the data in Internal RAM location 10h to 13h to Internal
RAM location 20h to 23h : Copy from 10h to 20h, 11h to 21h,
And so on.
Solution :
MOV 20H,10H : Copy data from RAM location 10h to 20h.
18
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Result :
Solution :
MOV DPTR,#0A397h : DPTR pointer at ext RAM Loc. A397H
MOVX A,@DPTR : Get data in accumulator.
MOV R7,A : Load data at R7 register.
Result:
[A] Before Execution [B] After Execution.
External RAM addr. Data Register Data
(A397)h 75h R5 75h
Program 05 : Copy the byte at Internal RAM address 27h to external RAM
Address 27h.
Solution :
MOV A, 27H : get the data from 27h.
MOV R0,#27H : R0 used as a pointer for RAM address 27h.
MOVX @R0,A : store the data at ext. RAM address 27h.
Result :
[A] Before Execution [B] After Execution
Internal RAM addr. Data External RAM addr. Data
(27)h 99h (27)h 99h
Program 06 : Copy the data in external RAM Location 0123h to TL0 and
Data external RAM location 0234h to TH0.
Solution :
MOV DPTR,#0123H : DPTR pointer at 0123H
MOVX A,DPTR : get data in register A
MOV TL0,A : put result at TL0
MOV DPTR,#0234H : DPTR pointer at 0234H
MOVX A,@DPTR : get data in register A
MOV TH0,A : put result at TH0
Result :
19
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Program 07 : copy the data byte at address 2050h to external RAM address
2050h.
Solution :
MOV A, #00H : clear accumulator
MOV DPTR,#2050H : DPTR pointer at 2050h
MOVC A,@A+DPTR : get data external ROM
MOVX @DPTR,A : Put the data at external RAM
Result :
[A] Before Execution [B] After Execution
Internal RAM addr. Data External RAM addr. Data
(2050)h 3Eh (2050)h 3Eh
Solution :
MOV A,#00H : Clear accumulator
MOV DPTR,#0030H : DPTR pointer at ext ROM addr 0040h
MOVC A,@A+DPTR : Get data ext ROM addr 0040h
MOV IE,A : Put the result at IE register
Result :
[A] Before Execution [B] After Execution
External ROM addr. Data Register Data
(0030)h 78h IE 78h
Solution :
MOV A,R0 : get the data from Ro
MOV A,R1 : exchange between A and R1
MOV R0,A : store the result in R0
Result :
[A] Before Execution [B] After Execution
Register Data Register Data
R0 34h R0 5Ah
R1 5Ah R1 34h
20
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Program 10 : Exchange both low nibbles of register R5 and R6: put the low
Nibble of R5,and the low nibble of R6 and R5.
Solution :
MOV A,R5 : Get the data from R5
MOV R0,#06H : R0 used for indirect operation for R6
XCHD A,@R0 : exchange the low nibbles of A and R6
MOV R5,A : put the result in R5
Result :
[A] Before Execution [B] After Execution
Register Data Register Data
R5 75h R5 79h
R6 89h R6 85h
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
21
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-5
AIM: Write & Run program using PUSH and POP instruction.
THEORY:
A push opcode copies data from the source address to the stack. SP is in cerement by 1 before
the data is copied to the internal RAM location contain in to the Internal RAM. The stack grows up in
memory as it is push. Excessive pushing can make the stack exceed 7FH, after which PUSH hed data
is lost
A push opcode copies data from the stack to the destination address SP is decremented by 1
after data is copied from the stack RAM location to the direct destination to ensure that data placed on
the stack is retrived in the sane order as it was stored.
MNEMONIC OPERATION
The SP register is set to 07h when the 8051 is reset, which is the same direct address
RAM as register R7 in bank 0. The first push opcode would write data to R0 of bank 1.
Program 01 : copy the byte in TCON to register R2 using at least fore different method.
22
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
23
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-6
AIM: Write & Run Programs using Logical Instructions.
THEORY:
[ A. AND LOGIC ]
1. ANL A,#DATA
2. ANL A,Rn
3. ANL A,direct
4. ANL A,@Rp
5. ANL direct,A
6. ANL drict,#DATA
[B. OR LOGIC]
1. ORL A, #DATA
2. ORL A,Rn
3. ORL A, direct
4. ORL A.@Rp
5. ORL driect,A
6. ORL driect,#Data
1. XRL A,#DATA
2. XRL A,Rn
3. XRL A,direct
4. XRL A,@Rp
5. XRL direct,A
6. XRL dricet,#data
1. CLR A
2. CPL A
1. ANL C,BIT
2. ANL C,/BIT
3. ORL C,BIT
4. ORL C,/BIT
5. CPL C
6. CPL BIT
7. CLR C
8. CLR BIT
9. STEB C
10. STEB BIT
24
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
1. RL A
2. RR A
3. RLC A
4. RRC A
5. SWAP A
Program 01 : Invert the data on the port 0 pins & write the data to port 1.
Program 02 : swap the nibbles of R0 and R1 , so that the lower nibble of R0 swaps with higher
nibble of R1 & vice-versa.
Program 03 : make the low nibble of R5 the complement of the high nibble of R6.
Program 04 : find a number that when xo red to A register , result in the number 3fh in A.
RESULT :
REGISTER DATA
A 3FH
25
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
RESULT :
REGISTER DATA
A 11000100
program 06 : rotate the contents of four times left and add it in the content of R1 and store the
result in internal RAM location 2AH.
RESULT :
INTENAL RAM DATA
2AH 5CH
Program 07 : double the number in reg. R2 ; and put the result in register R3 (high byte)
RESULT :
REGISTER DATA
R3 01H
R4 28H
26
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
RESULT :
REGISTER VALUE
DPTR 2468H
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
27
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-7
AIM: Write & Run programs using Arithmetic Instructions.
THEORE:
AIRTHMETIC INSTRUCTION
[ A. INCREMENT]
1. Inc A
2. Inc Rn
3. Inc add
4. Inc DPTR
[B. DECREMENT]
1. DEC A
2. DEC Rn
3. DEC add
4. DEC @Rp
[C. ADDITION]
1. ADD A,#DATA
2. ADD A,Rn
3. ADD A,DRIECT
4. ADD A,@Rp
[G. MULTIPLICATION]
1. MUL AB
[H. DIVISION]
1. DIV AB
Program o1 : Add the num found in Ram location 25h,26h and 27h put the result in ram
location 31h (LSB) and (MSB)
28
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
RESULT :
RAM ADDR. DATA
30H(LSB) C2H
31H(MSB) O1H
Program 02 : Add the byte in register R0 and R1 : put the resulet in external RAM
RESULT :
EXTENAL RAM ADDR DATA
2050 (LSB) F2H
2051 (MSB) 00H
Program 03 : subtract the connect of RAM location 13h from RAM location 2Bh; put result in
RAM location 3Ch.
RESULT :
RAM ADDR DATA
3CH 5FH
Program 04 : subtract the connect of R2 from the number F3h; put result in external RAM
location 028Bh
29
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
RESULET :
EXTENAL RAM ADDERSS DATA
028B H 2EH
Program 05 : Increment the content of RAM location 13h,14h and 15h using increment
addressing only.
RESULT :
RAM LOCATION DATA
13H C6H
14H 51H
15H 4FH
RESULT :
Then TL0=1oh, TH0=21H, TL1=32H AND TH1=43H
Program 07 : multiply the using number in register R3 by the unigened number on port 2 and
put the result in external RAM location 10H(MSB) and 11H(LSB)
RESULT :
EXTENAL RAM ADDERSS DATA
0010H (MSB) 02H
0011H (LSB) A0H
Program 08 : divide data in internal RAM location 45H by data 12H put quotient in external
RAM location 4545H and reminder in external RAM location 4546H.
30
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
DIV AB : divide
MOV DPTR,#4545H : DPTR pointer to 4545h
MOV @DPTR ,A : store result at external memory
INC DPTR : increment DPTR
MOV A,B : get remainder at A
MOV @DPTR,A : store remainder at next memory location
RESULT :
EXTENAL RAM ADDR. DATA
4545H 01H
4546H 03H
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
31
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-8
AIM: Write & run Programs using Looping Techniques.
THEORY:
Let us start with a simple example that will help you to learn how to create loops in assembly. In the
following code the instruction DJNZ is used to reduce the counter and is repeated till the counter
becomes zero.
Eg-1:
ORG 0H
MOV A, #0 ; clear A
MOV R1, #10 ; load counter R1 =10
AGAIN: ADD A, # 05 ; add five to register A
DJNZ R1, AGAIN ; repeat until R1=0 (10 times)
MOV R3, A ; save A in R3
END
In this code R1 acts as a counter. The counter value is initialized i.e. 10 HEX is loaded to R1. In each
iteration, the instruction DJNZ decrements R1 by one until it becomes zero. This loop adds 5 HEX to
A every time it runs. After ten iterations R1 becomes zero and the instructions below it are executed.
Note: - Some Jump statements can only be performed on some special register A (or bit CY) as
mentioned in the table below.
32
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Nested loops:
ORG 0H
MOV A, #55H ; A= 55 hex
MOV R1, #100 ; the outer counter R1 =100
NEXT: MOV R2, # 20 ; the inner counter
AGAIN: CPL A, # 05 ; add five to register A
DJNZ R2, AGAIN ; repeat until R1=0 (100 times)
DJNZ R1, NEXT ; repeat till 20 times (outer loop)
END
SJMP refers to short jump and LJMP refers to long jump. All the conditional jumps are short jumps.
SJMP: This instruction is of two bytes in which first one is opcode & second is the address. The
relative address of the instruction called should be in between -127 to 127 bytes from the current
program counter (PC).
LJMP: This instruction is of three bytes in which the first is the opcode and the second & third are for
address. The relative address of the instruction can be anywhere on the ROM.
So it is clear from the above examples that we can use different jump instructions with a
condition or counter called conditional loop. And when we create loop inside an existing loop it
is called nested loop.
CALL INSTRUCTIONS:
Example:
ORG 0H
BACK : MOV A, #55H ; load A= 55 hex value
MOV P1, A ; issue value of register A to port1
LCALL DELAY ; to call DELAY function created below
MOV A, #0AAH ;load AAH hex value to A
MOV P1,A ;issue value of register A to port 1
LCALL DELAY ; to call DELAY function as created below
SJMP BACK ; keep doing this
; ________ this is the delay subroutine
DELAY:
MOV R5, #0FFH ; R5= 255 hex, the counter
AGAIN: DJNZ R5, AGAIN ; stay here until R5 becomes zero
RET ; return to caller
END
33
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
In this code we keep on toggling the value of the register of port 1 with two different hex values and a
DELAY subroutine is used to control how fast the value is changing. Here in DELAY subroutine the
program is kept busy by running an idle loop and counting 256 counts. After the DELAY subroutine
is executed once the value of port 1 is toggled and this process goes on infinitely.
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
34
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-9
AIM: Write & Run Programs using Timers in different mode.
THEORY:
For the CPU to execute an instruction takes a certain number of clocks cycle 8051 family.
These clock cycles are referred to as machine cycles. To calculate a time delay.
We use this list. In the 8051 family the length of the machine cycle depends on the frequency
of the crystal oscillator connected the 8051 system. The crystal oscillator along with on-chip circuitry
provides the clock source for 8051 CPU.
The frequency of the craystal connects to the 8051 family can very from 4mhz to 30mhz
depanding on the chip rating and manufacture very often the11.092 MHz crystal oscillator is used to
make the 8051 base system compatible with the serial part of the IBM pc.
Thus. For an 8051 clocked at 12 MHz. the instruction. Cycle time is one microsecond.
Example
Find the size of the delay in the following program. If the crystal frequency is 11.0592 MHz
MOV A,#55H
AGAIN: MOV P1,A
ACALL DELAY
CPL A
SJMP AGAIN
;--------------------- TIME DELAY
DELAY: MOV R3, #200
HERE : DJNZ R3, HEAR
RET
Ans:
We have the following machine cycle for each instruction of the DELAY subroutine
machine cycle
35
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
Example
For a machine cycle of 1 Us find the time delay in following subroutine.
Machine cycle
DELAY : MOV R2, #200 1
AGAIN : MOV R3,#250 1
HERE : NOP 1
NOP 1
DJNZ R3,HERE 2
DJNZ R2,AGAIN 2
RET
Ans:
For the here loop we have
(4*250)*1 us = 1000us = 1Ms
The AGAIN loop repeat the HERE loop 200 times =200*100us
=200 Ms
It is a 16-bit timer : there for. It allows values of 0000 to FFFFH to be loaded in to the timer
register TL and TH.
Example
Indicate which mode and which timer are selected for each of the following
(A) MOV TOMD, #01H
(B) MOV TMOD,#20H
(C) MOC TMOD,#12H
Solution :
Program : write a program for counter 1 in mode 2 to count the pulses and display the state of TL1
and count on P2 . assume that clock pulses are fed in to pin T1.
MOV TMOD,#01100000B
MOV TH1,#00H
SETB P3.5
36
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
37
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-10
AIM: To study the interfacing of LED with µC 89C51
APPARATUS:
Micro controller 89C51
LED
12 MHz crystal
7805 voltage regulator IC
Bridge rectifier IC
33 PF capacitor
10 microf/63V capacitor
8K2 resistor
1K resistors
THEORY:
LEDs are by far the most widely used means of taking output. They find huge application as indicators
during experimentations to check the validity of results at different stages. They are very cheap and
easily available in a variety of shape, size and colors.
The principle of operation of LEDs is simple. The commonly available LEDs have a drop voltage of
1.7 V and need 10mA to glow at full intensity. The following circuit describes “how to glow a led”.
The value of resistance R can be calculated using the equation, R= (V-1.7)/10 mA. Since most of the
controllers work on 5V, so substituting V= 5V, the value of resistance comes out to be 330 ohm. The
resistance 220 ohm, 470 ohm is commonly used substitute in case 330 ohm is not available.
CIRCUIT DIAGRAM:
LEDs are connected to the port P0. LEDs need approximately 10mA current to flow through them in
order to glow at maximum intensity. However the output of the
controller is not sufficient enough to drive the LEDs, so if the
positive leg of the LED is connected to the pin and the negative
to ground as shown in the figure, the LED will not glow at full
illumination.
To overcome this problem LEDs are connected in the reverse
order and they run on negate.ve logic i.e., whenever 1 is given
38
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
on any pin of the port, the LED will switch off and when logic 0 is provided the LED will glow at full
intensity.
As soon as we provide supply to the controller, the LEDs start blinking i.e., they become on for a
certain time duration and then become off for the same time duration. This delay is provided by calling
the delay function.
ALGORITHM:
Start
Turn ON LED
Go to start
You can see in the modified algorithm that after turning ON the LED the controller waits for the delay
period & then turns OFF the led & again waits for the delay period & then goes back to the start.
39
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
PROGRAM:
ORG 0000h
loop: CLR P1.0
CALL DELAY
SETB P1.0
CALL DELAY
JMP loop
void setup()
{
pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
pinMode(12, OUTPUT); // initialize digital pin 12 as an output.
}
40
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CIRCUIT DAIGRAM :-
Interfacing of Two Different LED’S with Arduino Uno R3 Board.
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
41
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-11
AIM: To study the interfacing of relay with 8051 microcontroller.
APPARTUS:
Micro controller 89C51
Relay
12 MHz crystal
7805 voltage regulator IC
Bridge rectifier IC
33 PF capacitor
10 µF/63V capacitor
8K2 resistor
1K resistors
THEORY:
A type of relay that can handle the high power required to directly control an electric motor is called a
contractor. Solid-state relays control power circuits with no moving parts, instead using a
semiconductor device to perform switching. Relays with calibrated operating characteristics and
sometimes multiple operating coils are used to protect electrical circuits from overload or faults; in
modern electric power systems these functions are performed by digital instruments still called
"protective relays".
A simple electromagnetic relay consists of a coil of wire surrounding a soft iron core, an iron yoke
which provides a low reluctance path for magnetic flux, a movable iron armature, and one or more sets
of contacts (there are two in the relay pictured). The armature is hinged to the yoke and mechanically
linked to one or more sets of moving contacts. It is held in place by a spring so that when the relay is
de-energized there is an air gap in the magnetic circuit. In this condition, one of the two sets of
42
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
contacts in the relay pictured is closed, and the other set is open. Other relays may have more or fewer
sets of contacts depending on their function. The relay in the picture also has a wire connecting the
armature to the yoke. This ensures continuity of the circuit between the moving contacts on the
armature, and the circuit track on the printed circuit board (PCB) via the yoke, which is soldered to the
PCB.
When an electric current is passed through the coil it generates a magnetic field that attracts the
armature and the consequent movement of the movable contact either makes or breaks (depending
upon construction) a connection with a fixed contact. If the set of contacts was closed when the relay
was de-energized, then the movement opens the contacts and breaks the connection, and vice versa if
the contacts were open. When the current to the coil is switched off, the armature is returned by a
force, approximately half as strong as the magnetic force, to its relaxed position. Usually this force is
provided by a spring, but gravity is also used commonly in industrial motor starters. Most relays are
manufactured to operate quickly. In a low-voltage application this reduces noise; in a high voltage or
current application it reduces arcing.When the coil is energized with direct current, a diode is often
placed across the coil to dissipate the energy from the collapsing magnetic field at deactivation, which
would otherwise generate a voltage spike dangerous to semiconductor circuit components. Some
automotive relays include a diode inside the relay case. Alternatively, a contact protection network
consisting of a capacitor and resistor in series (snubber circuit) may absorb the surge. If the coil is
designed to be energized with alternating current (AC), a small copper "shading ring" can be crimped
to the end of the solenoid, creating a small out-of-phase current which increases the minimum pull on
the armature during the AC cycle.
CIRCUIT DIAGRAM:
43
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
WORKING:
Microcontrollers have internal pull up
resistors hence when a port pin is HIGH
the output current flows through this
internal pull up resistor. 8051
microcontrollers have an internal pull up
of 10KΩ. Hence the maximum output
current will be 5v/10k = 0.5ma. This
current is not sufficient to drive the
transistor into saturation and turn ON the
relay. Hence an external pull up resistor
R3 is used. Let us now calculate the value of R3. Normally a relay requires a pull in current of 70ma
to be turned ON. So our BC547 transistor will require enough bases current to make sure it remains
saturated and provide the necessary collector current i.e. 70ma. The gain (hfe) of BC547 is 100 so we
need to provide at least 70ma/100 = 0.7ma of base current. In practice you require roughly double the
value of this current so we will calculate for 1.4ma of base current.
From the above equation the value of R3 comes out to be 5.55KΩ. Typically I use 4.7KΩ resistor.
Whenever 8051 microcontroller is turned ON initially the controller is in reset state and all the
controller pins are HIGH which would result in TURNING ON the relay every time power is turned
ON or if there is a power fluctuation. This may also damage the device connected to relay so as to
avoid this problem another transistor Q2 has been added between the controller & the previous
transistor. This transistor acts as an inverter. Figure 2 shows the updated Relay Driver Circuit. So now
when a High is applied from the controller the TRANSISTOR Q2 turns ON so the base of transistor
Q1 gets 0 so the transistor Q1 turns OFF so the relay turns OFF. And when a LOW is applied from the
controller the TRANSISTOR Q2 turns OFF so the base of transistor Q1 gets high voltage through the
resistor R2 so the transistor Q1 turns ON which turns ON the relay.
PROGRAME:
relay equ P1.0
sw equ P2.0
org 0000h
44
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
45
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-12
AIM: To study the interfacing of LCD with µC 89C51
APPARTUS:
Micro controller 89C51
LCD
12 MHz crystal
7805 voltage regulator IC
Bridge rectifier IC
33 PF capacitor
10 µF/63V capacitor
8K2 resistor
1K resistors
THEORY:
Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for
debugging purpose.
These LCD's are very simple to interface with the controller as well as are cost effective.
46
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
PIN DISCRIPTION:
When RS is low (0), the data is to be created as a command. When RS is high (1), the data being sent
is considered as text data which should be displayed on the screen.
When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high
(1), the program is effectively reading from the LCD. Most of the times there is no need to read from
the LCD so this line can directly be connected to GND thus saving one controller line.
The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required
to latch the data. The LCD interprets and executes our command at the instant the EN line is brought
low. If you never bring EN low, your instruction will never be executed.
For Contrast setting a 10K pot should be used as shown in the figure.
In this interfacing we used the port 1 & port 3. The data line D0 to D7 of LCD is connected to the port
1.
The chip enable signal, RS signal and R/W signal is connected to the port number 3.
COMMAND OF LCD:
47
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CIRCUIT DIAGRAM:
PROGRAME:
ORG 0000H ;
MOV A, #38H ; INITIATE LCD
ACALL COMMWRT ;
ACALL DELAY ;
AGAIN :
SJMP AGAIN ;
COMMWRT:
MOV P1, A ;
CLR P2.0 ; RS = 0 FOR COMMAND REGISTER
CLR P2.1 ; R/W = 0 FOR WRITE
SETB P2.2 ; E = 1 FOR HIGH PULSE
ACALL DELAY ;
CLR P2.2 ; E = 0 FOR LOW PULSE
RET
DATAWRT:
MOV P1, A ;
SETB P2.0 ; RS = 1 FOR DATA REGISTER
CLR P2.1 ; R/W = 0 FOR WRITE
SETB P2.2 ; E = 1 FOR HIGH PULSE
ACALL DELAY ;
CLR P2.2 ; E = 0 FOR LOW PULSE
RET
48
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
DELAY :
MOV R3, #50H ;
BACK:
MOV R4, #255H ;
HERE:
DJNZ R4, HERE ;
DJNZ R3, BACK ;
RET
END
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// RS , E , D4 , D5 , D6 , D7 Connected on 12 , 11 , 5 , 4 , 3 , 2 digital pins of Arduino respectively.
void setup ( )
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("EC GPG"); // Print a message to the LCD.
void loop ( )
{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,1);
49
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CIRCUIT DIAGRAM :-
Interfacing of 16 * 2 LCD with Arduino Uno R3 Board.
CONCLUSION:
50
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-13
AIM: To study the interfacing of STEPPER MOTOR with µC 89C51
APPARTUS:
Micro controller 89C51
Stepper motor
ULN2003A
12 MHz crystal
5V regulated supply
33 PF capacitor
10 µF/63V capacitor
8K2 resistor
1K resistors
THEORY:
Uni-polar stepper motor
The unipolar stepper motor has five or six
wires and four coils (actually two coils
divided by center connections on each
coil). The center connections of the coils
are tied together and used as the power
connection. They are called uni-polar
steppers because power always comes in
on this one pole.
Bipolar stepper motor
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar
steppers have no common center connection. They have two independent sets of coils instead. You
can distinguish them from unipolar steppers by measuring the resistance between the wires. You
should find two pairs of wires with equal resistance. If you've got the leads of your meter connected to
two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance
(or no continuity).
51
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CIRCUIT DIAGRAM:
WORKING:
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.
52
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CONCLUSION:
53
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-14
AIM: To interface LDR and seven segment with microcontroller 89C51.
APPARTUS:
Micro controller 89C51
LDR
Seven segment display
SN7447A
12 MHz crystal
5V regulated power supply
33 PF capacitor
330Ω resistor
THEORY:
LDR
A photoresistor or light dependent resistor is a resistor
whose resistance decreases with increasing incident light
intensity; in other words, it exhibits photoconductivity.
A photoresistor is made of a high resistance
semiconductor. If light falling on the device is of high
enough frequency, photons absorbed by the semiconductor
give bound electrons enough energy to jump into the
conduction band. The resulting free electron (and its hole
partner) conduct electricity, thereby lowering resistance.
A photoelectric device can be either intrinsic or extrinsic.
An intrinsic semiconductor has its own charge carriers and is not an efficient semiconductor, e.g.
silicon. In intrinsic devices the only available electrons are in the valence band, and hence the photon
must have enough energy to excite the electron across the entire bandgap. Extrinsic devices have
impurities, also called dopants, added whose ground state energy is closer to the conduction band;
since the electrons do not have as far to jump, lower energy photons (i.e., longer wavelengths and
lower frequencies) are sufficient to trigger the device. If a sample of silicon has some of its atoms
replaced by phosphorus atoms (impurities), there will be extra electrons available for conduction. This
is an example of an extrinsic semiconductor. Photoresistors are basically photocells.
Seven segment display
A seven-segment display (SSD), or seven-segment indicator, is a form of electronic display device for
displaying decimal numerals that is an alternative to the more complex dot-matrix displays. Seven-
54
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
segment displays are widely used in digital clocks, electronic meters, and other electronic devices for
displaying numerical information.
Seven Segment are available in two configuration - (1) Common Anode (2) Common Cathode.
The circuit diagram shows the connections of seven segment to the controller. The pins a to g of the
Seven Segment are connected to the Port P2 of the microcontroller. The common pin of the seven
segment is connected to Vcc. The ‘h’ has not been used, which is the dot pin of the controller.
Since the seven segment display works on negative logic, we will have to provide logic 0 to the
corresponding pin to make an LED glow. Table below shows the hex values used to display the
different digits.
CIRCUIT DIAGRAM:
55
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
PROCEDURE:
1) Connect the set up as shown in the circuit above.
2) Switch ON the supply.
3) Now, when any object passes in front the photocell in the range of 1 ft., the photocell gives
the 12V in the output.
4) Now this 12V is given to 7404 IC which in turn gives 0V as output to the µC.
5) Here, sn7447a IC is used for conversion of BCD to 7 – Segment.
6) Now, as 0V is obtained at µC, 7 – Segment LED is incremented by 1.
7) Thus the passed object is counted.
PROGRAM:
Seven segment Interfacing
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
PROGRAM :- To Display 0 to 9 number with 1 second delay between each number on 7 Segment using
Arduino.
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
56
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop ( ) {
PORTD=0x03;
delay(1000);
PORTD=0x9F;
delay(1000);
PORTD=0x25;
delay(1000);
PORTD=0x0D;
delay(1000);
PORTD=0x99;
delay(1000);
PORTD=0x49;
delay(1000);
PORTD=0x41;
delay(1000);
PORTD=0x1F;
delay(1000);
PORTD=0x01;
delay(1000);
PORTD=0x09;
delay(1000);
}
57
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CIRCUIT DIAGARAM :-
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
58
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
EXPERIMENT-15
AIM: To study the interfacing of ADC-0804 with µC 89C51
APPARTUS:-
Micro controller 89C51
ADC-0804
12 MHz crystal
7805 voltage regulator IC
Bridge rectifier IC
33 PF capacitor
10 microf/63V capacitor
8K2 resistor
1K resistors
THEORY:
ADC0804 is one of the most commonly used analog to digital converter IC. In many applications it is
required to convert the output of the sensor, which is analogue in nature to a digital form. The data in
digital format can then be utilized for further processing by the digital processors. Typical applications
include sound processing, temperature processing etc. This circuit demonstrates the principle and
operation of interfacing a simple ADC 0804 using 8051 microcontroller (AT89C51).
ADC0804 is connected as shown in the circuit diagram. Here the input is taken from a preset, which
gives different analog signals to the ADC. The output pins of the ADC are connected to LEDs. The
control pins of the ADC are connected to the microcontroller AT89C51.
ADC0804 is a single channel analog to digital convertor i.e., it can take only one analog signal. An
ADC has n bit resolution (binary form) where n can be 8,10,12,16 or even 24 bits. ADC 0804 has 8 bit
resolution. The higher resolution ADC gives smaller step size. Step size is smallest change that can be
measured by an ADC. For an ADC with resolution of 8 bits, the step size is 19.53mV (5V/255).
The time taken by the ADC to convert analog data into digital form is dependent on the frequency of
clock source. ADC0804 can be given clock from external source. It also has an internal clock.
However the conversion time cannot be more than110us. To use the internal clock a capacitor and
59
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
resistor is connected to pin 19 and 4 as shown in the circuit diagram. The frequency is given by the
relation f= 1/ (1.1RC). The circuit uses a resistance of 10k and a capacitor of 150pF to generate clock
for ADC0804. Vin, which is the input pin, is connected to a preset to provide analog input.
In this circuit microcontroller is used to provide the control signals to the ADC. CS pin of ADC is
directly connected to ground. The pin P1.1, P1.0 and P1.2 are connected to the pin WR, RD and INTR
of the ADC respectively. When the input voltage from the preset is varied the output of ADC varies
which can be seen on the LCD. When the preset is at minimum level, no LED glows and when the
preset is at maximum position all the LEDs starts glowing.
PIN DIAGRAM:
PIN DISCRIPTION:
Pin No Function Name
2 Input pin; High to low pulse brings the data from internal registers Read
to the output pins after conversion
3 Input pin; Low to high pulse is given to start the conversion Write
60
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
9 Input pin; sets the reference voltage for analog input Vref/2
19 Used with Clock IN pin when internal clock source is used Clock R
PROCEDURE:
The following steps are used to interface the ADC0804.
1) Send a low to high pulse to pin WR to start the conversion.
2) Keep monitoring the INTR pin. If INTR is low, go to next step else keep checking the status.
3) A high to low pulse is sent to the RD pin to bring the converted data on the output pins.
CIRCUIT DIAGRAM:
61
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
PROGRAM:
rd equ P1.0 ;Read signal P1.0
wr equ P1.1 ;Write signal P1.1
cs equ P1.2 ;Chip Select P1.2
intr equ P1.3 ;INTR signal P1.3
org 0H
start: ;Start of Program
acall conv ;Start ADC conversion
acall read ;Read converted value
mov P3,adc_val ;Move the value to Port 3
sjmp start ;Do it again
62
GP GANDHINAGAR EC DEPARTMENT MC 2361101 (SEM-6)
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
______
63