8051-LED Blink and Suqare Wave Generation
8051-LED Blink and Suqare Wave Generation
S.Y.B.Sc. Electronics
LED Blink and Square Wave Generation
Aim: To blink the LED and to generate the square wave using 8051 micro-controller.
Objectives: 1. Study the use of I/O ports.
2. Learn the generation of delay using registers.
3. Generate the square wave of desired duty cycle and observe it on CRO.
4. Compile, Execute and simulate the program.
5. Learn to use programmer for microcontroller.
Block Diagram: Part-A: LED Blink
R
D1
39
P0.0
330 LED
8051 D2
38
R
P0.1 +5 V
330E LED
Procedure:
1) Connect circuit as shown above.
2) Write program in assembly language to blink the LED using delay subroutine.
3) Change the delay by changing values in registers and observe its effect on blinking rate of LED.
4) Burn this program in 89C51 IC using programmer.
5) Make appropriate connections on 8051 development board and observe the output.
Algorithm:
1) Begin program at address 0h
2) in program memory.
3) Set output at pin P0.0 of the micro-controller to 1.
4) Call delay routine of appropriate time in millisecond for ON time.
5) Clear the output of micro-controller’s pin P0.0 to 0.
6) Again, call the same delay routine in milliseconds for OFF time.
7) Repeat steps 1-5 above to loop forever.
8) Repeat the steps 1-6 for pin P0.1.
OR
1) Start at desired address in program memory.
2) Complement any one of the pins of microcontroller.
3) Call delay routine of appropriate time in milliseconds.
4) Repeat steps 1-3 continuously.
Flowchart: a) Main Program Subroutine
Start Start
Is NO
Main Program R4=0
b) ??
Start YES
Decrement R3 by one.
R3=R3-1
Complement port bit
P0.0
Is NO
R3=0?
?
Call delay
YES
Decrement R2 by one.
R2=R2-1
Repeat again
Is NO
R2=0?
?
YES
RET
Part B: Square Wave Generation
When any pin is made low or high with some delay, square wave is generated. For 50 % duty cycle
ON-OFF time is same/equal.
Block Diagram:
8051
38
P0.1
Procedure:
1) Write program in assembly language to generate square wave at P0.0 of 8051 using delay
subroutine.
2) Change the delay by changing values in registers R2, R3 and observe its effect on duty cycle and
frequency of square wave.
3) Compile and burn the hex file of program in 8051 IC using programmer.
4) Make appropriate connections on 8051 development board and observe the square wave generated
at P0.0 of 8051 on CRO.
Program: a) Square wave generation for 50 % duty cycle
ORG 0000h
start:CPL P0.0 ;complement oin P0.0 or any other pin
ACALL delay ;call delay subroutine
SJMP start ; loop forever
delay:MOV R2, #20h ;R2=20h
pqr:MOV R3, #80h ;R3=80h
xyz:DJNZ R3, xyz ;keep decrementing R3 untill it becomes zero
DJNZ r2, pqr ;decrement R2 by one and repeat delay routine till R2=0
RET ;return control to main program back
b) Square wave generation for duty cycle other than 50%
To change the duty cycle of square wave delay for ON time and OFF time should be separate.
ORG 0000h
start:SETB P0.1
ACALL del1
CLR P0.1
ACALL del2
SJMP start
del1:MOV R2, #80h ; delay routine for ON time
pqr:ACALL delay
DJNZ R2, pqr
RET
del2:MOV R2, #20h ; delay routine for OFF time
lmn:ACALL delay
DJNZ R2, lmn
RET
delay:MOV R3, #50h ; general delay routine
xyz:DJNZ R3, xyz
RET
Algorithm: a) For Square wave generation of 50 % duty cycle.
1. Start at desired address in program memory.
2. Complement any one of the pins of microcontroller (P0.1).
3. Call delay routine of appropriate time in milliseconds.
4. Repeat steps 1-3 continuously.
Start Start
Initialize register R2
Complement port bit
P0.0
Initialize register R3
Decrement R3 by one.
Call delay R3=R3-1
NO
Repeat again Is R3= 0?
YES
Decrement R2 by one.
R2=R2-1
Is NO
R2=0?
?
YES
RET
b) Algorithm: For square wave generation with duty cycle other than 50%.
Initialize register R2
Set bit P0.0=1
Call delay
Call delay subroutine 1
Decrement R2 by one.
Clear bit P0.0=0 R2=R2-1
NO Is
Repeat again
R2=0?
Delay Subroutine 2
Start YES
RET
Initialize register R2
Delay Subroutine
Call delay Start
Decrement R2 by one.
R2=R2-1 Initialize register R3
Decrement R3 by one.
NO Is R3=R3-1
R2=0?
?
YES
Is NO
RET R3=0?
YES
Result: RET
Conclusion: