Thursday 22 October 2015

How to implement free running counter in 8051 using seven segment display.




How to implement free running counter in 8051 using seven segment display


This post provides the implementation of free running counter ( using c language ) for 8051 micro-controller ( e-g for AT89C51 or AT89C52 etc ). This code is written in such a way that, the counter starts from a value of '0' ( displayed on the seven segment ) and then increments this value after every second. So, the seven segment display starts from '0' and then displays '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'b', 'C', 'd', 'E', and in the end 'F'. After displaying 'F', counter starts from '0' again and this pattern repeats forever.


The following figure shows the circuit of free running counter in Proteus.

Figure 1.   Free running counter implementation using 8051
A crystal of 12 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 12Mhz crystal makes this 8051 run at 1.0 MIPS (Million of instructions per second).

Port2 is used here to connect 8051 with the seven segment display. P2.0 pin is attached with the 'a' segment of the seven segment display. Similarly, P2.1 pin is connected with 'b' segment, P2.2 pin is connected with 'c' segment and so on.

8051 micro-controller can't provide more than few micro amperes
[2] from any of it's output pins. So, a buffer IC 74LS245 is used in between seven segment display and 8051 micro-controller. This ensures that seven segment gets enough current to display values correctly.

Common cathode seven segment display is used in this example. You can easily modify this circuit and code for the common anode seven segment display as well.

The following function is used in the code to display values on the seven segment display.


Figure 2.   DisplayOn7Segment() function code
For example, to display '0' on the seven segment display, we can call this function in the main as DisplayOn7Segment('0');.

The code for the main function is shown below.

Figure 3.   Main function for 8051 free running counter
In the main function code, a variable 'ch' is displayed on the seven segment display. Then there is a delay of about 1 second. After that, a new value of ch is selected using the switch statement. Then the new value present in ch variable is displayed on the seven segment. This process continues forever.

You can leave your comments in the comment section below. 
Notes and References
[1]  From http://en.wikipedia.org/wiki/Intel_MCS-51
[2]  From AT89C51 datasheet, DC characteristics section.
Downloads
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation 
click here.


No comments:

Post a Comment