Documente Academic
Documente Profesional
Documente Cultură
MCU's
TOC
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1
2
3
4
5
6
7
Program Counter
Program Counter is an engine running the program and points to the
memory address containing the next instruction to execute. After each
instruction execution, the value of the counter is incremented by 1. For this
reason, the program executes only one instruction at a time just as it is
written. Howeverthe value of the program counter can be changed at any
moment, which causes a jump to a new memory location. This is how
subroutines and branch instructions are executed. After jumping, the
counter resumes even and monotonous automatic counting +1, +1, +1
Central Processor Unit (CPU)
As its name suggests, this is a unit which monitors and controls all
processes within the microcontroller and the user cannot affect its work. It
consists of several smaller subunits, of which the most important are:
i.e. the bits value is 1. Otherwise, if the voltage is 0 V, the appropriate pin is
cleared (0), i.e. the bits value is 0. It is more complicated in theory where a
bit is referred to as a binary digit, but even in this case, its value can be
either 0 or 1.
Input/output ports (I/O Ports)
In order to make the microcontroller useful, it is necessary to connect it to
peripheral devices. Each microcontroller has one or more registers (called
a port) connected to the microcontroller pins.
This is only a simple explanation of the operation itself. Its somehow more
complicated in practice.
Watchdog timer
The Watchdog Timer is a timer connected to a completely separate RC
oscillator within the microcontroller.
If the watchdog timer is enabled, every time it counts up to the program
end, the microcontroller reset occurs and program execution starts from the
first instruction. The point is to prevent this from happening by using a
special command. The whole idea is based on the fact that every program
is executed in several longer or shorter loops.
If instructions resetting the watchdog timer are set at the appropriate
program locations, besides commands being regularly executed, then the
operation of the watchdog timer will not affect the program execution.
If for any reason (usually electrical noise in industry), the program counter
"gets stuck" at some memory location from which there is no return, the
watchdog will not be cleared, so the registers value being constantly
incremented will reach the maximum et voila! Reset occurs!
Power Supply Circuit
There are two things worth attention concerning the microcontroller power
supply circuit:
15
16
17
18
Pin 9: RS A logic one on this pin disables the microcontroller and clears
the contents of most registers. In other words, the positive voltage on this
pin resets the microcontroller. By applying logic zero to this pin, the
program starts execution from the beginning.
Pins10-17: Port 3 Similar to port 1, each of these pins can serve as
general input or output. Besides, all of them have alternative functions:
Pin 10: RXD Serial asynchronous communication input or Serial
synchronous communication output.
Pin 11: TXD Serial asynchronous communication output or Serial
synchronous communication clock output.
Pin 12: INT0 Interrupt 0 input.
Pin 13: INT1 Interrupt 1 input.
Pin 14: T0 Counter 0 clock input.
Pin 15: T1 Counter 1 clock input.
Pin 16: WR Write to external (additional) RAM.
Pin 17: RD Read from external RAM.
Pin 18, 19: X2, X1 Internal oscillator input and output. A quartz crystal
which specifies operating frequency is usually connected to these pins.
Instead of it, miniature ceramics resonators can also be used for frequency
stability. Later versions of microcontrollers operate at a frequency of 0 Hz
up to over 50 Hz.
Pin 20: GND Ground.
Pin 21-28: Port 2 If there is no intention to use external memory then these
port pins are configured as general inputs/outputs. In case external
memory is used, the higher address byte, i.e. addresses A8-A15 will
appear on this port. Even though memory with capacity of 64Kb is not
used, which means that not all eight port bits are used for its addressing,
the rest of them are not available as inputs/outputs.
Pin 29: PSEN If external ROM is used for storing program then a logic
zero (0) appears on it every time the microcontroller reads a byte from
memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller
puts the lower address byte (A0-A7) on P0 and activates the ALE output.
After receiving signal from the ALE pin, the external register (usually
74HCT373 or 74HCT375 add-on chip) memorizes the state of P0 and uses
it as a memory chip address. Immediately after that, the ALU pin is returned
19
its previous logic state and P0 is now used as a Data Bus. As seen, port
data multiplexing is performed by means of only one additional (and cheap)
integrated circuit. In other words, this port is used for both data and
address transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data
and address transmission with no regard to whether there is internal
memory or not. It means that even there is a program written to the
microcontroller, it will not be executed. Instead, the program written to
external ROM will be executed. By applying logic one to the EA pin, the
microcontroller will use both memories, first internal then external (if exists).
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins
can be used as general inputs/outputs. Otherwise, P0 is configured as
address output (A0-A7) when the ALE pin is driven high (1) or as data
output (Data Bus) when the ALE pin is driven low (0).
Pin 40: VCC +5V power supply.
20
21
Output pin
A logic zero (0) is applied to a bit of the P register. The output FE
transistor is turned on, thus connecting the appropriate pin to
ground.
22
Input pin
A logic one (1) is applied to a bit of the P register. The output FE
transistor is turned off and the appropriate pin remains connected
to the power supply voltage over a pull-up resistor of high
resistance.
Logic state (voltage) of any pin can be changed or read at any
moment. A logic zero (0) and logic one (1) are not equal. A logic
one (0) represents a short circuit to ground. Such a pin acts as an
output.
A logic one (1) is loosely connected to the power supply voltage
over a resistor of high resistance. Since this voltage can be easily
reduced by an external signal, such a pin acts as an input.
Port 0
The P0 port is characterized by two functions. If external memory is used
then the lower address byte (addresses A0-A7) is applied on it. Otherwise,
all bits of this port are configured as inputs/outputs.
The other function is expressed when it is configured as an output. Unlike
other ports consisting of pins with built-in pull-up resistor connected by its
end to 5 V power supply, pins of this port have this resistor left out. This
apparently small difference has its consequences:
All port pins can be used as general I/O, but they also have an alternative
function. In order to use these alternative functions, a logic one (1) must be
applied to appropriate bit of the P3 register. In tems of hardware, this port is
similar to P0, with the difference that its pins have a pull-up resistor built-in.
Pin's Current limitations
When configured as outputs (logic zero (0)), single port pins can receive a
current of 10mA. If all 8 bits of a port are active, a total current must be
limited to 15mA (port P0: 26mA). If all ports (32 bits) are active, total
maximum current must be limited to 71mA. When these pins are configured
as inputs (logic 1), built-in pull-up resistors provide very weak current, but
strong enough to activate up to 4 TTL inputs of LS series.
25
All 8051 microcontrollers have a 16-bit addressing bus and are capable of
addressing 64 kb memory. It is neither a mistake nor a big ambition of
engineers who were working on basic core development. It is a matter of
smart memory organization which makes these microcontrollers a real
programmers goody.
Program Memory
The first models of the 8051 microcontroller family did not have internal
program memory. It was added as an external separate chip. These models
are recognizable by their label beginning with 803 (for example 8031 or
8032). All later models have a few Kbyte ROM embedded. Even though
such an amount of memory is sufficient for writing most of the programs,
there are situations when it is necessary to use additional memory as well.
A typical example are so called lookup tables. They are used in cases
when equations describing some processes are too complicated or when
there is no time for solving them. In such cases all necessary estimates
and approximates are executed in advance and the final results are put in
the tables (similar to logarithmic tables).
26
for the later models this number was incremented by additional 128
registers. However, the first 256 memory locations (addresses 0-FFh) are
the heart of memory common to all the models belonging to the 8051
family. Locations available to the user occupy memory space with
addresses 0-7Fh, i.e. first 128 registers. This part of RAM is divided in
several blocks.
The first block consists of 4 banks each including 8 registers denoted by
R0-R7. Prior to accessing any of these registers, it is necessary to select
the bank containing it. The next memory block (address 20h-2Fh) is bitaddressable, which means that each bit has its own address (0-7Fh). Since
there are 16 such registers, this block contains in total of 128 bits with
separate addresses (address of bit 0 of the 20h byte is 0, while address of
bit 7 of the 2Fh byte is 7Fh). The third group of registers occupy addresses
2Fh-7Fh, i.e. 80 locations, and does not have any special functions or
features.
Additional RAM
In order to satisfy the programmers constant hunger for Data Memory, the
manufacturers decided to embed an additional memory block of 128
locations into the latest versions of the 8051 microcontrollers. However, its
not as simple as it seems to be The problem is that electronics
performing addressing has 1 byte (8 bits) on disposal and is capable of
reaching only the first 256 locations, therefore. In order to keep already
existing 8-bit architecture and compatibility with other existing models a
small trick was done.
What does it mean? It means that additional memory block shares the
same addresses with locations intended for the SFRs (80h- FFh). In order
to differentiate between these two physically separated memory spaces,
different ways of addressing are used. The SFRs memory locations are
accessed by direct addressing, while additional RAM memory locations are
accessed by indirect addressing.
28
29
Memory expansion
In case memory (RAM or ROM) built in the microcontroller is not sufficient,
it is possible to add two external memory chips with capacity of 64Kb each.
P2 and P3 I/O ports are used for their addressing and data transmission.
From the users point of view, everything works quite simply when properly
connected because most operations are performed by the microcontroller
itself. The 8051 microcontroller has two pins for data read RD#(P3.7) and
30
PSEN#. The first one is used for reading data from external data memory
(RAM), while the other is used for reading data from external program
memory (ROM). Both pins are active low. A typical example of memory
expansion by adding RAM and ROM chips (Hardward architecture), is
shown in figure above.
Even though additional memory is rarely used with the latest versions of
the microcontrollers, we will describe in short what happens when memory
chips are connected according to the previous schematic. The whole
process described below is performed automatically.
Direct Addressing
On direct addressing, the address of memory location containing data to be
read is specified in instruction. The address may contain a number being
changed during operation (variable). For example:
Since the address is only one byte in size (the largest number is 255), only
the first 255 locations of RAM can be accessed this way. The first half of
RAM is available for use, while another half is reserved for SFRs.
MOV A,33h; Means: move a number from address 33 hex.
to accumulator
Indirect Addressing
On indirect addressing, a register containing the address of another
register is specified in instruction. Data to be used in the program is stored
in the letter register. For example:
Indirect addressing is only used for accessing RAM locations available for
use (never for accessing SFRs). This is the only way of accessing all the
latest versions of the microcontrollers with additional memory block (128
locations of RAM). Simply put, when the program encounters instruction
including @ sign and if the specified address is higher than 128 ( 7F
hex.), the processor knows that indirect addressing is used and skips
memory space reserved for SFRs.
MOV A,@R0; Means: Store the value from the register
whose address is in the R0 register
into accumulator
On indirect addressing, registers R0, R1 or Stack Pointer are used for
specifying 8-bit addresses. Since only 8 bits are avilable, it is possible to
access only registers of internal RAM this way (128 locations when
speaking of previous models or 256 locations when speaking of latest
models of microcontrollers). If an extra memory chip is added then the 16bit DPTR Register (consisting of the registers DPTRL and DPTRH) is used
32
A Register (Accumulator)
33
34
PSW register is one of the most important SFRs. It contains several status
bits that reflect the current state of the CPU. Besides, this register contains
Carry bit, Auxiliary Carry, two register bank select bits, Overflow flag, parity
bit and user-definable status flag.
P - Parity bit. If a number stored in the accumulator is even then this bit
will be automatically set (1), otherwise it will be cleared (0). It is mainly
used during data transmit and receive via serial communication.
- Bit 1. This bit is intended to be used in the future versions of
microcontrollers.
OV Overflow occurs when the result of an arithmetical operation is larger
than 255 and cannot be stored in one register. Overflow condition causes
the OV bit to be set (1). Otherwise, it will be cleared (0).
RS0, RS1 - Register bank select bits. These two bits are used to select
one of four register banks of RAM. By setting and clearing these bits,
registers R0-R7 are stored in one of four banks of RAM.
RS1
RS2
S P AC E I N R AM
Bank0 00h-07h
Bank1 08h-0Fh
Bank2 10h-17h
Bank3 18h-1Fh
A value stored in the Stack Pointer points to the first free stack address and
permits stack availability. Stack pushes increment the value in the Stack
Pointer by 1. Likewise, stack pops decrement its value by 1. Upon any
reset and power-on, the value 7 is stored in the Stack Pointer, which means
that the space of RAM reserved for the stack starts at this location. If
another value is written to this register, the entire Stack is moved to the
new memory location.
37
If neither external memory nor serial communication system are used then
4 ports with in total of 32 input/output pins are available for connection to
peripheral environment. Each bit within these ports affects the state and
performance of appropriate pin of the microcontroller. Thus, bit logic state is
reflected on appropriate pin as a voltage (0 or 5 V) and vice versa, voltage
on a pin reflects the state of appropriate port bit.
As mentioned, port bit state affects performance of port pins, i.e. whether
they will be configured as inputs or outputs. If a bit is cleared (0), the
appropriate pin will be configured as an output, while if it is set (1), the
appropriate pin will be configured as an input. Upon reset and power-on, all
port bits are set (1), which means that all appropriate pins will be
configured as inputs.
39
Since the timer T0 is virtually 16-bit register, the largest value it can store is
65 535. In case of exceeding this value, the timer will be automatically
cleared and counting starts from 0. This condition is called an overflow. Two
registers TMOD and TCON are closely connected to this timer and control
its operation.
TMOD Register (Timer Mode)
The TMOD register selects the operational mode of the timers T0 and T1.
As seen in figure below, the low 4 bits (bit0 - bit3) refer to the timer 0, while
the high 4 bits (bit4 - bit7) refer to the timer 1. There are 4 operational
modes and each of them is described herein.
T1M0
MODE
DESCRIPTION
13-bit timer
16-bit timer
8-bit auto-reload
Split mode
o
o
o
o
T0M0
MODE
DESCRIPTION
13-bit timer
16-bit timer
8-bit auto-reload
Split mode
41
42
55th pulse, the best solution is to write the number 200 to the TH0 register
and configure the timer to operate in mode 2.
44
The only application of this mode is when two timers are used and the 16bit Timer 1 the operation of which is out of control is used as a baud rate
generator.
Timer Control (TCON) Register
TCON register is also one of the registers whose bits are directly in control
of timer operation.
Only 4 bits of this register are used for this purpose, while rest of them is
used for interrupt control to be discussed later.
1 - Timer 1 is enabled.
0 - Timer 1 is disabled.
o
o
1 - Timer 0 is enabled.
0 - Timer 0 is disabled.
46
The TR0 bit is set and the timer starts operation. If the quartz crystal with
frequency of 12MHz is embedded then its contents will be incremented
every microsecond. After 65.536 microseconds, the both registers the timer
consists of will be loaded. The microcontroller automatically clears them
and the timer keeps on repeating procedure from the beginning until the
TR0 bit value is logic zero (0).
How to 'read' a timer?
Depending on application, it is necessary either to read a number stored in
the timer registers or to register the moment they have been cleared.
- It is extremely simple to read a timer by using only one register configured
in mode 2 or 3. It is sufficient to read its state at any moment. That's all!
- It is somehow complicated to read a timer configured to operate in mode
2. Suppose the lower byte is read first (TL0), then the higher byte (TH0).
The result is:
TH0 = 15 TL0 = 255
Everything seems to be ok, but the current state of the register at the
moment of reading was:
TH0 = 14 TL0 = 255
47
In case of negligence, such an error in counting (255 pulses) may occur for
not so obvious but quite logical reason. The lower byte is correctly read
(255), but at the moment the program counter was about to read the higher
byte TH0, an overflow occurred and the contents of both registers have
been changed (TH0: 1415, TL0: 2550). This problem has a simple
solution. The higher byte should be read first, then the lower byte and once
again the higher byte. If the number stored in the higher byte is different
then this sequence should be repeated. It's about a short loop consisting of
only 3 instructions in the program.
There is another solution as well. It is sufficient to simply turn the timer off
while reading is going on (the TR0 bit of the TCON register should be
cleared), and turn it on again after reading is finished.
Timer 0 Overflow Detection
Usually, there is no need to constantly read timer registers. It is sufficient to
register the moment they are cleared, i.e. when counting starts from 0. This
condition is called an overflow. When it occurrs, the TF0 bit of the TCON
register will be automatically set. The state of this bit can be constantly
checked from within the program or by enabling an interrupt which will stop
the main program execution when this bit is set. Suppose it is necessary to
provide a program delay of 0.05 seconds (50 000 machine cycles), i.e. time
when the program seems to be stopped:
First a number to be written to the timer registers should be calculated:
When enabled, the timer will resume counting from this number. The state
of the TF0 bit, i.e. whether it is set, is checked from within the program. It
happens at the moment of overflow, i.e. after exactly 50.000 machine
cycles or 0.05 seconds.
How to measure pulse duration?
49
50
51
SM0 - Serial port mode bit 0 is used for serial port mode
selection.
SM1 - Serial port mode bit 1.
SM2 - Serial port mode 2 bit, also known as multiprocessor
communication enable bit. When set, it enables
multiprocessor communication in mode 2 and 3, and
eventually mode 1. It should be cleared in mode 0.
REN - Reception Enable bit enables serial reception when
set. When cleared, serial reception is disabled.
TB8 - Transmitter bit 8. Since all registers are 8-bit wide, this
bit solves the problem of transmiting the 9th bit in modes 2
and 3. It is set to transmit a logic 1 in the 9th bit.
RB8 - Receiver bit 8 or the 9th bit received in modes 2 and
3. Cleared by hardware if 9th bit received is a logic 0. Set by
hardware if 9th bit received is a logic 1.
TI - Transmit Interrupt flag is automatically set at the moment
the last bit of one byte is sent. It's a signal to the processor
that the line is available for a new byte transmite. It must be
cleared from within the software.
RI - Receive Interrupt flag is automatically set upon one byte
receive. It signals that byte is received and should be read
quickly prior to being replaced by a new data. This bit is also
cleared from within the software.
As seen, serial port mode is selected by combining the SM0 and SM2 bits:
52
SM0
SM1
MODE
DESCRIPTION
B AU D R ATE
8-bit UART
9-bit UART
9-bit UART
53
RECEIVE - Data receive through the RXD pin starts upon the two following
conditions are met: bit REN=1 and RI=0 (both of them are stored in the
SCON register). When all 8 bits have been received, the RI bit of the
SCON register is automatically set indicating that one byte receive is
complete.
Since there are no START and STOP bits or any other bit except data sent
from the SBUF register in the pulse sequence, this mode is mainly used
when the distance between devices is short, noise is minimized and
operating speed is of importance. A typical example is I/O port expansion
by adding a cheap IC (shift registers 74HC595, 74HC597 and similar).
Mode 1
54
RECEIVE - The START bit (logic zero (0)) on the RXD pin initiates data
receive. The following two conditions must be met: bit REN=1 and bit RI=0.
55
Both of them are stored in the SCON register. The RI bit is automatically
set upon data reception is complete.
RECEIVE - The START bit (logic zero (0)) on the RXD pin initiates data
receive. The following two conditions must be met: bit REN=1 and bit RI=0.
Both of them are stored in the SCON register. The RI bit is automatically
set upon data reception is complete.
Mode 3
Mode 3 is the same as Mode 2 in all respects except the baud rate. The
baud rate in Mode 3 is variable.
The parity bit is the P bit of the PSW register. The simplest way to
check correctness of the received byte is to add a parity bit to it.
Simply, before initiating data transmit, the byte to transmit is
stored in the accumulator and the P bit goes into the TB8 bit in
order to be a part of the message. The procedure is opposite on
receive, received byte is stored in the accumulator and the P bit is
compared with the RB8 bit. If they are the same- everything is
OK!
57
Baud Rate
Baud Rate is a number of sent/received bits per second. In case the UART
is used, baud rate depends on: selected mode, oscillator frequency and in
some cases on the state of the SMOD bit of the SCON register. All the
necessary formulas are specified in the table:
B AU D R ATE
BITSMOD
Mode 0
Fosc. / 12
Mode 1
1 Fosc.
16 12 (256-TH1)
BitSMOD
Mode 2
Fosc. / 32
Fosc. / 64
1
0
Mode 3
1 Fosc.
16 12 (256-TH1)
FOSC. (MHZ)
BIT SMO
11.0592
12
14.7456
150
40 h
30 h
00 h
300
A0 h
98 h
80 h
75 h
52 h
600
D0 h
CC h
C0 h
BB h
A9 h
1200
E8 h
E6 h
E0 h
DE h
D5 h
2400
F4 h
F3 h
F0 h
EF h
EA h
F3 h
EF h
EF h
4800
4800
FA h
F8 h
9600
FD h
FC h
16
20
0
1
F5 h
0
0
58
9600
19200
F5 h
FD h
FC h
38400
FE h
76800
FF h
Multiprocessor Communication
As you may know, additional 9th data bit is a part of message in mode 2
and 3. It can be used for checking data via parity bit. Another useful
application of this bit is in communication between two or more
microcontrollers, i.e. multiprocessor communication. This feature is enabled
by setting the SM2 bit of the SCON register. As a result, after receiving the
STOP bit, indicating end of the message, the serial port interrupt will be
generated only if the bit RB8 = 1 (the 9th bit).
This is how it looks like in practice:
Suppose there are several microcontrollers sharing the same interface.
Each of them has its own address. An address byte differs from a data byte
because it has the 9th bit set (1), while this bit is cleared (0) in a data byte.
When the microcontroller A (master) wants to transmit a block of data to
one of several slaves, it first sends out an address byte which identifies the
target slave. An address byte will generate an interrupt in all slaves so that
they can examine the received byte and check whether it matches their
address.
59
Of course, only one of them will match the address and immediately clear
the SM2 bit of the SCON register and prepare to receive the data byte to
come. Other slaves not being addressed leave their SM2 bit set ignoring
the coming data bytes.
60
IE Register (Interrupt
Enable)
o
o
o
o
Interrupt Priorities
It is not possible to forseen when an interrupt request will arrive. If several
interrupts are enabled, it may happen that while one of them is in progress,
another one is requested. In order that the microcontroller knows whether
to continue operation or meet a new interrupt request, there is a priority list
instructing it what to do.
The priority list offers 3 levels of interrupt priority:
62
Priority 0
Priority 1
Priority 0
Priority 1
o
o
Priority 0
Priority 1
Priority 0
Priority 1
o
o
Priority 0
Priority 1
V E C T O R ( AD D R E S S )
IE0
3h
TF0
Bh
TF1
1B h
RI, TI
23 h
Reset occurs when the RS pin is supplied with a positive pulse in duration
of at least 2 machine cycles (24 clock cycles of crystal oscillator). After that,
the microcontroller generates an internal reset signal which clears all SFRs,
except SBUF registers, Stack Pointer and ports (the state of the first two
ports is not defined, while FF value is written to the ports configuring all
their pins as inputs). Depending on surrounding and purpose of device, the
RS pin is usually connected to a power-on reset push button or circuit or to
both of them. Figure below illustrates one of the simplest circuit providing
safe power-on reset.
What is going on? As soon as the P3.2 pin is cleared (for example, by
pressing the button), the microcontroller will stop program execution and
jump to the 03hex address will be executed. This address stores a short
interrupt routine consisting of 3 instructions.
66
The first instruction is executed until the push button is realised (logic one
(1) on the P3.2 pin). The second instruction is executed until the push
button is pressed again. Immediately after that, the RETI instruction is
executed and the processor resumes operation of the main program. Upon
execution of any program instruction, the interrupt INT0 is generated and
the whole procedure is repeated (push button is still pressed). In other
words, one button press - one instruction.
67
Idle mode
Upon the IDL bit of the PCON register is set, the microcontroller turns off
the greatest power consumer- CPU unit while peripheral units such as
serial port, timers and interrupt system continue operating normally
consuming 6.5mA. In Idle mode, the state of all registers and I/O ports
remains unchanged.
68
In order to exit the Idle mode and make the microcontroller operate
normally, it is necessary to enable and execute any interrupt or reset. It will
cause the IDL bit to be automatically cleared and the program resumes
operation from instruction having set the IDL bit. It is recommended that
first three instructions to execute now are NOP instructions. They don't
perform any operation but provide some time for the microcontroller to
stabilize and prevents undesired changes on the I/O ports.
Power Down mode
By setting the PD bit of the PCON register from within the program, the
microcontroller is set to Power down mode, thus turning off its internal
oscillator and reduces power consumption enormously. The microcontroller
can operate using only 2V power supply in power- down mode, while a total
power consumption is less than 40uA. The only way to get the
microcontroller back to normal mode is by reset.
While the microcontroller is in Power Down mode, the state of all SFR
registers and I/O ports remains unchanged. By setting it back into the
normal mode, the contents of the SFR register is lost, but the content of
internal RAM is saved. Reset signal must be long enough, approximately
10mS, to enable stable operation of the quartz oscillator.
PCON register
Arithmetic Instructions
Branch Instructions
70
Mnemonic
Description
Byte
ADD A,Rn
ADD A,direct
ADD A,@Ri
ADD A,#data
ADDC A,Rn
ADDC A,direct
ADDC A,@Ri
ADDC A,#data
SUBB A,Rn
SUBB A,direct
SUBB A,@Ri
SUBB A,#data
INC A
INC Rn
INC Rx
INC @Ri
DEC A
DEC Rn
DEC Rx
DEC @Ri
INC DPTR
MUL AB
Multiplies A and B
DIV AB
Divides A by B
72
DA A
Branch Instructions
There are two kinds of branch instructions:
Unconditional jump instructions: upon their execution a jump to a new
location from where the program continues execution is executed.
Conditional jump instructions: a jump to a new program location is
executed only if a specified condition is met. Otherwise, the program
normally proceeds with the next instruction.
B R AN C H I N S T R U C T I O N S
Mnemonic
Description
Byte C
ACALL addr11
LCALL addr16
RET
RETI
AJMP addr11
Absolute jump
LJMP addr16
Long jump
SJMP rel
JC rel
JNC rel
JB bit,rel
JBC bit,rel
JMP @A+DPTR
JZ rel
JNZ rel
CJNE A,direct,rel
CJNE A,#data,rel
73
CJNE
Rn,#data,rel
CJNE
@Ri,#data,rel
DJNZ Rn,rel
DJNZ Rx,rel
NOP
No operation
Description
Byte C
MOV A,Rn
MOV A,direct
MOV A,@Ri
MOV A,#data
MOV Rn,A
MOV Rn,direct
MOV Rn,#data
MOV direct,A
MOV direct,Rn
MOV direct,direct
MOV direct,@Ri
MOV
direct,#data
MOV @Ri,A
MOV @Ri,direct
MOV @Ri,#data
MOV
DPTR,#data
3
74
MOVC
A,@A+DPTR
MOVC A,@A+PC
MOVX A,@Ri
MOVX A,@DPTR
MOVX @Ri,A
MOVX @DPTR,A
PUSH direct
POP direct
XCH A,Rn
XCH A,direct
XCH A,@Ri
XCHD A,@Ri
Logic Instructions
Logic instructions perform logic operations upon corresponding bits of two
registers. After execution, the result is stored in the first operand.
LOGIC INSTRUCTIONS
Mnemonic
Description
Byte
ANL A,Rn
ANL A,direct
ANL A,@Ri
ANL A,#data
ANL direct,A
ANL direct,#data
ORL A,Rn
OR register to accumulator
ORL A,direct
ORL A,@Ri
1
75
Cy
ORL direct,A
ORL direct,#data
XRL A,Rn
XRL A,direct
XRL A,@Ri
XRL A,#data
XRL direct,A
XORL direct,#data
CLR A
CPL A
SWAP A
RL A
RLC A
RR A
RRC A
Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic
operations. The difference is that these are performed upon single bits.
B I T-O R I E N T E D I N S T R U C T I O N S
Mnemonic
Description
Byte
CLR C
CLR bit
SETB C
SETB bit
CPL C
CPL bit
ANL C,bit
ANL C,/bit
ORL C,bit
76
Cy
ORL C,/bit
MOV C,bit
MOV bit,C
A - accumulator;
Rn - is one of working registers (R0-R7) in the currently
active RAM memory bank;
Direct - is any 8-bit address register of RAM. It can be any
general-purpose register or a SFR (I/O port, control register
etc.);
@Ri - is indirect internal or external RAM location addressed
by register R0 or R1;
#data - is an 8-bit constant included in instruction (0-255);
#data16 - is a 16-bit constant included as bytes 2 and 3 in
instruction (0-65535);
addr16 - is a 16-bit address. May be anywhere within 64KB
of program memory;
addr11 - is an 11-bit address. May be within the same 2KB
page of program memory as the first byte of the following
instruction;
rel - is the address of a close memory location (from -128 to
+127 relative to the first byte of the following instruction). On
the basis of it, assembler computes the value to add or
subtract from the number currently stored in the program
counter;
bit - is any bit-addressable I/O pin, control or status bit; and
C - is carry flag of the status register (register PSW).
ACALL addr11 - Absolute subroutine call
addr11: Subroutine address
77
EXAMPLE:
79
A: accumulator
Direct: Arbitrary register with address 0 - 255 (0 - FFh)
Description: Instruction adds the direct byte to the accumulator. As it is
direct addressing, the direct can be any SFR or general-purpose register
with address 0-7 Fh. The result is stored in the accumulator.
Syntax: ADD A, register name;
Bytes: 2 (instruction code, direct byte address);
STATUS register flags: C, OV and AC;
EXAMPLE:
80
Before execution: A= C3h (195 dec.) R0= AAh (170 dec.) C=1
After execution: A= 6Eh (110 dec.) AC=0, C=1, OV=1
ADD A,#data - Adds the immediate data to the accumulator
A: accumulator
Data: constant within 0-255 (0-FFh)
Description: Instruction adds data (0-255) to the accumulator. After
addition, the result is stored in the accumulator.
Syntax: ADD A,#data;
Bytes: 2 (instruction code, data);
STATUS register flags: C, OV and AC;
EXAMPLE:
81
Before execution: A= C3h (195 dec.) TEMP = AAh (170 dec.) C=1
After execution: A= 6Eh (110 dec.) AC=0, C=1, OV=1
ADDC A,@Ri - Adds the indirect RAM to the accumulator with a carry flag
A: accumulator
Ri: Register R0 or R1
Description: Instruction adds the indirect RAM to the accumulator with a
carry flag. RAM address is stored in the Ri register (R0 or R1). After
addition, the result is stored in the accumulator.
Syntax: ADDC A,@Ri;
Byte: 1 (instruction code);
STATUS register flags: C, OV and AC;
EXAMPLE:
82
83
85
86
87
C AN D B I T
BIT
C AN D B I T
89
location must be relatively near the current one (-128 to +127 locations
relative to the first following instruction).
Syntax: CJNE A,X,[jump address];
Bytes: 3 (instruction code, data, jump address);
STATUS register flags: C;
EXAMPLE:
91
EXAMPLE:
94
Syntax: CPL C;
Byte: 1 (instruction code);
STATUS register flags: C;
EXAMPLE:
98
is a short jump instruction, which means that the address of a new location
must be relatively near the current one (-128 to +127 locations relative to
the first following instruction).
Syntax: DJNZ direct,[jump address];
Bytes: 3 (instruction code, direct, jump address);
STATUS register flags: No flags are affected;
EXAMPLE:
100
relatively near the current one (- 128 to +127 locations relative to the first
following instruction).
Syntax: DJNZ Rn, [jump address];
Bytes: 2 (instruction code, jump address);
STATUS register flags: No flags are affected;
EXAMPLE:
101
Register (R0 or R1). If the register includes the number 255, the result of
the operation will be 0.
Syntax: INC @Ri;
Byte: 1 (instruction code);
STATUS register flags: No flags are affected;
EXAMPLE:
103
105
106
107
108
109
110
116
117
118
121
122
Before execution:
DPTR=1000:
A=0
A=1
A=2
A=3
After execution:
A=66h
A=77h
A=88h
A=99h
Note: DB (Define Byte) is a directive in assembly language used to define
constant.
MOV DPTR,#data16 - Loads the data pointer with a 16-bit constant
Data: constant in the range of 0-65535 (0-FFFFh)
DPTR: Data Pointer
Description: Instruction stores a 16-bit constant to the DPTR register. The
8 high bits of the constant are stored in the DPH register, while the 8 low
bits are stored in the DPL register.
Syntax: MOV DPTR,#data;
Bytes: 3 (instruction code, constant (15-8), constant (7-0));
STATUS register flags: No flags affected;
EXAMPLE:
123
addition is then used as a memory address from which the 8-bit data is
moved to the accumulator.
Syntax: MOVC A,@A+PC;
Byte: 1 (instruction code);
STATUS register flags: No flags affected;
EXAMPLE:
After the subroutine "Table" has been executed, one of four values is stored
in the accumulator:
Before execution:
A=0
A=1
A=2
A=3
After execution:
A=66h
A=77h
A=88h
A=99h
Note: DB (Define Byte) is a directive in assembly language used to define
constant.
MOVX @Ri,A - Moves the accumulator to the external RAM (8-bit address)
125
Ri: register R0 or R1
A: accumulator
Description: Instruction moves the accumulator to a register stored in
external RAM. Its address is stored in the Ri register.
Syntax: MOVX @Ri,A;
Byte: 1 (instruction code);
STATUS register flags: No flags affected;
EXAMPLE:
126
128
129
130
131
132
BIT
C AN D B I T
134
Syntax: RET;
Byte: 1 (instruction code);
STATUS register flags: No flags affected;
EXAMPLE:
136
137
RLC A - Rotates the accumulator one bit left through the carry flag
A: accumulator
Description: All eight bits in the accumulator and carry flag are rotated one
bit left. After this operation, the bit 7 is rotated into the carry flag position
and the carry flag is rotated into the bit 0 position.
Syntax: RLC A;
Byte: 1 (instruction code);
STATUS register flags: C;
EXAMPLE:
138
139
140
141
142
144
EXAMPLE:
146
147
148
149
150
TOC
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1
2
3
4
5
6
7
154
A L TE R N ATE F U N C T I O N
P1.0
T2 (Timer 2 input)
P1.1
P1.4
P1.5
P1.6
P1.7
A LTE R N ATE F U N C T I O N
P3.0
P3.1
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
RST Logic one (1) on this pin causes the microcontroller to be reset.
ALE/PROG In normal operation, the ALE pin is activated at a constant rate
of 1/16 the oscillator frequency and can be used for external clocking and
timing purposes. When external memory is used, a signal from this pin is
used to latch the low byte of an address (A0-A7) from P0. During the
process of writing a program to the microcontroller, this pin also serves as a
control input.
PSEN This pin provides a signal used for accessing external program
memory (ROM).
EA/VPP When this pin is connected to ground, the microcontroller reads
program instructions from external program memory. If internal program
memory is used, which is the common case, this pin should be connected
to the positive power supply voltage (VCC). During the process of
programming internal Flash mamory, this pin is supplied with +12V.
156
157
EEPROM Memory
EEPROM is a special type of memory having features of both RAM and
ROM. The contents of the EEPROM may be changed during operation, but
remains permanently saved even after the loss of power. The AT89S8253
microcontroller has in total of 2K of EEPROM, that is 2048 locations.
Memory Expansion
All mentioned above about ROM and RAM memory expansion remains in
force when it comes to the AT89S8253 microcontroller as it is based on the
8051 core. In other words, both memories can be added as external chips
with the capacity of up to 64Kb. The process of addressing is also the
same as in the 8051 standard.
158
Types of addressing
Similar to all microcontrollers compatible with the 8051, there are two ways
of addressing:
As shown in the table above, each of these registers has its name and
specific address in RAM. Unoccupied locations are intended for the future
upgraded versions of the microcontroller and shouldnt be used. As their
159
name suggests, these registers are mostly in control of one specific circuit
within the microcontroller such as timers or SPI etc. and they will be
discussed later in the book. This chapter covers only those SFRs
controlling more than one circuit within the microcontroller.
Accumulator (ACC)
The accumulator, otherwise marked as ACC or A, belongs to the core
registers of the 8051 microcontroller. Its contents is not modified.
B register
The B register also belongs to the core registers of the 8051
microcontroller. Bits of this register are not modified. It is used during
multiply and divide operations (MUL and DIV instructions) to store the
operands upon which these operations are performed.
161
DISALE
o
o
Intel_Pwd_Exit
0 - When the microcontroller is in Power Down mode, the program
proceeds with execution on high-to-low transition (1-0).
o
1 - When the microcontroller is in Power Down mode, the program
proceeds with execution on low-to-high transition (0-1).
o
DPS=1 -> Data pointer consists of DP1L and DP1H registers and is marked
as DPTR1.
163
EECON register
Bits of the EECON register controls the operation of EEPROM memory:
WRTINH
The WRTINH bit is read-only. When the power supply voltage is too low for
programming EEPROM, hardware automatically clears this bit, which
means that write to EEPROM cannot be completed or is aborted if in
progress.
RDY/BSY
165
EEMEN
EEMWE
When set, the EEMWE bit enables write to EEPROM using the MOVX
instruction. After completing EEPROM write, the bit must be cleared from
within the program.
EELD
When set, the EELD bit enables up to 32 bytes to be written
simultaneously. The bit is set and the MOVX instruction writes data to
EEPROM (buffer is loaded). The bit is cleared before writing the last data.
When the last MOVX is executed, the entire buffer is automatically loaded
to EEPROM for 4mS.
N O M I N AL T I M E
PS2
PS1
PS0
16ms
32ms
64ms
128ms
256ms
512ms
1024ms
2048ms
PS2,PS1,PS0
These three bits are in control of the prescaler and determine the nominal
time of the watchdog timer. If the program doesnt clear the WSWRST bit
during that time, the watchdog timer will reset the microcontroller. When all
three bits are cleared to 0, the watchdog timer has a nominal period of 16K
machine cycles. When all three bits are set to 1, the nominal period is
2048K machine cycles.
WDIDLE
The WDIDLE bit enables/disables the watchdog timer in Idle mode:
167
WDTEN
The WDTEN bit enables/disables the watchdog timer in software mode
(HWDT=0):
0 - Watchdog disabled.
1 - Watchdog enabled.
When the watchdog timer is in hardware mode (HWDT=1), this bit is readonly and reflects the status of the watchdog timer (whether it is enabled or
disabled).
4.6 Interrupts
The AT89S8253 has in total of six interrupt sources, which means that it
can recognize up to 6 different events that can interrupt regular program
execution. Each of these interrupts can be individually enabled or disabled
by setting bits of the IE register, whereas the whole interrupt system can be
disabled by clearing the EA bit of the same register.
Since this microcontroller has embedded Timer T2 and SPI (they don't fall
under the 8051 Standard) which can generate an interrupt, it was
necessary to make some changes in registers controlling interrupt system.
Besides, there is a new interrupt vector (address 2B), i.e. program memory
address from which the program proceeds with execution when the Timer
T2 generates an interrupt. All these changes are made on the previously
unused bits. This enables all programs written for the previous versions of
the microcontrollers to be used in this one too without being modified. This
is why the 8051-based microcontrollers are so popular.
169
0 - Priority 0
1 - Priority 1
PS Serial port interrupt priority:
172
0 - Priority 0
1 - Priority 1
PT1 Timer T1 interrupt priority:
0 - Priority 0
1 - Priority 1
PX1 External interrupt INT1 priority:
0 - Priority 0
1 - Priority 1
PT0 Timer T0 interrupt priority:
0 - Priority 0
1 - Priority 1
PX0 External interrupt INT0 priority:
0 - Priority 0
1 - Priority 1
IPH Register (Interrupt Priority High)
IPH BIT
INTERRUPTS
173
Priority 0 (lowest)
Priority 1 (low)
Priority 2 (high)
Priority 3 (highest)
Processing interrupt
When an interrupt request arrives, the microcontroller automatically detects
the interrupt source and the following occurs:
J U M P AD D R E S S
IE0
3h
TF0
Bh
IE1
13h
TF1
1Bh
23h
TF2, EXF2
2Bh
All addresses are in hex format
175
EXEN2 is timer 2 external enable bit used to include the T2EX pin in timer
2 operation:
1 - Timer 2 enabled.
0 - Timer 2 disabled.
177
178
179
As seen in figure above, unlike Capture mode, the contents of the capture
register (RCAP2H, RCAP2L) is now copied in the opposite direction upon
an overflow occurs, from capture (RCAP2H, RCAP2L) to counter register
(TH2, TL2).
Settings of Auto Reload mode are shown in the table below:
180
181
On counting up, the whole procedure is similar to the previous mode with
one exception referring to the function of the EXF2 bit.
On counting down, an overflow occurs when values stored in the counter
and capture registers match. It causes the TF2 bit as well as all bits of
registers T2H and T2L to be set while the counter keeps on counting down:
65535, 65534,65533...
In either case, the EXF2 bit is assigned a new function. When an overflow
occurs, this bit inverts the signal and cannot be used for generating an
interrupt anymore. Instead, it serves as supplementary bit (the 17th bit) of
the counter register, making this counter virtually a 17-bit register.
Timer T2 as a baud rate generator
The Timer T2 can be used as a baud rate generator and a clock generator
simultaneously. If the RCLK or TCLK bit of the register TCON is set, timer
T2 turns into a clock generator, so called Baud Rate generator). This mode
is very similar to auto-reload mode. The baud rate is computed using the
following formula:
182
183
Multiprocessor Communication
Multiprocessor communication (the SM2 bit of the SCON register is set)
enables automatic address recognition by allowing the serial port to
examine the adress of each incoming command. The process of writing a
program is much easier therefore as the microcontrollers sharing the same
interface don't have to check each address received via the serial port.
Let's make it clear.
Two special function registers, SADDR and SADEN, enable multiprocessor
communication. Each device has an individual address that is specified in
the SADDR register, while the so called mask address is written to the
SADEN register. The mask address contains don't care bits which provide
the flexibility to address one or more slaves at a time. In other words, it
defines which bits of the SADDR register are to be used and which are to
be ignored.
When the master wants to transmit data to one of several slaves, it first
sends out an address byte which identifies the target device. An address
184
byte differs from a data byte in that the 9th bit is 1 in an address byte and 0
in a data byte. After receiving the address byte, all slaves check whether it
matches their address. The adressed slave clears its SM2 bit and prepares
to receive the data bytes to come. The slaves that weren't addressed leave
their SM2 bits set and ignores the coming data bytes.
The most simple example is a mini-network comprising only 3
microcontrollers:
Microcontroller A is the master and communicates with devices B and C.
Microcontroller B: SADDR = 1100 0000
SADEN = 1111 1101
Address = 1100 00X0
Microcontroller C: SADDR = 1100 0000
SADEN = 1111 1110
Address = 1100 000X
If transmit address is 1100 0010, the data will be sent to slave device B.
If transmit address is 1100 0001 the data will be sent to slave device C.
If transmit address is 1100 0000 the data will be sent to both slave devices.
186
187
appears on the MOSI pin. An initial delay may occur for the sake of
synchronization with the main oscillator.
After sending one byte, the SPI clock generator stops, the SPIF bit (flag) is
set, the received byte is transferred to the SPDR register and, if enabled,
an interrupt is generated.
Any attempt to write another byte to the SPDR register while byte transmit
is in progress will cause the WCOL bit to be set. It indicates that an error
has occured. However, the byte will be succesfully transmitted, while the
new byte will be ignored, i.e. it will not be transmitted.
Enhanced SPI mode (buffer in use)
Enhanced mode is similar to normal except that this time data goes through
one more register while being transmitted. It makes no sense at first sight,
but communication is really faster. Look at the figure below...
Data written to the SPI data register SPDR is automatically transferred to
the capture register (buffer), which causes the WCOL bit to be set. It means
that the buffer is full and any further write will cause an overflow. Control
electronics (hardware) cleares this bit after transmitting data from buffer to
the shift register and after commencing serial data transmit. If the byte sent
188
is the first, the data is immediately transmitted to the shift register (still
empty), thus clearing the WCOL bit (buffer is empty).
While one byte transmit is in progress, the next byte to transmit may be
written to the SPDR register. It will be immediately moved to buffer. In order
to check whether data transmit is in progress, it is sufficient to check the
logic state of the LDEN bit of the SPSR register. If this bit is set (Load
Enable) and the WCOL bit is cleared, data transmit is in progress and
buffer is empty so the next byte can be written to the SPDR register.
How to select the right mode? If individual bytes are sent occasionally then
there is no need to complicate- the best solution is the normal mode. If it is
necessary to send a great amounts of data, it is better to use enhanced
mode in which the clock oscillator is enabled as far as buffer is regularly
loaded and the WCOL bit is set. In addition, no time is needed for
synchronization and data is easily and efficiently transferred.
The SPI system is under control of 3 special function registers. These are
SPDR, SPSR and SPCR.
SPDR (SPI Data Register)
189
The SPDR register is used for storing data to be transferred via SPI (in
serial format). It is also used for storing received data.
SPIF Interrupt flag. Upon data transfer, this bit is automatically set and an
interrupt is generated if SPIE=1 and ES=1. The SPIF bit is cleared by
reading SPSR followed by reading/writing SPDR register.
WCOL This bit is set in normal mode (ENH=0) if the SPDR register is
written during data transfer is in progress. The write is premature and has
no effect. It is called Write Collision. This bit is cleared in the same
manner as the SPIF bit.
The bit is set in enhanced mode (ENH=1) when buffer is full. It is indication
that a new data is ready to be transmitted to the shift register.
In enhanced mode, a new data can be written to buffer when the WCOL
bit is set. In addition, the WCOL bit must be cleared.
DISSO When set, this bit causes the MISO pin to float, thus enabling
several slave microcontrollers to share the same interface. Normally, the
first byte, called address byte, is received by all of them, but only one
should clear its DISSO bit.
ENH
0 SPI system operates in normal mode (without buffer).
1 SPI system operates in enhanced mode.
SPCR (SPI Control Register)
SPIE When this bit is set, the SPI system can generate an interrupt.
190
SPE This bit enables SPI communication. When set, pins SS, MOSI, MISO
and SCK are connected to the microcontroller pins P1.4, P1.5, P1.6 and
P1.7.
DORD Bit determines which bytes in serial communication are to be sent
first:
0 - Operate as slave.
1 - Operate as master.
CPOL Bit controls the SCK pin logic state when the SPI communication is
not in progress:
CPHA This bit along with the CPOL bit controls relation between clock and
data in serial format. Refer to the figure below.
SPR1,SPR0 When SPI system operates as master, these two bits
determine boud rate, i.e. clock signal frequency of the master device.
When operates as slave, these bits have no effect and SPI system
operates at a rate imposed by the master device.
SPR1
SPR0
SCK
Fosc/4
Fosc/16
Fosc/64
Fosc/128
191
When writing bits to the SPCR register, the SPE bit enabling
SPI should be set last, i.e. after setting all other parameters.
PCON
register
POF Bit is automatically set when the voltage level reaches maximum
(must be higher than 3V) after powering on. It is used for detecting cause
for reset (power on or restart condition after exiting Power Down mode).
GF1 General purpose bit (available for use).
GF0 General purpose bit (available for use).
PD By setting this bit, the microcontroller is set in Power Down mode.
IDL By setting this bit, the microcontroller is set in Idle mode.
When something goes wrong...
If something unexpected happens during the operation of the
microcontroller, what most bothers is the fact that its never the
microcontroller's fault. Although its not self-evident, the microcontroller
always obediently follows program instructions. For this reason, it is
193
If we neglect this detail, there is a risk that the program suddenly starts to
perform unpredictably. In order to prevent it, it is necessary to take care of
the following:
If only registers R0-R7 from bank 0 are in use, everything is easily kept
under control and program memory locations from 08h to 1Fh are available
for use. If registers, otherwise having the same names, from some other
bank are in use, you should be careful when using locations whose
addresses are less than 20h because it can cause R registers to be
erased.
If bit-variables are not used in the program, program memory locations
20h-2Fh are available for use. If the program contains bit-variables, you
should be careful when using these location in order not to change them
accidentaly.
By default, the data pushed onto stack occupy program memory locations
starting from 08h. If the banks 1, 2 or 3 are in use, their contents will be
certainly erased. For this reason, it is recommended to set the Stack
Pointer value to be greater than 20h or even greater at the beginning of the
program.
SFRs are used for controlling the microcontroller operation. Each of them
has its specific purpose and it should be observed. It means that they
194
cannot be used as general purpose registers even in the event that some
of their locations is not occupied.
Instruction set, recognized by the microcontroller, contains instructions
which can be used for controlling individual bits of registers at program
memory location 20h-7Fh. Besides, individual bits of some SFRs (not all of
them) can also be directly accessed. Addresses of these registers are
divisible by 8.
If memory is expanded by adding external RAM or ROM memory chip,
ports P0 and P2 are not available for use regardless of how many pins are
actually used for memory expansion.
The DPTR register is a 16-bit register comprised of registers DPH and DPL
which are 8-bit wide each. The DPTR register should be considered like
that practically. For example, when pushing it onto the Stack, DPL should
be pushed first, then DPH.
When used, serial communication is under control of the SCON register.
Besides, registers TCON and TMOD should be configured for this purpose
as well since the timer T1 is mostly used for boud rate generation.
When some of the interrupts is enabled, you should be careful because
there is a risk that program starts to perform unexpectedly. When an
interrupt request arrives, the microcontroller will execute instruction in
progress, push the address of the first following location onto the stack (in
order to know from where to continue) and jump to the specified interrupt
routine address. When the routine has been executed, the microcontroller
will pop the address from the stack and continue from where it left off.
However...
The microcontroller saves only the address to continue from after routine
execution. What is usually neglected is the fact that the contents of many
registers can be changed during routine execution. The program normally
procedees with execution considering the changed registers correct if their
original vaules haven't been saved, thus causing a total chaos. The worst
thing is that this problem can be manifested anytime: at the moment or
195
PSW
DPTR (DPH, DPL)
ACC
B
Registers R0 - R7
Note: Contents of registers are usually saved by being pushed onto the
Stack using the PUSH instruction. However, instructions such as PUSH
R0 cannot be used here because the microcontroller doesnt know which
register is concerned as there are 4 banks with registers haing the same
names R0-R7. For this reason, it is necessary to save addresses of these
registers instead of their names using the PUSH 00h instruction.
When some of the instructions for indirect addressing is used, you should
be careful not to use them for accessing SFRs as the microcontroller
ignores their addresses and accesses free RAM locations having the same
addresses as SFRs.
When UART system for serial communication is used, setting bits RI and TI
of the SCON register generated the same interrupt. If such an interrupt is
generated, it is first necessary to detect interrupt source (byte is sent,
received or both). It is important to remember that the microcontroller only
sets these bits so that they must be cleared from within the program.
Otherwise, the program gets stuck and executes the same interrupt routine
over and over again.
A list of bit-addressable registers
Accumulator (Address: E0)
After reset
AC C
0
0
196
Bit name
Bit address
E7
E6
E5
E4
E3
E2
E1
After reset
Bit name
Bit address
F7
F6
F5
F4
F3
F2
F1
After reset
0
IP
PT2
Bit name
PS
PT1
PX1
PT0
Bit address
BF
BE
BD
BC
BB
BA
B9
ES
ET1
EX1
ET0
AC
AB
AA
A9
After reset
Bit name
EA
0
IE
ET2
Bit address
AF
AE
AD
After reset
Bit name
Bit address
87
86
85
84
83
82
81
After reset
Bit name
Bit address
97
96
95
94
93
92
91
P0
P1
After reset
Bit name
Bit address
A7
A6
After reset
Bit name
Bit address
B7
B6
P2
A5
A4
A3
A2
A1
B5
B4
B3
B2
B1
RS1
RS0
OV
D4
D3
D2
D1
P3
After reset
Bit name
CY
0
PSW
AC
F0
Bit address
D7
D6
D5
After reset
Bit name
SM0
Bit address
9F
0
0
SCON
SM1
SM2
9E
9D
REN
TB8
RB8
TI
9C
9B
9A
99
After reset
Bit name
TF1
Bit address
8F
0
0
TCON
TR1
TF0
8E
8D
TR0
IF1
IT1
IF0
8C
8B
8A
89
After reset
Bit name
TF2
EXF2
T2CON
0
RCLK
TCLK
EXEN2
TR2
C/T2
198
CP/R
Bit address
CF
CE
CD
CC
CB
CA
C9
X XX
AU
R
Intel_Pwd_Exit
C8
After reset
Bit name
DISA
After reset
C L K RXE G X
Bit name
After reset
D P00 H
Bit name
After reset
D P00 L
Bit name
After reset
D P01 H
Bit name
After reset
D P01 L
Bit name
199
After reset
E E0
CON
Bit name
EELD
EEMWE
EEMEN
DPS
RDY/BSY
WRTI
After reset
I P H0
Bit name
PT2H
PSH
PT1H
PX1H
PT0H
PX
After reset
P C OXN
Bit name
SMOD
GF1
GF0
PD
After reset
S AD D0R
Bit name
After reset
S AD E0N
Bit name
After reset
S BXU F
Bit name
After reset
Bit name
0 SP 0
After reset
Bit name
SPIE
SPE
S0P C R
DORD
MSTR
CPOL
CPHA
SPR1
SP
After reset
SPDR -
Bit name
After reset
Bit name
SPIF
S0P S R
WCOL
LDEN
DISSO
0
TL0
-
0
TL1
-
After reset
Bit name
0
TL2
-
0H 0
T
-
0H 1
T
-
0H 2
T
-
T M O0D
Bit name
GATE1
C/T1
T1M1
T1M0
GATE0
C/T0
T0M1
After reset
Bit name
T 2XM O D X
-
T2OE
DCE
After reset
Bit name
PS2
PS1
0 W D T C0
ON
PS0
WDIDLE
DISRTO
HWDT
WSWRST
WD
After reset
WDTCON
202
Bit name
P AR A M E T E R
CONDITION
MIN.
VIL
Input Low-voltage
-0.5 V
0.2
0
VIL1
-0.5 V
0.2
0
VIH
Input High-voltage
0.2 Vcc
+ 0.9V
Vc
0.
VIH1
0.7 Vcc
Vc
0.
VOL
Output High-voltage
VOH1
IIL
-5
IILI
RRST
CIO
f = 1Mhz, Ta = 25C
10
Power-supply current
Normal mode: f =
12Mhz, Vcc = 5.5V Ta
= -40C
Idlle mode f = 12Mhz,
Vcc = 5.5V Ta = -40C
25
6.5
Power-down mode
100
40
ICC
0.
2.4 V
0.75 Vcc
0.9 Vcc
50 K
TOC
Chapter 1
203
150
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
2
3
4
5
6
7
language cannot be executed practically unless this file is loaded into the
microcontroller memory. This is the moment when the last link in the chain the programmer - appears on the scene. It is a small device connected to a
PC via some of the ports and has a socket for placing chip in.
Labels;
Orders;
Directives; and
Comments.
205
Numbers
If octal number system, otherwise considered as obsolite, is disregarded,
assembly laguage allows numbers to be used in one out of three number
systems:
Decimal Numbers
If not stated otherwise, the assembly language considers all the numbers
as decimal. All ten digits are used (0,1,2,3,4,5,6,7,8,9). Since at most 2
bytes are used for saving them in the microcontroller, the largest decimal
number that can be written in assembly language is 65535. If it is
necessary to specify that some of the numbers is in decimal format, then it
has to be followed by the letter D. For example 1234D.
Hexadecimal Numbers
Hexadecimal numbers are commonly used in programming. There are 16
digits in hexadecimal number system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D,
E, F). The largest hexadecimal number that can be written in assembly
language is FFFF. It corresponds to decimal number 65535. In order to
distinguish hexadecimal numbers from decimal, they are followed by the
letter h(either in upper- or lowercase). For example 54h.
Binary Numbers
207
Binary numbers are often used when the value of each individual bit of
some of the registers is important, since each binary digit represents one
bit. There are only two digits in use (0 and 1). The largest binary number
written in assembly language is 1111111111111111. In order to distinguish
binary numbers from other numbers, they are followed by the letter b
(either in upper- or lowercase). For example 01100101B.
Operators
Some of the assembly-used commands use logical and mathematical
expessions instead of symbols having specific values. For example:
IF (VERSION>1)
LCALL Table_2
USING VERSION+1
ENDIF
...
As seen, the assembly language is capable of computing some values and
including them in a program code, thus using the following mathematical
and logical operations:
N AM E
O P E R ATI O N
E X AM P L E
R E S U LT
Addition
10+5
15
Subtraction
25-17
Multiplication
7*4
28
7/4
MOD
Remainder of division
7 MOD 4
SHR
1000B SHR 2
0010B
SHL
1010B SHL 2
101000B
NOT
NOT 1
11111111111111
208
AND
Logical AND
0101B
OR
Logical OR
1101B OR 0101B
1101B
XOR
Exclusive OR
1000B
LOW
LOW(0AADDH)
0DDH
HIGH
HIGH(0AADDH)
0AAH
EQ, =
Equal
7 EQ 4 or 7=4
0 (false)
NE,<>
Not equal
7 NE 4 or 7<>4
0FFFFH (true)
GT, >
Greater than
7 GT 4 or 7>4
0FFFFH (true)
GE, >=
Greater or equal
7 GE 4 or 7>=4
0FFFFH (true)
LT, <
Less than
7 LT 4 or 7<4
0 (false)
LE,<=
Less or equal
7 LE 4 or 7<=4
0 (false)
Symbols
Every register, constant, address or subroutine can be assigned a specific
symbol in assembly language, which considerably facilitates the process of
writing a program. For example, if the P0.3 input pin is connected to a push
button used to stop some process manually (push button STOP), the
process of writing a program will be much simpler if the P0.3 bit is assigned
the same name as the push button, i.e. pushbutton_STOP. Of course, like
in any other language, there are specific rules to be observed as well:
AB
ACALL
ADD
ADDC
AJMP
AND
ANL
AR0
AR1
AR2
AR3
AR4
AR5
AR6
AR7
BIT
BSEG
CALL
CJNE
CLR
CODE
CPL
CSEG
DA
DATA
DB
DBIT
DEC
DIV
DJNZ
DPTR
DS
DSEG
DW
END
EQ
EQU
GE
GT
HIGH
IDATA
INC
210
ISEG
JB
JBC
JC
JMP
JNB
JNC
JNZ
JZ
LCALL
LE
LJMP
LOW
LT
MOD
MOV
MOVC
MOVX
MUL
NE
NOP
NOT
OR
ORG
ORL
PC
POP
PUSH
R0
R1
R2
R3
R4
R5
R6
R7
RET
RETI
RL
RLC
RR
RRC
SET
SETB
SHL
SHR
SJMP
SUBB
SWAP
USING
XCH
XCHD
XDATA
XOR
XRL
XSEG
Labels
A label is a special type of symbols used to represent a textual version of
an address in ROM or RAM memory. They are always placed at the
beginning of a program line. It is very complicated to call a subroutine or
execute some of the jump or branch instructions without them. They are
easily used:
212
213
...
...
214
215
216
DS 32
;Current value of address counter is
incremented by 32
SP_BUFF DS 16 ;Reserve space for serial port buffer
;(16 bytes)
IO_BUFF DS 8
8 bytes
217
Example 2:
ORG 100
DS 8
The DW directive is similar to the DB directive. It is used for writing a twobyte value into program memory. The higher byte is written first, then the
lower one.
IF, ENDIF and ELSE directives
These directives are used to create so called conditional blocks in the
program. Each of these blocks starts with directive IF and ends with
directive ENDIF or ELSE. The statement or symbol (in parentheses)
following the IF directive represents a condition which specifies the part of
the program to be compiled:
219
Example 2:
If the value of the symbol called Model is equal to one, the first two
instructions following directive IF will be compiled and the program
continues with instructions following directive ENDIF (all instructions
between ELSE and ENDIF are ignored). Otherwise, if Model=0, instructions
between IF and ELSE are ignored and the assembler compiles only
instructions following directive ELSE.
IF (Model)
MOV R0,#BUFFER
MOV A,@R0
ELSE
MOV R0,#EXT_BUFFER
MOVX A,@R0
ENDIF
...
Control directives
Control directives start with a dollar symbol $. They are used to determine
which files are to be used by the assembler during compilation, where the
executable file is to be stored as well as the final layout of the compiled
program called Listing. There are many control directives, but only few of
them is of importance:
\$INCLUDE directive
This directive enables the assembler to use data stored in other files during
compilation. For example:
220
\$INCLUDE(TABLE.ASM)
\$MOD8253 directive
This $MOD8253 directive is a file containing names and addresses of all
SFRs of 8253 microcontrollers. By means of this file and directive having
the same name, the assembler can compile the program on the basis of
register names. If they are not used, it is necessary to specify name and
address of every SFRs to be used at the beginning of the program.
TOC
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
Chapter
1
2
3
4
5
6
7
Chapter 6 : Examples
privilege nor a talent issue, but the ability of simply putting puzzle pieces
together using directives. Rest assured that design and development of
devices mainly consists of the following method test-correct-repeat. Of
course, the more you are in it, the more complicated it becomes since the
puzzle pieces are put together by both children and first-class architects...
Power supply:
Reset signal: and
Clock signal.
Clearly, it is about very simple circuits, but it does not have to be always
like that. If the target device is used for controlling expensive machines or
222
226
227
TYPE
T Y P I C AL
C U R R E N T I D ( M A)
M AX I M AL
C U R R E N T I F ( M A)
V O LTAG E D R
UD (V)
Infrared
30
50
1.4
Red
Standard
20
30
1.7
Red
Super
Bright
20
30
1.85
Red
Low
Current
30
1.7
Orange
10
30
2.0
Green
Low
Current
20
2.1
Yellow
20
30
2.1
Blue
20
30
4.5
White
25
35
4.4
229
being cyclically repeated at high speed for all digits and corresponding
transistors.
The fact that the microcontroller is just a kind of miniature computer
designed to understand only the language of zeros and ones is fully
expressed when displaying any digit. Namely, the microcontroller doesn't
know what units, tens or hundreds are, nor what ten digits we are used to
look like. Therefore, each number to be displayed must be prepared in the
following way:
First of all, a multy digit number must be split into units, tens etc. in a
particular subroutine. Then each of these digits must be stored in special
bytes. Digits get familiar format by performing masking. In other words, a
binary format of each digit is replaced by a different combination of bits in a
simple subroutine. For example, the digit 8 (0000 1000) is replaced by the
binary number 0111 111 in order to activate all LEDs displaying digit 8. The
only diode remaining inactive in this case is reserved for the decimal point.
If a microcontroller port is connected to the display in such a way that bit 0
activates segment a, bit 1 activates segment b, bit 2 segment c etc.,
then the table below shows the mask for each digit.
232
D I G I T S T O D I S P L AY
D I S P L AY S E G M E N T S
dp
233
The model described here is for its low price and great
capabilities most frequently used in practice. It is based on the
HD44780 microcontroller (Hitachi) and can display messages in
two lines with 16 characters each. It displays all the letters of
alphabet, Greek letters, punctuation marks, mathematical
symbols etc. In addition, it is possible to display symbols made up
by the user. Other useful features include automatic message
shift (left and right), cursor appearance, LED backlight etc.
LCD Pins
There are pins along one side of a small printed board. These are used for
connecting to the microcontroller. There are in total of 14 pins marked with
numbers (16 if it has backlight). Their function is described in the table
bellow:
234
FUNCTION
PIN
NUMBER
N AM E
LOGIC
S TATE
DESCRIPTION
Ground
Vss
0V
Power supply
Vdd
+5V
Contrast
Vee
0 - Vdd
RS
0
1
D0 D7 are interpreted a
commands
D0 D7 are interpreted as
R/W
0
1
0
1
From 1 to 0
D0
0/1
Bit 0 LSB
D1
0/1
Bit 1
D2
0/1
Bit 2
10
D3
0/1
Bit 3
11
D4
0/1
Bit 4
12
D5
0/1
Bit 5
13
D6
0/1
Bit 6
14
D7
0/1
Bit 7 MSB
Control of
operating
Data / commands
LCD screen
235
236
If there are no characters displayed or if all of them are dimmed when the
display is on, the first thing that should be done is to check the
potentiometer for contrast regulation. Is it properly adjusted? The same
applies if the mode of operation has been changed (writing in one or two
lines).
LCD Memory
The LCD display contains three memory blocks:
DDRAM Memory
DDRAM memory is used for storing characters to be displayed. The size of
this memory is sufficient for storing 80 characters. Some memory locations
are directly connected to the characters on display.
237
238
239
240
241
EXECUTI
TIME
C O M M AN D
RS
RW
D7
D6
D5
D4
D3
D2
D1
D0
Clear display
1.64mS
Cursor home
1.64mS
I/D
40uS
Display on/off
control
40uS
Cursor/Display
Shift
D/C
R/L
40uS
Function set
DL
40uS
Set CGRAM
address
Set DDRAM
address
DDRAM address
40uS
BF
DDRAM address
CGRAM address
40uS
242
Write to CGRAM
or DDRAM
D7
D6
D5
D4
D3
D2
D1
D0
40uS
Read from
CGRAM or
DDRAM
D7
D6
D5
D4
D3
D2
D1
D0
40uS
S 1 = Display shift on
interface
0 = Display shift off
interface
DL 1 = 8-bit
D 1 = Display on
lines
0 = Display off
line
N 1 = Display in two
U 1 = Cursor on
format 5x10 dots
0 = Cursor off
format 5x7 dots
F 1 = Character
B 1 = Cursor blink on
0 = Cursor blink off
0 = 4-bit
0 = Display in one
0 = Character
way explained on the previous page. The main purpose of 4-bit LED mode
is to save valuable I/O pins of the microcontroller. Only 4 higher bits (D4D7) are used for communication, while other may be left unconnected.
Each data is sent to the LCD in two steps: four higher bits are sent first
(normally through the lines D4-D7), then four lower bits. Initialization
enables the LCD to link and interpret received bits correctly. Data is rarely
read from the LCD (it is mainly transferred from the microcontroller to LCD)
so that it is often possible to save an extra I/O pin by simple connecting
R/W pin to ground. Such saving has its price. Messages will be normally
displayed, but it will not be possible to read the busy flag since it is not
possible to read the display either.
244
1. Display is cleared
2. Mode
o DL = 1 Communication through 8-bit interface
o N = 0 Messages are displayed in one line
o F = 0 Character font 5 x 8 dots
3. Display/Cursor on/off
o D = 0 Display off
o U = 0 Cursor off
o B = 0 Cursor blink off
4. Character entry
o ID = 1 Displayed addresses are automatically incremented by 1
o S = 0 Display shift off
245
It is not a mistake!
In this algorithm, the same value is transferred three times in a row.
In case of 4-bit initialization, the procedure is as follows:
246
247
6.3 Examples
The schematic below is used in the several following examples:
LED Blinking
The purpose of this example is not to demonstrate the operation of LEDs,
but the operating speed of the microcontroller. Simply put, in order to
enable LED blinking to be visible, it is necessary to provide sufficient
amount of time to pass between on/off states of LEDs. In this example time
delay is provided by executing a subroutine called Delay. It is a triple loop
in which the program remains for approximately 0.5 seconds and
decrements values stored in registers R0, R1 or R2. After returning from
the subroutine, the pin state is inverted and the same procedure is
repeated...
;
******************************************************
******************
;* PROGRAM NAME : Delay.ASM
;* DESCRIPTION: Program turns on/off LED on the pin
P1.0
;* Software delay is used (Delay).
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(DELAY.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
249
$NOPAGING
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
;RESET VECTORS
XRESET: MOV
pointer
SP,#STACK_START
MOV
P1,#0FFh
configured as inputs
;Reset vector
;Define Stack
;All pins are
LOOP:
CPL
P1.0
state is inverted
LCALL
Delay
;Pin P1.0
;Time delay
250
SJMP
LOOP
MOV
R2,#20
;500 ms time
F02:
MOV
R1,#50
;25 ms
F01:
MOV
R0,#230
DJNZ
R0,$
DJNZ
R1,F01
DJNZ
R2,F02
Delay:
delay
END
program
;End of
251
;
******************************************************
******************
;* PROGRAM NAME : WatchDog.ASM
;* DESCRIPTION : After watch-dog reset, program
increments number in
;* register R3 and shows it on port P1 in binary
format.
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(WATCHDOG.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
WMCON
DATA
WDTEN
EQU
is enabled
96H
00000001B
; Watch-dog timer
252
PERIOD
EQU
11000000B
period is set to be 1024ms
;RESET
; Nominal Watch-dog
VECTOR
CSEG
AT
JMP
XRESET
0
; Reset vector
CSEG
100H
ORL
WMCON,#PERIOD
; Define Watch-dog
ORL
WMCON,#WDTEN
; Watch-dog timer
MOV
A,R3
; R3 is moved to
MOV
P1,A
XRESET:
period
ORG
is enabled
port 1
INC
incremented by 1
R3
; Register R3 is
253
LAB:
SJMP
dog reset
LAB
END
; End of program
Timer T0 in mode 1
This program spends most of its time in an endless loop waiting for timer
T0 to count up a full cycle. When it happens, an interrupt is generated,
routine TIM0_ISR is executed and logic zero (0) on port P1 is shifted right
by one bit. This is another way of demonstrating the operating speed of the
microcontroller since each shift means that counter T0 has counted up
216 pulses!
;
******************************************************
******************
;* PROGRAM NAME : Tim0Mod1.ASM
;* DESCRIPTION: Program rotates "0" on port 1. Timer
T0 in mode 1 is
;* used
;
******************************************************
******************
;BASIC DIRECTIVES
254
$MOD53
$TITLE(TIM0MOD1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DECLARATION OF VARIABLES
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
;RESET VECTORS
CSEG
AT 0
JMP
XRESET
ORG
00BH
; Reset vector
255
JMP
TIM0_ISR
ORG
100H
; Timer T0 reset
vector
XRESET: MOV
pointer
SP,#STACK_START
; Define Stack
MOV
TMOD,#01H
; MOD1 is selected
MOV
A,#0FFH
MOV
P1,#0FFH
SETB
TR0
; Timer T0 is
IE,#082H
; Interrupt
enabled
MOV
enabled
CLR
LOOP1: SJMP
LOOP1
; Remain here
TIM0_ISR:
RRC
A
accumulator A through Carry flag
; Rotate
MOV
P1,A
accumulator A is moved to PORT1
; Contents of
256
RETI
; Return from
interrupt
END
; End of program
257
;BASIC DIRECTIVES
$MOD53
$TITLE(SPLIT.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DECLARATION OF VARIABLES
BSEG
AT
;DECLARATION OF BIT-VARIABLES
258
SEMAPHORE:
DBIT
DIRECTION
BIT
SEMAPHORE
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
ORG
00BH
;RESET VECTORS
; Reset
vector
JMP
reset vector
ORG
JMP
reset vector
TIM0_ISR
; Timer T0
01BH
TIM1_ISR
; Timer T1
259
ORG
XRESET: MOV
Stack pointer
MOD3
100H
SP,#STACK_START
; Define
MOV
TMOD,#00001011B
; Define
MOV
A,#0FFH
MOV
P1,#0FFH
MOV
R0,#30D
SETB
turned on
TR0
; TL0 is
SETB
turned on
TR1
; TL1 is
MOV
IE,#08AH
; Interrupt
CLR
enabled
CLR
the right
DIRECTION
; Rotate to
LOOP1:
here
LOOP1
; Remain
SJMP
TIM0_ISR:
260
DJNZ
R0,LAB3
rotation by 256 times
JB
; Slow down
DIRECTION,LAB1
RRC
A
; Rotate
contents of Accumulator to the right through
; Carry flag
SJMP
LAB2
LAB1:
RLC
A
; Rotate
contents of Accumulator to the left through
; Carry flag
LAB2:
MOV
P1,A
of Accumulator is moved to port P1
; Contents
LAB3:
RETI
from interrupt
; Return
TIM1_ISR:
DJNZ
R1,LAB4
direction of rotation by 256 times
; Slow down
DJNZ
R2,LAB4
expires, change rotation direction
; When time
CPL
SMER
MOV
R2,#30D
261
LAB4:
RETI
END
; End of
program
Simultaneous use of timers T0 and T1
This program can be considered as continuation of the previous one. They
share the same idea, but in this case true timers T0 and T1 are used. In
order to demonstrate the operation of both timers on the same port at the
same time, timer T0 reset is used to shift logic zero (0) on the port, while
Timer T1 reset is used to change rotation direction. This program spends
most of its time in the loop LOOP1 waiting for an interrupt to be caused by
reset. By checking the DIRECTION bit, information on rotation direction of
both bits in accumulator as well as of moving port LED is obtained.
;
******************************************************
******************
;* PROGRAM NAME : Tim0Tim1.ASM
;* DESCRIPTION: Timer TO rotates bit on port P1 while
Timer1
;* changes rotation direction. Both timers are
configured to operate in mode 1.
;
******************************************************
******************
262
;BASIC DIRECTIVES
$MOD53
$TITLE(TIM0TIM1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DECLARATION OF VARIABLES
BSEG
AT
;DECLARATION OF BIT-VARIABLES
SEMAPHORE:
DBIT
DIRECTION
BIT
SEMAPHORE
;STACK
263
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
; Reset
00BH
; Timer 0
STACK_START:
;RESET VECTORS
vector
ORG
Reset vector
JMP
ORG
Reset vector
TIM0_ISR
01BH
JMP
TIM1_ISR
ORG
100H
XRESET: MOV
Stack pointer
SP,#STACK_START
; Timer 1
; Define
264
MOV
TMOD,#11H
MOD1 for both timers
MOV
A,#0FFH
MOV
P1,#0FFH
; Select
MOV
initialized
R0,#30D
; R0 is
SETB
turned on
TR0
; TIMER0 is
SETB
turned on
TR1
; TIMER1 is
MOV
IE,#08AH
Timer1 Interrupt enabled
CLR
; Timer0 and
CLR
the right
DIRECTION
; Rotate to
LOOP1:
here
LOOP1
; Remain
SJMP
TIM0_ISR:
JB
DIRECTION,LAB1
265
RRC A
; Rotate
contents of accumulator to the right through
; Carry flag
SJMP
LAB2
LAB1:
RLC
A
; Rotate
contents of Accumulator to the left through
; Carry flag
LAB2:
MOV
P1,A
of Accumulator is moved to port P1
; Contents
RETI
from interrupt
; Return
TIM1_ISR:
DJNZ
R0,LAB3
expires, change rotation direction
CPL
DIRECTION
MOV
R0,#30D
; When time
; Initialize
R0
LAB3:
RETI
END
; End of
program
Using Timer T2
266
This example describes the use of Timer T2 configured to operate in AutoReload mode. In this very case, LEDs are connected to port P3 while the
push button used for forced timer reset (T2EX) is connected to the P1.1
pin.
Program execution is similar to the previous examples. When timer ends
counting, an interrupt is enabled and subroutine TIM2_ISR is executed,
thus rotating a logic zero (0) in accumulator and moving the contents of
accumulator to the P3 pin. At last, flags which caused an interrupt are
cleared and program returns to the loop LOOP1 where it remains until a
new interrupt request arrives...
If push button T2EX is pressed, timer is temporarily reset. This push button
resets timer, while push button RESET resets the microcontroller.
;
******************************************************
******************
267
;BASIC DIRECTIVES
$MOD53
$TITLE(TIMER2.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;DEFINITION OF VARIABLES
T2MOD
DATA
0C9H
268
;STACK
DSEG
AT
03FH
STACK_START:
DS
040H
CSEG
AT
JMP
XRESET
; Reset
02BH
; Timer T2
;RESET VECTORS
vector
ORG
Reset vector
JMP
TIM2_ISR
ORG
100H
XRESET: MOV
Stack pointer
SP,#STACK_START
MOV
A,#0FFH
MOV
P3,#0FFH
MOV
RCAP2L,#0FH
16-bit auto-reload mode
; Define
; Prepare
269
MOV
RCAP2L,#01H
CLR
CAP2
bit auto-reload mod
; Enable 16-
SETB
EXEN2
reset is enabled
; Pin P1.1
SETB
TR2
; Enable
IE,#0A0H
; Interrupt
Timer T2
MOV
is enabled
LOOP1:
here
CLR
SJMP
LOOP1
; Remain
TIM2_ISR:
RRC
A
; Rotate
contents of Accumulator to the right through
; Carry flag
MOV
P3,A
contents of Accumulator A to PORT3
CLR
CLR
timer T2 flag EXF2
; Move the
TF2
; Clear
EXF2
; Clear
270
RETI
; Return
END
; End of
from interrupt
program
271
;
******************************************************
******************
;* PROGRAM NAME : Int.ASM
;* DESCRIPTION : Program counts interrupts INT0
generated by appearance of high-to-low
;* transition signal on pin P3.2 Result appears on
port P0. Interrupts INT1 are also
;* counted up at the same time. They are generated
byappearing high-to-low transition
272
;BASIC DIRECTIVES
$MOD53
$TITLE(INT.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;RESET VECTORS
CSEG
AT
JMP
XRESET
ORG
003H
routine address for INT0
0
; Reset vector
; Interrupt
273
JMP
Isr_Int0
ORG 013H
routine address for INT1
JMP
Isr_Int1
ORG
100H
; Interrupt
XRESET:
MOV
TCON,#00000101B
is generated by appearing
transition signal on pin P3.2
is generated by appearing
transition signal on pin P3.3
enabled
MOV
MOV
starting value
; Interrupt INT0
; high-to-low
; Interrupt INT0
; high-to-low
IE,#10000101B
; Interrupt
R0,#00H
; Counter
MOV
R1,#00H
MOV
P0,#00H
; Reset port P0
MOV
P1,#00H
; Reset port P1
274
LOOP:
SJMP
LOOP
; Remain here
Isr_Int0:
INC R0
value of interrupt INT0 counter
; Increment
MOV P0,R0
RETI
Isr_Int1:
INC R1
value of interrupt INT1 counter
; Increment
MOV P1,R1
RETI
END
; End of program
275
In order to save I/O pins, four LED displays are connected to operate in
multiplex mode. It means that all segments having the same name are
connected to one output port each and only one display is active at a time.
Tranzistors and segmenats on displays are quickly activated, thus making
impression that all digits are active simultaneously.
Multiplex mode is not used this time. Instead, digit 3 is displayed on only
one of them (first one on the right).
Since the microcontroller does not know how we write number 3, a small
subroutine called Disp is used (the microcontroller writes this number as
0000 0011). This subroutine enables all decimal digits (0-9) to be displayed
(masked). The principle of operation is simple. A number to be displayed is
added to the current address and program jump is executed. Different
numbers require different jump length. Precisely determined combination of
zeroes and ones appears on each of these new locations (digit 1 mask,
digit 2 mask...digit 9 mask). When this combination is transferred to the
port, the display shows desired digit.
;
******************************************************
******************
;* PROGRAM NAME : 7Seg1.ASM
;* DESCRIPTION: Program displays number "3" on 7segment LED display
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG1.ASM)
$PAGEWIDTH(132)
277
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
MOV
SP,#STACK_START
STACK_START:
;RESET VECTORS
XRESET:
pointer
MOV
P1,#0
segments on displays
MOV
P3,#20h
; Reset vector
; Define Stack
; Turn off all
; Activate
display D4
278
LOOP:
MOV
3 to display
A,#03
LCALL
Disp
appropriate masking for the number
MOV
P1,A
SJMP
LOOP
Disp:
for displaying digits
INC
MOVC
A,@A+PC
; Send number
; Perform
; Subroutine
RET
DB
3FH
; Digit 0 mask
DB
06H
; Digit 1 mask
DB
5BH
; Digit 2 mask
DB
4FH
; Digit 3 mask
DB
66H
; Digit 4 mask
DB
6DH
; Digit 5 mask
DB
7DH
; Digit 6 mask
DB
07H
; Digit 7 mask
279
DB
7FH
; Digit 8 mask
DB
6FH
; Digit 9 mask
END
; End of
program
Writing and changing digits on LED display
This program is only an extended verson of the previous one. There is only
one digit active- the first one on the right, and there is no use of
multiplexing. Unlike the previous example, all decimal numbers are
displayed (0-9). In order to enable digits to change at reasonable pace, a
soubroutine L2 which causes a short time delay is executed prior to each
change occurs. Basically, the whole process is very simple and takes place
in the main loop called LOOP which looks as follows:
280
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG2.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
STACK_START:
;RESET VECTORS
vector
; Reset
281
ORG
100H
XRESET: MOV
Stack pointer
SP,#STACK_START
; Define
MOV
initial value
R3,#0
; Counter
MOV
P1,#0
all display segments
; Turn off
MOV
display D4
; Activate
P3,#20h
LOOP:
MOV
A,R3
LCALL
Disp
appropriate masking for number in
; Perform
;
Accumulator
MOV
P1,A
INC
R3
number in register by 1
; Increment
CJNE
R3,#10,L2
whether the number 10 is in R3
; Check
282
MOV
reset counter
R3,#0
; If it is,
MOV
time delay
R2,#20
; 500 mS
F02:
MOV
R1,#50
; 25 mS
F01:
MOV
R0,#230
DJNZ
R0,$
DJNZ
R1,F01
DJNZ
R2,F02
SJMP
LOOP
L2:
Disp:
Subroutine for writing digits
INC
MOVC
A,@A+PC
RET
mask
mask
DB
3FH
; Digit 0
DB
06H
; Digit 1
283
DB
5BH
; Digit 2
DB
4FH
; Digit 3
DB
66H
; Digit 4
DB
6DH
; Digit 5
DB
7DH
; Digit 6
DB
07H
; Digit 7
DB
7FH
; Digit 8
DB
6FH
; Digit 9
mask
mask
mask
mask
mask
mask
mask
mask
program
END
; End of
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG3.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
285
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
ORG
100H
STACK_START:
;RESET VECTORS
vector
XRESET: MOV
Stack pointer
SP,#STACK_START
; Reset
; Define
LOOP:
MOV
P1,#0
off all display segments
; Turn
MOV
P3,#20h
Activate display D4
MOV
A,#03
digit 3 on display D4
; Write
LCALL
Disp
appropriate mask for that digit
; Find
MOV
mask on the port
; Put the
P1,A
286
MOV
P1,#0
off all dislay segments
; Turn
MOV
P3,#10h
Activate display D3
MOV
A,#02
digit 2 on display D3
; Write
LCALL
Disp
mask for that digit
; Find
MOV
mask on the port
P1,A
; Put the
SJMP
to the label LOOP
LOOP
; Return
Disp:
Subroutine for writing digits
INC
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0
DB
06H
; Digit 1
DB
5BH
; Digit 2
mask
mask
mask
287
DB
4FH
; Digit 3
DB
66H
; Digit 4
DB
6DH
; Digit 5
DB
7DH
; Digit 6
DB
07H
; Digit 7
DB
7FH
; Digit 8
DB
6FH
; Digit 9
mask
mask
mask
mask
mask
mask
mask
program
END
; End of
288
;
******************************************************
******************
;* PROGRAM NAME : 7Seg5.ASM
;* DESCRIPTION : Program displays number"1234" on 7segment LED display
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG5.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
STACK_START:
AT
03FH
DS
040H
289
;RESET VECTORS
vector
CSEG
AT
JMP
XRESET
ORG
100H
XRESET: MOV
Stack pointer
SP,#STACK_START
; Reset
; Define
LOOP:
MOV
P1,#0
all display segments
; Turn off
MOV
display D4
; Activate
P3,#20h
MOV
A,#04
digit 4 on display D4
; Write
LCALL
for that digit
Disp
; Find mask
MOV
mask on the port
P1,A
; Put the
MOV
P1,#0
all display segments
; Turn off
290
MOV
display D3
P3,#10h
; Activate
MOV
A,#03
digit 3 on display D3
; Write
LCALL
for that digit
Disp
; Find mask
MOV
mask on the port
P1,A
; Put the
MOV
P1,#0
all display segments
; Turn off
MOV
display D2
; Activate
P3,#08h
MOV
A,#02
digit 2 on display D2
; Write
LCALL
for that digit
Disp
; Find mask
MOV
mask on the port
P1,A
; Put the
MOV
P1,#0
all display segments
; Turn off
MOV
display D1
; Activate
P3,#04h
MOV
A,#01
digit 1 on display D1
; Write
LCALL
for that digit
; Find mask
Disp
291
MOV
mask on the port
P1,A
; Put the
SJMP
the lable LOOP
LOOP
; Return to
Disp:
for writing digits
; Subroutine
INC
MOVC
A,@A+PC
RET
DB
3FH
; Digit 0
DB
06H
; Digit 1
DB
5BH
; Digit 2
DB
4FH
; Digit 3
DB
66H
; Digit 4
DB
6DH
; Digit 5
DB
7DH
; Digit 6
mask
mask
mask
mask
mask
mask
mask
292
DB
07H
; Digit 7
DB
7FH
; Digit 8
DB
6FH
; Digit 9
mask
mask
mask
293
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(7SEG4.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
STACK_START:
;RESET VECTORS
vector
; Reset
294
ORG
100H
XRESET: MOV
Stack pointer
SP,#STACK_START
; Define
MOV
starting value
R2,#0
; Counter
MOV
R3,#0
MOV
R4,#0
LOOP:
INC
R4
display to be "refreshed" for 100 times
;Wait for
CJNE
R4,#20d,LAB1
incrementing the counter
;before
MOV
R4,#0
MOV
P1,#0
all display segments
; Turn off
INC
R2
Increment Register containing units by 1
CJNE
R2,#10d,LAB1
MOV
R2,#0
; Reset
units
295
INC
R3
Increment Register with tens by 1
tens
CJNE
R3,#10d,LAB1
MOV
R3,#0
; Reset
P3,#20h
; Activate
LAB1:
MOV
display D4
MOV
A,R2
Register containing units to A
; Copy
LCALL
Disp
mask for that digit
; Call
MOV
P1,A
units on display D4
; Write
LCALL
; 25ms
delay
Delay
MOV
P1,#0
all display segments
; Turn off
MOV
display D3
; Activate
P3,#10h
MOV
A,R3
Register contaning tens to A
; Copy
LCALL
Disp
mask for that digit
; Call
296
MOV
P1,A
tens on display D3
; Write
LCALL
Delay
; 25ms
SJMP
LOOP
MOV
R1,#50
MOV
R0,#250
DJNZ
R0,$
DJNZ
R1,F01
delay
Delay:
delay
F01:
; 5 ms
RET
Disp:
Subroutine for displaying digits
INC
MOVC
A,@A+PC
RET
mask
DB
3FH
; Digit 0
297
DB
06H
; Digit 1
DB
5BH
; Digit 2
DB
4FH
; Digit 3
DB
66H
; Digit 4
DB
6DH
; Digit 5
DB
7DH
; Digit 6
DB
07H
; Digit 7
DB
7FH
; Digit 8
DB
6FH
; Digit 9
mask
mask
mask
mask
mask
mask
mask
mask
mask
program
END
; End of
Handling EEPROM
This program writes data to on-chip EEPROM memory. In this case, the
data is a hexadecimal number 23 which is to be written to the location with
address 00.
298
To make sure that this number is correctly written, the same location of
EEPROM is read 10mS later in order to compare these two numbers. If
they match, F will be displayed. Otherwise, E will be displayed on the LED
display (Error).
;
******************************************************
******************
;* PROGRAM NAME: EEProm1.ASM
;* DESCRIPTION: Programming EEPROM at address 0000hex
and displaying message
;* on LED display.
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(EEPROM1.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
299
WMCON
DATA
96H
EEMEN
EQU
00001000B
internal EEPROM is enabled
; Access to
EEMWE
EQU
EEPROM is enabled
; Write to
00010000B
TEMP
DATA
030H
Auxiliary register
; Define
THE END
"F"
EQU
071H
; Display
ERROR
"E"
EQU
033H
; Display
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
;STACK
STACK_START:
;RESET VECTORS
; Reset
vector
300
ORG
100H
XRESET:
MOV
IE,#00
interrupts are disabled
MOV
; All
SP,#STACK_START
MOV
DPTR,#0000H
location address in EEPROM
; Choose
ORL
EEPROM is enabled
WMCON,#EEMEN
; Access to
ORL
EEPROM is enabled
WMCON,#EEMWE
; Write to
MOV
TEMP,#23H
written to EEPROM is moved to
; Number
MOV
A,TEMP
TEMP and Accumulator
; register
to EEPROM
MOVX
@DPTR,A
; Write byte
CALL
DELAY
; 10ms delay
MOVX
A,@DPTR
same location and compare to TEMP,
; Read the
CJNE
A,TEMP,ERROR
don't match, jump to label ERROR
; If they
301
MOV
A,#KRAJ
MOV
P1,A
; Display F
(correct)
XRL
WMCON,#EEMWE
EEPROM is disabled
; Write to
XRL
WMCON,#EEMEN
EEPROM is disabled
; Access to
LOOP1:
here
SJMP
LOOP1
; Remain
ERROR:
(error)
MOV
A,#ERROR
; Display E
MOV
P1,A
LOOP2:
SJMP
LOOP2
DELAY:
MOV
A,#0AH
MOV
R3,A
LOOP3:
NOP
LOOP4:
DJNZ
B,LOOP4
LOOP5:
DJNZ
B,LOOP5
DJNZ
R3,LOOP3
; Delay
RET
302
program
END
; End of
303
;
******************************************************
******************
;* PROGRAM NAME : UartR.ASM
;* DESCRIPTION: Each data received from PC via UART
appears on the port
;* P1.
;*
304
;
******************************************************
******************
;BASIC DIRECTIVES
$MOD53
$TITLE(UARTR.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
AT
03FH
DS
040H
CSEG
AT
JMP
XRESET
STACK_START:
;RESET VECTORS
; Reset vector
305
ORG
023H
address of UART interrupt routine
JMP
IR_SER
ORG
100H
; Starting
XRESET: MOV
IE,#00
interrupts are disabled
; All
MOV
SP,#STACK_START
Initialization of Stack pointer
MOV
TMOD,#20H
; Timer1 in
MOV
TH1,#0FDH
rate at the frequency of
; 9600 baud
mode2
; 11.0592MHz
MOV
SCON,#50H
enabled, 8-bit UART
; Receiving
MOV
IE,#10010000B
interrupt enabled
; UART
CLR
transmit flag
TI
; Clear
CLR
receive flag
RI
; Clear
TR1
; Start Timer1
SETB
306
LOOP:
SJMP
IR_SER: JNB
is received,
LOOP
; Remain here
RI,OUTPUT
; If any data
; move it to
the port
MOV
A,SBUF
MOV
P1,A
CLR
receive flag
OUTPUT
program
RI
; P1
; Clear
RETI
END
; End of
;BASIC DIRECTIVES
$MOD53
$TITLE(UARTS.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;STACK
DSEG
STACK_START:
AT
03FH
DS
040H
AT
;RESET VECTORS
CSEG
308
JMP
XRESET
ORG
100H
XRESET: MOV
are disabled
IE,#00
MOV
SP,#STACK_START
of Stack pointer
MOV
; Reset vector
; All interrupts
; Initialization
TMOD,#20H
; Timer1 in mode
MOV
TH1,#0FDH
at the frequency of
; 11.0592MHz
MOV
SCON,#40H
; 8-bit UART
CLR
TI
; Clear transmit
CLR
RI
; Clear receive
MOV
R3,#00H
; Reset caunter
SETB
TR1
; Start Timer 1
bit
flag
START:
MOV
SBUF,R3
from counter to a PC
; Move number
309
LOOP1:
JNB
TI,LOOP1
until byte transmission is
; Wait here
; complete
bit
CLR
TI
; Clear transmit
INC
R3
counter value by 1
; Increment the
CJNE
R3,#00H,START
are not sent return to the
; If 255 bytes
; label START
LOOP:
SJMP
LOOP
END
; Remain here
; End of program
******************************************************
*******************
;* PROGRAM NAME : Lcd.ASM
;* DESCRIPRTION : Program for testing LCD display. 4bit communication
;* is used. Program does not check BUSY flag but uses
program delay
;* between 2 commands. PORT1 is used for connection
;* to the microcontroller.
;
******************************************************
******************
311
;BASIC DIRECTIVES
$MOD53
$TITLE(LCD.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;Stack
DSEG
AT
0E0h
Stack_Start:
DS
020h
Start_address
EQU
0000h
;Reset
vectors
CSEG
AT
ORG
Start_address
312
JMP
Inic
ORG
Start_address+100h
MOV
IE,#00
interrupts are disabled
MOV
Inic:
CALL
Initialize LCD
; All
SP,#Stack_Start
LCD_inic
;*************************************************
;* MAIN PROGRAM
;*************************************************
START:
MOV
A,#80h
character will appear on the first
; Next
CALL
LCD_status
location in the first line of LCD display.
MOV
character M.
; Display
A,#'M'
313
CALL
LCD_putc
subroutine for character transmission.
; Call
MOV
character i.
; Display
CALL
MOV
character k.
CALL
MOV
character r.
CALL
MOV
character o.
CALL
MOV
character e.
CALL
MOV
character l.
CALL
MOV
character e.
CALL
A,#'i'
LCD_putc
A,#'k'
; Display
LCD_putc
A,#'r'
; Display
LCD_putc
A,#'o'
; Display
LCD_putc
A,#'e'
; Display
LCD_putc
A,#'l'
; Display
LCD_putc
A,#'e'
; Display
LCD_putc
314
MOV
character k.
CALL
MOV
character t.
CALL
MOV
character r.
CALL
MOV
character o.
CALL
MOV
character n.
CALL
MOV
character i.
CALL
MOV
character k.
CALL
MOV
character a.
A,#'k'
; Display
LCD_putc
A,#'t'
; Display
LCD_putc
A,#'r'
; Display
LCD_putc
A,#'o'
; Display
LCD_putc
A,#'n'
; Display
LCD_putc
A,#'i'
; Display
LCD_putc
A,#'k'
; Display
LCD_putc
A,#'a'
; Display
315
CALL
LCD_putc
MOV
A,#0c0h
character will appear on the first
; Next
CALL
LCD_status
location in the second line of LCD display.
MOV
character R.
; Display
A,#'R'
CALL
LCD_putc
subroutine for character transmission.
; Call
MOV
character a.
; Display
CALL
MOV
character z.
CALL
MOV
character v.
CALL
MOV
character o.
CALL
MOV
character j.
A,#'a'
LCD_putc
A,#'z'
; Display
LCD_putc
A,#'v'
; Display
LCD_putc
A,#'o'
; Display
LCD_putc
A,#'j'
; Display
316
CALL
MOV
character n.
CALL
MOV
character i.
CALL
MOV
character .
CALL
MOV
character s.
CALL
MOV
character i.
CALL
MOV
character s.
CALL
MOV
character t.
CALL
LCD_putc
A,#'n'
; Display
LCD_putc
A,#'i'
; Display
LCD_putc
A,#' '
; Display
LCD_putc
A,#'s'
; Display
LCD_putc
A,#'i'
; Display
LCD_putc
A,#'s'
; Display
LCD_putc
A,#'t'
; Display
LCD_putc
317
MOV
character e.
CALL
MOV
character m.
CALL
MOV
character i.
CALL
MOV
time (20x10ms)
display
A,#'e'
; Display
LCD_putc
A,#'m'
; Display
LCD_putc
A,#'i'
; Display
LCD_putc
R0,#20d
; Wait
CALL
Delay_10ms
MOV
DPTR,#LCD_DB
; Clear
MOV
A,#6d
CALL
LCD_inic_status
R0,#10d
; Wait
MOV
time(10x10ms)
CALL
Delay_10ms
JMP
START
318
;*********************************************
;* Subroutine for wait time (T= r0 x 10ms)
;*********************************************
Delay_10ms: MOV
R5,00h
1+(1+(1+2*r7+2)*r6+2)*r5 approximately
MOV
R6,#100d
; (if
MOV
R7,#100d
r7>10)
2*r5*r6*r7
DJNZ R7,$
indicates current instruction.
DJNZ
R6,$-4
DJNZ
R5,$-6
; $
RET
;
******************************************************
********************************
;* SUBROUTINE: LCD_inic
;* DESCRIPTION: Subroutine for LCD initialization.
;*
319
LCD_enable
BIT
P1.3
activating pin E on LCD.
; Bit for
LCD_read_write BIT
P1.1
activating pin RW on LCD.
; Bit for
LCD_reg_select BIT
P1.2
activating pin RS on LCD.
; Bit for
LCD_port
SET
P1
for connection to LCD.
; Port
320
Busy
BIT
P1.7
pin on which Busy flag appears.
; Port
LCD_Start_I_red EQU
00h
of the first message character
; Address
; in the
; Address
; in the
LCD_DB:
DB
00111100b
2/1 lines, 5x10/5x7 format
; 0 -8b,
DB
00101100b
2/1 lines, 5x10/5x7 format
; 1 -4b,
DB
00011000b
-Display/cursor shift, right/left
; 2
DB
00001100b
-Display ON, cursor OFF, cursor blink off
; 3
DB
00000110b
-Increment mode, display shift off
; 4
DB
-Display/cursor home
; 5
00000010b
321
DB
00000001b
; 6
-Clear display
DB
00001000b
-Display OFF, cursor OFF, cursor blink off
; 7
LCD_inic:
;*****************************************
MOV
DPTR,#LCD_DB
MOV
A,#00d
initialization in 8-bit
; Triple
CALL LCD_inic_status_8
performed at the beginning
; mode is
MOV
A,#00d
case of slow increment of
; (in
CALL LCD_inic_status_8
supply when the power supply is on
; power
MOV
A,#00d
lcall LCD_inic_status_8
MOV
A,#1d
; Change
322
CALL
LCD_inic_status_8
MOV
A,#1d
CALL
LCD_inic_status
; 4-bit
mode
MOV
A,#3d
this point the program executes in
; As from
;4-bit
mode
CALL
LCD_inic_status
MOV
A,#6d
CALL
LCD_inic_status
MOV
A,#4d
CALL
LCD_inic_status
RET
LCD_inic_status_8:
;******************************************
PUSH
323
MOVC
A,@A+DPTR
CLR
LCD_reg_select
; RS=0 -
CLR
Write data on LCD
LCD_read_write
; R/W=0 -
Write command
MOV
B,LCD_port
bits from LCD port are memorized
ORL
B,#11110000b
ORL
A,#00001111b
ANL
A,B
; Lower 4
MOV
LCD_port,A
moved from A to LCD port
; Data is
SETB LCD_enable
to-low transition signal
; high-
; is
LCD_enable
MOV
B,#255d
delay in case of improper reset
; Time
324
DJNZ
B,$
; during
initialization
DJNZ B,$
DJNZ B,$
POP B
RET
LCD_inic_status:
;
******************************************************
**********************
MOVC
A,@A+DPTR
CALL
LCD_status
RET
;
******************************************************
**********************
;* SUBROUTINE: LCD_status
;* DESCRIPTION: Subroutine for defining LCD status.
325
;
******************************************************
**********************
LCD_status:
PUSH
MOV
B,#255d
DJNZ
B,$
DJNZ
B,$
DJNZ
B,$
CLR
LCD_reg_select
Command is sent to LCD
CALL
LCD_port_out
SWAP A
are swapped in accumulator
DJNZ
B,$
DJNZ
B,$
DJNZ
B,$
CLR
LCD_reg_select
Command is sent to LCD
CALL
; RS=O:
; Nibles
; RS=0:
LCD_port_out
326
POP
RET
;
******************************************************
**********************
;* SUBROUTINE: LCD_putc
;* DESCRIPTION: Sending character to be displayed on
LCD.
;
******************************************************
**********************
LCD_putc:
PUSH
MOV
B,#255d
DJNZ
B,$
SETB LCD_reg_select
Character is sent to LCD
CALL
; RS=1:
LCD_port_out
SWAP A
are swapped in accumulator
; Nibles
327
DJNZ
B,$
SETB LCD_reg_select
Character is sent to LCD
CALL
LCD_port_out
POP
; RS=1:
RET
;
******************************************************
**********************
;* SUBROUTINE: LCD_port_out
;* DESCRIPTION: Sending commands or characters on LCD
display
;
******************************************************
**********************
LCD_port_out:
PUSH
ACC
PUSH
MOV
B,LCD_port
bits of LCD port are memorized
; Lower 4
328
ORL
B,#11110000b
ORL
A,#00001111b
ANL
A,B
MOV
LCD_port,A
copied from A to LCD port
; Data is
SETB LCD_enable
to-low transition signal
; high-
LCD_enable
POP
POP
ACC
; is
RET
program
END
; End of
BINDEC:
MOV
decimal number 10 in B
B,#10d
; Store
DIV
Remainder remains in B
AB
; A:B.
R3,B
; Move units
B,#10d
; Store
MOV
to register R3
MOV
decimal number 10 in B
330
DIV
Remainder remains in B
AB
; A:B.
R2,B
; Move tens
MOV
decimal number 10 in B
B,#10d
; Store
DIV
Remainder remains in B
AB
; A:B.
MOV
hundreds to accumulator
A,B
; Move
MOV
to register R2
RET
; Return to
331
VECTOR
CSEG
AT
JMP
XRESET
0
; Reset vector
CSEG
100H
ORL
WMCON,#PERIOD
; Define Watch-dog
ORL
WMCON,#WDTEN
; Watch-dog timer
XRESET:
period
ORG
is enabled
333
E6D8FED9FADAF6DD00000001FF2
55AFED589EAF3698E8EB25BA585
FEA2569AD96DAC59D9FADAF6D
D00000001FF255AFED8FED9FADA
F6DD000F7590FFB29013278E6D82
78E6D8FED9FA589EAF3698E8EB2
5BA585FEA2569AD96DAF6DD000
00001FF2DAF6DD00000001FF255A
ADAF6DD00000001FF255AFED8FE
D9FA
In the event that other software for program writing in assembly language is
used, a special software for compiling the program must be installed and
used as follows - set up the compiler, open the document with .asm
extension and compile. The result is the same- a new document with
extension .hex. The only problem now is that it is stored in your PC.
Programming a microcontroller
In order to transfer a hex code to the microcontroller, it is necessary to
provide a cable for serial communication and a special device, called
programmer, with software. There are several ways to do it.
A large number of programs and electronic circuits having this purpose can
be found on the Internet. Do as follows: open hex code document, set a
few parameters and click the icon for compiling. After a while, a sequence
of zeros and ones will be programmed into the microcontroller through the
serial connection cable and programmer hardware. What's left is to place
334
the programmed chip into the taget device. In the event that it is necessary
to make some changes in the program, the previous procedure may be
repeated an unlimited number of times.
335
336
The program is written and successfully compiled. All that's left is to dump
the program to the microcontroller. For this purpose it is necessary to have
a software that takes the written and compiled program and passes it to the
microcontroller.
Start the program 8051 Flash_setup.exe...
337
338
339
The following text describes in short some circuits within this development
system. It is rather illustration of its features than complete manual.
Besides, by learning about this device, one understands that
microcontrollers and its tools are intended to everybody, not only to the
privileged.
Sockets
Programmer
342
8MHz Oscillator
344
Each I/O port pin is connected to one LED which enables visual
indication of its logic state. In the event that the presence of
directly polarized LEDs and serial resistors is not acceptable in
some applications, DIP switch SW2 enables them to be
disconnected from the port.
345
346
347
348
LCD displays
349
350
351
352
353
355
356
357
358
359
360