0% found this document useful (0 votes)
133 views

Write An 8051 Assembly Program To Generate A 2ms Waveform Wi

CKD

Uploaded by

Aritra Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Write An 8051 Assembly Program To Generate A 2ms Waveform Wi

CKD

Uploaded by

Aritra Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Write an 8051 assembly program to generate a 2ms waveform with

50% duty cycle (Square wave generator) on the port pin P1.5. Assume
the clock frequency of 8051 is 12 MHz and use the timer T1.

ORG 0000H ; Set the origin to address 0000H


MOV TMOD,#01H ; Set Timer 1 in mode 1 (16-bit timer mode)
Begin: MOV TH1,#0FCH ; Load TH1 with 0FCH for the OFF time (high
byte)
MOV TL1,#18H ; Load TL1 with 18H for the OFF time (low byte)
ACALL DELAY ; Call the DELAY subroutine
MOV TH1,#0FCH ; Load TH1 with 0FCH for the ON time (high byte)
MOV TL1,#18H ; Load TL1 with 18H for the ON time (low byte)
ACALL DELAY ; Call the DELAY subroutine
SJMP Begin ; Jump back to the beginning to create a loop
DELAY: CPL P1.5 ; Complement the bit P1.5 (toggle the pin)
SETB TR1 ; Start Timer 1
L1: JNB TF1,L1 ; Wait here until Timer 1 overflows (TF1 is set)
CLR TF1 ; Clear the Timer 1 overflow flag
CLR TR1 ; Stop Timer 1
RET ; Return from the subroutine
END ; End of the program
Clock Frequency and Timer Calculation
Clock Frequency: 13 MHz
Machine Cycle Frequency: 13 MHz / 12 = 1.0833 MHz (since 8051
divides the clock by 12)
Machine Cycle Period: 1 / 1.0833 MHz ≈ 0.923 µs
Desired Waveform Characteristics
Total Period: 3 ms
Duty Cycle: 60% ON, 40% OFF
Calculating Timer Values
ON Time: 60% of 3 ms = 1.8 ms
OFF Time: 40% of 3 ms = 1.2 ms
Timer Counts
ON Time Counts: 1.8 ms / 0.923 µs ≈ 1950 counts
OFF Time Counts: 1.2 ms / 0.923 µs ≈ 1300 counts
Timer Reload Values
The 8051 timer counts from the loaded value up to 65536 (2^16).
Therefore, the reload values are calculated as:

ON Time Reload Value: 65536 - 1950 = 63586 (0F8B2H)


OFF Time Reload Value: 65536 - 1300 = 64236 (0FB1CH)
Assume a switch is connected at port pin P1.2, monitor the status of
the switch. If the status of the switch is HIGH (“1”) then transfer
data from R1 (Bank 0) to Port P3, else transfer data from R0 (Bank 0)
to Port P3.

ORG 0000H ; Set the origin to address 0000H


CLR PSW.3 ; Clear PSW.3 to select Register Bank 0
CLR PSW.4 ; Clear PSW.4 to select Register Bank 0
MOV R0, #0FH ; Load R0 with the value 0FH (not mandatory)
MOV R1, #0F0H ; Load R1 with the value 0F0H (not mandatory)

L2: JB P1.2, L1 ; Jump to label L1 if P1.2 is set (HIGH)


MOV A, R0 ; Move the content of R0 to the accumulator
MOV P3, A ; Transfer the content of the accumulator to Port P3
SJMP L2 ; Jump back to label L2 to continue monitoring

L1: MOV A, R1 ; Move the content of R1 to the accumulator


MOV P3, A ; Transfer the content of the accumulator to Port P3
SJMP L2 ; Jump back to label L2 to continue monitoring

END ; End of the program


DD
Assuming that the frequency is 11.0592 MHz. What value do we
need to load the timer’s register if we want to have a time delay of
5mS? Show the program for timer 0 to create a pulse width of 5mS
on P2.3.
Crystal frequency is 11.0592MHz. The counter counts up every
1.085uS. • This means that out of many 1.085uS intervals, we must
make a 5mS pulse. • To get that, 5mS/1.085uS = 4608 clocks. •
Then 65536 – 4608 = EE00H, we have TH = EE and TL = 00
CLR P2.3 ; Clear P2.3 to start with a low pulse
MOV TMOD, #01 ; Set Timer 0 in 16-bit mode
HERE: MOV TL0, #00 ; Load low byte of Timer 0 with 00H
MOV TH0, #0EEH ; Load high byte of Timer 0 with EEH (for 5ms
delay)
SETB P2.3 ; Set P2.3 to start the high pulse
SETB TR0 ; Start Timer 0
AGAIN: JNB TF0, AGAIN ; Wait here until Timer 0 overflows (TF0 = 1)
CLR TR0 ; Stop Timer 0
CLR TF0 ; Clear Timer 0 overflow flag for the next cycle
CISC vs RISC Comparison

CISC (Complex Instruction Set Computer)

• Hardware Emphasis

• Multiple Instruction Sizes and Formats

• Instructions of Same Set with Few Formats

• Less Registers

• More Addressing Modes

• Complexity in Compiler

• Instructions Take a Varying Amount of Cycle Time


• Pipelining is Difficult

RISC (Reduced Instruction Set Computer)

• Software Emphasis

• Fewer Addressing Modes

• Extensive Use of Microprogramming

• Uses More Registers

• Instructions Take One Cycle Time

• Pipelining is Easy
Microprocessor vs Microcontroller

Microprocessor

• Just a processor.

• Memory and I/O components have to be connected externally.

• Since I/O and memory are connected externally, the circuit

becomes large.

• Power consumption and cost are high.

• Most of the microprocessors do not have power saving

modes.Mainly used in personal computers.Can't be used in

compact systems and hence inefficient.

Microcontroller

• Contains an external processor along with internal memory

and I/O components.Since I/O and memory are present

internally, the circuit is small. Power consumption and cost

are low. Most of the microcontrollers have power saving

modesUsed mainly in washing machines, MP3 players,


another embedded systems.Microcontroller is an efficient

technique.
• On-chip ROM: Stores program code.On-chip RAM: Used for

data storage.Timer : Generates interrupts after a specific time

interval.

• CPU: Executes instructions.OSC: Oscillator, provides clock

signal. Bus Control: Manages data transfer between

components.I/O Ports: Interface with external devices.

• Serial Port: Sends and receives data serially.Interrupt control

is a mechanism that lets a CPU handle urgent events.


intel Core i3:

• Entry-level processor
• 2-4 cores
• Lower clock speed
• Less cache memory
• Suitable for basic tasks like browsing, office work
Intel Core i5:

• Mid-range processor
• 4-6 cores
• Higher clock speed than i3
• More cache memory than i3
• Suitable for gaming, video editing, and heavy multitasking
Intel Core i7:

• High-end processor
• 6-8 cores
• Highest clock speed
• Most cache memory
• Suitable for demanding tasks like 3D modeling, video
production, and extreme gaming
Example 1: Toggling 55H and AAH on Port 0
BACK: MOV A, #55H ; Load A with 55H
MOV P0, A ; Send 55H to Port 0
ACALL DELAY ; Call delay subroutine
MOV A, #0AAH ; Load A with AAH
MOV P0, A ; Send AAH to Port 0
ACALL DELAY ; Call delay subroutine
SJMP BACK ; Jump back to repeat the process

Example 2: Transfer Data from Port 0 to Port 1

MOV A, #00H ; Clear accumulator


MOV P1, A ; Clear Port 1
MOV A, #0FFH ; Load A with FFH
MOV P0, A ; Set Port 0 to input mode
BACK: MOV A, P0 ; Read data from Port 0
MOV P1, A ; Send data to Port 1
SJMP BACK ; Repeat the process

Example 3: Toggle All Bits of Port 1 Continuously

MOV A, #55H ; Load A with 55H


BACK: MOV P1, A ; Send 55H to Port 1
ACALL DELAY ; Call delay subroutine
CPL A ; Complement A (toggle bits)
SJMP BACK ; Repeat the process
Using Port 1 as Input Port

MOV A, #0FFH ; Set Port 1 as input


MOV P1, A
MOV A, P1 ; Read data from Port 1
MOV R7, A ; Store data in R7
ACALL DELAY ; Call delay subroutine
MOV A, P1 ; Read data from Port 1 again
MOV R6, A ; Store data in R6
ACALL DELAY ; Call delay subroutine
MOV A, P1 ; Read data from Port 1 again
MOV R5, A ; Store data in R5

Example 4: Create Square Wave of 50% Duty Cycle on Bit 0


of Port 1

HERE: SETB P1.0 ; Set bit 0 of Port 1


LCALL DELAY ; Call delay subroutine
CLR P1.0 ; Clear bit 0 of Port 1
LCALL DELAY ; Call delay subroutine
SJMP HERE ; Repeat the process

Example 5: Create Square Wave of 66% Duty Cycle on Bit 3


of Port 1

HERE: SETB P1.3 ; Set bit 3 of Port 1


LCALL DELAY ; Call delay subroutine
LCALL DELAY ; Call delay subroutine again
CLR P1.3 ; Clear bit 3 of Port 1
LCALL DELAY ; Call delay subroutine
SJMP HERE ; Repeat the process

Example 6: Toggle All Bits of P0, P1, and P2 Every 100 µS

ORG 0
BACK: MOV A, #55H ; Load A with 55H
MOV P0, A ; Send 55H to Port 0
MOV P1, A ; Send 55H to Port 1
MOV P2, A ; Send 55H to Port 2
ACALL DELAY ; Call delay subroutine
MOV A, #0AAH ; Load A with AAH
MOV P0, A ; Send AAH to Port 0
MOV P1, A ; Send AAH to Port 1
MOV P2, A ; Send AAH to Port 2
ACALL DELAY ; Call delay subroutine
SJMP BACK ; Repeat the process

Delay Subroutine

MOV R5, #120 ; Load R5 with 120


H3: MOV R4, #255 ; Load R4 with 255
H2: MOV R3, #255 ; Load R3 with 255
H1: DJNZ R3, H1 ; Decrement R3 until zero
DJNZ R4, H2 ; Decrement R4 until zero
DJNZ R5, H3 ; Decrement R5 until zero
RET ; Return from subroutine
Example 7: Monitor P1.2 and Perform Actions

SETB P1.2 ; Make P1.2 an input


MOV A, #45H ; Load A with 45H
AGAIN: JNB P1.2, AGAIN ; Wait until P1.2 is high
MOV P0, A ; Send 45H to Port 0
SETB P2.3 ; Set bit 3 of Port 2
CLR P2.3 ; Clear bit 3 of Port 2 (H-to-L
pulse)

Example 8: Monitor P2.3 and Control Buzzer

HERE: JNB P2.3, HERE ; Wait until P2.3 is high


SETB P1.5 ; Set bit 5 of Port 1
CLR P1.5 ; Clear bit 5 of Port 1 (H-to-L
pulse)
SJMP HERE ; Repeat the process

Example 9: Check Switch Status and Send Data to P2

SETB P1.7 ; Make P1.7 an input


AGAIN: JB P1.7, OVER ; Jump if P1.7 is high
MOV P2, #‘D’ ; Send 'D' to Port 2 if switch is
low
SJMP AGAIN ; Repeat the process
OVER: MOV P2, #‘S’ ; Send 'S' to Port 2 if switch is
high
SJMP AGAIN ; Repeat the process
Example 10: Check Switch Status Using Carry Flag

SETB P1.7 ; Make P1.7 an input


AGAIN: MOV C, P1.7 ; Read switch status into carry
flag
JC OVER ; Jump if switch is high
MOV P2, #‘D’ ; Send 'D' to Port 2 if switch is
low
SJMP AGAIN ; Repeat the process
OVER: MOV P2, #‘E’ ; Send 'E' to Port 2 if switch is
high
SJMP AGAIN ; Repeat the process

Example 11: Send Switch Status to LED

SETB P1.7 ; Make P1.7 an input


AGAIN: MOV C, P1.7 ; Read switch status into carry
flag
MOV P2.7, C ; Send switch status to LED
SJMP AGAIN ; Repeat the process

I hope these comments help you understand the code better! If


you have any more questions or need further assistance, feel free
to ask.

You might also like