Assembly Program for 1-Second Delay
ORG 0H ; Start address
; Initialize Timer 0
MOV TMOD, #01H ; Set Timer 0 in Mode 1 (16-bit timer)
MOV TH0, #3CH ; Load high byte of Timer 0 (for 65536 - 3036 counts)
MOV TL0, #0B0H ; Load low byte of Timer 0 (for 65536 - 3036 counts)
SETB TR0 ; Start Timer 0
; Delay loop
DELAY_LOOP:
CLR TF0 ; Clear Timer 0 overflow flag
JNB TF0, $ ; Wait until Timer 0 overflows
; Load Timer 0 with initial value for next overflow
MOV TH0, #3CH
MOV TL0, #0B0H
DJNZ R7, DELAY_LOOP ; Decrement R7 and repeat until R7 is zero
; Timer overflowed 16 times, 1 second delay achieved
; Reset R7 for next 1-second delay
MOV R7, #16
SJMP DELAY_LOOP ; Repeat the delay continuously
END
Explanation
1. Initialize Timer 0:
o MOV TMOD, #01H: Set Timer 0 to Mode 1 (16-bit timer mode).
o MOV TH0, #3CH and MOV TL0, #0B0H: Load Timer 0 registers with initial values. The
value 3036 (0B03CH in hex) is calculated as 65536 - 3036 = 62500 counts to achieve
1-second delay using 16 overflows.
2. Start Timer 0:
o SETB TR0: Start Timer 0.
3. Delay Loop:
o DELAY_LOOP: Label for the delay loop.
o CLR TF0: Clear Timer 0 overflow flag.
o JNB TF0, $: Wait until Timer 0 overflows ($ indicates current address, creating a short
wait loop).
4. Reload Timer 0:
o Reload Timer 0 with the initial value for the next overflow.
o DJNZ R7, DELAY_LOOP: Decrement R7 (initialized to 16) and repeat the loop until R7
reaches zero, indicating 16 overflows (approximately 1 second).
5. Repeat:
o MOV R7, #16: Reset R7 for the next 1-second delay.
o SJMP DELAY_LOOP: Jump back to the beginning of the delay loop to create a
continuous delay.
This program will generate a continuous 1-second delay using Timer 0 on the 8051 microcontroller.
Adjustments may be needed for different clock frequencies or specific timing requirements.
Write an assembly language program to count display 0-9 on Seven Segment Display with one
second delay for microntroller 8051
ChatGPT
ChatGPT
To display numbers 0-9 on a seven-segment display with a one-second delay using an 8051
microcontroller, we need to:
1. Set up the seven-segment display: Connect the seven-segment display to Port 1 (P1).
2. Initialize Timer 0: Configure Timer 0 to generate a one-second delay.
3. Create a loop to display the numbers 0-9: Iterate through the numbers and display each one
on the seven-segment display with a one-second delay.
Seven-Segment Display Mapping
Assume a common anode seven-segment display where 0-9 are represented as follows (binary for
P1):
0: 0xC0 (1100 0000)
1: 0xF9 (1111 1001)
2: 0xA4 (1010 0100)
3: 0xB0 (1011 0000)
4: 0x99 (1001 1001)
5: 0x92 (1001 0010)
6: 0x82 (1000 0010)
7: 0xF8 (1111 1000)
8: 0x80 (1000 0000)
9: 0x90 (1001 0000)
Assembly Program
assembly
Copy code
ORG 0H ; Start address
; Seven-segment display codes for digits 0-9
SEGMENT_CODES:
DB 0C0H, 0F9H, 0A4H, 0B0H, 0A9H, 0B2H, 082H, 0F8H, 080H, 090H
; Initialize
MOV P1, #0FFH ; Turn off all segments initially (assuming common anode)
MOV TMOD, #01H ; Set Timer 0 in Mode 1 (16-bit timer mode)
; Main loop
MAIN_LOOP:
MOV R0, #00H ; Initialize counter to 0
DISPLAY_LOOP:
MOV DPTR, #SEGMENT_CODES ; Point to the segment codes
MOV A, R0 ; Load current digit into accumulator
MOVC A, @A+DPTR ; Get the corresponding segment code
MOV P1, A ; Output to Port 1 (display digit)
; 1 second delay
MOV TH0, #3CH ; Load high byte of Timer 0 (for 65536 - 3036 counts)
MOV TL0, #0B0H ; Load low byte of Timer 0 (for 65536 - 3036 counts)
SETB TR0 ; Start Timer 0
DELAY_LOOP:
JNB TF0, DELAY_LOOP ; Wait until Timer 0 overflows
CLR TR0 ; Stop Timer 0
CLR TF0 ; Clear Timer 0 overflow flag
INC R0 ; Increment counter
CJNE R0, #0AH, DISPLAY_LOOP ; Repeat for digits 0-9
SJMP MAIN_LOOP ; Repeat the main loop
END
Explanation
1. Seven-Segment Display Codes:
o SEGMENT_CODES: A table storing the binary codes for displaying digits 0-9 on a
seven-segment display.
2. Initialization:
o MOV P1, #0FFH: Turn off all segments initially.
o MOV TMOD, #01H: Set Timer 0 in Mode 1 (16-bit timer mode).
3. Main Loop:
o MAIN_LOOP: Label for the main loop.
o MOV R0, #00H: Initialize counter R0 to 0.
4. Display Loop:
o DISPLAY_LOOP: Label for the display loop.
o MOV DPTR, #SEGMENT_CODES: Point DPTR to the segment codes table.
o MOV A, R0: Load the current digit into the accumulator.
o MOVC A, @A+DPTR: Get the corresponding segment code using MOVC (move code
byte) instruction.
o MOV P1, A: Output the segment code to Port 1 to display the digit.
5. 1-Second Delay:
o MOV TH0, #3CH and MOV TL0, #0B0H: Load Timer 0 with the initial value to achieve
a 1-second delay.
o SETB TR0: Start Timer 0.
o DELAY_LOOP: Wait in a loop until Timer 0 overflows.
o CLR TR0 and CLR TF0: Stop Timer 0 and clear the overflow flag.
6. Increment Counter:
o INC R0: Increment the counter R0.
o CJNE R0, #0AH, DISPLAY_LOOP: If R0 is not equal to 10 (0x0A), repeat the display
loop for the next digit.
7. Repeat:
o SJMP MAIN_LOOP: Jump back to the main loop to continuously display digits 0-9.
This program will display numbers 0-9 on a seven-segment display with a one-second delay between
each number, continuously cycling through the digits. Adjustments may be needed for different
hardware setups or timing requirements.