Program For Digital Clock Using 8051

  1. Assembly Language Code For Digital Clock Using 8051
  2. Program For Digital Clock Using 8051 Switch

Intro

Hi everyone, i'm trying to code a Clock with 2 7segment (commun anode) with 8051 microcontroller in assembly language. I use only two 7 segment led because i want just minutes and secondes for hours i will see later. For this clock, we can set the time at any instant. Here, the clock can work in either 24 hour mode or 12 hour mode and the RTC chip is configured by programming 8051 controller. I will demonstrate two circuits of Digital Clocks using 8051 Microcontroller: one uses the RTC DS12C887 and the other uses the RTC DS1307. Digital clock using DS12C887 and 8051 microcontroller (AT89C51) in 12 hour mode. This article is an improved version of LCD based clock using RTC DS12C887 and 8051 microcontroller (AT89C51) using update interrupt. Has two modes of operation i.e., 12 hour and 24 hour mode. In our earlier articles we explained how to use 24 hour mode.

The 8051 microcontroller, also known as MCS-51, is a 8-bit microcontroller produced by Intel in the 1980s. It is widely used in embedded systems such as automobiles, telephones, robotics, security systems, control systems, video games, medical devices, and many other electronic appliances. The 8051 microcontroller offers variety of functions that makes it very powerful and versatile. These include the Central Processing Unit (CPU), 128 byte of Internal Random Access Memory (RAM), 4K Read Only Memory (ROM), 32 Input/Output (I/O) ports, five interrupts, and many other functions.

Problem Statement

This lab will give hands on experience with designing, building, and programming an 8051 embedded system which will function as an accurate timer and stopwatch. One will first construct a schematic design of the circuit using WinDraft. Then, one will wire the chips accordingly to construct the hardware component of the project. Finally, Assembly programming language is used to display the time on the 7 Segment LED display. Assembly is used because of its compact size, speed, and specific application use.

The complete system will perform as a clock and a stopwatch which displays each output interchangeably with the interrupt function. The interrupt function will allow the system to start, freeze, reset, and switch between the clock and stopwatch.

To explain in more detail, the clock system keeps track of time from “00.00” to “59.59”, updating in one second intervals. The clock will display 10’s min, 1’s minute, 10’s second, and 1’s second on the LED display. The counting will start right as power is given to the circuit. The clock will reset and start counting from “00.00” after one hour passes. When the interrupt is activated for the first time, the system will switch to stopwatch mode and display “00.00” while the clock is running in the background. The stopwatch will now display 10’s second, 1’s second, 1/10th of a second, and 1/100th of a second respectively. When the interrupt is activated for the second time, the stopwatch will start counting. The third time the interrupt is activated, the stopwatch will freeze and maintain the time on the LED display. While the system goes through the two modes, the clock will continue to count every one-second interval. Clock will be displayed on the fourth interrupt. The cycle will return to zero and display a stopwatch “00.00” on the fifth interrupt.

Purpose

The purpose of this lab is to use the completed hardware and assembly program to build a 8051 embedded system which performs as a digital watch. One must understand the time-division-multiplex (TDM) control and on-chip timer on the 8051. Then, one must develop, debug, and implement an assembly program for the digital watch. Finally, using the timer-overflow and external interrupt, one must control the timing and operation of the digital watch.

Materials Used

Table 1 – Materials Used for the Digital Clock

Chips
  • 8051 MicroController
  • 2816 EEPROM
  • 74573 D-type Latch
  • 74138 2×4 Demultiplexer
  • BCD to 7-Segment Decoder
  • 7400 Inverter
  • 7-Segment LED Display
Other Components
  • Push button
  • Capacitors
  • Resistors
  • 12MHz Crystal
  • Cadet board
  • PC Board
  • Soldering Iron
  • Wires
Programs
  • WinDraft
  • Keil

Hardware Design Strategy

To accomplish this lab, one must understand the function of each chip and how they work together, and construct the schematic diagram. The complete system can be divided into two categories: control system and the display system. The wiring of these chips will be discussed in the Construction of Board Section.

The control system consists of the EEPROM, Latch, and the 8051. The main function of these chips is to execute the given Assembly program and generate the output to be shown on the display. The display system consists of 2×4 Demultiplexer, BCD to 7 Segment Decoder, resistors, a hex inverter, and the 7-Segment LED display. These chips will take the output from the 8051 and display it on the 7-Segment LED.

The 8051 has 40 pins. There are four ports (p0,p1,p2,p3) with eight pins respectively to be used as inputs and outputs. The three control signals, External Access (EA), Address Latch Enable (ALE), and Program Store Enable (PSEN), serve as external memory interfacing [1]. EA is grounded because external memory is not required in this lab [2]. ALE is used to provide memory whereas PSEN is used to read the content of the memory. ALE is connected to the latch and controls the entering and exiting of the input and the output from the 8051 to the EEPROM. XTAL1 and XTAL2 are connected to the crystal circuit to provide a timing mechanism for the system. RST is used as a method of resetting the circuit to the starting point. VCC and GND are used as power and ground for the chip to function.

The 2816 EEPROM has 11 address line and 8 data line. EEPROM contains the Assembly code and delivers it to the 8051. The software component of the EEPROM will be discussed in Software Design Strategy Section.

BCD to 7-Segment Decoder and 2×4 Demultiplexer are used to display the output from the 8051 on the 7 Segment LED display. The decoder takes in four inputs and outputs seven signals. The seven signals each corresponds to one segment of the LED Display (a,b,c,d,e,f,g). The 2×4 demultiplexer is used to assign which of the four LED will be used. The two inputs become four outputs that are assigned as DG1, DG2, DG3, and DG4. Hex inverter was used because the BCD to 7 Segment Decoder is active LOW.

Construction of Board

The chips listed in Table 1 were arranged on the PC board as shown in Fig.1. The chips were divided into two categories for efficient wiring and organization purposes. The right hand side of the board includes the EEPROM, Latch, and the 8051. The left hand side of the board includes 2×4 Demultiplexer, BCD to 7 Segment Decoder, resistors, inverter and the 7-Segment LED display. The reset, crystal, and the power are placed on the top portion of the PC board.

The chips were securely soldered onto the PC Board by their two diagonals. The left and right pins on the power and ground pin were short circuited with soldering iron. This made it possible for all chips to be properly powered and grounded.

Assembly Language Code For Digital Clock Using 8051

Figure 1 – Layout of Chips on the Board

The hardware of the 8051 embedded system was built following the schematic diagram shown in Fig. 2 built using the WinDraft program. As explained in Hardware Design Strategy section, 8051, Latch, and EEPROM work together as the control system. 8051 uses both the BCD to 7-Segment Display and 2×4 Demultiplexer to generate an output to be shown on the 7 segment LED display.

Figure 2 – Schematic Diagram of the Digital Watch

Eight inputs from port 0 (P0.0-P0.7) and ALE from 8051 are connected to the D-type Latch. The eight inputs that goes through the Latch is then connected the first eight address lines (A0-A7) of the EEPROM. This is outputted as data (D0-D7) to the same eight inputs (P0.0-P0.7) in the 8051. ALE is connected to the latch and controls the entering and exiting of the input and the output from the 8051 to the EEPROM.

The three inputs from port 2 (P2.0-P2.2) and PSEN from 8051 are connected directly to the last three address line (A8-A10) and Output Enable (OE) of the EEPROM respectively.

Four inputs from p1 (P1.0-P1.3) are connected to the BCD to 7-Segment Display. The seven output (a-f) from the BCD to 7-Segment Display goes through 330 Ohm resistor respectively. Coming out of the resisters, the seven signals connects to the 7-Segment LED Display accordingly. Fig.2 does not accurately show how it is connected to the LED display because the pin layout was different from the actual LED that was used in the lab. Fig.3 shows how each corresponding alphabet from the BCD to 7- Segment Decoder was wired to the LED display.

Two inputs from p1(p1.4-p1.5) are connected to the 2×4 demultiplexer. The four outputs (Y0-Y3) go through a hex inverter and connects to the LED display (DG1-DG3) as a method of assigning the four displays.

XTAL1 and XTAL2 are connected to the crystal circuit which consists of 12MHz crystal and two 22pF capacitors. XTAL1 and XTAL2 are connected to each end of the crystal. The two ends of the crystal are connected to the capacitor then to GND.

RST is connected the reset circuit which consists of a switch, 47uF capacitor, and a 100 Ohm resistor. The reset circuit will always be LOW until the switch is pressed. The circuit will deliver 5V and reset the system.

The interrupt circuit is wired to P3.2 on the 8051. The interrupt circuit uses a debounced pushbutton on the cadet board, a 1000 Ohm resistor, and 5V of power. When the button is pressed, the interrupt goes from LOW to HIGH and send the signal to the 8051.

There are pins that were not connected in the schematic in Fig.2 but were connected in Fig.3. All pins were powered and grounded respectively. Chip Enable (CE) for EEPROM, Output Enable (OE) for Latch, and C, G2A, and G2B for 2×4 Demultiplexer are all grounded. Write Enable (WE) and G1 for 2×4 Demultiplexer are all powered.

Figure 3. Complete Wiring of the Digital Clock

Software Design Strategy

One must first understand how data memory works in the 8051 before writing the code. 128 byte of RAM is divided into three blocks. The first block consists of the four Banks (B0-B3) which holds seven registers (R0-R7) respectively. The second block consists of 16 bit-addressable registers. The last block consists of remaining 80 byte of locations that are free to be used.

The first step in coding for the 8051 chip in assembly is to establish the origin values for the different interrupts:

Program For Digital Clock Using 8051 Switch

ORG 0000h ; entry address for 8051 RESET

AJMP MAIN ; MAIN starts beyond interrupt vector space

ORG 0003h ; EXTERNAL 0 INTERRUPT

AJMP ISTREX0

ORG 000Bh ; TIMER 0 INTERRUPT

AJMP CLOCK

ORG 0100h ; defines where MAIN starts

The first origin to establish is the entry address for the 8051 reset, which will run the MAIN and reset all the registers and locations to their original values. The next origin, ORG 0003h, will indicate the use of external interrupt 0 and then jump to the external interrupt code. The next origin establishes the timer 0 overflow interrupt, which jumps to the CLOCK code. The final origin defines where the MAIN starts.

The MAIN of this program sets all of the initial values, locations, and registers to be used for the entire code. The code below establishes some example values, locations, and registers, similar to those used in the CLOCK program, as well as the initial timer settings and interrupt enables:

MAIN:

; SET CLOCK INITIAL VALUES

MOV 36h, #00h ; move number 0 on digit 1 to memory location 36

; SET DISPLAY REGISTER

MOV R0, #36h ; move memory stored in location 36 to Register 0

; SET DELAY COUNTER REGISTERS

MOV R7, #00 ; Set Register 7 to 00 for clock delay Vivosonic integrity user manual pdf.

; SET TIMERS

MOV TMOD, #11h ; configure Timer-0 and Timer-1 as 16 bit timers

MOV TH0, #0ECh ; initialize with a starting number for timer 0

MOV TL0, #85h

; SET INTERRUPTS

SETB TR0 ; enable Timer-0 to

SETB ET0 ; enable TF0

SETB EA ; enable interrupts

The MOV command moves a value into a location or register. Four locations were used to display the four different digits of the clock. These four locations had to be consecutive in order for the display code to function properly. The display code will be explained later in this section. The timer 0 was set to speed up the overflow interrupt so that the CLOCK will start immediately after the MAIN is completed. Finally, the timer overflow interrupts are set and enabled, allowing the timer to overflow and jump to the CLOCK program. This portion of the program will only run once before moving to the wait loop, unless the reset is triggered. If the reset is triggered, the main will reset all the values to their initials as defined by the first origin.

After the MAIN, the program will proceed into a wait loop. This is what causes the delay that will be used to delay incrementation of the the clock and the stopwatch:

WAIT:

NOP

SJMP WAIT ; mainline program waits here

This loop will run until the timer overflow interrupt executes, at which time, the program will jump back to the timer overflow code, in this case, the CLOCK code.

The display loop used the following logic:

DISPLAY:

MOV P1, @R0 ; sends the current location value of the register to P.1

INC R0 ; increases the location number of register 0

CJNE R0, #3Ah, RETURN ; if location number ≠ 3Ah, jump to RETURN

MOV R0, #36h ; if location number = 3Ah, reset back to 36h

AJMP RETURN ; jump to RETURN

RETURN:

RETI ; “return to interrupt” always goes to WAIT

Assuming that R0 is set to #36h, the binary representation of the value stored in the location of 36h will be sent to port1. Therefore, if #00h is located at 36h, then a zero will be displayed on the leftmost digit of the display. The first digit of the hex value represents the digit location on the display; 0 being the leftmost digit, and 3 being the rightmost. The second digit of the hex value represents the actual number that will appear on the display. For example, should #24h be sent to port 1, a 4 will appear on the third digit from the left. After the first digit is displayed, the location in register 0 will be increased and checked to be one value greater than the last of the locations used for the display. This is why all of the locations for the clock or stopwatch must be consecutive. If the location is not equal to the check value, in this case #3Ah, then the program will jump to RETURN. This only contains “return to interrupt” (RETI), which will go to WAIT and loop until the timer overflow interrupt executes and starts the cycle again, this time displaying the next location. If the location is equal to the check value, then the register will be reset to the initial location so the whole loop can repeat. This happens so quickly, that the display will look like all digits are on simultaneously, even though only one digit is on at a particular time.

After completing the hardware implementation of the design, the system was tested using a sample program which outputs a single digit to port 1. The test number used in this case was a 6 on the third digit from the left (#26h), which was defined in the MAIN using the above techniques. After successfully displaying this number on the appropriate location on the display, another test program was developed to establish a foundation of code on which the clock and stopwatch would be designed. The program developed displayed 1, 2, 3, and 4 respectively across the four digit display from left to right. Once the board displayed the numbers, the system was confirmed as properly constructed. With the verification of proper hardware design and construction, the implementation of software began.

After testing the construction of the system, a CLOCK program was designed to display up to an hour’s time. This was represented as “00.00” through “59.59” on the display. After the fourth digit from the left, which represented a second, reached 9, the digit to its right was increased by one. This was repeated for the remaining digits, with the first and third digit only increasing until 5. When the clock reached “59.59”, the entire clock was reset to “00.00” to display the next hour.

This program consisted of two major parts, the delay that updated the clock every second, and the clock incrementations. The 12 Mhz crystal established that each cycle of the 8051 microcontroller was 1μs. In order to update the clock properly, 106 cycles must execute before an output is sent to port 1. A simple method of creating this delay is by writing a nested loop that repeats 106 times, minus the number of cycles used for incrementing the clock. However, by manipulating the time interrupts so that there are 5000 cycles to overflow, each loop would take 5ms because of the infinite WAIT loop. Then the nested loop can be repeated only 200 times for the clock to update every second, as seen in the code below:

MOV R7, #00 ; Set Register 7 to 00 for clock delay

CLOCK:

CLR EA ; clear interrupt

MOV TH0, #0ECh ; Sets 5000 cycles = 5 milliseconds

MOV TL0, #7Ch ; FFFF – (5000 – C)

SETB EA ; enable interrupt

INC R7 ; Increase the value of delay counter

CJNE R7, #200, CLOCK ; 5 milliseconds * 200 = 1 second

MOV R7, #00 ; Resets the delay for the next loop after 200 loops

The time interrupt values are calculated by adding up the cycles used in jumping to the clock and setting the timer starting value and subtracting that from the 5000 cycles needed to delay a millisecond, and then subtract that value from FFFF, which is the maximum possible value of a four digit hexadecimal number. For example, if the jump to clock and timer set code consists of 5 cycles, then TH0 should be set to the first two digits of EC7C (FFFF – (5000 – 5)) and TL0 to the last two digits.

The clock incrementation was developed using the following logic:

MOV 39h, #30h

MOV R0, #36h

CLOCK:

INC 39h ; increase second value

MOV R2, 39h

CJNE R2, #3Ah, DISPLAY ; display second update

MOV 39h, #30h ; reset second value

DISPLAY:

MOV P1, @R0

INC R0

CJNE R0, #3Ah, RETURN

MOV R0, #36h

First, a location is filled with a hex value that corresponds to 0 on a certain digit of the display. In the above case, #30h is stored in 39h, which will display 0 on the last digit from the left when sent to port 1. The above code will increase the number displayed every cycle until the number 9, at which time it will be reset to zero. In the CLOCK program, this increment method will be used four times consecutively, with the following digit updating with the completion of the previous digit’s loops. Every time the loop increases a digit, an output is sent to port 1 for each digit quickly enough that the display looks static.

Once the clock was completed, the next step was to design a stopwatch using the external interrupt of the 8051. The program counted how many times the interrupt was used, and executed specific code based on the counter value. Each interrupt would increase the value of the counter until 4, at which time it would reset to 00 and display the clock again. When the counter was not 00 or 02, the stopwatch would freeze and display the current stopwatch value. After the fourth press, the stopwatch would be reset to zero and the clock would display again. To accomplish this, four more locations were used to store stopwatch digits and two separate display loops were designed, one for the clock and one for the stopwatch. The first display would run by default and check for a interrupt. If the interrupt counter was anything but zero, the program would jump to a FREEZE loop that would jump to display whatever value the stopwatch had at the time:

DISPLAY0:

; DISPLAY FOR CLOCK AND CHECK FOR STOPWATCH INTERRUPT

CJNE R5, #00, FREEZE ; check for external interrupt

MOV P1, @R0

INC R0

CJNE R0, #3Ah, RETURN

MOV R0, #36h

AJMP RETURN

FREEZE:

CJNE R5, #02, DISPLAY1 ; freeze if external interrupt not on step 2

STOPWATCH:

; CLOCK DELAY

INC R6 ; Increase the value of delay counter

CJNE R6, #2, DISPLAY1 ; After 2 loops the clock will update (5 ms * 2 = 10ms)

MOV R6, #00 ; Resets the delay for the next loop

; STOPWATCH UPDATE

DISPLAY1: ; DISPLAY STOPWATCH

MOV P1, @R1

INC R1

CJNE R1, #4Ah, RETURN

MOV R1, #46h

AJMP RETURN

At the first hit, the values of all stopwatch digits are 0, so 0000 is displayed. The FREEZE also acts as a check for the counter to equal 2. If the counter equals 2, the program will continue to increment the stopwatch in a similar method to the clock. However, the stopwatch will update every 100th of a second, so the delay loop for this section of code only repeats twice. Upon the third hit, the counter will increase and no longer be equal to 2, so the FREEZE loop will display the values of the last incrementation. The fourth hit will reset the stopwatch values to 0000 and jump back to the clock loop. See Appendix 1 for the full code.

Results

After designing the delay, incrementation, and display code, the clock program was tested and compared to the timer of an iPhone. After 10 minutes, the was little to no difference between the two timers. After 30 minutes, a slight delay of about 0.25s could be seen in the built timer. After approximately 60 minutes, there was about 0.5s of offset between the two timers. This slight offset in the two timing is due to the slight difference in the timing calculation. However, for the purpose of this lab, half a second offset after an hour is acceptable. Thus, the accuracy of the clock is verified.

The stopwatch was also tested against an iPhone stopwatch. There was about 0.03s of an offset after couple seconds due to human error. The accuracy of the stopwatch was also verified. While switching from the clock to the stopwatch, the clock did not lose accuracy and continued to count every one second interval. The whole digital watch system worked without a problem.

Discussion and Conclusion

Program For Digital Clock Using 8051

The digital clock system was accurate and complete although there were many issues in the building process. The PWR/GND chip was very unreliable after it was first soldered. Some pins delivered power while other pins did not. This was fixed by securing the pins with ample amount of solder.

After all pins were wired accurately, the system did not display the number that was intended. This is because the connection between the pins were unstable due to loose wiring. After countless hours of debugging and rewiring of the circuit, the system displayed the intended number.

There were couple coding issues that had to be debugged. One of the error was putting 000Bh in front of 0003h. The program did not function correctly as 0003h had to come before 000Bh. Another issue was moving the register value @R0 to P1 before increasing it. The value had to increase first, before the MOV function. Another issue was playing with the timer values so that the interrupt would overflow every 5 milliseconds. Because the code most commonly There were many other syntax and semantic errors that had to be debugged that were figured out along the coding process.

Appendix

(1) Digital Watch Assembly Code

ORG 0000h ; entry address for 8051 RESET

AJMP MAIN ; MAIN starts beyond interrupt vector space

ORG 0003h ; EXTERNAL 0 INTERRUPT

AJMP ISTREX0

ORG 000Bh ; TIMER 0 INTERRUPT

AJMP CLOCK

ORG 0100h ; defines where MAIN starts

MAIN: ; SET LOCATIONS AND VALUES

; SET CLOCK INITIAL VALUES

MOV 36h, #00h ; move number 0 on digit 0 to memory location 36

MOV 37h, #10h ; move number 0 on digit 0 to memory location 37

MOV 38h, #20h ; move number 0 on digit 0 to memory location 38

MOV 39h, #30h ; move number 0 on digit 0 to memory location 39

; SET STOPWATCH INITIAL VALUES

MOV 46h, #00h ; move number 0 on digit 0 to memory location 46

MOV 47h, #10h ; move number 0 on digit 0 to memory location 47

MOV 48h, #20h ; move number 0 on digit 0 to memory location 48

MOV 49h, #30h ; move number 0 on digit 0 to memory location 49

; SET DISPLAY REGISTERS

MOV R0, #36h ; move memory stored in location 36 to Register 0

MOV R1, #46h ; move memory stored in location 46 to Register 1

; SET DELAY COUNTER REGISTERS

MOV R7, #00 ; Set Register 7 to 00 for clock delay

MOV R6, #00 ; Set Register 6 to 00 for stopwatch delay

; SET EXTERNAL INTERRUPT COUNTER

MOV R5, #00 ; Set external interrupt counter to register 5

; SET TIMERS

MOV TMOD, #11h ; configure Timer-0 and Timer-1 as 16 bit timers

MOV TH0, #0ECh ; initialize with a starting number for timer 0

MOV TL0, #85h

; SET INTERRUPTS

SETB TR0 ; enable Timer-0 to

SETB ET0 ; enable TF0

SETB EX0 ; enable external interrupt

SETB IT0 ; set external interrupt 0 as edge activated

SETB EA ; enable interrupts

WAIT:

NOP

SJMP WAIT ; mainline program waits here end of the main program

CLOCK:

; RESET INTERRUPTS AND TIMER

CLR EA ; clear interrupt

MOV TH0, #0ECh ; Sets 5000 cycles = 5 milliseconds

MOV TL0, #7Ch ; FFFF – (5000 – 5)

SETB EA ; enable interrupt

; CLOCK DELAY

INC R7 ; Increase the value of delay counter

CJNE R7, #200, DISPLAY0 ; After 200 loops the clock will update (5ms * 200 = 1s)

MOV R7, #00 ; Resets the delay for the next loop

; CLOCK UPDATES

INC 39h ; increase second value

MOV R2, 39h

CJNE R2, #3Ah, DISPLAY0 ; display second update

MOV 39h, #30h ; reset second value

INC 38h ; increase ten second value

MOV R2, 38h

CJNE R2, #26h, DISPLAY0 ; display ten second update

MOV 38h, #20h ; reset ten second value

INC 37h ; increase minute value

MOV R2, 37h

CJNE R2, #1Ah, DISPLAY0 ; display minute update

MOV 37h, #10h ; reset minute value

INC 36h ; increase ten minute value

MOV R2, 36h

CJNE R2, #06h, DISPLAY0 ; display ten minute update

; RESET CLOCK AFTER 1 HOUR

MOV 36h, #00h ; move number 0 on digit 0 to memory location 36

MOV 37h, #10h ; move number 0 on digit 1 to memory location 37

MOV 38h, #20h ; move number 0 on digit 2 to memory location 38

MOV 39h, #30h ; move number 0 on digit 3 to memory location 39

AJMP DISPLAY0

DISPLAY0: ; DISPLAY FOR CLOCK AND CHECK FOR STOPWATCH INTERRUPT

CJNE R5, #00, FREEZE ; check for external interrupt

MOV P1, @R0

INC R0

CJNE R0, #3Ah, RETURN

MOV R0, #36h

AJMP RETURN

FREEZE:

CJNE R5, #02, DISPLAY1 ; freeze if external interrupt not on step 2

STOPWATCH:

; CLOCK DELAY

INC R6 ; Increase the value of delay counter

CJNE R6, #2, DISPLAY1 ; After 2 loops the clock will update (5 ms * 2 = 10ms)

MOV R6, #00 ; Resets the delay for the next loop

; STOPWATCH UPDATE

INC 49h ; increase 1/100 second value

MOV R2, 49h

CJNE R2, #3Ah, DISPLAY1

MOV 49h, #30h

INC 48h ; increase 1/10 second value

MOV R2, 48h

CJNE R2, #2Ah, DISPLAY1

MOV 48h, #20h

INC 47h ; increase second value

MOV R2, 47h

CJNE R2, #1Ah, DISPLAY1

MOV 47h, #10h

INC 46h ; increase ten second value

MOV R2, 46h

CJNE R2, #06h, DISPLAY1

; RESET STOPWATCH AFTER 1 MINUTE

MOV 46h, #00h ; move number 0 on digit 0 to memory location 46

MOV 47h, #10h ; move number 0 on digit 0 to memory location 47

MOV 48h, #20h ; move number 0 on digit 0 to memory location 48

MOV 49h, #30h ; move number 0 on digit 0 to memory location 49

AJMP DISPLAY1

DISPLAY1: ; DISPLAY STOPWATCH

MOV P1, @R1

INC R1

CJNE R1, #4Ah, RETURN

MOV R1, #46h

AJMP RETURN

RETURN:

RETI ; return to WAIT

ISTREX0:

; EXTERNAL INTERRUPT COUNTER

INC R5 ; increase interrupt counter

CJNE R5, #04, FINISH

MOV R5, #00 ; resets counter when counter = 4

; RESET STOPWATCH

MOV 46h, #00h ; move number 0 on digit 0 to memory location 46

MOV 47h, #10h ; move number 0 on digit 0 to memory location 47

MOV 48h, #20h ; move number 0 on digit 0 to memory location 48

MOV 49h, #30h ; move number 0 on digit 0 to memory location 49

FINISH: ; RETURN TO CLOCK

RETI

END ; end directive

Bibliography

[1]”8051 Microcontroller – Electronic Circuits and Diagram-Electronics Projects and Design”, Circuitstoday.com, 2014. [Online]. Available: http://www.circuitstoday.com/8051-microcontroller. [Accessed: 01- Oct- 2016].

[2]”External Memory”, 8051-microcontrollers.blogspot.com, 2013. [Online]. Available: http://8051-microcontrollers.blogspot.com/2013/02/external-memory.html#.V_cjOZMrKu6. [Accessed: 03- Oct- 2016].