8051_Counter_255
This project shows the application of 8051 as a counter that is designed to count upto 255. To simulate circuit in this project, initially activate Mixed Mode simulator from the Schematic Editor window. Simulation can be performed by selecting Run Transient analysis (Oscillograph) from Simulation menu. For increasing the speed of the counter, stop simulation inside Oscillograph. Simulation is activated again after giving step = 50us and observe the speed of counting.
The circuit uses an 8051 chip, three 7-Segment Displays and three BCD to Seven Segment Decoders. In this circuit, Port P0 of 8051 is assigned as the input port while the ports P1 and P2 are assigned as output ports. The counter counts from a hex value initially given to port P0 of 8051. Currently it is set as "LLLLLLLL". This hex value can be edited by choosing Tools menu -> Instruments -> Preset Logic State -> HIGH/ LOW state. The BCD to Seven-Segment Decoder decodes the data from ports P1 and P2 to 7-segment format, which are displayed in
7-segment displays.
The source code written either in C or Assembly language can be viewed from the
code editor window
The program is as shown:
#include<8051.h> // This header file is included for all 8051 projects.
void Delay (int Time);
void main()
{
int a,b,c,p,q;
int i;
repeat: // A loop is given to repeat the counting sequence from initial value
a= P0; // Variable a is initialized to port 0 input value
// Default is 0x00, since we have given "LLLLLLLL" at port 0
for (i = a; i <= 255; i++) // Incrementing variable 'i' from initialized value of Port 0 to 255
{
P3 = 0x01; // RXD pin in Port3 (P3.0) is connected to LE of Decoder. It is set to ‘1’ for disabling decoder.
b = i /100; // For getting the left most nibble in the counting number of three digits
// For getting the middle nibble in the counting number of three digits
c= i % 100;
p = c / 10;
// For getting the middle nibble in the counting number of three digits Ends
q = p << 4; // Shifting the middle nibble 4 bits left and last nibble of 'q' is set to "0000" automatically by this shifting
// For getting the right most nibble in the counting number of three digits
p = c % 10;
// For getting the right most nibble in the counting number of three digits ends
P1 = q + p; // Combining middle nibble and right most nibble and the result is given to Port 1
P2 = b; // Left most nibble in the counting number which is stored in variable 'b' is given to Port2
Delay (50); // Giving some delay by calling Delay Function
P3 = 0x00; // Enable decoder by setting P3.0 to ‘0’. It decodes the values and drives seven-segment display.
}
goto repeat;
}
void Delay(int Time)
{
while (Time--);
}
The source code in the
code editor window
has to be
compiled
after making any modifications
(editing).
Also the code can be
debugged
during
simulation.