8051 LED Blinking
This post provides the LED blinking code for 8051
micro-controller ( e-g for AT89C51 or AT89C52 etc ). The code and Proteus simulation
is given in the 'Downloads' section at the bottom of this page. The
circuit required is shown below.
In the above circuit diagram, AT89C52 is used to simulate the
LED blinking code. You can place AT89C51 here without any other change and the
code will still run the same. So, this example code can work on both AT89C51
and AT89C52 micro-controllers.
A crystal of 11.0592 MHz is used here. You can use any crystal value from 3 to 24MHz with 8051. As we know that 8051 micro controller has an architecture which executes an instruction in 12 CPU cycles [1], hence this 11.0592Mhz crystal makes this 8051 run at 0.92 MIPS (Million of instructions per second). The code for this project is shown below.[2]
A crystal of 11.0592 MHz is used here. You can use any crystal value from 3 to 24MHz with 8051. As we know that 8051 micro controller has an architecture which executes an instruction in 12 CPU cycles [1], hence this 11.0592Mhz crystal makes this 8051 run at 0.92 MIPS (Million of instructions per second). The code for this project is shown below.[2]
In the code above, LED is defined as the pin 0
of the port 1 ( i-e P1.0 ). In the main function, LED is toggled after every
half second. The function 'delay' executes null statements. A value of
30000 generates about 0.5 second null statements execution time[3], when 11.0592 MHz crystal is being used. In this way, LED
attached on P1.0 pin is made to blink using the code given above.
You can leave your comments in the comment section below.
You can leave your comments in the comment section below.
Notes and References
[1] From http://en.wikipedia.org/wiki/Intel_MCS-51
[2] This code was compiled using Keil micro-vision4 software.
[3] For the delay, I simulated the code in the Keil micro-vision4 software and 30000 value gives a delay of about half second. Strictly speaking this delay is compiler dependent. Different compilers will give different delays. Even new version of the same compiler can give different delay.
Using the procedure given below, you can calculate exact delay produced by 30000 value. In the delay function, there is a for loop which executes null statements. When this for loop is converted into assembly, then it becomes something like this,
repeat block for 30000 times
{
Copy i value from memory into ALU register (takes about 1 cycle)
check if i is less than a or not (takes about 1 cycle)
jump to end of for loop if i>a (takes about 1 cycle)
.
.
.
.
store new value of i in memory (takes about 1 cycle)
jump to start of loop again (takes about 2 cycle)
}
There are about 15 assembly instructions inside the for loop (using Keil uvision4 compiler). Exact assembly code for delay function in the Keil software (captured using 'Debug' mode) is shown below.
[2] This code was compiled using Keil micro-vision4 software.
[3] For the delay, I simulated the code in the Keil micro-vision4 software and 30000 value gives a delay of about half second. Strictly speaking this delay is compiler dependent. Different compilers will give different delays. Even new version of the same compiler can give different delay.
Using the procedure given below, you can calculate exact delay produced by 30000 value. In the delay function, there is a for loop which executes null statements. When this for loop is converted into assembly, then it becomes something like this,
repeat block for 30000 times
{
Copy i value from memory into ALU register (takes about 1 cycle)
check if i is less than a or not (takes about 1 cycle)
jump to end of for loop if i>a (takes about 1 cycle)
.
.
.
.
store new value of i in memory (takes about 1 cycle)
jump to start of loop again (takes about 2 cycle)
}
There are about 15 assembly instructions inside the for loop (using Keil uvision4 compiler). Exact assembly code for delay function in the Keil software (captured using 'Debug' mode) is shown below.
So effectively a total of approximately
15*30000 = 450,000 instructions are executed. Since this 8051 is executing a
single instruction in 1.08 micro seconds ( 12/11.0592 micro seconds ), so it
takes about 450,000*1.08 = 0.49 seconds to execute 30,000 null statements.
Downloads
The code was compiled in Keil micro-vision4 and simulation
was made in Proteus v7.10.
To download code and proteus simulation click here.
To download code and proteus simulation click here.
No comments:
Post a Comment