Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Simple Programs in 8051 Assembly Language

Written By: 

Himanshu Choudhary

Statement 8:- the crystal frequency is given as 12 MHz. Make a subroutine that will generate delay of exact 1 ms. Use this delay to generate square wave of 50 Hz on pin P2.0

Solution: - 50 Hz means 20 ms. And because of square wave 10 ms ontime and 10 ms offtime. So for 10 ms we shall send 1 to port pin and for another 10 ms send 0 in continuous loop.

          <_x0021_xml:namespace prefix="st1" ns="urn:schemas-microsoft-com:office:smarttags"/>Loop:   Setb p2.0                     ; send 1 to port pin

                        Mov r6, #0Ah              ; load 10d in r6

                        Acall delay                   ; call 1 ms delay ×10 = 10 ms

                        Clr p2.0                       ; send 0 to port pin

                        Mov r6, #0Ah              ; load 10d in r6

                        Acall delay                   ; call 1 ms delay ×10 = 10 ms

                        Sjmp loop                    ; continuous loop

                 Delay:                                  ; load count 250d

            Lp2:     Mov r7, #0FAh           

Lp1:     Nop                             ; 1 cycle

Nop                             ; 1+1=2 cycles

Djnz r7, lp1                  ; 1+1+2 = 4 cycles

Djnz r6, lp2                  ; 4×250 = 1000 cycles = 1000 µs = 1 ms



Statement 9:-count number of interrupts arriving on external interrupt pin INT1. Stop whencounter overflows and disable the interrupt. Give the indication on pinP0.0

Solution: -as we know whenever interrupt occurs the PC jumps to one particular location where it’s ISR is written. So we have to just write one ISR that will do the job

Movr2, #00h                ; initialize the counter

Movie, #84h                 ; enable external interrupt 1

           Here:     Sjmp here             ; continuous loop


Org 0013h                               ; interrupt 1location

Incr2                            ; increment the count

Cjner2, #00h, out         ; check whether it overflows

Movie, #00h                 ; if yes then disable interrupt

Clr       p0.0                 ; and give indication

            Out      : reti                              ; otherwise keep counting

Statement 10: -continuously scan port P0. If data is other then FFh write a subroutine that will multiply it with 10d and send it to port P1

Solution: -here we have to use polling method. We shall continuously pole port P0 if there is any data other then FFh. If there is data we shall call subroutine


Again:    Mov p0, #0ffh  ; initialize port P0 as input port

Loop:   Mov a, p0                     ; get the data in acc

Cjne a, #0FFh, dat       ; compare it with FFh

Sjmp loop                    ; if same keep looping

Dat:      acall multi;                    if different call subroutine

Sjmp again                     ; again start polling


Mov b,#10d                 ; load 10d in register B

Mul ab                         ; multiply it with received data

Mov p1, a                    ; send the result to P1

Ret                               ;return to main program