8051 Assembly Language Program Examples
8051 Assembly Language Program Examples
The program configures Counter 1 in Mode 2 (8-bit auto-reload) by setting TMOD to 36H. The TH1 register is initialized to zero for counting from zero. The process involves setting bit P3.5 to enable counter input from an external source connected to T1. TR1 is set to start the counter. As the counter counts clock pulses, the current count value from TL1 is moved to Port P2, updating LED states in real-time with the counter value. When TF1 is set upon overflow, the counter is halted, cleared, and immediately restarted, allowing continuous counting and updating of the LED display .
The mechanism involves using the DPTR register to point to the starting address (200H) in code space, and the R0 register to point to the starting address (40H) in RAM. The program uses the MOVC instruction to move a byte of data from code memory addressed by DPTR to the accumulator, and then moves the accumulator's content to the RAM location addressed by R0. The DPTR and R0 are incremented after each byte is transferred to move to the next byte and address respectively, and a loop continues this process for the length of the string, controlled by R1 which represents the length of the string .
The POP instruction functions by transferring data from the top of the stack to the specified register. It utilizes the stack pointer (SP) to identify the current top of the stack position in memory. Upon execution, the data at the memory address pointed to by SP is moved into the specified register, and then the SP is incremented to point to the next stack location. This operation effectively retrieves and removes the topmost value from the stack, adhering to the Last-In-First-Out (LIFO) principle .
The program toggles all the bits of ports P0, P1, and P2 by writing the constant values 55H and AAH to these ports alternately. This creates a square wave or toggling effect on all bits. The time delay implemented by the DELAY subroutine inserts a pause between the toggling operations to control the frequency of the waveforms generated on the ports. The delay loop uses two nested loops to create the desired time between toggling states .
The SJMP (Short Jump) instruction enables loop execution by directing program flow to a specified address relative to the current PC. This operation forms an effective mechanism for creating infinite or controlled loops, allowing repeated execution of block instructions. For example, in a program that repeatedly toggles port bits or reads an input continuously, SJMP can be used to loop back to the beginning of the instruction sequence thereby ensuring sustained operation until an external event interrupts the process. An example from the document is the use of SJMP HERE to lock the program in a continuous loop that transfers data from one port to another without termination .
The SJMP HERE instruction creates an infinite loop, keeping the microcontroller locked in a continuous cycle of reading data from Port P1 and sending it to Port P2. This ensures that as long as the program is running, the data flow from the input port P1 to the output port P2 is uninterrupted .
To generate a 1 kHz square wave on P1.0, Timer 1 is configured in Mode 2 (auto-reload mode). The TMOD register is set to 10H for Timer 1 in Mode 2. Timer 1's registers TL1 and TH1 are loaded with the values 33H and FEH respectively, which correspond to the required delay for generating a 0.5 ms half-period of the waveform. The CPL P1.0 instruction toggles the pin state, providing the square wave output. The timer is started using the TR1 bit and repeatedly checked for overflows using TF1 until a half-period delay elapses, at which time the TF1 flag is cleared and the process repeats .
The JNC (Jump if No Carry) instruction is used to check the carry flag after performing an addition of two 8-bit numbers. If the addition results in a carry (overflow beyond 8 bits), the carry flag will be set, and the JNC will not take the jump, allowing an increment operation on the designated overflow register (such as R3 for carry count). If no carry occurs, the addition process continues without incrementing the overflow register .
To use Timer 0 for toggling a bit of port P2 every 100 microseconds, Timer 0 is set in Mode 2 (8-bit auto-reload) by initializing TMOD with 02H. The TH0 register is loaded with a calculated preset value (A4H) to create a 100-microsecond delay according to the clock period. The Timer 0 interrupt is enabled to automate the toggling process using the interrupt vector, which executes an ISR (Interrupt Service Routine) to toggle P2.1 every time the timer overflows. The CPL P2.1 instruction is used within the ISR to perform the toggle operation .
The program sets up the serial communication by initializing the DPTR with the starting address of the string data, configures Timer 1 in Mode 2 for baud rate generation, and sets TH1 with the required value to achieve a baud rate of 9600 (calculated to be -3). The SCON register is configured for 8-bit data transfer with one stop bit. The data is sequentially moved from code memory into the A register using the MOVC instruction, then sent to the SBUF register for serial transmission. The transmission completion is monitored by checking the TI flag, which indicates the end of transmission, upon which the flag is cleared and the next byte is processed .