Microcontroller is an electronic component that contains a microprocessor circuit (CPU), memory (RAM/ROM) and input-output (I/O), the circuit is in the chip level or commonly called a single chip microcomputer. In the microcontroller there are already microprocessor components with interconnected internal buses. These components are RAM-ROM, timer, parallel and serial I/O components and interrupt controller.
The AT89C51 microcontroller is a type of 8-bit CMOS microcontroller that has a 4 kbyte Flash Memory reprogramming system with a write/erase endurance of 1000 times.
In addition, there is an internal RAM with a capacity of 128 x 8 bits. The operating frequency is up to 24 Mbyte. This microcontroller also has 32 bit I/O ports divided into 4 ports with 8 I/O lines, and there is also a serial port with full duplex serial control, two 16-bit timers/counters and an internal oscillator and timer circuit.
This microcontroller has 40 pin configurations, the function of each pin can be grouped into: voltage source, crystal, control and input-output. Of the eight channels can be used as a unit connected to parallel devices such as digital to analog converter circuits and so on, or each line can operate its own single bit device, such as: switches, LED transistors, transducers, solenoids, motors and others.
1. AT89C51 Microcontroller Configuration
AT89C51 Microcontroller Configuration
- PIN 1 -- 8 (port 1) is an 8-bit bidirectional I/O bus that can be used for various purposes. This port is also used as an address channel during programming.
- Pin 9 (RST) is an active high reset input, the pin used to reset the microcontroller. The transition pulse from low to high will reset the microcontroller.
- Pins 10-17 (port 3) are 8-bit bidirectional channels/buses that have a replacement function. If the replacement function is not used, it can be used as a multi-purpose 8-bit parallel port. In addition, some of port 3 can function as control signals during the programming process. The replacement functions include:
| Bit | Nama | Fungsi Alternatif |
|------|------|------------------------------------------------------|
| P3.0 | RXD | (Recive data) untuk menerima data port serial |
| P3.1 | TXD | (Transmit data) untuk mengirim data port serial |
| P3.2 | INT0 | Interrupsi eksternal 0 |
| P3.3 | INT1 | Interrupsi eksternal 1 |
| P3.4 | T0 | (Timer 0) masukan (input) eksternal waktu/pencacah 0 |
| P3.5 | T1 | (Timer 1) masukan (input) eksternal waktu/pencacah 1 |
| P3.6 | WR | (Write) jalur menulis memori data eksternal |
| P3.7 | RD | (Read) jalur membaca memori data eksternal |
- Pins 18-19 are inputs to the high-power oscillator amplifier. This microcontroller has the required oscillator circuit on the same chip, except for the crystal circuit that controls the oscillator frequency. Therefore, pins 18-19 are very necessary to be connected to the crystal.
- Pin 20 (ground), this pin is connected to ground.
- Pin 21 - 28 (port 2) which is a bidirectional 8-bit channel/bus, this port is used to send address bytes when accessing external memory. When retrieving data from an external memory program or while accessing external memory data that uses a 16-bit address (MOVX @ DPTR), port 2 functions as a high address channel/bus (A8 - A15). While when accessing external memory data that uses an 8-bit address (MOVX @ RI).
- Pin 29 (Program Store Enable). PSEN is a control signal that allows external program memory to enter the bus during the instruction fetching process.
- Pin 30 (Address Latch Enable). ALE/PROG is used to hold external memory addresses during instruction execution. This pin also serves as a programming input signal (PROG) during the programming process.
- Pin 31 (External Access Enable. EA is a control signal for reading program memory. If set to low logic, the microcontroller will execute all instructions from external program memory, while if set to high logic, the microcontroller will execute instructions from internal program memory (instructions from ROM).
- Pins 32-39 (port 0) are bidirectional 8-bit bus lines. Also used as a multiplex of the low address bus and data bus during access to external program memory. During programming and verification, port 0 is used as a data bus line.
- Pin 40 (VCC) is the positive 5 volt voltage source.
2. Arithmetic and Logic Unit
ALU (Arithmetic Logic Unit) functions to carry out arithmetic operations (addition and subtraction) and logic (AND operations, OR operations and XOR operations). The results of these operations are then stored back into the accumulator. Operations that occur in the ALU are closely related to the accumulator and status bits in the register.
3. Register
The microcontroller has the following registers:
- Accumulator (Register A). Accumulator is an 8-bit register that is the center of all arithmetic and logic operations, storing and input operations.
- Register B is an 8-bit register that functions as a place to temporarily store data during a process.
- Program Counter (PC). The program counter is a 16-bit register that always indicates the memory location of the instruction to be accessed.
- Stack Pointer (SP). Stack Pointer is a 16-bit register that has a special function as an address or data pointer that is at the top of the stacking operation in RAM. The stack pointer always decreases by two every time data is pushed into the stacking location and always increases by two every time data is pulled out of the stacking location.
4. AT89S51 Timing Counter Source
The AT89S51 microcontroller is equipped with an internal clock source/oscillator that can be used as a clock source for the AT89S51. To use the internal oscillator, an additional crystal or ceramic resonator is required between the XTAL1 and XTAL2 legs and a capacitor to ground. For the crystal value, frequencies from 3 MHz can be used. to 24 MHz. While the capacitor value can be 30 pF +- 10 pF.
5. Memory
- RAM (Random Access Memory). Memory that can be read and written to, and data will only be stored if the microcontroller receives a voltage source.
- Flash Memory, a place to store program data that can only be read at the time of the microcontroller.
Microcontroller Concept
Microcontrollers are one of the basic components of a computer system. Although much smaller than personal computers and mainframe computers, microcontrollers are built from the same basic elements. Simply put, a computer will produce a specific output based on the input received and the program executed.
Like most computers, a microcontroller is a tool that carries out instructions given to it. This means that the most important and primary part of a computerized system is the program itself created by a programmer. This program instructs the computer to perform a long chain of simple actions to perform a more complex task desired by the programmer.
microcontroller concept
Computer Input System
Input devices provide information to the computer system from the outside world. In personal computer systems, the most common input device is the keyboard. Mainframe computers use keyboards and punched card readers as input devices. Systems with microcontrollers typically use much smaller input devices such as switches or small keypads.
Almost all microcontroller inputs can only process digital input signals with the same voltage as the logic voltage from the source. The zero level is called VSS and the positive source voltage (VDD) is generally 5 volts. In fact, in the real world there are many analog signals or signals with varying voltage levels. Therefore, there is an input device that converts analog signals into digital signals so that the computer can understand and use them. There are several microcontrollers that are equipped with this conversion device, called ADC, in one integrated circuit.
Computer Output System
Output devices are used to communicate information or actions from the computer system to the outside world. In a personal computer (PC) system, a common output device is a CRT monitor. While a microcontroller system has a much simpler output such as an indicator light or beeper. The controller phrase of the word microcontroller provides confirmation that this device controls something.
Microcontrollers or computers process signals digitally, so that in order to provide analog output, a conversion process from digital to analog signals is required. A device that can perform this conversion is called a DAC (Digital to Analog Converter).
CPU (Central Processing Unit)
The CPU is the brain of a computer system. The main job of the CPU is to execute programs consisting of instructions programmed by the programmer. A computer program will instruct the CPU to read information from input devices, read information from and write information to memory, and to write information to output.
In microcontrollers, there is generally only one program that works in an application. The M68HC05 CPU recognizes only 60 different instructions. Therefore, this computer system is very suitable as a model for studying the basics of computer operations because it is possible to examine each operation that is carried out.
Computer Clock and Memory
Computer systems use a clock oscillator to trigger the CPU to execute one instruction after another in a sequential flow. Each small step of a microcontroller operation takes one or more clocks to complete.
There are several types of computer memory that are used for different purposes in a computer system. The basic types that are often found in microcontrollers are ROM (Read Only Memory) and RAM (Random Access Memory). ROM is used as a permanent storage medium for programs and data that cannot be changed even if no voltage is applied to the microcontroller. RAM is used as a temporary storage place for data and calculation results during the operation process. Some microcontrollers include other types of memory such as EPROM (Erasable Programmable Read Only Memory) and EEPROM (Electrically Erasable Programmable Read Only Memory).
Computer Program
Programs are described as clouds because programs are actually the result of a programmer's imagination. The main components of a program are the instructions of the CPU's instruction set. Programs are stored in memory in a computer system where they can be sequentially executed by the CPU.
Microcontroller System
After explaining the parts of a computer system, now we will discuss the microcontroller. A computer system is depicted with parts surrounded by dotted lines. These parts are what make up the microcontroller. The part enclosed by the bottom box is a more detailed image of the arrangement of the parts enclosed by the dotted lines. The crystal is not included in the microcontroller system but is needed in the clock oscillator circuit.
A microcontroller can be defined as a complete computer system including a CPU, memory, clock oscillator, and I/O in a single integrated circuit. If some elements are removed, namely I/O and memory, then this chip will be called a microprocessor.
Introduction to AT89C51 Microcontroller
AT89C51 is a microcontroller produced by Atmel with 4K byte Flash PEROM (Programmable and Erasable Read Only Memory). AT89C51 has a Flash Memory reprogramming system with a durability of 1000 times write/erase.
This memory is usually used to store MCS51 standard commands so that it allows this microcontroller to work in single chip operation mode which does not require external memory to store the source code. - (Paulus Andi Nalwan, 2003: 1).
The following is a picture of the leg arrangement and block diagram of the AT89C51 microcontroller:
AT89C51 Pins
This microcontroller has 32 I/O ports divided into 4 ports with 8 I/O lines. Here is an explanation of each pin of the AT89C51 microcontroller IC:
1) Pin 1 to 8 (Port 1)
Port 1 is a bi-directional I/O port equipped with internal pullups. Port 1's output buffers are capable of supplying/absorbing currents of four TTL inputs.
Port 1 legs will be pulled high with internal pullups so that they can be used as input. As input, if Port 1 legs are connected to ground (pull low), then each leg will provide current because it is pulled high internally. Port 1 also receives the low byte address during program loading and flash verification. - (Agfianto Eko Putra, 2004: 90).
2) Pin 9 (RST)
Reset will be active by giving high input for 2 cycles.
3) Pins 10 to 17 (Port 3)
Port 3 is a bidirectional I/O port with internal pullups. Port 3's output buffers are capable of supplying/absorbing current from four TTL inputs.
The Port 3 legs will be pulled high with internal pullups so that they can be used as input. As input, if the Port 3 legs are connected to ground (pulled low), then each leg will provide current because it is pulled high internally. - (Agfianto Eko Putra, 2004: 90).
Table 1. Special Functions of Port 3 Pins
| Kaki Port | Fungsi Alternatif |
|-----------|--------------------------------------|
| P3.0 | RXD (port input serial) |
| P3.1 | TXD (port output serial) |
| P3.2 | INT0 (interupsi 0 eksternal) |
| P3.3 | INT1 (interupsi 1 eksternal) |
| P3.4 | T0 (input timer 0 eksternal) |
| P3.5 | T1 (input timer 1 eksternal) |
| P3.6 | WR (tulis ke memori data eksternal) |
| P3.7 | RD (baca dari memori data eksternal) |
4) Pin 18 (XTAL 2)
This pin is the oscillator output.
5) Pin 19 (XTAL 1)
This pin is the oscillator input.
6) Pin 20 (GND)
Connected to Ground.
7) Pins 21 to 28 (Port 2)
Port 2 is a bi-directional I/O port with internal pullups. Port 2's output buffers are capable of supplying/absorbing four TTL inputs.
Port 2 legs will be pulled high with internal pullups so that they can be used as inputs. As inputs, if Port 2 legs are connected to ground (pulled low), then each leg will provide current because it is pulled high internally.
Port 2 will provide the high byte address during instruction fetch from external program memory and during external data memory access using 16-bit address commands (eg: MOVX @DPTR). In this application, if you want to send "1", then use the internal pullup that has been provided. During external data memory access using 8-bit address commands (eg: MOVX @R1), Port 2 will send the contents of Shift Function Register Port 2. Port 2 also receives the high byte address during programming and verification. - (Agfianto Eko Putra, 2004: 90).
8) Pin 29 (PSEN)
Program Store Enable (PSEN) is a pin that functions when executing a program located in external memory. PSEN will be active twice per cycle.
9) Pin 30 (ALE)
This pin can function as Address Latch Enable which latches the low byte address when accessing external memory. ALE will be active when accessing external memory.
10) Pin 31 (EA)
In low condition, this pin will function as External Access Enable (EA) which means the microcontroller will run the program in external memory after the system is reset. If in high condition, this pin will function to run the program in internal memory.
11) Pin 32 to 39 (Port 0)
Port 0 is an open drain bidirectional output/input (I/0) port. As an output port, each leg can absorb eight TTL input currents. Port 0 can be used as high impedance inputs.
Port 0 can also be configured as a low byte address/data bus during the process of accessing external data and program memory. When used in this mode Port 0 has internal pullups but is weak. Port 0 also receives codes sent to it during the program loading process and issues codes during the program verification process that has been stored in the flash. In this case an external pullup is needed during the program verification process. - (Agfianto Eko Putra, 2004: 89).
12) Pin 40 (Vcc)
Connected to Vcc (+5 Volts).
Introduction to TIMER 89C51
Timer Registers
- Timer Mode Register (TMOD) at address 89H
- THx and TLx
- Timer Control Register (TCON) at address 88H
- The 89C51 has two timers, namely Timer 0 and Timer 1
- Each Timer consists of a 16 bit programmable counter.
Timer Mode Register
TMOD
- Gate: The timer will run if this bit is set and INT0 (for Timer 0) or INT1 (for Timer 1) is high.
- C/T: 1 = Counter
- 0 = Timer
- M1 & M0: To select timer mode.
THx and TLx (x is the Timer number)
It is a register that shows the value of the timer, where each timer has two registers, namely:
- THx for high byte
- TLx for low byte
- TH0 : Timer 0 High Byte is located at address 8AH
- TL0 : Timer 0 Low Byte is located at address 8BH
- TH1 : Timer 1 High Byte is located at address 8CH
- TL1 : Timer 1 Low Byte is located at address 8DH
Timer Control Register (TCON)
In this register, only 4 bits, namely TCON.4, TCON.5, TCON.6 and TCON.7, have functions related to the timer.
TCON
- TCON.7 or TF1: Timer 1 Overflow Flag that will be set when the timer overflows. This bit can be cleared by software or hardware when the program goes to the address pointed by the interrupt vector.
- TCON.6 or TR1: 1 = Timer 1 is active, 0 = Timer 1 is inactive
- TCON.5 or TF0: Same as TF1
- TCON.4 or TR0: Same as TR1
- TCON.3 to TCON.0 will be discussed in the interrupt section.
Timer Mode
Timer Mode consists of:
- Mode 0 Timer 13 bit
- Mode 1 Timer 16 bit
- Mode 2 Timer 8 bit auto reload
- Mode 3 Split Timer
Mode 0 (13 Bit Timer)
Mode 1 (16 Bit Timer)
TMOD
Mode 2 (16 Bit Timer)
TMOD
Mode 3 (16 Bit Timer)
TMOD
In this mode, Timer 0 is separated into 2 8-bit timers and TF1 from Timer 1 will not be affected even if Timer 1 overflows. Timer 1 can still be used for other modes such as serial baud rate.
How Timer Works
Timer Operation
The timer can work with clock sources from:
- Internal based on 1/12 oscillator frequency, C/T = 0
- External based on trigger from Tx (T0 for Timer 0 and T1 for Timer 1) C/T = 1.
The timer can work based on triggers from:
- Software (TRx Bit)
- Hardware (INTx leg).
Timer triggered from software
Timer Setting with Software
Timer triggered from Hardware
Timer Setting with Hardware
89C51 Serial Port Operation
89C51 has an On Chip Serial Port that can be used for Full Duplex serial data communication so that this Serial Port can still receive data when the data sending process occurs. To accommodate the data received or data to be sent, 89C51 has a register, namely SBUF located at address 99H where this register functions as a buffer so that when this microcontroller reads the first data and the second data has not been fully received, then this data will not be lost.
In reality, the SBUF register consists of two registers that occupy the same address, namely 99H. The registers are the Transmit Buffer Register which is write only (can only be written) and the Receive Buffer Register which is read only (can only be read). In the process of receiving data from the Serial Port, the data that enters the Serial Port will be stored in the Receive Buffer Register first and forwarded to the internal bus line when reading the SBUF register, while in the process of sending data to the Serial Port, the data written from the internal bus will be stored in the Transmit Buffer Register first before being sent to the Serial Port.
Figure 3.1 Serial Port Block Diagram
The 89C51 Serial Port can be used for synchronous or asynchronous data communication. Synchronous serial data communication is a form of serial data communication that requires a clock signal for synchronization where the clock signal will be fired at each bit of data transmission while asynchronous communication does not require a clock signal as synchronization. Data transmission in 89C51 serial communication is carried out starting from the lowest bit (LSB) to the highest bit (MSB).
Figure 3.2 Synchronous Communication and Asynchronous Communication
1. Synchronous Communication
The clock signal in synchronous communication is needed by the data receiving device to know that each bit of data has been sent. It can be seen in Figure 3.2 that the clock signal is fired (positive edge) when the first bit is sent and every change in the data bit. The receiving device or component will know that the first bit has been sent or a change in the data bit by detecting the clock signal.
In its application, synchronous communication from the AT89C51 serial port is always used to access the shift register, PISO (Parallel In Serial Out) for the process of receiving data from PISO to the AT89C51 Serial Port or SIPO (Serial In Parallel Out) for the process of sending data from AT89C51 to SIPO.
The Shift Register can be an IC Shift Register such as 74164, 74165 or an internal shift register from another microcontroller such as the AT89C51 Serial Port. Figure 3.2 shows the conditions that occur when sending data from the AT89C51 SBUF Register to SIPO and receiving data by the SBUF Register from PISO. Things to note if the Shift Register uses the IC 74164 or 74165 are, the serial communication form of this Shift Register starts from the highest bit (MSB) to the lowest bit (LSB) so that the data sent or received always has a reversed position of the bit weight.
Figure 3.3a 5AH Data Transmission from 89C51 Serial Port to SIPO
Figure 3.3b 5AH Data Reception from PISO to Serial Port 89C51
2. Asynchronous Communication
As mentioned earlier, asynchronous communication does not require a clock signal as a synchronization, but the sending of this data must be started with a start bit and ended with a stop bit as shown in Figure 3.2. The clock signal which is the baud rate of this data communication is generated by each of the data receiver and sender with the same frequency.
The receiver only needs to detect the start bit as the beginning of data transmission, then data communication occurs between two shift registers on the sender and receiver. After 8 bits of data are received, the receiver will wait for a stop bit as a sign that 1 byte of data has been sent and the receiver can be ready to wait for the next data transmission.
Figure 3.4 UART Communication
In its application, this asynchronous communication process is always used to access components that have UART (Universal Asynchronous Receiver/Transmitter) facilities such as PC Serial Ports or other microcontroller Serial Ports.
3. Serial Port Operation Mode
The 89C51 Serial Port has 4 operating modes which are regulated by bit 7 and bit 5 of the SCON (Serial Control) Register. SCON
1.2. Serial Port Operation Modes
- SM0: Serial Port Mode bit 0, Serial Mode Set bit
- SM1: Serial Port Mode bit 1, Serial Mode Setter bit
- SM2: Serial Port Mode bit 2, bit to enable multiprocessor communication when set condition.
- REN: Receive Enable, bit to enable data reception from Serial Port in set condition. This bit is set and cleared by software.
- TB8: Transmit bit 8, the 9th bit to be transmitted in mode 2 or 3. This bit is set and cleared by software.
- RB8: Receive bit 8, the 9th bit received in mode 2 or 3. In Mode 1 this bit functions as a stop bit.
- TI: Transmit Interrupt Flag, a bit that will be set at the end of a character transmission. This bit is set by hardware and cleared by software.
- RI: Receive Interrupt Flag, a bit that will be set at the end of receiving a character. This bit is set by hardware and cleared by software.
Table 3.1 Serial Port Operating Modes
4. Mode 0 Shift Register 8 bits
SCON
In this mode, the Serial Port functions as a synchronous data communication that requires a clock signal as synchronization. P3.1/TXD on the 89C51 functions as a Clock and P3.0/RXD as a data sending and receiving path.
Data transmission is done by writing the data to be sent into the SBUF Register (figure 3.3). Data will be sent serially synchronously via P3.0/RXD along with a clock signal via P3.1/TXD with a frequency of 1/12 of the crystal frequency used by the 89C51 oscillator.
Data reception is done by activating the REN bit (usually done at the beginning of the program) and clearing the RI bit when the data retrieval process will be carried out. When the RI condition is cleared, in the next machine cycle the clock signal will be sent out through the P3.1/TXD pin and the data on P3.0/RXD will be shifted into the SBUF.
5. Mode 1 UART 8 bit with adjustable Baud Rate
SCON
1.2.2. Mode 1 UART 8 bit with adjustable Baud Rate
In this mode, data communication is done in 8 bits of asynchronous data consisting of 10 bits, namely 1 start bit, 8 data bits and 1 stop bit. Baud Rate in this mode can be set using Timer 1.
Unlike in mode 0, in this mode which is UART mode, the alternative functions of P3.0/RXD and P3.1/TXD are used. P3.0 functions as RXD which is the pin for receiving serial data and P3.1 functions as TXD which is the pin for sending serial data. This also applies to other UART modes such as mode 2 and mode 3.
Data transmission is done by writing the data to be sent to the SBUF Register. Serial data will be shifted out starting with the start bit and ending with the stop bit starting from the lowest weighted bit (LSB) to the highest weighted bit (MSB). The TI bit will be set after the stop bit exits through the TXD leg indicating that the data transmission process has been completed. This bit must be cleared by the software after the data transmission is complete.
Data reception is done by the microcontroller by detecting a change in condition from high logic to low logic on the RXD leg where the change in condition is the start bit. Next, the serial data will be shifted into the SBUF and the stop bit into the RB8 bit. The RI bit will be set after 1 byte of data is received into the SBUF unless the stop bit = 0 in multiprocessor communication (SM2 = 1).
6. Mode 2 UART 9 bit with permanent Baud Rate
SCON
1.2.3. Mode 2 UART 9 bit with permanent Baud Rate
In this mode, data communication is done asynchronously with 11 bits, 1 start bit, 8 data bits, 1 9th bit that can be set and 1 stop bit. In the data sending process, the 9th bit is taken from Bit TB8 and in the data receiving process the 9th bit is placed on RB8.
7. Mode 3 UART 9 bit with adjustable Baud Rate
SCON
1.2.4. Mode 3 UART 9 bit with adjustable Baud Rate
This mode is the same as Mode 2, but the baud rate in this mode can be set via Timer 1.
8. Initialize and access Serial Port Register
To access the serial port, there are several things that must be set first by filling in several specific registers, namely:
- Determine Serial Mode
- Determine Serial Baud Rate
The process of determining the serial mode is done by filling in SCON as explained in 3.2. Serial Operation Mode.
9. Serial Baud Rate
The baud rate of the 89C51 Serial Port can be set in Mode 1 and Mode 3, but in Mode 0 and Mode 2, the baud rate has a permanent speed, namely for Mode 0 it is 1/12 of the oscillator frequency and Mode 2 is 1/64 of the oscillator frequency.
By changing the SMOD bit located in the PCON Register to set (the initial condition when the system is reset is clear), the baud rate in Modes 1, 2 and 3 will change to double.
In Mode 1 and 3 the baud rate can be set using Timer1. The method commonly used is Timer Mode 2 (8 bit auto reload) which only uses the TH1 register. The sending of each data bit occurs every 32 times of Timer 1 overflow so it can be concluded that:
Time to send each data bit = Timer 1 Overflow X 32 ........... (3.1)
Baud rate (number of data bits sent per second) =
If a baud rate of 9600 bps is desired, timer 1 must be set to overflow every time.
Timer 1 overflows every time TH1 reaches the overflow value with a frequency of fosc/12 or a period of 12/fosc. From here the following formula will be found:
With an oscillator frequency of 11.0592 MHz, TH1 is 253 or 0FDH. In addition to the variables above, there is another variable that regulates the serial baud rate, namely the SMOD Bit in the PCON Register. If this bit is set, the multiplier factor of 32 in formula 3.1 will change to 16. Therefore, it can be concluded that the formula for the serial baud rate for Mode 1 and Mode 3 is:
Table 3.2 Serial Mode vs baud rate table
Microcontroller Branching Instructions
ACALL addr11
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|--------------|----|----|-----|-----|----|---|---|
| 2 | 2 | ACALL Addr11 | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Performs a jump to a subroutine pointed to by the address in addr11. The possible jumps are in an area of 2K bytes.
The process that occurs when this instruction is executed is as follows:
- The data at Program Counter + 2 which is the program address when returning from the subroutine is stored in the stack.
- Stack pointer increased by 2 times
- Perform a jump to the address pointed to by addr11 by filling the Program Counter with that address. The address filled into the Program Counter is only 11 bits so the maximum jump is only 2K bytes.
Example:
2000 Acall Lompatan1
............
............
Lompatan1
2100 Mov A,#00H
The data in Program Counter + 2, namely 2002H, is stored on the stack where the high byte is stored at the address pointed by SP+1 and the low byte is stored at the address pointed by SP+2. If the previous SP position was at address 10H, the high byte, 20H will be stored at address 11H and the low byte, 02H is stored at 12H. Then the 11-bit data at jump address 1 is moved to the Program Counter.
LCALL addr16
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|--------------|----|----|-----|-----|----|---|---|
| 2 | 3 | LCALL Addr16 | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Performs a jump to a subroutine pointed to by the address in addr16. The jumps that can be performed are in an area of 64K bytes.
The process that occurs when this instruction is executed is as follows:
- The data at Program Counter + 2 which is the program address when returning from the subroutine is stored in the stack.
- Stack pointer increased by 2 times
- Perform a jump to the address pointed to by addr16 by filling the Program Counter with that address. The address filled into the Program Counter is 16 bits so the maximum jump can reach 64K bytes.
Example:
2000 Lcall Lompatan1
............
............
Lompatan1
3000 Mov A,#00H
The data in Program Counter + 2, namely 2002H, is stored on the stack where the high byte is stored at the address pointed by SP+1 and the low byte is stored at the address pointed by SP+2. If the previous SP position was at address 10H, the high byte, 20H, will be stored at address 11H and the low byte, 02H, will be stored at 12H. Then the 16-bit data at the jump address 1 is moved to the Program Counter, namely 3000H.
RET
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-----------|----|----|-----|-----|----|---|---|
| 2 | 1 | RET | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Performs a jump to the address stored in SP and SP-1. This instruction is commonly used when returning from a subroutine called with the ACALL or LCALL instruction.
The process that occurs is as follows:
- The contents of the address pointed to by the stack pointer are moved to the high nibble of the Program Counter.
- Stack pointer decreased by 1
- The contents of the address pointed to by the stack pointer are moved to the low nibble of the Program Counter.
- Stack pointer decrements by 1.
Example:
2000 Lcall Lompatan1
2002 .............
............
............
Lompatan1
3000 Mov A,#00H
3002 Ret
When the RET instruction is executed, the data 20H in the stack pointer is moved to the high Program Counter nibble, and the data 02H in the stack pointer --1 is moved to the low Program Counter nibble, so that the contents of the Program Counter become 2002H and the program will automatically execute the instruction at address 2002H.
RETI
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-----------|----|----|-----|-----|----|---|---|
| 2 | 1 | RETI | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Performs a jump to the address stored in SP and SP-1 and restores the interrupt flags so that the next interrupt with the same priority can be executed.
When an interrupt occurs, the data on PC+2 which is the address where the program must return after the interrupt process is completed is stored in SP and SP-1, so after the RETI instruction is executed, the addresses on SP and SP-1 are moved to the Program Counter and the program jumps to the address where the instruction that was being executed ended when the interrupt was detected.
AJMP addr11
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-------------|----|----|-----|-----|----|---|---|
| 2 | 2 | AJMP Addr11 | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Absolute Jump, jumps and runs the program located at the address specified by addr11. The process that occurs is, 11 bits of the address specified by addr11 are moved to the Program Counter so that the program will immediately run the instructions at that address.
Example:
AJMP Lompatan1
Mov A,#05H
Lompatan1:
Mov R0,#00H
The program will jump to jump address 1 and execute the Mov R0,#00H instruction without going through the MOV A,#05H instruction.
LJMP addr16
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-------------|----|----|-----|-----|----|---|---|
| 2 | 3 | LJMP Addr16 | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Long Jump, jumps and runs the program located at the address specified by addr16. The process that occurs is, 16 bits of the address specified by addr16 are moved to the Program Counter so that the program will immediately run the instructions at that address.
Example:
LJMP Lompatan2
Mov A,#05H
Lompatan2:
Mov R0,#00H
The program will jump to jump address 2 and execute the Mov R0,#00H instruction without going through the MOV A,#05H instruction.
SJMP rail
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-----------|----|----|-----|-----|----|---|---|
| 2 | 2 | SJMP rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Short Jump, performs a jump to the address specified by rel with a maximum jump of 128 bytes.
JMP @A+DPTR
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-------------|----|----|-----|-----|----|---|---|
| 2 | 1 | JMP @A+DPTR | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Performs a jump to the address generated by the sum between DPTR and accumulator A.
Example:
Mov A,#05H
Mov DPTR,#2000H
JMP A,@A+DPTR
When the JMP A,@A+DPTR instruction occurs, a jump occurs to address 2000H + 5H, namely 2005H, and the instruction at that address is executed.
JZ rail
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-----------|----|----|-----|-----|----|---|---|
| 2 | 2 | JZ rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Jumps to the specified address if accumulator A is 00H and immediately continues the instructions below if accumulator A is not 00H.
Example:
JZ Lompat1
MOV A,#07H
Lompat1:
MOV B,#00H
If the value of accumulator A is not 00H, the program will immediately continue the instruction below it, namely MOV A, # 07H and the program will execute the instruction at the Jump1 address, namely MOV B, # 00H if the value of accumulator A is 00H.
JNZ rail
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-----------|----|----|-----|-----|----|---|---|
| 2 | 2 | JNZ rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Performs a jump to the specified address if accumulator A is not 00H and immediately continues the instructions below if accumulator A is 00H.
Example:
JNZ Lompat1
MOV A,#07H
Lompat1:
MOV B,#00H
If the value of accumulator A is 00H, the program will immediately continue the instruction below it, namely MOV A, # 07H and the program will execute the instruction at the Jump1 address, namely MOV B, # 00H if the value of accumulator A is not 00H.
CJNE
This instruction performs a comparison between the destination data and the source data and jumps to the specified address if the comparison results are not the same.
Command form:
CJNE data tujuan, data sumber, alamat lompatan
The carry flag will be set if the destination data is smaller than the source data.
CJNE A,direct,rel
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-------------------|----|----|-----|-----|----|---|---|
| 2 | 3 | CJNE A,direct,rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | X | | | | | | | |
Perform a comparison between accumulator A and the direct address and perform a jump to the specified address if the comparison results are not the same.
Example:
CJNE A,00H,lompat1
The program will go to jump address 1 if the accumulator A data is not the same as the data at address 00H.
CJNE A,#data,rel
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|--------------|------------------|----|----|-----|-----|----|---|---|
| 2 | 3 | CJNE A,#data,rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | X | | | | | | | |
Perform a comparison between accumulator A and immediate data and perform a jump to the specified address if the comparison results are not the same.
Example:
CJNE A,#00H,lompat1
The program will go to jump address 1 if the accumulator data A is not the same as data 00H..
CJNE Rn,#data,rel
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|--------------|-------------------|----|----|-----|-----|----|---|---|
| 2 | 3 | CJNE Rn,#data,rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | X | | | | | | | |
Performs a comparison between Rn (R0...R7) and immediate data and performs a jump to the specified address if the comparison results are not the same.
Example:
CJNE R1,#00H,lompat1
The program will go to jump address 1 if the data on R1 is not the same as the data at address 00H.
CJNE @Ri,#data,rel
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|--------------|--------------------|----|----|-----|-----|----|---|---|
| 2 | 3 | CJNE @Ri,#data,rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | X | | | | | | | |
Make a comparison between data located at the address designated by the Register Index (R0 or R1) and immediate data and make a jump to the specified address if the comparison results are not the same.
Example:
CJNE @R1,#00H,lompat1
The program will go to jump address 1 if the data at the address designated by R1 is not the same as data 00H.
DJNZ Rn,rel
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|-------------|-------------|----|----|-----|-----|----|---|---|
| 2 | 2 | DJNZ Rn,rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Subtract Rn (R0...R7) by 1 and jump to the specified address if the result is not 00. If the result has reached 00, the program will continue to execute the instructions below.
Example:
Tunggu:
DJNZ R7,Tunggu
RET
Always jump to the waiting address and decrement R7 by 1 as long as the value of R7 has not reached 00.
DJNZ direct,rel
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|--------------|-----------------|----|----|-----|-----|----|---|---|
| 2 | 3 | DJNZ direct,rel | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
Subtract the data at the designated address directly by 1 and jump to the specified address if the result is not 00. If the result reaches 00, the program will continue to execute the instructions below.
Example:
Tunggu:
DJNZ 07H,Tunggu
RET
Always jump to the waiting address and reduce the data at address 07H by 1 as long as the value of the data at address 07H has not reached 00.
NOP
| Siklus | Jumlah Byte | Instruksi | | | | | | | |
|--------|--------------|-----------|----|----|-----|-----|----|---|---|
| 1 | 1 | NOP | | | | | | | |
| Flag | | C | AC | F0 | RS1 | RS0 | OV | | P |
| | | | | | | | | | |
This instruction functions to delay a program by 1 cycle without affecting registers or flags.