Unit IV Notes
Unit IV Notes
8051 MICROCONTROLLER
Architecture - Addressing modes and Instruction Sets – Interrupt structure – Timer –I/O
ports – Serial communication.
MICROCONTROLLERS VS MICROPROCESSORS
MICROPROCESSOR:
❖ A CPU built into a single VLSI chip is called a microprocessor.
❖ It is a general-purpose device and additional external circuitry is added to make it a
microcomputer.
❖ The microprocessor contains arithmetic and logic unit (ALU), Instruction decoder and
control unit, Instruction register, Program counter (PC), clock circuit (internal or external),
reset circuit (internal or external) and registers.
❖ But the microprocessor has no on chip I/O Ports, Timers, Memory etc.
❖ For example, Intel 8085 is an 8-bit microprocessor and Intel 8086/8088 a 16-bit
microprocessor.
❖ The block diagram of the Microprocessor is shown in Fig.4.1
MICROCONTROLLER:
❖ A microcontroller is a highly integrated single chip, which consists of on chip CPU
(Central Processing Unit), RAM (Random Access Memory), EPROM/PROM/ROM
(Erasable Programmable Read Only Memory), I/O (input/output) – serial and parallel,
timers, interrupt controller.
❖ For example, Intel 8051 is 8-bit microcontroller and Intel 8096 is 16-bit microcontroller.
❖ The block diagram of Microcontroller is shown in Fig.4.2.
❖ P0.0- P0.7(AD0-AD7) :
o The port 0 pins multiplexed with Address/data pins.
o If the microcontroller is accessing external memory these pins will act as
address/data pins otherwise they are used for Port 0 pins.
❖ P2.0- P2.7(A8-A15) :
o The port2 pins are multiplexed with the higher order address pins.
o When the microcontroller is accessing external memory these pins provide the
higher order address byte otherwise they act as Port 2 pins.
❖ P1.0- P1.7:
o These 8-pins are dedicated for Port1 to perform input or output port operations.
❖ P3.0- P3.7:
o These 8-pins are meant for Port3 operations and also for some control
operations like Read, Write, Timer0, Timer1, INT0, INT1, RxD and TxD
❖ RST:
o The RESET pin is an input pin and it is an active high pin.
o When a high pulse is applied to this pin the microcontroller will reset and
terminate all activities.
o Upon reset all the registers except PC will reset to 0000 Value and PC register
will reset to 0007 value.
̅̅̅̅ (External Access): Active Low Pin
❖ 𝑬𝑨
o This pin is connected to ground when microcontroller is accessing the program
code stored in the external memory and connected to Vcc when it is accessing
the program code in the on chip memory.
o This pin should not be left unconnected.
̅̅̅̅̅̅̅̅
❖ 𝑷𝑺𝑬𝑵(Program Store Enable): Active Low Pin
o When the microcontroller is accessing the program code stored in the external
ROM, this pin is connected to the OE (Output Enable) pin of the ROM.
❖ ALE (Address latch enable): Active High Pin
o When connected to external memory, port 0 provides both address and data i.e
address and data are multiplexed through port 0.
o This ALE pin will demultiplex the address and data bus.
o When the pin is High, the AD bus will act as address bus otherwise the AD bus
will act as Data bus.
oIf we want to load T0 with 16 bit data then we can load separate lower 8 bitin TL0
and higher 8 bit in TH0.
o In the same way for T1.
o TMOD, TCON registers are used for controlling timer operation.
❖ Serial Port
o There are two pins available for serial communication TXD and RXD
o Normally TXD is used for transmitting serial data which is in SBUF register, RXD
is used for receiving the serial data.
o SCON register is used for controlling the operation.
❖ Memory organization:
o The 8051 microcontroller has 128 bytes of Internal RAM and 4kB of on chip
ROM.
o The RAM is also known as Data memory and the ROM is known as program
memory.
o The program memory is also known as Code memory.
o This Code memory holds the actual 8051 program that is to be executed.
o In 8051 this memory is limited to 64K.
o Code memory may be found on-chip, as ROM or EPROM. It may also be stored
completely off-chip in an external ROM or, more commonly, an external EPROM.
o The 8051 has only 128 bytes of Internal RAM but it supports 64kB of external
RAM. As the name suggests, external RAM is any random access memory which
is off-chip.
o Since the memory is off-chip it is not as flexible interms of accessing, and is also
slower.
o For example, to increment an Internal RAM location by 1, it requires only 1
instruction and 1 instruction cycle but to increment a 1-byte value stored in
External RAM requires 4 instructions and 7 instruction cycles.
o So, here the external memory is 7 times slower
❖ Internal RAM OF 8051:
o This Internal RAM is found on-chip on the 8051.
o So it is the fastest RAM available, and it is also the most flexible in terms of
reading, writing, and modifying it’s contents.
INSTRUCTION SETS:
❖ The instructions of 8051 can be broadly classified under the following headings.
1. Data transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Program Branching Instructions
5. Bit Manipulation Instructions / Boolean Variable Manipulation Instructions
❖ Arithmetic Operations
o ADD A,Rn → Add register to Accumulator
o ADD A,direct → Add direct byte to Accumulator
o ADD A,@Ri → Add indirect RAM to Accumulator
o ADD A,#data → Add immediate data to Accumulator
o ADDC A,Rn → Add register to Accumulator with Carry
o ADDC A,direct→ Add direct byte to Accumulator with Carry
❖ LOGICAL OPERATIONS
o ANL A,Rn →AND Register to Accumulator
o ANL A,direct →AND direct byte to Accumulator
o ANL A,@Ri →AND indirect RAM to Accumulator
o ANL A,#data →AND immediate data to Accumulator
o ANL direct,A→ AND Accumulator to direct byte
o ANL direct,#data →AND immediate data to direct byte
o ORL A,Rn →OR register to Accumulator
o ORL A,direct →OR direct byte to Accumulator
o ORL A,@Ri →OR indirect RAM to Accumulator
o ORL A,#data→ OR immediate data to Accumulator
o ORL direct,A →OR Accumulator to direct byte
o ORL direct,#data→ OR immediate data to direct byte
o XRL A,Rn →Exclusive-OR register to Accumulator
o XRL A,direct →Exclusive-OR direct byte to Accumulator
o XRL A,@Ri →Exclusive-OR indirect RAM to Accumulator
o XRL A,#data→ Exclusive-OR immediate data to Accumulator
o XRL direct,A→ Exclusive-OR Accumulator to direct byte
o XRL direct,#data→ Exclusive-OR immediate data to direct byte
o CLR A →Clear Accumulator
o CPL A →Complement Accumulator
o RL A →Rotate Accumulator Left
o RLC A →Rotate Accumulator Left through the Carry
o RR A →Rotate Accumulator Right
o RRC A →Rotate Accumulator Right through the Carry
❖ DATA TRANSFER
o MOV A,Rn →Move register to Accumulator
o MOV A,direct →Move direct byte to Accumulator
o MOV A,@Ri →Move indirect RAM to Accumulator
o MOV A,#data→ Move immediate data to Accumulator
o MOV Rn,A→ Move Accumulator to register
o MOV Rn,direct →Move direct byte to register
o MOV Rn,#data→ Move immediate data to register
o MOV direct,A→ Move Accumulator to direct byte
o MOV direct,Rn→ Move register to direct byte
o MOV direct,direct→ Move direct byte to direct
o MOV direct,@Ri →Move indirect RAM to direct byte
o MOV direct,#data→Move immediate data to direct byte
o MOV @Ri,A→ Move Accumulator to indirect RAM
o MOV @Ri,direct →Move direct byte to indirect RAM
o MOV @Ri,#data →Move immediate data to indirect RAM
o MOV DPTR,#data16→ Load Data Pointer with a 16-bit constant
o MOVC A,@A+DPTR→ Move Code byte relative to DPTR to Acc
o MOVC A,@A+PC →Move Code byte relative to PC to Acc
o MOVX A,@Ri →Move External RAM (8- bit addr) to Acc
o MOVX A,@DPTR →Move Exernal RAM (16-bit addr) to Acc
o MOVX @Ri,A →Move Acc to External RAM (8-bit addr)
o MOVX @DPTR,A →Move Acc to External RAM (16-bit addr)
o PUSH direct →Push direct byte onto stack
o POP direct →Pop direct byte from stack
o XCH A,Rn →Exchange register with Accumulator
o XCH A,direct →Exchange direct byte with Accumulator
o XCH A,@Ri →Exchange indirect RAM with Accumulator
o XCHD A,@Ri →Exchange low-order Digit indirect RAM with Acc
❖ PROGRAM BRANCHING
o ACALL addr11→ Absolute Subroutine Call
o LCALL addr16→Long Subroutine Call
o RET →Return from Subroutine
o RETI →Return from interrupt
o AJMP addr11→ Absolute Jump
o LJMP addr16 →Long Jump
o SJMP rel →Short Jump (relative addr)
o JMP @A+DPTR→ Jump indirect relative to the DPTR
o JZ rel →Jump if Accumulator is Zero
o JNZ rel→ Jump if Accumulator is Not Zero
o CJNE A,direct,rel →Compare direct byte to Acc and Jump if Not Equal
o CJNE A,#data,rel →Compare immediate to Acc and Jump if Not Equal
o CJNE Rn,#data,rel →Compare immediate to register and Jump if Not Equal
o CJNE @Ri,#data,rel →Compare immediate to indirect and Jump if Not Equal
o DJNZ Rn,rel →Decrement register and Jump if Not Zero
o DJNZ direct,rel →Decrement direct byte and Jump if Not Zero
o NOP →No Operation
PROGRAMS:
PROGRAM FOR 8-BIT ADDITION USING 8051 MICROCONTROLLER
COMMENTS
ADDRESS LABEL OPCODE OPERAND
9000 MOV A, #80H MOVE THE VALUE TO A REGISTER
9001
9002 MOV R0, #90H MOVE THE VALUE TO R0 REGISTER
9003
COMMENTS
ADDRESS LABEL OPCODE OPERAND
COMMENTS
ADDRESS LABEL OPCODE OPERAND
MOV
9000 #9500 MOVE DPTR TO SPECIFIED ADDRESS
DPTR,
9001
9002
MOVE THE ADDRESS OF DPTR TO A
9003 MOVX A,@DPTR
REGISTER
9004 MOV F0,A MOVE A REGISTER TO F0
9005 INC DPTR INCREMENT DPTR
MOVE THE ADDRESS OF DPTR TO A
9006 MOVX A,@DPTR
REGISTER
9007 MUL AB MULTIPLY 8 BIT VALUE
9008 INC DPTR INCREMENT DPTR
MOVE A REGISTER TO ADDRESS OF
9009 MOVX @DPTR,A
DPTR
900A INC DPTR INCREMENT DPTR
900B MOV A,F0 MOVE VALUE FROM F0 TO A
MOVE A REGISTER TO ADDRESS OF
900C MOVX @DPTR,A
DPTR
SHORT JUMP TO SPECIFIED
900D XXX SJMP 900D
LOCATION
900E XXX
900F
PROGRAM FOR 8-BIT DIVISION USING 8051 MICROCONTROLLER
COMMENTS
ADDRESS LABEL OPCODE OPERAND
MOV
9000 #9500 MOVE DPTR TO SPECIFIED ADDRESS
DPTR,
9001
9002
MOVE THE ADDRESS OF DPTR TO A
9003 MOVX A,@DPTR
REGISTER
Note 1: In Multiplication,
❖ A holds MSB of the result and B holds LSB of the result
❖ This instruction always makes CY and OV flag as ‘0’, if result is less than ‘FF’
❖ If CY=0 and OV=1, indicates the result is above ‘FF’
Note 2: In division,
❖ A holds Quotient and B holds Remainder
❖ This instruction always makes CY and OV flag as ‘0’, if denominator is not ‘0’
❖ If CY=0 and OV=1, indicates an error that denominator is zero
PORT 0:
❖ Port 0 is an 8-bit I/O port with dual purpose.
❖ If external memory is used, these port pins are used for the lower address byte
address/data (AD0-AD7), otherwise all bits of the port are either input or output.
Port 1:
❖ Port 1 occupies a total of 8 pins (pins 1 through 8).
❖ It has no dual application and acts only as input or output port.
❖ In contrast to port 0, this port does not need any pull-up resistors since pull-up resistors
connected internally.
❖ Upon reset, Port 1 is configured as an output port.
❖ To configure it as an input port, port bits must be set i.e a high bit must be sent to all the
port pins.
❖ This is normally done by the instruction “SETB”.
Ex:
MOV A, #0FFH; A=FF HEX
MOV P1, A; make P1 an input port by writing 1’s to all of its pins
Port 2:
❖ Port 2 is also an eight bit parallel port. (pins 21- 28).
❖ It can be used as input or output port.
PORT 3:
❖ Port3 is also an 8-bit parallel port with dual function.( pins 10 to 17).
❖ The port pins can be used for I/O operations as well as for control operations.
❖ The details of these additional operations are given below in the table.
❖ Port 3 also do not need any external pull-up resistors as they are provided internally
similar to the case of Port2 & Port 1.
❖ Upon reset port 3 is configured as an output port .
❖ If the port is to be used as input port, all the port bits must be made high by sending FF
to the port.
For ex,
MOV A, #0FFH; A= FF hex
MOV P3, A; make P3 an input port by writing all 1’s to it
Alternate Functions of Port 3:
❖ P3.0 and P3.1 are used for the RxD (Receive Data) and TxD (Transmit Data) serial
communications signals.
❖ Bits P3.2 and P3.3 are meant for external interrupts.
❖ Bits P3.4 and P3.5 are used for Timers 0 and 1 and P3.6 and P3.7 are used to provide
the write and read signals of external memories connected in 8031 based systems.
o P3.1 – TXD – Serial data transmit.
o P3.2 – INT0 – External Interrupt 0.
o P3.3 – INT1 – External Interrupt 1.
o P3.4 – T0 – Clock input for counter 0.
o P3.5 – T1 – Clock input for counter 1.
o P3.6 – WR – Signal for writing to external memory.
DELAY:MOV R1,#FF
J1: DJNZ R1,J1
RET
❖ Write a program to generate a square waveform from bit0 of port1.
BACK: SETB P1.0
LCALL DELAY
CLR P1.0
LCALL DELAY
SJMP BACK
DELAY:MOV R1,#FF
J1: DJNZ R1,J1
RET
OR
BACK: CPL P1.0
LCALL DELAY
SJMP BACK
❖ A switch is connected to pin P1.7 and an LED to pin P2.0. Write a program to get
the status of the switch and send it to the LED.
LED BIT P1.7
SW BIT P2.0
HERE: MOV C, SW
MOV LED, C
SJMP HERE
Timer 0
❖ Timer1 registers is also a 16 bits register and is split into two bytes, referred to as TL1
and TH1.
TMOD Register
❖ Mode 1
o It is a 16-bit timer; therefore it allows values from 0000 to FFFFH to be loaded
into the timer’s registers TL and TH.
o After TH and TL are loaded with a 16-bit initial value, the timer must be started.
o This is done by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
o After the timer is started.
o It starts count up until it reaches its limit of FFFFH.
o When it rolls over from FFFF to 0000H, it sets high a flag bit called TF (timer
flag).
o This timer flag can be monitored. When this timer flag is raised, one option would
be stop the timer with the instructions “CLR TR0“ or CLR TR1 for timer 0 and
timer 1 respectively.
o Again, it must be noted that each timer flag TF0 for timer 0 and TF1 for timer1.
o After the timer reaches its limit and rolls over, in order to repeat the process the
registers TH and TL must be reloaded with the original value and TF must be
reset to 0.
o The figure 4.11 explains the Mode 1 operation of timer
❖ Mode 2
o It is an 8 bit timer that allows only values of 00 to FFH to be loaded into the
timer’s register TH.
o After TH is loaded with 8 bit value, the 8051 gives a copy of it to TL.
o Then the timer must be started.
o It is done by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer1.
TCON Register
Example
❖ Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
Solution:
We convert the value from hex to binary. From Figure 9-3 we have:
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are
❖ Find the timer’s clock frequency and its period for various 8051-based system,
with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:
(1/12 )× 11.0529 MHz = 921.6 MHz;
T = 1/921.6 kHz = 1.085 us selected.
❖ Write a program to create square wave of 50% duty cycle (with equal portions high
and low) on the P1.5 bit use Timer 0 to generate the time delay.
Solution:
MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0
;until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
❖ Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once
whenever the counter reaches a count of 100. Assume the timer clock is taken
from external source P3.5 (T1).
The TMOD value is 60H
The initialization value to be loaded into TH1 is
256 - 100 = 156 = 9CH
SERIAL COMMUNICATION:
Baud Rate:
❖ The 8051 transfers and receives data serially at many different baud rates.
❖ The baud rate in the 8051 is programmable.
❖ This is done with the help of Timer 1.
❖ The relationship between the crystal frequency and the baud rate is
o For XTAL = 11.0592 MHz, the machine cycle frequency is 921.6kHz
o The UART circuitry divides the machine cycle frequency of 921.6kHz by 32 once
more before it is used by Timer 1 to set the baud rate. Therefore it gives
28,800Hz.
o When Timer 1 is used to set the baud rate it must be programmed in mode 2,
i.e., 8-bit, auto reload.
SBUF Register
❖ SBUF is an 8-bit register used solely for serial communication in the 8051.
❖ For a byte of data to be transferred via the TxD line, it must be placed in the SBUF
register.
❖ Similarly, SBUF holds the byte of data when it is received by the RxD line.
❖ SBUF can be accesed like any other registers in the 8051
MOV SBUF, #‘D’
MOV SBUF, A
MOV A, SBUF
❖ The moment a byte is written into SBUF, it is framed with the start and stop bits and
transferred serially via the TxD pin
❖ Similarly when the bits are received serially via RxD, the 8051 deframes it by eliminating
the start and stop bits, making a byte out of the data received, and then placing it in the
SBUF
SCON Register:
❖ The SCON register is an 8-bit register used to program the start bit, stop bit and data
bits of data framing, among other things.
❖ SM0,SM1:
o These two bits determines the framing of data by the specifying the number of
bits per character, and the start and stop bits.
o They take the following combinations
Example
❖ Write a program to transfer a letter ‘Y’ serially at 9600 baud continuously, and also
to send a letter ‘N’ through Port 0, which is connected to a display device.
MOV TMOD,#20H
MOV TH1,#-3
MOV SCON, #50H
SETB TR1
YY: MOV SBUF,#’Y’
XX: JNB TI, XX
CLR TI
MOV P0, #’N’
SJMP YY
❖ Write a program to receive the data which has been sent in serial form and send it
out to port 0 in parallel form. Also save the data at RAM location 60H.
MOV TMOD,#20H
MOV TH1,#-3
INTERRUPT
Interrupts Vs Polling:
❖ A single microcontroller can serve several devices. There are two ways to do that:
o Interrupts
o Polling
❖ In the interrupt method, whenever any device needs its service, the device notifies the
microcontroller by sending it an interrupt signal.
❖ Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and
serves the device.
❖ The program associated with the interrupt is called the interrupt service routine (ISR) or
interrupt handler.
❖ In polling, the microcontroller continuously monitors the status of a given device; when
the status condition is met, it performs the service.
❖ After that it moves on to monitor the next device until each one is serviced.
❖ Although polling can monitor the stats of several devices and serve each of them as
certain conditions are met, it is not an efficient use of the microcontroller.
❖ The advantage of interrupts is that the microcontroller can serve many devices; each
device can get the attention of the microcontroller based on the priority assigned to it.
❖ The polling method cannot assign priority since it checks all devices in a round-robin
fashion.
❖ In interrupt method, interrupts can be masked which is not possible in polling method.
❖ Polling method wastes microcontroller’s time by polling devices.
Interrupt Sources
❖ The 8051 architecture can handle interrupts from 5 sources.
❖ These are:
o Two external interrupt lines,
o Two timers
o Serial interface
❖ Each one of these is assigned an interrupt vector address. This is quite similar to the
RST interrupt vectors in the case of 8085.
❖ External Interrupts
o Port P3 of 8051 is a multi-function port.
o Different lines of this port carry out functions which are additional to data input-
output on the port.
o Lines P3.2 and P3.3 can be used as interrupt inputs.
o Interrupts will be caused by a ‘LOW’ level, or a negative edge on these lines.
o Half of the special function register TCON is used for setting the conditions for
causing interrupts from external sources. This register is bit addressable.
SFR TCON at byte address 88H
❖ Internal Interrupts
o Internally generated interrupts can be from either timer, or from the serial
interface.
o The serial interface causes interrupts due to a receive event (RI) or due to a
transmit event (TI).
o The receive event occurs when the input buffer of the serial line (SBUF in) is full
and a byte needs to be read from it.
o The transmit event indicates that a byte has been sent a new byte can be written
to output buffer of the serial line (SBUF out).
o 8051 timers always count up. When their count rolls over from the maximum
count to 0000, they set the corresponding timer flag TF1 or TF0 in TCON.
o Counters run only while their run flag (TR1 or TR0) is set by the user program.
o When the run flag is cleared, the count stops incrementing.
o The 8051 can be setup so that an interrupt occurs whenever TF1 or TF0 is set.
❖ Enabling Interrupts
o At power-up, all interrupts are disabled.
o Suppose Timer 0 is started. When it times out, TF0 in the special function
register TCON will be set.
o However, this will not cause an interrupt.
o To enable interrupts, a number of steps need to be taken.
o Interrupts are enabled in a manner which is quite similar to the 8085.
o There is an interrupt enable special function register IE at byte address A8H.
o This register is bit addressable. (The assembler gives special mnemonics to
each bit address.)
SFR Interrupt Enable (IE) Register
❖ Interrupt Priorities
o 8051 has two levels of interrupt priorities:
▪ High or Low.
o When 8051 is powered up, the priorities are assigned according to the table 4.13.
Serial Interrupts
❖ Serial interrupts are handled somewhat differently from the timers.
❖ There are independent interrupt flags for reception and transmission of serial data,
called RI and TI.
❖ RI indicates that a byte has been received and is available for reading in the input buffer.
❖ TI indicates that the previous byte has been sent serially and a new byte can be written
to the serial port.
❖ A serial interrupt occurs if either of these flags is set. (Of course the serial interrupt must
be enabled for this to occur).
❖ The interrupt service routine should check which of these events caused the interrupt.
❖ This can be done by examining the flags.
❖ Either or both of the flag might be set, requiring a read from or write to the serial buffer
SBUF (or both).
❖ Recall that the input and output buffers are distinct but are located at the same address.
❖ A read from this address reads the input buffer while a write to the same address writes
to the output buffer.
❖ The RI and TI flags are not automatically cleared when an interrupt is serviced.
❖ Therefore, the interrupt service routine must clear them before returning.
❖ Here is an example handler for serial interrupts:
❖ Write a program in which the 8051 reads data from P1 and writes it to P2
continuously while giving a copy of it to the serial COM port to be transferred
serially. Assume that XTAL=11.0592MHz. Set the baud rate at 9600.
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL
ORG 30H
MAIN: MOV P1,#0FFH
MOV TMOD,#20H
MOV TH1, #0FDH
MOV SCON,#50H
MOV IE,#10010000B
SETB TR1
BACK: MOV A,P1
MOV SBUF, A
MOV P2,A
SJMP BACK
ORG 100
SERIAL: JB TI, TRANS
MOV A, SBUF
CLR RI
❖ Write a program in which 10 bytes of data stored in RAM locations starting from
45H are transferred serially. At the end of data transfer, the value of R0 is
displayed on P1. Assume that XTAL=11.0592MHz. Set the baud rate at 9600.
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL
ORG 30H
MAIN: MOV TMOD,#20H
MOV TH1, #0FDH
MOV SCON,#50H
MOV IE,#10010000B
SETB TR1
MOV R0,#10
MOV R1,#45H
BACK: MOV A,@R1
MOV SBUF, A
DJNZ R0, BACK
HERE: SJMP HERE
ORG 100
SERIAL: JNB TI, RECE
MOV A,R0
MOV P1,A
CLR TI
RETI
RECE: MOV A, SBUF
CLR RI
RETI
Example:
❖ Write a program that displays a value of ‘Y’ at port 0 and ‘N’ at port 2 and also
generates a square wave at 10kHz, with Timer 0 in mode 2 at port pin P1.2.
XTAL=22MHz.
ORG 0000H
LJMP MAIN
ORG 000B
CPL 1.2
RETI
ORG 0030H
MAIN: MOV TMOD,#02H
MOV TH0,#0B6H
MOV IE,#82H
SETB TR0
BACK: MOV P0,#’Y’
MOV P2,#’N’
SJMP BACK
END
❖ Write a ALP to generates a wave form with high portion of 1085uS and a low
portion of 15us. Also acquire the value from Port 0 continuously and move it to
Port 1. Assume XTAL=11.059MHz. Use Timer 1.
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN: MOV TMOD,#10H
MOV TH1,#0FCH
MOV TL1,#018H
MOV P0,#0FFH
MOV IE,#88H
SETB TR1
BACK: MOV A, P0
MOV P1,A
SJMP BACK
ORG 0000
LJMP MAIN
ORG 0003
LED1: MOV P0,#0FF
MOV R0,#255
DJNZ R0,LED1
RETI
ORG 0013
LED2: MOV P2,#0FF
MOV R2,#255
DJNZ R0,LED2
RETI
ORG 0030
MAIN: MOV IE,#85H
HERE: SJMP HERE
END
❖ Generate from all pins of Port 0, a Square wave which is half the frequency of the
signal applied at INT0 pin.
ORG 0000
LJMP MAIN
ORG 0003
CPL P0
RETI
ORG 0030
MAIN: SETB TCON.0 ; EDGE TRIGGERED
MOV IE,#81
HERE: SJMP HERE