Beruflich Dokumente
Kultur Dokumente
Salient Features
Compatible with MCS-51 Products 4K Bytes of In-System Reprogrammable Flash Memory Endurance: 1,000 Write/Erase Cycles Fully Static Operation: 0 Hz to 24 MHz Three-level Program Memory Lock 128 x 8-bit Internal RAM 32 Programmable I/O Lines Two 16-bit Timer/Counters Six Interrupt Sources Programmable Serial Channel Low-power Idle and Power-down Modes
P3.6
P3.7
WR
RD
CONTROL PINS
PSEN (Program Store Enable): An output signal on pin 29.
When working with external code memory it works as the memory read signal. It generally connects to the Output Enable (OE) pin of the external code memory. When working with internal code memory, this pin remains high.
CONTROL PINS
ALE (Address Latch Enable): An output signal on pin 30. While
working with external memory ALE is used to de-multiplex the address and data bus (alternate mode of port 0). The ALE latches the low byte of address on an external octal latch during the 1st half of a memory read/write cycle. During the 2nd half of the memory read/write cycle, port 0 pins are available for data I/O.
The ALE signal pulses at 1/6th the on-chip oscillator frequency. So in every machine cycle (= 12 oscillator periods), 2 ALE pulses are available. Only exception is during each access to external data memory, when one ALE pulse is missed. This pin also receives the PROG pulse input during programming of the internal code memory.
CONTROL PINS
RST (Reset): An input signal on pin that acts as the master reset
for the processor. A high on this pin for at least two machine cycles while the oscillator is running, resets the internal registers for an orderly start up. Register values after reset are as follows: Program Counter: 0000H, Accumulator: 00H, B register: 00H, DPTR: 0000H, PSW: 00H, SP: 07H, Ports 0-3: FFH, SCON: 00H, SBUF: 00H, IP: XXX00000B, IE: 0XX00000B, TCON: 00H, TMOD: 00H, PCON: 0XXX0000B.
ON-CHIP OSCILLATOR
As shown in fig. 1(a) , the 51 microcontroller features an on-chip oscillator that is typically driven by a crystal connected between pin 19 (XTAL1-input to the oscillator amplifier) and pin 18 (XTAL2-output from the oscillator amplifier). Stabilizing capacitors are also required as shown. Fig. 1(b) gives the external clock drive configuration.
POWER CONNECTIONS
The 51 microcontroller operates from a single +5 volts supply. The VCC connection is on pin 40 and the VSS connection is on pin 20.
There is both read latch and read pin facilities. A readmodify-write instruction (like CPL (complement) a port bit) reads the port pin latch to avoid misinterpreting the logic level in case the pin is heavily loaded.
MEMORY ORGANIZATION
MEMORY ORGANIZATION
The 51 microcontroller implements a Harvard architecture for its memory: a separate program space for code and data. Both code and data could be internal or external. External memory can be expanded up to a maximum of 64K code memory and 64K data memory.
The internal memory consists of 4K 8-bit wide EPROM/Flash and 128 bytes on-chip RAM along with 21 8-bit wide special function registers. The 128 bytes of RAM contains four register banks each containing 8 registers, 16 byte of bit-addressable memory locations and 80-bytes of general purpose storage. All RAM locations (including the SFRs) are memory mapped.
MEMORY ORGANIZATION
D7H Carry flag D6H Auxiliary Carry flag D5H Flag 0 D4H Register Bank Select 1 D3H Register Bank Select 0 D2H Overflow flag PSW.1 Reserved PSW.0 P
EXTERNAL MEMORY
EXTERNAL MEMORY
The 51 family of microcontrollers can address 64K external code memory and 64K external data memory. Part or whole of the external data memory can be used to interface peripheral ICs as memory-mapped I/O. When external code memory is used EA is tied low and PSEN pulses low to read the code from external memory (fig-2(a)). P0 becomes a multiplexed address (A0-A7) and data (D0-D7) bus. P2 becomes the high byte (A8-A15) of the address bus. When external data memory is used (fig-2(b)) P3.6 and P3.7 function as the external data memory WR and RD pins.
EXTERNAL MEMORY
EXTERNAL MEMORY
EXTERNAL MEMORY
EXTERNAL MEMORY
INSTRUCTION SET
INSTRUCTION SET
Introduction: The 8051 instruction set is optimized for 8-bit
control applications.The instruction set provides a variety of fast, compact addressing modes for accessing the internal RAM to facilitate operations on small data structures. It also provides extensive support for 1-bit variables, allowing direct bit manipulation in control and logic systems that require Boolean processing. 8051 instructions have 8-bit opcodes. Out of the possible 256 opcodes, 255 are implemented and 1 is undefined. Some instructions have 1 or 2 additional bytes (operands) for data or addresses. In all, there are 139 1-byte instructions, 92 2-byte instructions and 24 3byte instructions.
INSTRUCTION SET
Addressing Modes: For instructions operating on data,
addressing modes specifies the source and destination of the data. The 8051 microcontrollers implements 8 different addressing modes. This section examines all the possible addressing modes with examples.
INSTRUCTION SET
For example, to add the content of R7 to the accumulator, the following instruction is used ADD A,R7 and the opcode is 00101111B. The upper 5 bits, 00101, implement the instruction. The lower 3 bits, 111, indicates the register R7.
Some instructions are register-specific, such as those that operate on the accumulator or data-pointer and do not require explicit address bits. The opcode itself indicates the register. For example, INC DPTR
is a 1-byte instruction that increments the data-pointer by 1.
INSTRUCTION SET
Direct Addressing: In direct addressing the operand is
specified by an 8-bit address field in the instruction. Only internal Data RAM and SFRs can be directly addressed. For example, the 2 byte instruction MOV P1,A
moves the content of the accumulator to port 1. The direct address of port 1 (90H) is determined by the assembler and inserted as byte 2 of the instruction. The source of the data, the accumulator, is specified implicitly in the opcode. MOV 30H,40H is a 3 byte instruction using the direct addressing mode. Here bytes 2 and 3 contain the addresses of the source and destination of the data to be transferred.
INSTRUCTION SET
Indirect Addressing: In indirect addressing the instruction
specifies a register which contains the address of the operand. Both internal and external RAM can be indirectly addressed. The address register for 8-bit addresses can be R0 or R1 of the selected bank, or the Stack Pointer. The address register for 16-bit addresses can only be the 16-bit data pointer register, DPTR.
For example, supposing the register R0 contains 40H, the instruction MOV A,@R0
INSTRUCTION SET
Immediate Addressing: In immediate addressing the opcode
is followed by the data itself, instead of the location of the data. For example,
MOV A, #10H loads the accumulator with data 10H. All instructions using immediate addressing mode uses 8-bit data constant for the immediate data. The only exception is loading the DPTR with an immediate data. The instruction
INSTRUCTION SET
Relative Addressing: Relative addressing is used with certain
jump instructions. A relative address is an 8-bit signed value (-128 to 127), which is added to the program counter to determine the address location of the next instruction executed.
The programmer needs to assign a label for the 8-bit signed value. The assembler determines the relative offset accordingly. If the 2byte instruction SJMP LOCATION is located at program memory locations 1000H and 1001H and the label LOCATION refers to an instruction location at 1040H, the assembler will assign a relative offset of 3EH to the label LOCATION.
INSTRUCTION SET
Absolute Addressing: Absolute addressing is used only with
the ACALL and AJMP instructions. These 2-byte instructions allow branching within the current 2K page of code memory. The first 5 bits of the opcode implements the instruction and the last 3 bits along with the 2nd byte provides the branching address.
INSTRUCTION SET
Indexed Addressing: Indexed addressing uses a base register
(either the PC or the DPTR) and an offset (the accumulator) in forming the effective address of for a JMP or MOVC instruction. These instructions are used to create jump tables or look-up tables.
For example, the instruction
MOVC A, @A+PC
adds the content of the accumulator with that of the program counter to generate an address location in the program memory. Data from that location is then moved to the accumulator.
TIMER OPERATION
TIMER OPERATION
The 51 microcontroller implements two 16-bit timers each of which can be configured to work in one of four possible modes. Following are the SFRs associated with the 51 timer module: TCON (88H): Bit-addressable timer control register.
TMOD (89H): Upper nibble sets timer 1 mode, lower nibble sets timer 0 mode.
TL0 (8AH): Timer 0 low-byte. TL1 (8BH): Timer 1 low-byte. TH0 (8CH): Timer 0 high-byte. TH1 (8DH): Timer 1 high-byte.
TIMER OPERATION
Timer Mode Register (TMOD) Bit 7 6 5 4 3 2 1 0 Name Timer GATE C/T M1 M0 GATE C/T M1 M0 1 1 1 1 0 0 0 0 Description When set, timer runs only if INT1 is high Counter(1) / timer(0) select bit Mode bit 1 Mode bit 0 When set, timer runs only if INT0 is high Counter(1) / timer(0) select bit Mode bit 1 Mode bit 0
TIMER OPERATION
Timer Modes M1 0 0 M0 0 1 MODE 0 1 DESCRIPTION 13-bit timer mode 16-bit timer mode
1
1
0
1
2
3
TIMER OPERATION
Timer Control Register (TCON) Bit TCON.7 Symbol TF1 Bit-address 8FH Description Timer 1 overflow flag. Set by h/w whenever counter overflows. Reset by s/w, or by h/w when processor vectors to an ISR. Timer 1 run control bit. Set or reset by s/w to start/stop counter. Timer 0 overflow flag.
TCON.6 TCON.5
TR1 TF0
8EH 8DH
TCON.4
TR0
8CH
TIMER OPERATION
Timer Control Register .. continued Bit TCON.3 Symbol IE1 Bit-address 8BH Description External interrupt 1 edge flag.Set by h/w when a falling edge is detected on INT1. Cleared by s/w, or by h/w when processor vectors to an ISR. External interrupt 1 type flag. Set/cleared by s/w for fallingedge/low-level activated external interrupt. External interrupt 0 edge flag. External interrupt 0 type flag.
TCON.2
IT1
8AH
TCON.1 TCON.0
IE0 IT0
89H 88H
INTERRUPTS
Interrupt is an event that causes a temporary suspension of a program and fires another program to service the same.
Interrupts allow the system to respond asynchronously to event(s) while executing another program. Interrupts do not occur by the execution of an instruction. It is the response of the system to an event that occurs asynchronously with the main program.
The CPU, on occurrence of an interrupt, suspends execution of the main program, executes another program to service the interrupt condition, and then returns to the execution of the main program. An interruptdriven system thus gives the illusion of executing many programs simultaneously. The program that deals with an interrupt is called the Interrupt Service Routine (ISR) or Interrupt Handler.
INTERRUPTS
Time
Main Program
Program execution without interrupts.
Interrupt-level
execution ISR
Main
Time
Program execution without interrupts.
INTERRUPTS
8051 Interrupt Organization The 51 family of micro-controllers has five interrupt sources: two external interrupts, two timer interrupts and a serial port interrupt. All interrupts are disabled by default after a system reset and are enabled individually by software. In the event of two or more interrupts occurring simultaneously or an interrupt occurring while another interrupt is being serviced, there is both a fixed polling sequence and a programmable two-level priority scheme for scheduling the interrupts. The 51 family implements the following SFRs to enable/disable and prioritize interrupts: IE (A8H): The Interrupt Enable Register (bit-addressable) IP (B8H): The Interrupt Priority Register (bit-addressable)
INTERRUPTS
Interrupt Enable Register
Bit IE.7 Symbol EA Bit-Address AFH Description (1 = Enable) Global enable/disable
IE.6
IE.5 IE.4
ES
AEH
ADH ACH
Undefined
Undefined Enable serial port interrupt
IE.3
IE.2 IE.1
ET1
EX1 ET0
ABH
AAH A9H
IE.0
EX0
A8H
INTERRUPTS
Enabling/Disabling Interrupts Each of the interrupt sources can be individually enabled/disabled by setting/clearing the corresponding bit in the IE SFR at location A8H. All interrupts can be globally disabled by clearing the global enable bit at AFH. To enable an interrupt source two bits must be set. The individual enable bit and the global enable bit. For example the serial port interrupt is enabled as follows: SETB ES SETB EA ; Enable serial interrupt ; Set global enable bit
INTERRUPTS
Interrupt Priority Register Bit IP.7 IP.6 IP.5 IP.4 IP.3 Symbol PS PT1 Bit-Address BFH BEH BDH BCH BBH Description (1 = Higher Priority) Undefined Undefined Undefined Priority for serial port interrupt Priority for timer 1 interrupt
IP.2
IP.1 IP.0
PX1
PT0 PX0
BAH
B9H B8H
INTERRUPTS
Interrupt Priority Each interrupt source can be programmed to one of two priority levels through IP - the bit-addressable SFR at location B8H. IP is cleared by a system reset to place interrupts at low priority level by default. Priority of an interrupt source is set to high by setting the corresponding bit in the IP register. Whenever a high priority interrupt occurs when a low priority ISR is running, the ISR is interrupted. However, a high priority ISR cannot be interrupted.
The main program, operating at base level, can always be interrupted. If two interrupts occur simultaneously, the higher priority one is serviced first.
Serial Port
One of the 8051s many powerful features is its integrated UART, otherwise known as a serial port. It can operate in several modes over a wide range of frequencies. Hardware access to the serial port is through the TxD and RxD pins. These are the alternate functions of P3.1 (pin 11) and P3.0 (pin 10) respectively. The serial port is full duplex, meaning it can transmit and receive simultaneously. It is also receive-buffered, meaning it can commence reception of a second byte before a previously received byte has been read from the receive register. (However, if the first byte still hasn't been read by the time reception of the second byte is complete, the first byte received will be lost). The next slide illustrates the functional block diagram of the serial port.
Serial Port
Serial Port
The serial port receive and transmit registers are both accessed through a Special Function Register SBUF at address 99H. Actually, SBUF represents two separate buffers, a transmit buffer and a receive buffer. Writing to SBUF loads the write-only transmit register, and reading SBUF accesses a physically separate read-only receive register. The serial port control register, SCON, is a bit-addressable register at address 98H, containing the status bits and control bits. The control bits set the mode of operation of the serial port. The status bits indicate the end of a character transmission or reception. The status bits can be tested in software or can be programmed to generate an interrupt.
Serial Port
Serial Port Mode setting The 8051 serial port has four modes of operation that can be set by writing 1s and 0s to the SCON.7 (SM0) and SCON.6 (SM1) bits. The first mode implements a simple shift register and the other three modes enables asynchronous serial communication. Serial Port Modes: SM0 0 SM1 0 MODE 0 DESCRIPTION Shift Register BAUD RATE Fixed (Osc. Fq. / 12)
0
1 1
1
0 1
1
2 3
8-bit UART
9-bit UART 9-bit UART
Serial Port
Mode 0: Serial data enters and exits through RXD. TXD outputs the shift clock. 8 bits (LSB first) are transmitted/received.The baud rate is fixed at 1/12 the oscillator frequency. Mode 1: 10 bits are transmitted (through TXD) or received (through RXD): a start bit (0), 8 data bits (LSB first), and a stop bit (1). On receive, the stop bit goes into RB8 in Special Function Register SCON. The baud rate is variable. Mode 2: 11 bits are transmitted (through TXD) or received (through RXD): a start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). While transmitting, the 9th data bit (TB8 in SCON) can be assigned the value of 0 or 1. On reception, the 9th data bit goes into RB8 in SCON, while the stop bit is ignored. The baud rate is programmable to either 1/32 or 1/64 the oscillator frequency. Mode 3: 11 bits are transmitted (through TXD) or received (through RXD): a start bit (0), 8 data bits (LSB first), a programmable 9th data bit and a stop bit (1). The baud rate is variable.
Serial Port
SCON Register Summery BIT
SCON.7
SCON.6 SCON.5 SCON.4 SCON.3 SCON.2 SCON.1
SYMBOL ADDRESS
SMO
SM1 SM3 REN TB8 RB8 TI
DESCRIPTION
Serial port mode bit 0
Serial port mode bit 1 Serial port mode bit 2 Receiver Enable Transmit bit 8 (9-bit UART) Receive bit 8 (9-bit UART) Transmit interrupt flag. Set at end of character transmission. Cleared by S/W.
9FH
9EH 9DH 9CH 9BH 9AH 99H
SCON.0
RI
98H
Serial Port
Other important bits in SCON register The 5th bit in SCON, SM2, enables multiprocessor communication in modes 2 and 3. RI will not be activated if received 9th bit is 0. The 4th bit in SCON, REN, enables the receiver and must be set to receive characters. The 3rd bit in SCON, TB8, is the 9th bit transmitted in modes 2 and 3. TB8 is set/cleared by software. The 2nd bit in SCON, RB8, is the 9th bit received in modes 2 and 3. The 1st bit in SCON, TI, is the transmit interrupt flag. If set indicates that the transmission of the last byte written on SBUF is over. Must be cleared by software. The 0th bit in SCON, RI, is the receive interrupt flag. If set indicates that the reception of the character is over. Must be cleared by software.
Serial Port
Serial Port Baud Rate Setting
Mode 0 (Fixed clock) Baud rate clock = On chip oscillator clock / 12.
Serial Port
Mode 1 & 3 Baud Rate Calculation
Baud rate clock (BR) = Timer 1 overflow rate (TOR) /n (n = 32 if PCON.7 (SMOD) is clear,; = 16 if PCON.7 (SMOD) is set.) = (TOR*2SMOD) / 32; Timer clock period (TCP) = 12/ oscillator frequency (OF); TOR = 1/ (TCP*COUNT) = OF / (12*COUNT); Therefore,