Hardware Description of The 8051
Hardware Description of The 8051
| |
| |
| |
| =|||= =|||= ||||||| =|| |
| || || || || || =| || |
| || || || || || || |
| || || || || || == || |
| ||||| || || ||===|= || |
| || || || || || || |
| || || || || || || |
| || || || || = || || |
| =|||= =|||= =|||= || |
| |
| |
| |
| |
| |
| |
| H H A RRRRr DDDDd W W A RRRRr EEEEEE |
| H H A A R R D D W W A A R R E |
| H H A A R R D D W W A A R R E |
| HHHHHH AAAAA RRRR D D W W W AAAAA RRRR EEEEE |
| H H A A R R D D W W W W A A R R E |
| H H A A R R D D W W W W A A R R E |
| H H A A R R DDDD W W A A R R EEEEEE |
| |
| |
| |
| |
===========================================================================
Forward:
--------
When possible, in the various chapter short examples are given. In addition
in an extra chapter, as an example, the set up and programming of a computer
card with two different PCs connected by serial ports are described.
The basic type of the MCS-51 family is the 8051 controller from Intel.
Other types from Intel, and controllers designed by other companies based on
this family possess the same functions as the 8051. Some have added on
several extra functions. Hence the 8052 has an extra timer and a larger
internal RAM.
The 8031, 8751 types are identical to the 8051, but they possess no internal
ROM and an internal EPROM, respectively.
First we will look at only the 8051, explaining its architecture. Following,
several functions of the other CPU's of the MCS-51 family will be considered.
Table of contents:
-------------------
1. 8051 characteristics
-----------------------
The 8051 is often embedded in control circuits, for that reason the name
microcontroller has been commonly used for the 8051.
The 8051 operates on an 8 bit data bus. Externally it can address 64 kByte of
data and code separately. Additionally it has internal RAM (128 Byte) and
internal ROM (4 kByte) of program memory.
Instructions from addresses 0000 to 0FFFh operate from internal program
memory. Instructions from higher addresses will always be read from external
program memory. Through an input pin the internal program memory can be
switched off.
A special feature of the 8051 is that almost all its electrical connections
(pins) can be accessed with software. It also has access to two integrated
timers, one serial port (UART) as well as two interrupt inputs.
Additionally it is possible to manipulate single BITs with MOV, SETB, CLR and
CPL.
Division can only be done byte wise (8 BIT). For larger data, more complicated
procedures are necessary. The size of the internal RAM is often limiting.
The assignment of characters to numeric values has naturally been the norm.
In the ASCII standard (American Standard Code for Information
Interchange) 8 binary digits are used to represent numeric values. Such a
numeric value between 0 and 255 are designated as a BYTE. Each of its binary
digits is a BIT.
The choice of 8 bits per BYTE comes also into consideration in the
representation of numeric values for computations, for in hexadecimal
notation, this can be represented by just two digit hex numbers. Larger values
(with more digits) use several bytes.
To support computing, controllers and processors are equipped with 8 bit wide
data lines. Newer, faster CPU's in future will hold 16 or 32 BIT wide data
lines. With all this, the ASCII table is in addition used for the coding of
text (see Appendix). The following designations are normally used:
MSB LSB
+++++++++
| Bit7 | Bit6 | Bit5 | Bit4 | Bit3 | Bit2 | Bit1 | Bit0 | BYTE
+++++++++
| High NYBBLE | Low NYBBLE |
The 8051 is an 8 bit computer, that is: numeric values, characters and also
the code are stored as bytes. The data bus has 8 parallel data lines.
In order to store a number the size of a BYTE, 8 flip flops will be needed.
To store a complete text, many such 8 flip flop memory locations will be
needed. In addition, each memory location should be individually accessible.
For each memory location a select line is needed.
Memory chips produced today are highly integrated (a 62256 RAM chip has
for example 32768 memory locations: it can store 32 kBytes). In order to
access a given memory location, it is not necessary to have 32768 select
lines. Instead, coded addresses are used, which are decoded only in the
RAM chip. So with 16 address lines, which the 8051 prepares, 65536 memory
locations can be addressed.
The 8051 uses an 8 bit wide data bus. It has several internal registers,
which are likewise 8 bit wide. It can access external components (as memory)
over drivers. It has internal RAM, 2 Timers, serial input/output and internal
program memory.
++
++||++
| ++ |
++++ ++
ALE | | | |
_PSEN |Oscillator++ Arithmetic Logic Unit ++
| | +===>| | |
+++ | ++ |
| | | <Code-Fetch> | |
| | | | | _EA
Timer: | |i | | | ++
++ |n | | +>| Program- |
| TMOD |<=====+t | +=============| Counter, |
++ |e | |i | | Address- |
| TCON |<=====+r | |n | +==| Register |
++ |n ++ |t | |e ++
| TH0:TL0 |<=====+ | internal | |. | |x
++ |D | | |A | |t
| TH1:TL1 |<=====+A | PROM |<==+d | |.
++ |T | | r | |A
|A ++ | |d
| | |r
| | |
++ | Port-Latches: | | Driver:
| ACC |<=====+ ++ | +======>|
++ +==========>| P0 |<=====+============>| Port 0
| B |<=====+ ++ |
++ | ++ +======>|
| PSW |<=====+==========>| P2 |<==================>| Port 2
++ | ++
| PCON |<=====+==========>| P1 |<==================>| Port 1
++ | ++
| DPH:DPL |<=====+==========>| P3 |<==================>| Port 3
++ | ++
| SP |<=====+
++ |
|
| ++
serial port: | | internal |
++ |======>| |
| SCON |<=====+ | RAM |
++ | | |
| SBUF |<=====+ ++
++
As indicated in the block diagram, the CPU can access the SFR (see chapter
3.4) and internal RAM over the internal data bus. For external memory access
(XDATA) ports P0 and P2 are used. Concurrent with the high address byte being
placed in P2, at first the low address byte will be placed in P0. After the
negative edge of the ALE (address latch enable) data will be expected or output.
The instructions (CODE) that the 8051 is supposed to execute, will be read
from the internal ROM or optionally from an external program memory. A read-in
of CODE occurs twice every instruction cycle. The instructions are likewise
placed as numeric values in the size of a BYTES. Therefore a maximum of 256
different instructions are possible. Every number is assigned a definite
instruction: so means e.g. the CODE 0A4h --> multiplies the Register
ACC with the Register B (for the complete instruction set see chapter 4).
The next diagram shows an example of an external wiring of the 8051 with an
external RAM and an EPROM. Since Port 0, during the first half of an access,
first guides the low address and then for the second half switch
(multiplexer) to data, the low address must be stored in a latch. (There are
however RAMs, which have multiplexed inputs such as the DPR SAE 81C80 from
Siemens, which does not need a latch).
8051 RAM
++ ++
| _WR+>|_WR |
| _RD+>|_RD |
| | high Adr. | |
| P2+===========================+======>| |<======+
| | |A | | |
| | LATCH |D | | D|
| | ++ |D ++ A|
| ALE+>|74 LS| |R EPROM T|
| | | 373 |low Adr. |E ++ A|
| P0|<===+====>| +==========+S | | |
| | | | | |S | | |
| | | ++ | | |<======+
| | | +======>| | |
| | | _PSEN | | |
| _PSEN+|>|_RD | |
++ | ++ |
| external DATA |
+===============================================+
The 8051 runs on +5V. Most of the connections are through the ports. These
port pins can be directly accessed from software. In addition they have extra
hardware functions (indicated above in brackets). Only the RESET pin, the
external/internal code fetch toggle (_EA), the quartz input, the oscillator
output for the address latch as well as the external program memory read
signal cannot be accessed by software.
Summary:
Port2: _PSEN:
8-Bit input/output. Alternatively Program Store Enable (read signal)
serves as the output of the high during access of external code.
address when accessing external
memory.
reset:
Port3: must be set high for at least 2
8-Bit input/output. Alternately cycles, then Reset.
P3.0 and P3.1 is also input
/output for serial port, P3.2 _EA:
and P3.3 are also interrupt in- External Access: when low, all code
puts, P3.4 and P3.5 as timer will be read externally.
gates, P3.6 and P3.7 as write/read When high, code from 0000...0FFFh
signals when accessing external data. will be internally read, upper
addresses externally.
The logic in the 8051 operates on a clock. The oscillator is derived from a
quartz crystal. The maximum usable frequency is 12 MHz (there are already
newer CPUs with 16 MHz). The quartz crystal must oscillate as suggested.
Additionally, an inverting amplifier must be connected in the oscillator
circuit. This driver is integrated into the 8051. In order that the crystal
does not oscillate at higher frequencies, two small capacitors are connected
to it.
Warning!: 8051 computers use HMOS and CMOS technologies. The CMOS
implementation contains a somewhat differently wired quartz driver.
In an external wiring with a crystal (as shown above) nothing is
changed. However when using an external oscillator clock and this clock
(TTL Level) feeds it, a separate wiring is needed as shown below:
With HMOS execution the external With CMOS execution the external
oscillator
crystal is fed to pin XTAL2. To is fed to XTAL1. The output XTAL2 of the
deactivate the internal crystal internal crystal driver remains open.
drivers connect XTAL1 to GND.
++ ++
| ++ | external oscillator | ++ | external oscillator
| +||+ ++ | +||+ ++
| ++ | | | ++ | |
++ | ++ |
+ GND | |
| | ----o XTAL1----o XTAL2---
--------o XTAL1----o XTAL2--- | |
| | | | \ |
HMOS | | CMOS ++ \+
| | \ | | | /
++ \+> internal |
| / oscillator +> internal oscillator
The oscillations that come from XTAL2 or XTAL1 or the crystal driver is
divided into 2. After that a symmetrical oscillation of maximum 6 MHz is
available. This clock will be used as the 8051 system clock: that is, all
logic inside the 8051 will change state based on this clock.
The execution of an instruction will need at least 6 system clocks
(State 1 to 6 = 1 cycle).
++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++++ ++ +
XTAL + ++ ++ ++ ++ ++ ++ ++ ++ ++ ++++ ++ ++ ++
+ ++ ++ ++ ++ ++ ++ +
SysClk ++ ++ ++ ++ ++ ++ ++
State | 1 | 2 | 3 | 4 | 5 | 6 | 1
^ ^ ^
1.Code-Fetch 2.Code-Fetch
For every cycle the CPU always allows for 2 bytes from the program memory
(code fetch). This happens automatically at state 1 and state 4 (see also
timing for external code access):
--- Example:
With a NOP a code byte too many will be read. It will not be evaluated,
rather re-read in the next cycle. With INC DPTR in first cycle 1 code byte
too many will be read. In the 2nd cycle the code byte of the next instruction
will again be read but also not be evaluated yet.
With an external data memory access (MOVX) there is a special feature. Here
in the 2nd cycle, no code fetch takes place, rather the external RAM will be
accessed.
Accessing a table in program memory through MOVC in the 1st cycle, a byte of
the next instruction will be read along already (too much). In the 2nd cycle,
the access to the table takes place first and then yet another code fetch,
which will however be rejected.
An LJMP instruction needs 3 code bytes and 2 cycles. In the 1st cycle the
opcode and the high byte of the target address (1st operand) will be read. In
the 2nd cycle the low byte of the target address (2nd operand) and (too much)
the opcode of the next code byte will be read. After carrying out the jump,
the program counter will be at the new address. The next code fetch takes
place there. From here is also apparent, why the over-read instructions in
the previous cycles are repeatedly rejected.
The execution of instructions takes place mainly in state 5. There the code
address for the code fetch of the next cycle will again also be already
set.
Ports: Timer:
P0 DATA 80h TCON DATA 88h
P1 DATA 90h TMOD DATA 89h
P2 DATA 0A0h TL0 DATA 8Ah
P3 DATA 0B0h TL1 DATA 8Bh
TH0 DATA 8Ch
Arithmetic registers and CPU control: TH1 DATA 8Dh
PSW DATA 0D0h
ACC DATA 0E0h Interrupt control:
B DATA 0F0h IE DATA 0A8h
PCON DATA 87h IP DATA 0B8h
Some SFR are bit addressable (always those whose DATA Addresses
end with 0 or 8). For an 8051 these are:
PSW - Bit's: IE - Bit's IP - Bit's
CY BIT 0D7h EA BIT 0AFh PS BIT 0BCh
AC BIT 0D6h ES BIT 0ACh PT1 BIT 0BBh
F0 BIT 0D5h ET1 BIT 0ABh PX1 BIT 0BAh
RS1 BIT 0D4h EX1 BIT 0AAh PT0 BIT 0B9h
RS0 BIT 0D3h ET0 BIT 0A9h PX0 BIT 0B8h
OV BIT 0D2h EX0 BIT 0A8h
P BIT 0D0h SCON - Bit's
P3 - Bit's SM0 BIT 9Fh
TCON - Bit's RD BIT 0B7h SM1 BIT 9Eh
TF1 BIT 8Fh WR BIT 0B6h SM2 BIT 9Dh
TR1 BIT 8Eh T1 BIT 0B5h REN BIT 9Ch
TF0 BIT 8Dh T0 BIT 0B4h TB8 BIT 9Bh
TR0 BIT 8Ch INT1 BIT 0B3h RB8 BIT 9Ah
IE1 BIT 8Bh INT0 BIT 0B2h TI BIT 99h
IT1 BIT 8Ah TXD BIT 0B1h RI BIT 98h
IE0 BIT 89h RXD BIT 0B0h
IT0 BIT 88h
The SFRs are accessed directly. The have in code stand the HEX address of the
SFR as operand. Example for an SFR access:
When accessing the ACC, special (shorter and faster) instructions are
available. To make the distinction, the ASM51 mnemonic abbreviation "A" is
written instead of "ACC". For the 8051 different opcodes are generated.
Example:
MOV ACC, #44h Hex code: 75 E0 44 2 cycles
or MOV A, #44h Hex code: 74 44 1 cycle
3.4.2 B-Register:
------------------
The B-Register (DATA F0h) serves as a general purpose register. It can be used
as intermediate storage for values. In multiplication and division it is used
as an assisting calculation register. It can be treated bitwise:
The PSW (DATA D0h) holds the most important flags of the CPU as well as two
bits for register bank switching (see 3.9 internal RAM). The flags are
important for computation operations and comparisons:
+++++++++
| CY | AC | F0 | RS1 | RS0 | OV |PSW.1| P | PSW (D0h)
+++++++++
++ ++
| PSW.1 | and | F0 | have no hardware function. They can be used by the
++ ++ user for any purpose. PSW.5 is predefined by
ASM51 as F0 (Flag 0). PSW.1 and PSW.5 can be used as
intermediate storage for any bit value.
++
| P | is the parity flag. It always reflects the status of the ACC.
++ If the sum of all bits in the ACC is even (even Parity), then P = 0.
If the sum is odd (odd Parity), then P = 1. The parity flag cannot
be written by software (only read).
++
| OV | is the overflow flag (overflow flag). It is set, after a
++ multiplication, when the result is larger than a byte (then it
no longer fits in the ACC alone). It will be set when division by 0
occurs. In addition, it will be set during additions and subtractions when
there is a sign error.
In computations with signed numbers, the highest bit represents the sign
(example: 31 = 1Fh, -31 = 9Fh). So only numbers from -128 to +127 are
represented. Now when a positive number is added to a positive number, the
result must (again) be positive. A negative number minus a negative number
must (again) be negative. With an overflow, this is not the case:
++
| AC | is the auxiliary carry flag (auxiliary overflow flag): it will be set
++ when there is a carry over from bit 3 during addition and subtraction.
++
| CY | is the carry flag (carry flag): it will be set on a carry from
++ bit 7 during addition, subtraction, by decimal adjust and on
rotate left through carry. Besides it will be set on a carry
from bit 0 by rotate right through carry and when the left operand is less
than the right during "Compare and Jump if Not Equal".
As in the ACC, there are special instructions also for accessing the carry
flag. These are characterised by the use of the mnemonic "C" which stands for
"CY".
++ ++
| RS1 | and | RS0 | serve to select the register bank. These bits are
++ ++ set by software (only read through the CPU).
The CPU sets up a stack memory in internal RAM. The CPU will place the
return address in the stack with every subprogram call (LCALL or ACALL).
The same takes place automatically through the CPU when entering an interrupt
routine. A RET or RETI will fetch the return addresses from the stack. An
application program can access stack memory with the PUSH and POP
instructions.
The SP (DATA 81h) always shows the final pushed value. To store a byte
the SP will first increment and then the value will be written into internal
RAM address, where the SP points to. Return addresses are of type WORD, with
which the CPU will first put the low byte then the high byte to the stack.
The stack grows from the bottom to the top. After a reset the SP shows the
internal RAM address 07h. The first pushed byte will be written in 08h
(with that reg bank 0 will not be overwritten by the stack). An application
program should soon after a reset load the SP with the program's last used
internal RAM address, so that the stack will be placed above the program's
data.
Warning! The 8051 internal RAM ends at address 7Fh. Should the SP show upper
address values, an additional PUSH will increment it- with a POP garbage
comes back!
The data pointer (DPTR) is the sole 16 bit register of the 8051, which can
also be loaded for 16 bit access. It is put together from the Special Function
Registers DPH = data pointer high (DATA 83h) and DPL = data pointer low (DATA
82h). The data pointer serves to access external RAM (see MOVX), tables in
code memory (see MOVC) and for indirect jumps (see JMP).
3.4.6 PCON (Power-Control):
----------------------------
The PCON register (DATA 87h) hold further CPU flags. The power control
flags exist only in the CMOS versions (80C51). The most significant
bit (SMOD) belongs to the setting of the baud rate of the serial port
(see serial port). PCON is not addressable bitwise.
+++++++++
|SMOD | - | - | - | GF1 | GF0 | PD | IDL | PCON (87h)
+++++++++
++
| SMOD | determines the gain factor for the driving oscillator of the serial
++ port. For serial modes 1, 2 or 3 the baud rate (Shift Time) will be
divided by 2, when SMOD = 0.
++ ++
| GF1 | and | GF0 | have no hardware functions. They cannot be manipulated by
++ ++ BIT Addresses (set and clear only with ORL respectively
PCON). They can be used from the program for any purpose.
A proposed use is for indicating whether idle mode has been activated.
++
| IDL | switches the CPU into idle mode (only CMOS!). When IDL = 1 is set
++ (with OR PCON, #1), the next instruction will not be executed.
The 8051 goes into IDLE mode. This means:
instruction following the idle mode activation. GF1 or GF0 may serve to
indicate idle mode was set, by the code for the interrupt routine (IDL will
be cleared by hardware).
If a reset occurs during the idle mode, then the IDL in PCON will be cleared
immediately. The execution of instructions continues with subsequent
instructions --> until two cycles later the reset logic starts up and all SFRs
as well as the program counter are set back. Execution starts again at the
reset address 0000.
First of all the two or three instructions following the idle mode will be
executed. The correct execution of the instruction in this condition is not
guaranteed. Accessing the ports or external data memory should therefore
be avoided after idle mode activation (possibly insert 3 NOP's).
Access to the internal RAM is not a problem, for the hardware prevents access
to the internal RAM at this time.
After leaving the idle mode upon reset, all SFRs are set back. Also GF1 and
GF0 are cleared. But the internal and external RAM are unchanged.
++
| PD | switches the CPU into power down mode (only CMOS!). If PD = 1
++ is set (via OR PCON, #2), then the next instruction will not be
executed. The 8051 goes into POWER DOWN Mode. This means:
The power down mode can only be exited through a reset. This reset will then
reset all SFRs (also ports to 0FFh), but not the internal and external RAM.
During the power down state the supply voltage can be reduced to 2V without
the contents of the internal RAMs being lost.
To do this successfully, first PD is activated, and the voltage must go back
to 5V, before the reset is released.
3.5 Ports:
-----------
In order that the ports may be used for input and output, they have a special
driver:
| /|read pin
| +< ++
| | \| |
i| | |
n| | /|read latch +5V o |
t|\<+< ++ | |
e| \| | |++ |
r| | ++++ |
n| | | | | Pin0.x
a| ++ | | ++o
l|\>+D Q++ switch | |
| | | ++ | |++
D| | /Q++ _\ ++++
a| ++ +++ | |
t| Latch | | +
a| | |
| Address/Data output
If code is executed from the internal ROM only and no external data memory
is used (no MOVX instruction), then the alternative functions of port 0 and
port 2 will never be activated. Port 0 and Port 2 can then be dedicated to
any function (in/output pins). At Port 0, pull up resistances will then be
required.
If port 0 and port 2 are used for code fetches, then the other functions
will mostly be shut out. Then the hardware automatically clears the contents
of the port 0 latches with every external data access.
With MOVX @Ri instructions, port 2 will not be controlled by the hardware
with high addresses.
The upper transistor will only take control, when a logic 1 for the alternate
function happens to be placed on the pin. If there is a 1 in the latch, and
no value is given for the alternate function, then the output at Port 0 is at
high impedance (floating). At port 2, the integrated pull up takes effect. In
both cases, the pin can be used as input. If there is a 0 in the latch, then
the lower transistor will be activated, and the pin will always be low (not
available as input).
An example:
CPL P2.1 is a read-modify-write instruction: The value in the latch from pin
P2.1 is to be changed. If there is a 0 inside, then a 1 is to be written in
and vice versa.
If read from a pin, a mistake can o | <--- Load
occur when the circuit is switched on | | +o
as in the example given here. +++ | |
The base of the NPN transistor is | | | |
driven directly from the pin - made +++ P2.1 /
possible by the internal pull up. +o+< NPN
With the latch set, the voltage at | | \E
the pin will only be 0.6 V. From the |++ | |
pin, a low level will be read back, +++ | |
even though there is a 1 in the latch. | | +
The CPL instruction will be wrongly + |
executed.
to the Distinction:
JB (jump, when bit is set) is not a read modify write instruction, since
the contents of the latch here is not used. Here the pin will be read.
| /|Pin read
| +< ++
| | \| |
i| | |
n| | /|Latch read +5V o |
t|\<+< ++ | |
e| \| | +++ |
r| | | | |
n| ++ | +++ | Pin 3.x
a|\>+D Q++ NAND ++o
l| | | | ++ |++ | Pin 1.x
| | /Q| ++ & +o+++ |
D| ++ ++ | | |
A| Latch | ++ + |
T| | |
A| | /| |
| | +< ++
| | \|
alternative: |
output input
In regard to the "normal" access to a port address (SFR) through the software
there is no difference from port P0 (see Comments about P0).
P1 and P3 possess integral pull up resistors. The upper transistor drawn into
the diagram for P0 does not exist. However there is a special feature: On the
chip of the 8051 the pull up resistors are not achieved through permanently
fixed resistors rather through special field effect transistors. If a pin is
switched from 0 to 1, port P1 and P3 will, for the duration of 2 crystal
clocks (= 1/6 cycle) switch the resistance of the
FET, so that a low impedance is imposed (at port 2 this will be for the entire
period of the alternate function of the high address output). Thereby, faster
settling time of the output signal of a pin is achieved, and yet the pull up
is of sufficiently high impedance so that the pin can be used for input.
With port P3, no alternative output takes place, so the input of the NAND
gate is high. For this case the logic condition of the pins depends on the
latch.
Should the pin be used for input, in the same way as with P0, #0FFh is written
to the port latches from P1 or P3. However this must take place if alternate
output functions are to be used, since contrary to Port P0 and P2, no switch
exists with P1 and P3.
Port 0: Address (low Byte) and data for external memory access.
Port 2: Address (high Byte) for external memory access.
Important: with all port pins there must be a 1 in the latch, when the pin
is to be used for input!
The _EA-Pin serves to turn off the internal ROM. _EA cannot be read like the
port pins from the software.
The 8031 has no internal ROM. Here all instructions are read externally.
It is nonetheless recommended that the _EA pin (since it is available with
the 8031 as it has a similar package construction) be put to GND. In the case
of the 8344 (the ROMless version of the 8044) I was already able to note that
it does not function correctly when _EA = 1. It seemed as though it had an
internal ROM. Perhaps it is more convenient for the manufacturer to make one
mask and then print the chips separately.
3.6 external program memory Page 21
_PSEN (Program Store Enable serves as a read signal for the external program
memory. Similarly, the _PSEN cannot be accessed via software.
The code fetch access begins already in state 5 of the previous cycle with the
application of address. In the middle of state 5 the ALE goes low. For
external access ALE serves as a signal for the address latch to take over the
low address from P0 to store them (see 3.1 ff).
With the end of state 5 the _PSEN goes low and simultaneously P0 goes high
impedance (as input: indicated with a ---). The associated EPROM now places
data to the external data bus (as long as _PSEN = 0).
From the start of state 1 the P0 in 8051 expects data (indicated by the ???,
in addition high impedance). These lie already at the output from the EPROM.
In the middle of state 1 the data will be transferred over P0 to the internal
data bus and _PSEN and ALE will both again be high.
Towards the end of state 1 the (incremented) address for the next code fetch
will be placed.
The above timing diagram shows the ideal execution of both code fetch accesses
in a cycle. Actually all transfers are affected by the switching times.
For correct functioning of the 8051 the observance of the following
timings are required :
- at the latest tPLIV after _PSEN is low, the program memory must put valid
data to the bus.
- at the latest tAVIV after valid addresses from the 8051 have been given,
the program memory must put valid data to the bus.
The time value depends on the 8051 type (manufacturer). Exact values are to
be taken from the data sheet. Typical valid values (with T = 1/fOscillation):
For program memory often EPROMs and PROMs are used. A typical EPROM indicated
as 27256-250 means that the EPROM has 256 kBIT (= 32 kByte) in terms of
memory cells with an address access time of 250 ns.
Indicated is the address access time: the time from the issue of the address
until valid data is put on the output. The chip enable access time is about
the same (time of application of an active chip select signal until valid
data). The output enable access time (time of application of an active _PSEN
until valid data) is significantly smaller (range in the order of 20 ns to 60
ns). The critical time for the use as program memory of 8051 is the chip
access time (= address access time). If these requirements are fulfilled,
then the other times are also ok.
With a CPU crystal of 12 MHz, one is always safe with EPROMs with 200 ns. For
many uses, 250 ns is enough.
With a MOVC instruction, the read access takes place at the program memory
through the software in the second cycle instead of the 1st code fetch.
The access of the external data memory takes place in the second cycle of the
execution of the MOVX instruction (see 3.3 instruction execution). That's why
in this cycle there is no code fetch (the only the case where ALE and _PSEN
do not appear). An XDATA access lasts 6 system clocks (twice as long as a
CODE access).
_RD (Port P3.7 pin) serves as a read signal for the external data memory.
_WR (Port P3.6 pin) server as a write signal.
When MOVX instructions are used, these bits in Port 3 - Latch are to be
set (this occurs after a reset).
XDATA read:
------------
The XDATA address will already be placed at state 5 of the first cycle of the
MOVX instruction. With MOVX A,@DPTR: at P2 the high address (DPH) and at P0
the low address (DPL). With MOVX A,@Ri: the value from the latch (SFR of P2)
remains at P2 and the address (from Ri) will be placed at P0.
In the middle of state 5 ALE will go low. Consequently, the addresses from P0
can be transferred into an (external) address latch (see 3.1 ff). About the
end of State 6, P0 will go to high impedance (address taken), following which
_RD becomes low. A connected RAM now lays data on the external data bus
(_RD = 0).
From the beginning of state 3, the 8051 expects data at P0 (indicated by the
???). In the middle of state 3, the data will be moved over onto the internal
data bus, following which _RD and ALE will again be high.
Towards the end of state 4 the address for the code fetch of the next cycle
will be placed if a code fetch takes place externally (if internally, then P0
and P2 remain at high impedance - see 3.6 and 3.3).
An access of the external data memory lasts longer than a code fetch. The ALE
is once not given out. For the access longer times are available:
- at the latest tRLDV after _RD becomes low, the external data memory must
place valid data on the bus.
- at the latest tAVDV after valid addresses from the 8051 have been given,
valid data from the external data memory must be put on the bus.
XDATA write:
------------
The XDATA address will already be placed at state 5 of the first cycle of the
MOVX instruction. With MOVX A,@DPTR: at P2 the high address (DPH) and at P0
the low address (DPL). With MOVX A,@Ri: at P2 the value from the latch
(SFR of P2) and at P0 the address (from Ri).
In the middle of state 5 ALE will go low. Consequently, the addresses from P0
can be transferred into an (external) address latch.
Towards the end of state 6 at P0, the address will be taken away and data be
placed, thereafter the _WR goes low.
At the end of state 3 _WR will again be high. With it the external data
memory takes over the data from P0. Thereafter, P0 will again be at high
impedance and ALE again high.
At the end of state 4 the address for the code fetch of the next cycle will
be placed if the code fetch takes place externally (if internally, then P0
and P2 remains at high impedance - see 3.6 and 3.3).
- The 8051 guarantees, that at least tQVWH before _WR is high, valid
data lies on the bus.
- the 8051 guarantees, that at least tWLWH + tAVWL before _WR is high,
a valid address has been given out.
For the data memory there exists therefore the requirements of address access
time of 585 ns for reading and 496 ns for writing (at 12 MHz CPU clock). Most
of the types of the series 6264 ... 62256 (static RAMs) have address access
time under 150 ns, so that here no problem should occur.
++
+_WR>| | ++
| +_RD>| RAM |<o|INV|+
| | | | ++ |
| | +========>| 32kx8 |<===+ |
8051 CPU | | | ++ | |
++ | | | ++ | |
| _WR++|_WR>| | | |
| _RD++|_RD>| RAM |<|+ A15
| | | | | |
| P2+=======ADDRESSBUS==+========>| 32kx8 |<===+
| | ++ | ++ |
| ALE+>|LATCH| | ++ |
| P0|<===+====>| +==+ | | |
| | | ++ +========>| EPROM |<===+
| | | | | |
| _PSEN+|PSEN>| 64kx8 |<+ |
++ | ++ + |
+===DATABUS===========================+
Since static RAMs are available only up to 32kx8, two RAM chips are needed.
The 1st RAM receives as chip select the address A15 and the second RAM the
inverted value of A15. As CODE memory, an EPROM 64kx8 is used. Its chip
enable pin can be put at GND (always selected).
In hardly any circuitry will such a large external data memory be needed.
Therefore most use only one RAM chip. Often a yet smaller RAM is used
(cheaper). Recommended are the types 6264 (8kx8), 62128 (16kx8) and 62256
(32kx8). Smaller types than 8kx8 can admittedly be used, however they are
more expensive.
If only a RAM is inserted in XDATA space, then its chip enable pin can be
placed at GND (always selected). The same memory cell in the RAMs can be
accessed over different XDATA addresses (multiply selected). This is not a
problem when the software is ok.
The XDATA region, besides being used for external data memory, can be used
for other purposes. Some examples:
- for the reading of jumper settings or other data through a tristate driver.
- as universal port (8 bit input/output) over bidirectional tristate
drivers (74LS245).
- for connecting special components such as to external analog/digital
converters, to a SCC (serial communication controller) for a second serial
port, to a display module and others.
- as ports to other processors through 8 bit register (realised by the Intel
8044 BITBUS processor card) or over DPR (Dual Port RAM).
(-- see example in Appendix)
When using an EPROM 16kx8 and a RAM 32kx8 the following setups can, for
example, be made:
| | |
_PSEN + _PSEN ++_OE |
| | | EPROM |
P2.6 + A14 ++_CE 16kx8|
| | | ++ ++
| | ++ | ++
| | ++ | NOR ++ | ++
8051 | ++ INV ++ | | NOR ++_OE |
CPU | ++ ++ ++ | | |
| ++ | ++ | |
P3.7 + _RD + INV ++ ++_CE |
| ++ + | RAM |
| | 32kx8|
P3.6 + _WR +_WR |
| | |
An especially simple realisation of the code execution from the external RAM
(load driver code) can be achieved, when the load routines are burnt into
internal EPROM and _EA = 1 is set. Now _PSEN and _RD need only be connected
over an AND gate. An external EPROM is no longer required.
| o +5V
_EA ++ | |
| ++ ++_CE |
_PSEN + _PSEN + | + | |
| | AND ++_OE |
P3.7 + _RD + | | RAM |
| ++ | 32kx8|
P3.6 + _WR +_WR |
| | |
A special feature of the 8051 is its internal RAM of 128 bytes. The access is
faster compared to access over the external RAM and there are different
access methods. With simple programs the 8051 can (in combination with the
internal EPROM) act as a stand alone processor.
Register:
---------
The 8051 has 8 general purpose registers defined. These are not counted as
SFRs (special function registers), since they do not have special hardware
functions such as possessed by TCON, SCON and other SFRs.
The registers are differentiated by the fact that there are special (simple
and fast) instructions for accessing them. They are not like the SFRs, placed
in extra memory cells, rather their values are stored in internal RAM.
This takes place in 8 sequential internal RAM addresses = one register bank.
The active register bank can be selected through two BITs in the program
status word (see 3.4.3 PSW).
Since by a reset only some SFRs, but not the internal RAM are cleared, the
contents of the register during a reset remain provided reg bank 0 was
selected. With a reset, the register bank is switched back to reg bank 0.
R0 and R1 still have, besides the possibility of general purpose use through
the software, the function of index registers in indirect addressing in
internal RAM and pagewise access of the external RAM (see instruction
set).
indirect addressing:
--------------------
In addition, one can have access to the internal RAM indirectly through
pointers. The registers R0 and R1 or the stack pointer (SP) can serve as
pointers. For RAM addresses above 80h (only 8052...) the only possibility
of accessing internal RAM is indirect addressing.
When using R0 or R1 to access internal RAM the address of the memory location
must first be loaded into R0 or R1. The access takes place with a further
instruction:
Example:
MOV R0, #30h ; internal RAM address 30h is loaded
MOV @R0, #0Dh ; #0Dh is written in internal RAM 30h
An indirect access must seem complicated, but there are applications, where
indirect addressing is indispensable (e.g. with the handling of text strings,
with receive and send buffers...).
direct addressing:
------------------
Direct addressing permits quick access of data. From the programmer's
viewpoint, this allows for the storage of reserved values at prearranged
fixed addresses in internal RAM memory location. The assembler permits the
definition of variable names.
In the internal RAM of the 8051 (and also the enhanced CPU's 8052, 80535,...)
only 128 bytes can be directly addressed (addresses 00...7Fh). With the
linking of a program the address is put into the code.
Example:
Mnemonic: Opcode:
MOV A, 20h ; content of int RAM 20h in ACC --> E5 20
MOV A, #20h ; the difference: here value in ACC --> 74 20
MSB LSB
+++++++++
| 7F | 7E | 7D | 7C | 7B | 7A | 79 | 78 | DATA 2Fh
| 77 | 76 | 75 | 74 | 73 | 72 | 71 | 70 | DATA 2Eh
| 6F | 6E | 6D | 6C | 6B | 6A | 69 | 68 | DATA 2Dh
| 67 | 66 | 65 | 64 | 63 | 62 | 61 | 60 | DATA 2Ch
| 5F | 5E | 5D | 5C | 5B | 5A | 59 | 58 | DATA 2Bh
| 57 | 56 | 55 | 54 | 53 | 52 | 51 | 50 | DATA 2Ah
| 4F | 4E | 4D | 4C | 4B | 4A | 49 | 48 | DATA 29h
| 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 | DATA 28h
| 3F | 3E | 3D | 3C | 3B | 3A | 39 | 38 | DATA 27h
| 37 | 36 | 35 | 34 | 33 | 32 | 31 | 30 | DATA 26h
| 2F | 2E | 2D | 2C | 2B | 2A | 29 | 28 | DATA 25h
| 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | DATA 24h
| 1F | 1E | 1D | 1C | 1B | 1A | 19 | 18 | DATA 23h
| 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | DATA 22h
| 0F | 0E | 0D | 0C | 0B | 0A | 09 | 08 | DATA 21h
| 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 | DATA 20h
+++++++++
The LSB of DATA 20h has the BIT address 00. The next bit of DATA 20h
has BIT address 01 and so on:
The BIT addresses 80h...FFh are reserved for Special Function Registers, even
when not all SFR addresses are used in the 8051. The SFR are always bitwise
addressable, by which the low nybble is address 0 or 8:
+++++++++ 8051-SFB
B | F7 | F6 | F5 | F4 | F3 | F2 | F1 | F0 | DATA F0h --------
+++++++++
ACC | E7 | E6 | E5 | E4 | E3 | E2 | E1 | E0 | DATA E0h
+++++++++
PSW | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 | DATA D0h
+++++++++
IP | BF | BE | BD | BC | BB | BA | B9 | B8 | DATA B8h
P3 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 | DATA B0h
+++++++++
IE | AF | AE | AD | AC | AB | AA | A9 | A8 | DATA A8h
P2 | A7 | A6 | A5 | A4 | A3 | A2 | A1 | A0 | DATA A0h
+++++++++
SCON | 9F | 9E | 9D | 9C | 9B | 9A | 99 | 98 | DATA 98h
P1 | 97 | 96 | 95 | 94 | 93 | 92 | 91 | 90 | DATA 90h
+++++++++
TCON | 8F | 8E | 8D | 8C | 8B | 8A | 89 | 88 | DATA 88h
P0 | 87 | 86 | 85 | 84 | 83 | 82 | 81 | 80 | DATA 80h
+++++++++
- "fill" internal RAM beginning with the lowest address. That is:
*** first consider the registers: (in the case of ASM51 and A51 with USING
0, USING 1, etc.) reserve locations, according to the number of the
used register banks.
*** then define flags (since these can only be accommodated in DATA
(20h...2Fh)
*** then define direct DATA variables (if there is space, individually
assigned before the flags).
*** then allot locations for use of buffer (IDATA), which are to be
addressed indirectly
the assignment of addresses is performed here "by hand". The ASM51 also offers
the possibility to let this be done through the linker (see assembler).
All controllers of the 8051 family use the same instruction set. With the
newer (further developed) types, only some SFRs are used to control the
additional hardware functions.
++
MOV A, Rn | 1110 1nnn | 1 P
++
MOV Rn, A | 1111 1nnn | 1 -
+++
MOV A, direct | E5h | dataAdr | 1 P
+++
MOV direct, A | F5h | dataAdr | 1 -
+++
MOV A, @Ri | 1110 011i | 1 P
++
MOV @Ri, A | 1111 011i | 1 -
+++
MOV A, #data | 74h | data | 1 P
+++
MOV Rn, #data | 0111 1nnn | data | 1 -
+++
MOV @Ri, #data | 0111 011i | data | 1 -
+++
MOV Rn, direct | 1010 1nnn | dataAdr | 2 -
+++
MOV direct, Rn | 1000 1nnn | dataAdr | 2 -
+++
MOV @Ri, direct | 1010 011i | dataAdr | 2 -
+++
+++
MOV direct, @Ri | 1000 011i | dataAdr | 2 -
++++
MOV direct, #data | 75h | dataAdr | data | 2 -
++++
MOV direct1, direct2 | 85h | dataAdr2 | dataAdr1 | 2 -
++++
What is worth noting is that in the hex code for "MOV direct1, direct2",
the sequence in the hexcode across that in the mnemonic is interchanged.
Example: MOV A, R5 ; ED ->write contents of R5 to ACC
MOV PSW, A ; F5 D0 ->write contents of A to PSW
MOV A, @R1 ; E7 ->write contents of internal RAM,
; address which is in R1, to ACC
MOV R2, #22h ; 7A 22 ->write 22h to R2
MOV R7, 22h ; AF 22 ->write contents of internal RAM
; address 22h to R7
MOV 30h, SP ; 85 81 30 ->write SP to internal RAM addr 30h
With the XCH instruction, data will be exchanged between internal register
or internal RAM and the accumulator.
++
XCH A, Rn | 1100 1nnn | 1 P
+++
XCH A, direct | C5h | dataAdr | 1 P
+++
XCH A, @Ri | 1100 011i | 1 P
++
XCHD interchanges the low nybble of the addressed byte in internal RAM with
the low nybble from the ACC.
++
XCHD A, @Ri | 1101 011i | 1 P
++
++++
MOV DPTR, #data16 | 90h | data16(high) | data16(low) | 2 -
++++
This is the only 16 bit operation of the 8051. It has the same
function as:
MOV DPH, #HIGH(data16)
and MOV DPL, #LOW(data16)
++
MOVX A, @DPTR | E0h | 2 P
++
MOVX @DPTR, A | F0h | 2 -
++
MOVX A, @Ri | 1110 001i | 2 P
++
MOVX @Ri, A | 1111 001i | 2 -
++
When accessing external data memory the _RD becomes the read signal
and _WR becomes the write signal. The access takes place through the
alternative functions of Port 0 and Port 2 (see 3.5). The value in the
latch of Port 2 remains the same. The value in the latch of Port 0
will be overwritten during reading with 0FFH by the hardware.
IMPORTANT!
When indirectly accessing with Ri, only the low address multiplexed
with Port 0 is given out. The content of the latch of P2 remains during
the access at the port output. The user has to write the high byte of
the address into P2 before access when using external RAM.
Example:
MOV DPTR, #0FF00h ; load data pointer
MOVX A, @DPTR ; access (read) external RAM
++
MOVC A, @A+DPTR | 93h | 2 P
++
MOVC A, @A+PC | 83h | 2 P
++
+++
PUSH direct | C0h | dataAdr | 2 -
+++
POP direct | D0h | dataAdr | 2 -
+++
Upon a PUSH the Stack Pointer is incremented by 1 and then the value
is written to this address. With a POP the SP is first read and then
decremented. The stack pointer always points to the final pushed byte.
+++
ADD A, #data | 24h | data | 1 C AC P OV
+++
ADD A, @Ri | 0010 011i | 1 C AC P OV
++
ADD A, Rn | 0010 1nnn | 1 C AC P OV
+++
ADD A, direct | 25h | dataAdr | 1 C AC P OV
+++
After the execution the ACC holds the result, the C Flag holds the
overflow in Bit 7 and AC holds the overflow from Bit 3. The OV flag is
set, when a sign error occurs (see 3.4.3 PSW).
+++
ADDC A, #data | 34h | data | 1 C AC P OV
+++
ADDC A, @Ri | 0011 011i | 1 C AC P OV
++
ADDC A, Rn | 0011 1nnn | 1 C AC P OV
+++
ADDC A, direct | 35h | dataAdr | 1 C AC P OV
+++
As addition without carry. However, the value of the Carry flag is added
as well.
+++
SUBB A, #data | 94h | data | 1 C AC P OV
+++
SUBB A, @Ri | 1001 011i | 1 C AC P OV
++
SUBB A, Rn | 1001 1nnn | 1 C AC P OV
+++
SUBB A, direct | 95h | dataAdr | 1 C AC P OV
+++
++
INC A | 04h | 1 P
++
INC Rn | 0000 1nnn | 1 no Flags
++
INC @Ri | 0000 011i | 1 no Flags
+++
INC direct | 05h | dataAdr | 1 no Flags
+++
INC DPTR | A3h | 2 no Flags
++
++
DEC A | 14h | 1 P
++
DEC Rn | 0001 1nnn | 1 no Flags
++
DEC @Ri | 0001 011i | 1 no Flags
+++
DEC direct | 15h | dataAdr | 1 no Flags
+++
++
MUL AB | A4h | 4 C P OV
++
The result is in ACC. When the result is a word, the low Byte is in ACC,
the high byte in B and the OV flag is set. The C Flag is always cleared
(C = 0).
++
DIV AB | 84h | 4 C P OV
++
++
DA A | D4h | 1 C P
++
When two BCD numbers are added together, the result must thereafter be
corrected, it should also come out in BCD format.
If the AC flag is set, or the low nybble > 9, then 6 is added. If the
C flag is set, or the high nybble > 9, then 60h is added. When there is
an overflow of bit 7 of the ACC in the result, then the carry flag (C=1)
is set.
+++
ANL A, #data | 54h | data | 1 P
+++
ANL A, @Ri | 0101 011i | 1 P
++
ANL A, Rn | 0101 1nnn | 1 P
+++
ANL A, direct | 55h | dataAdr | 1 P
+++
ANL direct, A | 52h | dataAdr | 1 no Flags
++++
ANL direct, #data | 53h | dataAdr | data | 2 no Flags
++++
+++
ORL A, #data | 44h | data | 1 P
+++
ORL A, @Ri | 0100 011i | 1 P
++
ORL A, Rn | 0100 1nnn | 1 P
+++
ORL A, direct | 45h | dataAdr | 1 P
+++
ORL direct, A | 42h | dataAdr | 1 no Flags
++++
ORL direct, #data | 43h | dataAdr | data | 2 no Flags
++++
+++
XRL A, #data | 64h | data | 1 P
+++
XRL A, @Ri | 0110 011i | 1 P
++
XRL A, Rn | 0110 1nnn | 1 P
+++
XRL A, direct | 65h | dataAdr | 1 P
+++
XRL direct, A | 62h | dataAdr | 1 no Flags
++++
XRL direct, #data | 63h | dataAdr | data | 2 no Flags
++++
++
CLR A | E4h | 1 P
++
++
CPL A | F4h | 1 P
++
++
RL A | 23h | 1 no Flags
++
++
RR A | 03h | 1 no Flags
++
++
RRC C | 13h | 1 P C
++
++
SWAP A | C4h | 1 no Flags
++
the low nybble of the ACC is interchanged with the high nybble
4.4 Bit-Manipulation:
----------------------
+++
MOV C, bit | A2h | bitAdr | 1 C
+++
MOV bit, C | 92h | bitAdr | 2 no Flags
+++
+++
ORL C, bit | 72h | bitAdr | 2 C
+++
ORL C, /bit | A0h | bitAdr | 2 C
+++
++
CLR C | C3h | 1 C
+++
CLR bit | C2h | bitAdr | 1 no Flags
+++
++
CPL C | B3h | 1 C
+++
CPL bit | B2h | bitAdr | 1 no Flags
+++
++
SETB C | D3h | 1 C
+++
SETB bit | D2h | bitAdr | 1 no Flags
+++
CALL codeAdress
JMP codeAdress
+++
ACALL codeAdr | A10 A9 A8 1 0001 | A7 A6 A5 A4 A3 A2 A1 A0 | 2 no Flags
+++
++++
LCALL codeAdr | 12h | CodAdrHigh | CodAdrLow | 2 no Flags
++++
Long CALL:
The PC (Program Counter) is incremented by 3 and the return address is
loaded on the stack (low byte first). Then it is loaded with the value
from the opcode.
++
RET | 22h | 2 no Flags
++
The return address is fetched from the stack (high byte first) and
jumped to.
+++
SJMP codeAdr | 80h | relOffset | 2 no Flags
+++
+++
AJMP codeAdr | A10 A9 A8 0 0001 | A7 A6 A5 A4 A3 A2 A1 A0 | 2 no Flags
+++
Long JUMP:
Jump to the address indicated in the opcode.
++
JMP @A+DPTR | 73h | 2 no Flags
++
+++
JZ codeAddress | 60h | relOffset | 2 no Flags
+++
JNZ codeAddress | 70h | relOffset | 2 no Flags
+++
+++
JC codeAddress | 40h | relOffset | 2 no Flags
+++
JNC codeAddress | 50h | relOffset | 2 no Flags
+++
jump, when C = 1 or C = 0
++++
JB bit, codeAdress | 20h | bitAdr | relOffset | 2 no Flags
++++
JNB bit, codeAdress | 30h | bitAdr | relOffset | 2 no Flags
++++
JBC bit, codeAdress | 10h | bitAdr | relOffset | 2 no Flags
++++
++++
CJNE A, direct,codeAdr | B5h | dataAdr | relOffset | 2 C
++++
CJNE A, #data, codeAdr | B4h | data | relOffset | 2 C
++++
CJNE Rn, #data,codeAdr | 1011 1nnn | data | relOffset | 2 C
++++
CJNE @Ri,#data,codeAdr | 1011 011i | data | relOffset | 2 C
++++
+++
DJNZ Rn, codeAdr | 1101 1nnn | relOffset | 2 no Flags
++++
DJNZ direct, codeAdr| D5h | dataAdr | relOffset | 2 no Flags
++++
++
NOP | 00h | 1 no Flags
++
no action. NOP is often used when a waiting time has to be passed through.
With a crystal of 12 MHz one NOP takes 1 s.
++
reserved | A5h | ? ?
++
invalid opcode. No instruction is assigned to this HEX code.
5. Assembler Page 49
5. Assembler:
~~~~~~~~~~~~~~
An assembler is used to translate a program written with mnemonic symbols
into executable HEX code. Every instruction is individually written in
source code. Some assemblers may offer the possibility of defining MACROs,
but even there every single instruction is defined in the source file. MACROs
act only as an abbreviation for instructions which often repeat themselves.
For the 8051 there are a number of commercial assemblers such as the ASM51
from Intel or the A51 from Keil. But there are also several shareware products
such as TASM, which are equally suitable. From the magazine Elektor, there is
an assembler course which is currently being carried out (start: 09.1991) and
at the same time, there is also a cost effective assembler EASM51. However
EASM51 generates HEX which is self defined. Most other assemblers generate HEX
code in Intel HEX format.
--- There is now also a public domain macro assembler (AS), from
Alfred Arnold, Prlat-Lewen-Str.3, 5206 Neukirchen 1
Request from: ftp.rog.rwth-aachen.de (directory pub/assembler)
5.1 ASM51:
~~~~~~~~~~~~~~~
The ASM51 and the A51 are largely compatible. The assembler itself generates
an object file. This file cannot be loaded into an EPROM or into an 8051
processor. The object file contains as yet no absolute addresses. In the
source code, defined segments (in the code region), being relocatable, can
still be moved around within the 64 kByte address location.
The absolute file still cannot be loaded into various Eproms. It still
requires a converter program from Absolute to HEX format (OH.EXE).
5. Assembler Page 50
Note: The absolute and the object file have the same structure: The
files consist of contiguous RECORDs. Every record starts with a byte,
which defines the record type, after that follows a word with the number
of bytes which follow in the record body. At the end another byte follows
with a checksum. The structure of the record body varies according to
the record type. Absolute and object files hold code in binary
form. Additionally it can contain variable names defined in the source
file.
Even when an object file contains code in binary form, it should not be
confused with a binary file. Binary files are used by simple assemblers
and eprom programmers. Binary files only contain code. The offset inside
the files is equal to the address.
Binary files such as those of short programs which begin at offset 0000
are very compact. With object files, the record type and the record
length are still placed at the beginning. With programs which contain
code with high addresses, the binary file becomes more substantial
quickly. Unused addresses which lie before that must be filled up with
0FFh.
The ASM recognizes all mnemonics, such as defined in chapter 4. The ASM51
also recognizes the default SFR and SFB definitions of the 8051. Therefore
the abbreviation PSW DATA 0D0h need not, for example be defined; PSW can be
directly used.
For bit addresses the predefined SFB name can likewise be brought in. The SFR
name of the (DATA-)byte, in which this bit is located, can also be used. The
the number of the bit can be indicated after a period.
Example:
The Auxiliary Carry flag (BIT 0D6h) is located SETB 0D6h
in the Program Status Word (DATA 0D0h). SETB AC
The following instructions lead to the same SETB PSW.6
result.
If code for another CPU is be generated, then the additional SFR and SFB must
be defined. Here an Include File is used in practice. With ASM51, include
files with the SFR definitions for other CPUs are included on the disk. The
Include files contain all the SFR definitions for the CPU, including those
which the ASM51 already recognizes by default (of the 8051). The ASM51 brings
in such a case an error message; for that reason the SFR definitions which are
known to be default will be turned off with the instruction $NOMOD51.
5. Assembler Page 51
$noMOD51 the SFRs for the 8051, which are otherwise known to be the
default, are turned off. $NOMOD51 must be placed at the beginning of
the source file.
$DEBUG the variable definitions are taken over as debug information into
the Object File. $DEBUG is placed at the beginning of the source files.
$DEBUG and $NOMOD51 can also be given at the command line (then without $).
With USING n (n = 0..3) the Register bank can be reserved. The Linker reserves
the appropriate addresses in the internal RAM. Default is set at USING 0.
A program can already be prepared with fixed addresses in the source code, or
written to be relocatable. These options may also be used in combination.
The addressing mode of the type DATA, IDATA, XDATA, BIT or CODE is
correspondingly assigned to the variables of the various memory regions or of
the code. A segment indication of the correct type is to be preassigned to
each variable definition or code procedure.
5. Assembler Page 52
At professional programmer sites, fixed segments are only used where the
hardware necessitates fixed addresses. For example, with jump addresses for
reset and the interrupts. But also for certain fixed addresses in XDATA
region (if a hardware extension is built under a certain definite address
there).
The additional "AT addr" may follow the above keywords. With that, the
beginning of the segment is set on a definite fixed address. Otherwise, the
ASM51 by default begins every segment with 0000.
A segment remains open until a new key word (from the above) is indicated. At
the beginning of the source files the assembler assumes a default CSEG AT 0000.
Naturally, code can only be entered in the CSEG. For that, the mnemonics,
which are explained in chapter 4 are used. In addition, there are the
following pseudo instructions to facilitate the inclusion of text, tables or
fixed values in the code:
Example:
In other segments (DSEG, ISEG, XSEG or BSEG) code mnemonics cannot be stored.
Here only variable names are defined. The location for every defined variable
is defined with a DS. In every segment type, variables are fixed by a label.
Every label has a : placed after it.
DS n reserve n Bytes
DBIT n reserve n Bit
5. Assembler Page 53
Example:
ISEG AT 30h
buffer: DS 20 ; 20 bytes reserved for buffer
stack: DS 12
For a very simple program the use of segments may be omitted. Then, the
variable names are defined with following directives (example):
This form is used also for the definition of SFRs for other CPUs other than
the 8051, in order to incorporate it with an INCLUDE.
Simple assemblers often recognize only the EQU assignment. There, variables
of type DATA, XDATA, IDATA and BIT are also defined with EQU. The ASM51 can do
this as well. However, error messages which are generated by ASM51 are
lost, if a variable with a wrong type is used in an instruction.
Simple assemblers also do not use the directive CSEG AT Addr, in order to
begin the assembling of a code address, rather they use ORG addr.
When creating a program, often only the following directives are needed:
END ; This key word must be placed at the end of the assembler
; source file.
5. Assembler Page 54
---------------------------------
With relocatable segment statements, first the segment names are defined in
the following form. Several identical segment types can also be defined.
segNameC SEGMENT CODE
segNameX SEGMENT XDATA
segNameD SEGMENT DATA
segNameI SEGMENT IDATA
segNameB SEGMENT BIT
The following keywords are then to be placed before the individual areas:
RSEG segNameC
RSEG segNameX
RSEG segNameD
RSEG segNameI
RSEG segNameB.
The entry of code mnemonics and variable definition takes place just as with
fixed segments.
The calling up of the assembler, linker and converter program occurs in the
form below. The statements in the <> are optional. Otherwise, as default,
the object file receives the extension .obj, the listfile .lst and the
absolute file receives no extension. The default setting of the size of the
internal RAM in the linker is 128 bytes.
5. Assembler Page 55
We are working only with CSEG. The code begins with the default setting of
CSEG AT Reset with address 0. The interrupt routine is fixed and situated at
the address 23h.
++
|TxFlag BIT 00 ; Bit0 in DATA 20h as flag |
|stack IDATA 21h ; address in internal RAM for stack |
| |
|;CSEG AT reset ; Code starts at default label Reset |
| AJMP init ; Jump to the initialisation |
| |
|CSEG AT SINT ; address 23h is the entry point for SINT|
| JNB RI, sintChkTx ; has something been received? |
| PUSH ACC |
| MOV A, SBUF ; read in |
| CLR RI ; receive buffer again ready |
| JB TxFlag, echoChar |
| JNB TI, $ ; wait until send buffer is free |
|echoChar: |
| MOV SBUF, A ; send back as echo |
| CLR TxFlag |
| CLR TI ; Mark that send buffer is full |
| POP ACC |
|sintChkTx: |
| JNB TI, sintRet ; when send buffer empty interrupts, then|
| SETB TxFlag ; flag is set, thereafter clear TI so |
| CLR TI ; serial interrupt is accessible again |
|sintRet: |
| RETI |
| |
|init: |
| MOV SP, #(stack - 1) ; stack placed |
| MOV IE, #90h ; enable SINT |
| MOV SCON, #50h ; serial port and timer 1 |
| MOV TH1, #0FDh ; initialise |
| MOV TMOD, #20h |
| SETB TR1 |
| JMP $ ; Waiting loop. Interrupt on receive |
| |
|END |
++
In the above program, the subsequent placement of the code in program
memory is already fixed during the assembly.
5. Assembler Page 56
++
|idataSeg SEGMENT IDATA ; Name for relocatable segments |
|dataSeg SEGMENT DATA ; are defined. |
|codeSeg SEGMENT CODE |
| |
| PUBLIC initRingBuf |
| PUBLIC putCharToBuf, getCharFromBuf |
| |
| USING 0 ; reserve Register bank 0 |
| |
|RSEG dataSeg ; |
| rdBufPtr: DS 1 ; Read pointer for ringbuffer |
| wrBufPtr: DS 1 ; Write pointer for ringbuffer |
| |
|RSEG idataSeg ; read in |
| ringBuf: DS 30 ; 30 Bytes for Ringbuffer |
| |
|RSEG codeSeg |
| initRingBuf: ; Initialisation routine |
| MOV rdBufPtr, #ringBuf ; read pointer on buffer start |
| MOV wrBufPtr, #ringBuf ; write pointer on buffer start |
| RET |
| |
| putCharToBuf: ; wrBufPtr shows the |
| XCH A, wrBufPtr ; location in buffer, |
| INC A ; where it will be written |
| CJNE A, #(ringBuf+30), noRingBufTop ; to next. |
| MOV A, #ringBuf |
| noRingBufTop: ; write without regard, |
| CJNE A, rdBufPtr, leaveRdBufPtr ; as to whether reading |
| INC rdBufPtr |
| CJNE rdBufPtr, #(ringBuf+30), leaveRdBufPtr |
| MOV rdBufPtr, #ringBuf ; has been completed. |
| leaveRdBufPtr: ; If a character is not |
| XCH A, wrBufPtr ; read in time it will be |
| MOV @R0, A ; lost. |
| RET |
| |
| getCharFromBuf: |
| MOV A, rdBufPtr ; when the read pointer is |
| CJNE A, wrBufPtr, getCharDo ; equal to the write pointer, |
| SETB C ; the buffer is empty. Then RET|
| RET ; with carry. |
| getCharDo: |
| MOV R0, A |
| MOV A, @R0 ; After reading, increment the |
| INC R0 ; read pointer. |
| CJNE R0, #(ringBuf+30), leaveRdBufPtrR0 |
| MOV R0, #ringBuf |
| leaveRdBufPtrR0: ; The read pointer points to |
| MOV rdBufPtr, R0 ; the next byte to be read. |
| CLR C |
| RET |
|END |
++
5. Assembler Page 57
The above program is fully relocatable. It can be linked with another program
by the linker. The program can be used by routines which have been made ready
here to handle ringbuffers by invoking it through CALLs. Note however that
R0 which passes over the character in the accumulator, is destroyed and that
in a read the carry is set in the event the buffer is empty, and that as
such no character can be read.
The ASM51 does not recognize PCON DATA 87h, although this is an SFR of
the 8051.
Several editors set asc26 as end of file code at the end of the
source code. ASM51 brings this up as an error, but continues correctly.
With such files the error message 2 ERRORS FOUND can be ignored.
When reading in characters from a file, ASM51 clears the most significant
bit of every byte. As such, asc8Dh is treated like asc0Dh. For that reason
values greater than asc7Fh cannot be used in strings (also not , , , ).
asc8Dh = '' (will also be confused with end of line inside a comment.)
The 8051 has 5 sources of interrupts. The interrupt logic incorporates the
registers IE (Interrupt Enable), IP (Interrupt Priority) and several flags
of TCON (Timer Control).
The interrupts can be disabled with software. They can only be generated
when the corresponding bit in the Interrupt Enable register is set.
Additionally, EA must be 1. The flags in the IE register can be bit addressed.
Consequently, all interrupts can be disabled with CLR EA. With SETB EA all
those that were set are enabled again. After a reset all interrupts are
disabled (IE = 0xx00000b).
+++++++++
| EA | _ | _ | ES | ET1 | EX1 | ET0 | EX0 | IE DATA 0A8h
+++++++++
The 8051 works with 2 interrupt priority levels. Through the Interrupt
Priority register (IP) a priority level may be assigned to every interrupt
source (1 = high Priority).
+++++++++
| _ | _ | _ | PS | PT1 | PX1 | PT0 | PX0 | IP DATA 0B8h
+++++++++
Before every poll, the new interrupt requirements will be taken over anew in
state 5 of the previous cycle. The CPU will not jump on an active interrupt,
when it was blocked, and now removed.
TF0 and TF1 will be set by the hardware, when a timer overflow past 0
takes place. By jumping into the interrupt routine TF0 and TF1 will be
automatically cleared by the hardware.
With level triggering (IT0 = 0, rsp. IT1 = 0) the inverted value of the port
pins _INT0 = P3.2 rsp. _INT1 = P3.3 are placed into IE0 or IE1 in state 5
of every cycle. Level triggering therefore has no value to set or clear the
flags IE0 and IE1 by software. The flags always reflect the status of the
inputs.
With edge triggering the flags IE0 or IE1 will first be set by the hardware,
when a negative edge occurs at _INT0 or _INT1. By jumping into the interrupt
routine IE0 or IE1 will automatically be cleared by the hardware. But they
can also be changed by the software.
TCON contains different flags for controlling the Timer and for the Interrupt
logic. The flags can be bit addressed:
+++++++++
| TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | IE0 | IT0 | TCON DATA 88h
+++++++++
The 8051 has two timer/counters: timer 0 and timer 1. Each timer consists
of two 8 bit registers. They can be set to 4 different running modes.
The timer will (when running) be incremented by 1 at every cycle. They can
also be used as a counter, and will be incremented depending on the count
input, by T0 or T1.
The mode is selected with the TMOD register. TMOD is not bit addressable.
The high nybble of TMOD selects the mode for Timer 1 and the low nybble for
Timer 0:
+++++++++
| GATE | C/T | M1 | M0 | GATE | C/T | M1 | M0 | TMOD DATA 89h
+++++++++
When Timer 0 finds itself in Mode 3, TR1, T1 and _INT1 cannot control Timer
1 anymore. When Timer 1 overflows it does not set TF1 anymore and cannot
generate any more interrupts. Timer 1 can be stopped by switching it to Mode
3, otherwise it runs. Although no more control bits are assigned to Timer 1,
it may run when Timer 0 is in Mode 3, and is good for generating baud rate
for the serial port.
The 8051 can send and receive either synchronously or asynchronously over
the serial port. In asynchronous mode it can send and receive simultaneously.
The 8051 has separate registers for sending and for receiving. The contents
are separate from each other, but both use the same DATA address (SBUF DATA
99h).
| ++
S |>| SBUF Tx-Shift +> TxD
o | +++
f | +++
t | SysTime >|| 16 |
w | Timer 1 >|++
a | Timer 2 >|| DPLL 16 ++
r | +++ |
e | ++ +++ |
|<| SBUF Rx ++ Rx-Shift Reg. ++< RxD
| ++ ++
When a character is written to SBUF (transmit), the port starts shifting the
data out into TxD. When all the bits are sent, the logic sets the
TI flag (BIT 99h in SCON). The shift clock comes from an internal 16 bit-
counter.
Received bits are first shifted into the receive Shift Register. When
all bits are received, the character is taken into SBUF (receive) and RI is
set. The shift clock comes from a DPLL, which is synchronised with
the received data.
| ++
S |>| SBUF Tx-Shift ++
o | ++ +++ |
f | SysTimer>| 6 ++ send |
t | ++ o|<> TxD
w | / receive |
a | | |
r | ++ +++ |
e |<| SBUF Rx ++ Rx-Shift Reg. ++<> RxD
| ++ ++
Send and receive shift functions the same way as in asynchronous mode. But
the data is sent and received at RxD. At TxD, the shift clock is sent or
synchronously received.
The LSB (low bit) is always transmitted first. Sender and receiver must
agree in the number of stopbits. The baud rate should also agree, so that
during the transfer of a character the DPLL does not "run away" too far.
Example for transmitting the character 'A' = asc41h in asynchronous mode:
+ ++ ++ +
++ ++ ++
|Start| LSB | | | | | | | MSB |Stop |
The 8051 can run at a maximum of 1 MBit/s in synchronous mode. The maximum is
375 kBit/s in asynchronous mode.
The serial port can operate in 4 different modes. The mode is set in SCON
(Serial Control Register). SCON is bit addressable.
+++++++++
| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI | SCON DATA 98h
+++++++++
SM0 (BIT 9Fh) and SM1 (BIT 9Eh) acts as mode setting.
SM2 (BIT 9Dh) enables special reception requirement.
REN (BIT 9Ch) is the Receiver Enable Flag.
TB8 (BIT 9Bh) is also the 9th bit sent.
In RB8 (BIT 9Ah) the 9th Bit is received.
TI (BIT 99h) is the Transmit Interrupt Flag.
RI (BIT 98h) is the Receive Interrupt Flag.
8 data bits (LSB first) are sent and received over RxD. The shift clock is
is sent or received over TxD. It is not possible to receive and send
simultaneously.
The shift clock is derived by sending in association with the system clock.
The shift clock corresponds to the cycle frequency (= 1 MHz at 12 MHz
oscillator). When SBUF (transmit) is once again empty, TI will be set.
To accept characters the receiver must be enabled. For this the software has
to set REN = 1 and RI = 0. The character will be received into an internal
shift register. Only when this is full, will the character be moved to SBUF
and RI set.
In Mode 0, SM2 = 0 must be chosen, TB8 and RB8 have no meaning.
Particulars of Mode 0:
----------------------
The shift clock TxD is low during State 3, 4 and 5 of the system clock
(see chapter 3.2 and 3.3). With the positive edge in the shift clock, the
value changes at RxD and the next bit is shifted out.
After a new character to be sent is written to SBUF, a full cycle passes first
before the shifting starts. After the last bit (MSB) of the character has
been shifted out, another 1 will be shifted behind it (so that RxD remains
undisturbed at 1), only then will TI be set. 10 cycles will pass after the
write SBUF event by the software.
During reception, the positive edge at TxD shifts the data into the receive
shift register. For this time period SBUF can still hold the previous data.
When all the bits are received, then will the hardware overwrite it with the
new character. After which RI = 0 is set. For REN = 0 (receiver not enabled)
or RI = 1 (SBUF still full), incoming data will not be received and will be
lost.
A clock will not be sent, only the data. What is sent over TxD is 1 start
bit, then 8 data bits (LSB first) and 1 stop bit. When SBUF (transmit) is
empty again, TI will be set.
Receiving takes place over RxD. The clock will be recovered in the receiver
from the edges of the data. To receive characters we must have REN = 1
and RI = 0 (Receive enable). In addition, if SM2 = 1, the received stop bit
must in addition = 1 making it valid. That character is received in an
internal shift register. Only when this is full, will the value in SBUF be
transferred and RI set. The stop bit is registered in RB8.
Particulars of Mode 1:
----------------------
Sending starts with the first overflow of the clock generator after a
character has been written to SBUF. First, a start bit of 0 is sent, then the
next 8 bits of the character (LSB first). Finally a 1 is sent as a stop bit.
Then will TI = 1.
The reception starts with the first negative edge at RxD. The start bit
is watched out for in states 7, 8 and 9 of the shift clock count. It must
always be 0 (for data integrity, to mask out disturbances). If this is not
the case, or REN not = 1, the reception will be cancelled and the logic goes
back to the initial state (wait for negative edge).
After recognizing the start bit and REN = 1, the incoming bits will be moved
into the receive shift register. At this point in time old data can still
remain in SBUF, RI need not be 1 yet. The reception is completed upon
receiving the stop bit. Only when the following conditions are met, will
the character be placed into SBUF, RI = 1 be set and the stop bit written
into RB8.
RI = 0 and SM2 = 0
or RI = 0 and SM2 = 1 and Stopbit = 1.
| | | + 16 bit-counter / DPLL
| | + divide by 2
| + count value of Timer 1
+ Trigger for timer per cycle
Alternatively, the 8052 can select the Timer2 overflow for triggering the
shift clock counter and/or the DPLL. If Timer2 is to be used for sending,
the bit TCLK in T2CON is set (see 8052 Timer). If Timer2 is to be used for
receiving, bit RCLK in T2CON is set. In this way sender and receiver can run
with different baud rates (one with Timer1 and the other with Timer2), but
they can also be the same: both from Timer1 or both from Timer2.
Mode 2: Asynchronous mode with fixed baud rate for 9 Bit UART:
---------------------------------------------------------------
SM0:SM1 = 10
No clock will be sent, only data. What is sent over TxD is 1 start bit, then
8 data bits (LSB first) the bit from TB8 and 1 stop bit. When SBUF (transmit)
is empty again, TI will be set.
Receiving takes place over RxD. The clock will be recovered in the receiver
from the edges of the data. To receive characters we must have REN = 1
and RI = 0 (Receive enable). If SM2 = 1, so additionally must the received 9th
bit = 1. That character is received in an internal shift register only. Only
when this is full will the value in SBUF be transferred and RI is set. The
stop bit will be put into RB8.
Details of Mode 2:
------------------
Sending begins with the first overflow of the clock generator after a
character has been written to SBUF. First a startbit of 0 is sent, then the
next 8 bits of the character (LSB first). After that the bit from TB8 is sent
and finally a 1 as stop bit. Then will TI = 1.
The reception starts with the first negative edge at RxD. The start bit
will be watched out for in states 7, 8 and 9 of the shift clock count. It
must always be 0 (for data security, to mask out disturbances). If this is
not the case, or REN not = 1, the reception will be cancelled and the logic
goes back to the initial state (wait for negative edge).
After recognizing the start bit and REN = 1, the incoming bits will be
shifted into the receive shift register. At this time, old data can remain in
SBUF-RI need not be 1 yet. The reception is completed upon receiving the stop
bit. Only when the following existing conditions are met, will the character
be placed into SBUF, RI = 1 be set and the stop bit written into RB8.
RI = 0 and SM2 = 0
or RI = 0 and SM2 = 1 and 9th Bit = 1.
When sending over the serial port, the next character can only be written into
SBUF after the old character is gone. As soon as SBUF (transmit) is empty, the
hardware sets the TI flag. The user can write the next character into SBUF
only when TI = 1.
As soon as a character is received over the serial port, the hardware sets
the RI flag. Only when RI = 1, is a valid character present in SBUF (receive).
The hardware sets only RI and TI, but does not clear them. However after a
reset, RI, TI as well as REN (SCON = 0) are cleared.
Polled Mode:
------------
For correct functioning of the serial port, the software must observe the
following :
iniSCON:
CLR ES ; serial interrupt cleared
CLR ET1 ; Timer 1 interrupt cleared
MOV TH1, #-3 ; preload value for Timer 1 = -3
MOV TL1, TH1
SETB TR1 ; start Timer 1
ANL PCON, #7Fh ; SMOD = 0
MOV SCON, #52h ; Mode 1, SM2=0,TB8=0,RB8=0, TI=1, REN=1,RI=0
RET
sendeChar:
JNB TI, sendeChar ; wait until SBUF (transmit) is empty
MOV SBUF, A
CLR TI
RET
receiveChar:
JNB RI, receiveChar
MOV A, SBUF
CLR RI
RET
after a reset the serial port is initialised for 8 bit UART at 9600 baud
(with 11,059 MHz crystal) as above. The receiver is enabled and TI=1 is set.
Sending will wait until SBUF is empty. Then the character which is called up
by the subprogram is written into SBUF from the accumulator. TI must be
cleared by the software.
Interrupt Mode:
---------------
With larger, more comprehensive programs, if the CPU must be processing other
routines, often the serial receiver can only be polled at certain particular
times. If polling is not done often enough, data can be lost. Sometimes it is
also not permitted to wait until a character can be sent. The solution is to
have the serial port running in interrupt mode.
With a set RI or TI flag, the hardware will generate an interrupt. For that,
the interrupt must be enabled (bit ES = 1). By jumping into the interrupt
routine (at CODE 0023h) RI and TI will not be cleared by the hardware. They
are to be cleared inside the interrupt routine by the software, otherwise
after the RETI the interrupt routine will again be activated.
Under normal conditions the send buffer is empty. When working with interrupts
TI cannot remain unchanged. As for code for send buffer empty, an extra flag
must be defined.
This table contains the control values used to obtain a specific baud rate.
For the equation see section 6.3.2
Count * (SMOD + 1)
BaudRate =
32 * (100h - TH1)
In order to obtain lower baud rates a further possibility exists with Timer 1
operating as a 16 bit timer. In addition, a Timer 1 interrupt routine is
installed, which writes a 16 bit reload value into TH1:TL1.
The 8052 permits more exact baud rates to be set with Timer 2 (see chapter
7.1).
Contrary to the 8051, the 8052 possesses a larger internal RAM, a larger
internal ROM and an additional timer: otherwise it is identical with the 8051.
internal RAM:
-------------
In the case of the 8052, several additional SFRs exist for control of Timer2:
7.1.3 Timer 2:
---------------
In the reload mode, an overflow or external trigger to Port1 input pin T2EX
will cause the value from RCAP2 to be loaded into the count register.
In capture mode an external trigger will cause the value in the count register
to be loaded into RCAP2.
+++++++++
| TF2 | EXF2 | RCLK | TCLK | EXEN2 | TR2 | C_T2 | CP_RL2 | T2CON DATA 0C8h
+++++++++
CP_RL2 BIT 0C8h --> With CP_RL2 = 1 the capture mode is selected,
otherwise the reload mode. Reload mode will also be selected when Timer2 is
used as a baud rate generator for the serial port. (see also EXEN2, RCLK and
TCLK).
C_T2 BIT 0C9h --> With C_T2 = 1 Timer2 will operate as a counter, or else
as a timer.
EXEN2 BIT 0CBh --> The external trigger input pin T2EX is only active with
EXEN2 = 1. Only then, and with a negative edge at T2EX, will a transfer take
place in RCAP2 (in Capture Mode) or a Reload (in Reload Mode).
TCLK BIT 0CCh --> When TCLK is set, the Timer2 overflow acts as a trigger for
the baud rate generator for the serial port when sending.
RCLK BIT 0CDh --> When RCLK is set, the Timer2 overflow acts as a trigger
for the baud rate generator (DPLL) for the serial port when receiving.
EXF2 BIT 0CEh --> External trigger interrupt flag. EXF2 will be set by
the hardware, after an overflow has occurred due to there being a negative
edge at T2EX (only when EXEN2 = 1). EXF2 must be cleared by software.
TF2 BIT 0CFh --> Timer2 overflow interrupt flag. TF2 will be set by the
hardware after an overflow from 0FFFFh to 0000 in TH2:TL2 occurs. TF2 must be
cleared by software.
Timer2 Interrupt:
-----------------
TF2 and EXF2 must be cleared by software (in the interrupt routine). Otherwise
after a RETI a renewed jump into the interrupt routine takes place .
TF2 and/or EXF2 may be cleared or set with software. If one of the two, EXF2
or TF2 is not used, it can be misused to produce a software interrupt.
The prerequisite for the generation of the serial baud rates with Timer 2
is that the serial port must be running in mode 1 (SM0:SM1 = 01) or Mode 3
(SM0:SM1 = 11). By default the Timer1 overflow is used to trigger the
baud rate generators. When TCLK or/and RCLK is set, the Timer2 overflow will
be used for sending or receiving.
Osci 1 1
BaudRate = * * ; RCAP2 = RCAP2H:RCAP2L
2 10000h - RCAP2 16
This table contains the control values for obtaining a given baud rate.
Compare also chapter 6.3.4. Here SM0:SM1 = 01 or 11.
Adjustable by PC:
A negative number can be used in an assembler. But it can also be added up:
with -1 = 0FFFFh, -2 = 0FFFEh ....
The values less than 20h (blank characters) in general are not printable, and
also many editors and text processing systems do not display them. There and
in data transmission they often act as control characters.
The important ones are:
32 20 0 48 30 @ 64 40 P 80 50 ` 96 60
! 33 21 1 49 31 A 65 41 Q 81 51 a 97 61
" 34 22 2 50 32 B 66 42 R 82 52 b 98 62
# 35 23 3 51 33 C 67 43 S 83 53 c 99 63
$ 36 24 4 52 34 D 68 44 T 84 54 d 100 64
% 37 25 5 53 35 E 69 45 U 85 55 e 101 65
& 38 26 6 54 36 F 70 46 V 86 56 f 102 66
' 39 27 7 55 37 G 71 47 W 87 57 g 103 67
( 40 28 8 56 38 H 72 48 X 88 58 h 104 68
) 41 29 9 57 39 I 73 49 Y 89 59 i 105 69
* 42 2A : 58 3A J 74 4A Z 90 5A j 106 6A
+ 43 2B ; 59 3B K 75 4B [ 91 5B k 107 6B
, 44 2C < 60 3C L 76 4C \ 92 5C l 108 6C
- 45 2D = 61 3D M 77 4D ] 93 5D m 109 6D
. 46 2E > 62 3E N 78 4E ^ 94 5E n 110 6E
/ 47 2F ? 63 3F O 79 4F _ 95 5F o 111 6F