You are on page 1of 24

Flashing LED ALGORITHM

1. 2. 3. Start. Turn ON LED. Turn OFF LED.

4. GO TO 2. We now want to flash a LED. It works by turning ON a LED & then turning it OFF & then looping back to STA the operating speed of microcontroller is very high so the flashing frequency will also be very fast to be detec eye. Modified Flashing LED ALGORITHM 1. Start. 2. 3. 4. 5. Turn ON LED. Wait for some time (delay). Turn OFF LED. Wait for some time (delay).

6. Go To 2. You can see in the modified algorithm that after turning ON the LED the controller waits for the delay period OFF the led & again waits for the delay period & then goes back to the start. ORG 0000h loop: CLR P2.0 CALL DELAY SETB P2.0 CALL DELAY JMP loop In the above program LED is connected to P2.0 The above program can also be written as follows:

ORG 0000h loop: CPL P2.0 CALL DELAY JMP loop

The only drawback of the second program is that the LED's ON time will be equal to LED's OFF time. Where program if different delay routines are called the LED's ON time can be different than that of LED's OFF time.

GENERATING DELAY
LOOP TECHNIQUE
1. 2. 3. 4. Start. Load a number in a RAM location. e.g. R0. Decrement RAM Location. Is RAM = 00? If NO GO TO 3.

5. STOP. As you can see in the algorithm a number is loaded in a RAM location. It is then decremented & then if the conte RAM location is not equal to zero a jump is made to the decrementing instruction. In 8051 a single instruction "DJNZ" is specifically designed for this kind of programs. It stands for Decrement & Not Zero. This instruction takes care or STEP 3 & STEP 4 of the above algorithm.

Program for LOOP TECHNIQUE


delay: mov R7,#100 l1_delay: djnz r7,l1_delay ret

In above program number 100 is loaded in R7 so the LOOP (djnz instruction) is executed 100 times. To increase will have to load a larger number. The largest delay can be achieved by loading R7 with 255 i.e. 0FFh.

LOOP WITHIN LOOP TECHNIQUE


For longer delays we use this technique. In previous case only a single RAM location was used here the number locations depends on the number of LOOPS used. Here we discuss delays using two RAM locations. 1. Start. 2. 3. 4. 5. 6. 7. Load R7. Load R6. Decrement R6. Is R6=0 if NO go to 4. Decrement R7 Is R7=0 if NO go to 3.

8. Stop. The above algorithm contains two loops the INNER LOOP i.e. STEP 4 & 5. The OUTER LOOP consist of ste & 7.

Program for LOOP WITHIN LOOP TECHNIQUE


Delay: Mov r7,#100 L2_delay: Mov r6,#200 L1_delay: Djnz r6,l1_delay Djnz r7,l2_delay ret

Here the inner loop (l1_delay: djnz r6,l1_delay) takes 200 iterations before R6 becomes 0. When this happe exited & then R7 is decremented & if R7 is not equal to 0 then R6 is again loaded with 200. & again the executed. This continues till R7=0 i.e. the inner loop is executed 100 times before the before the controller can subroutine. The delay generated can be controlled by changing the values that are loaded in R6 & R7.

Program for LOOP WITHIN LOOP TECHNIQUE using three RAM

locations.
Delay: Mov r7,#50 L3_delay: Mov r6,#100 L2_delay: Mov r5,#200 L1_delay: Djnz r5,l1_delay Djnz r6,l2_delay Djnz r7,l3_delay ret

PIN DESCRIPTION OF 8051

VCC and GND: - The pin 40 is Vcc i.e. it is given 5V and pin 20 is GND i.e. it is given 0V (supplied from power source) for powering up the microcontroller. CONNECTING THE CRYSTAL (XTAL1- XTAL2): The pin numbers 18 - 19 are used to connect the crystal. The frequency of this crystal determines the machine cycle. With 8051 family we can use a crystal having frequency from 3 to 24 MHz.

RESET PIN CONNECTION (RST): The reset pin i.e. pin number 9 is used to reset the program just like we restart a computer, the program starts executing from the very beginning. When reset is used the program counter is set back to zero and the values in all other registers are lost.

EA/VPP: The EA pin no. 31 is known as external access. The 8051 microcontrollers have on chip ROM so the EA/VPP is set to one (or Vcc). In case there is no on- chip ROM, like in case of 8031 we set this pin equal to zero. This pin cannot be left unconnected. PSEN: This is an output pin. PSEN stands for program store enable. In 8051 we leave this pin unconnected until otherwise mentioned. ALE/PROG: ALE refers to address latch enable. In 8051 we leave this pin unconnected until otherwise mentioned.

This is the basic setup of microcontroller that you need to have in order to perform an 8051 activity. The pins ALE/ PROG, EA/ VPP and PSEN may have some modifications. We will discuss this wherever it is necessary.

This is a module that has been prepared by us. You can see that a 40 pin base has been mounted on the PCB (printed circuit board) instead of directly soldering the microcontroller to PCB which maybe damaged while soldering.

Here a microcontroller AT89S52 has been shown which is a member of 8051 family. The above circuit is valid for all the members of 8051 family except the AT89S51. In AT89S51, we also have to add pull up resistor at port zero for Input/ Output. (Check the data sheet of AT89S51 microcontroller for the same)

I/O Port Programming in 8051 (using Assembly Language) So far we have discussed the basic setup required for initializing the microcontroller and now comes the interesting part i.e. how to program 8051 using assembly language i.e. carry out input/output operations. All the ports of 8051 can be used for Input or Output. Lets get started. Some illustrations are listed below showing how to access the different pins and ports.

In the following code, we have programmed the microcontroller to toggle the output pins of port 1and hence it will continuously toggle. ORG 0H MOV BACK: A, #55H MOV P1, A ; A= 55 hex ; P1= A ;Wait say for 256 counter ; complement A ; keep doing it ;delay subroutine for 256 counts ;load FF hex in R1 counter ; a delay loop

ACALL DELAY CPL SJMP A BACK

DELAY: MOV R1,#0FFH AGAIN: RET END DJNZ R1, AGAIN

;return to caller

Now suppose we wish to receive data on Port 0 and send it to Port 2. In other words the input on Port 0 can be directed as an output to port 2. Remember for receiving input the input pins or port must be initialized to logic HIGH i.e. 1 and in the subsequent example complete port has been used for receiving input. So rather than assigning logic high to each pin, FF hex value i.e. binary 11111111 is loaded to the input port. ORG 0H MOV A, #0FFH ; A= FF hex or binary 11111111 MOV all 8 pins BACK: MOV SJMP BACK END MOV P1,A A, P0 ; get data from P0 P0,A ; make P0 an input port by initializing logic high to

; send it to port1 ; keep doing it

Lets see one more example. In the following code, Port 1 act as input and its complement is delivered as an output to Port 3.

ORG MOV

0H A, #0FFH ; A= FF hex MOV A, P1 A, P1 ; make P1 an input port by writing all ones to it. ; get data from P1

BACK: CPL MOV

MOV A P3, A

; complement A ; send it to port3 ; keep doing it

SJMP BACK END Checking an Input bit

Sometimes we may only need to observe a single bit and perform an action based upon it like: receiving an input of temperature alarm and if the signal goes high (i.e. temperature is above a certain limit) the buzzer goes off. Or receiving an input from IR sensor of a line follower robot and instructing it to switch motors (on/off) to turn or move straight. In the following code we will receive an input from P1.5 and if it is high then an output 55H is sent to port 0. ORG 0H SETB P1.5 ; make P1.5 an input

MOV A, #55H AGAIN: JNB P1.5, AGAIN ; get out when P1.5 = 0

MOV P0, A END

; issue A to P0

Now you are ready for the real thing. We will start with a simple activity to give you a feel and also you can use this tutorial to implement your own ideas. Suggestion: You should make the basic circuit on a PCB because it will save you a lot of time as you will always need this setup.

ORG 0H BACK:

; start at origin SETB P3.1 ; switch on the LED at P3.1

CLR

P3.3

; switch off the LED at P3.3 ; to call delay subroutine ; switch on the LED at P3.3 ; switch off the LED at P3.1 DELAY

LCALL DELAY SETB P3.3 CLR P3.1

LCALL SJMP BACK

; _______ this is a delay subroutine DELAY: MOV R1, # 255H AGAIN: become zero RET END ; load the counter with 255 hex DJNZ R1, AGAIN ; remain in loop until counter

; return to the caller

After you have written a program you have to burn/program it to a C (symbol for microcontrollers). For this you will need a typical parallel programmer or an ISP (In system programmer). The ISP is generally cheaper and you dont need to remove the Controller from the circuit to burn it. The burners/programmers will cost you around Rs 1500-2500. Do try to buy a USB burner/programmer as parallel ports are no more there in PCs & laptops these days. You can also do a simple activity to understand how you can receive inputs and give outputs from a microcontroller. Here we have used a simple switch, which will give a pin an input as low/high and corresponding to this input, an output will be generated at other pin to light an LED.

ORG 0H SETB P1.4 BACK: CLR JB P0.5

; start at origin ; make P1.4 an input (initialization ) P1.4, LED ON ; jump if byte = 1 ; switch off the LED P0.5 ; keep doing it ; turn on the LED if switch is pressed

SJMP BACK LED ON: SETB P0.5

SJMP BACK END

; keep doing it

Related Articles

Introduction to 8051 Assembly Language Programming Jump, Loop and Call Instructions Line Follower Input-Output instructions for 8051 in Assembly language Numbering Systems

Tags:

8051

Comments
Submitted by robo.genius on Thu, 2009-10-22 23:32. #1 Member since: 12 April 2009 Last activity: 2 weeks 20 hours

i have highlighted where you need to add the line.. check this out..

ORG 0H SETB P1.4 BACK: JB P1.4, LED ON CLR P0.5 SJMP BACK

; start at origin ; make P1.4 an input (initialization ) ; jump if byte = 1 ; switch off the LED P0.5 ; keep doing it

LED ON: SETB P0.5 ; turn on the LED if switch is pressed ;----------------------------------------------------------;Add this line here as shown below
SETB P0.2 ;sound beeping

SJMP

BACK

; keep doing it

END
Submitted by robotman on Thu, 2009-10-22 11:10. #2 Member since: 22 October 2009 Last activity: 2 years 14 weeks

Given this code from the example:

ORG 0H SETB P1.4 BACK: JB CLR P1.4, LED ON P0.5

; start at origin ; make P1.4 an input (initialization ) ; jump if byte = 1 ; switch off the LED P0.5 ; keep doing it

SJMP BACK

LED ON:

SETB P0.5 SJMP BACK END

; turn on the LED if switch is pressed ; keep doing it

suppose in the LED ON mark and after the line SETB P0.5, i want a sound to beep i. e

LED ON:

SETB P0.5

; turn on the LED if switch is pressed

//i want a beaping sound here SJMP BACK ; keep doing it

END

how do i do that with the 8051 assembly language assuming the sound system is attaached to p0.2?

live is lived forward but understood backward


Submitted by mukeshrudra on Sat, 2009-08-22 13:55. #3 Member since: 19 August 2009 Last activity: 2 years 4 weeks

a great tuts thank u


Submitted by program_fever on Mon, 2009-08-10 23:19. #4 Member since: 10 August 2009 Last activity: 2 years 29 weeks

I need help in coding for Printing a Pattern using emulator8086 implemented in assembly language. Pattern is like this:

1 12 123

1234 12345
Submitted by ankit on Thu, 2009-07-16 13:59. #5 Member since: 24 March 2009 Last activity: 2 years 29 weeks

If you don't use a switch, you will not be a able to reset... When you press the reset switch it make the program start from begning... In that case you will have to switch off and on the power again to restart
Submitted by heloitsadi on Sat, 2009-06-13 18:18. #6 Member since: 15 March 2009 Last activity: 2 years 29 weeks

In some books i came across with a reset circuitry with no switch connected.. just a capacitor and resistor is there.. can anyone plz explain how this works..
Submitted by heloitsadi on Sat, 2009-06-13 01:17. #7 Member since: 15 March 2009 Last activity: 2 years 29 weeks

Again the limiting resistence should be between LED and contoller not between LED and ground.. please correct it...
Submitted by heloitsadi on Tue, 2009-06-09 06:18.

#8 Member since: 15 March 2009 Last activity: 2 years 29 weeks

Ya i got it.. its active high... in AVR reset is active Low...


Submitted by heloitsadi on Tue, 2009-06-09 06:17. #9 Member since: 15 March 2009 Last activity: 2 years 29 weeks

in 8051 reset is active low or active high...


Submitted by sam (not verified) on Thu, 2009-06-04 14:41. #10

Over all tutorial is excellent ! everything has been clearly explained Just a little correction. The symbol of LED is little incorrect.(ie without a bar >|) and GND should be with three bars.. thats it .

Over 90 recipes to create your own exciting Facebook applications at an incredibly fast pace with Facebook Application Development with Graph API Cookbook. Order your copy now!

Like and share this book!

Enter your em

bOtskOOl

en_US

Subscribe

Advertise Here Are you a Geek?


How to create ToolTip for a Windows Form Control using Visual C# (in .NET Framework) How to create and render dialog in a web page using jQuery Mobile How to create random alphanumeric password in PHP How to display a retweet button in your posts in WordPress How to make website mobile friendly in WordPress

1 of 23

Recent comments

Please see this thread 23 hours 40 min ago lcd not working in Atmega32 50 weeks 17 hours ago Nice game 1 year 2 days ago can't visualise indetail the circuitry 1 year 3 days ago regarding the above project 1 year 1 week ago tanx fr ur reply but i need 1 year 3 weeks ago http://www.engineersgarage.co 1 year 3 weeks ago Hey DEEPU626, Check out this 1 year 3 weeks ago http://www.botskool.com/image 1 year 7 weeks ago sir, i wanna ask if in 8051 1 year 8 weeks ago

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx) Getting Started with Ubuntu 10.04 (Lucid Lynx) is a comprehensive beginners guide for the Ubuntu operating system; it features comprehensive guides, How Tos and information on anything you need to know after first installing Ubuntu. >> A Newbie's Getting Started Guide to Linux Learn the basics of the Linux operating systems. Get to know what it is all about, and familiarize yourself with the practical side. Basically, if you're a complete Linux newbie and looking for a quick and easy guide to get you started this is it. >>

You might also like