Académique Documents
Professionnel Documents
Culture Documents
1. Overview of 8051 Architecture, Timing, On-chip Resources, Instruction Set etc. Derivative products 2. Programming the 8051: Basic techniques, tips & tricks.
THE 8051
An 8-bit Microcontroller optimized for control applications.
A Microcontroller because you can make a one-chip system with the one chip containing: Program & Data Memory I/O Ports Serial Communication Counters/Timers Interrupt Control logic A-to-D and D-to-A convertors & so on ...
XTAL2
EA PS EN ALE P O R T 1
SECONDARY FUNCTIONS
P O R T 3
P O R T 2
ADDRES S BUS
External Interrupts
Interrupt Control
CPU
OSC
Bus Control
I/O Ports
Serial Port
TXD RXD
P0 P2
(Address/Data)
P1
P3
ADDRESSING SPACE
- 64K X 8 ROM - Program memory. - 64K x 8 RAM - External data memory. - 256 x 8 RAM - Internal data memory. - 128 x 8 Special function registers (SFRs). - Bit addressing of 16 RAM locations and 16 SFRs.
PROGRAM MEMORY
- 16 bit Program Counter (PC). - 16 bit Data Pointer (DPTR). - 64K byte address space each for Program & Data. - Table lookup using relative addressing: PC + ACC (Move). DPTR + ACC (Move and jump). - EA pin disables internal ROM and
7F 30 2F 20 1F 20 17 18 0F 08
07 R7
R6 R5 R4 R3 R2 R1
00 R0
REGISTER BANK 0
8051 PORT 2
PORT 0
PSEN
8051 TIMING
State 1 State 2 State 3 State 4 State 5 State 6 State 1 State 2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 XTAL2
P0
P2
ADDRESS
LATCH ADDRESS INPUTS
A7 - A0 D7 - D0
PSEN
DATA
OUTPUTS OE
ADDRESS LATCH
WR RD
External reset is asychronous to the internal clock. RST pin must be high for at least two machine cycles while the oscillator is running. Internal RAM not affected by reset, but indeterminate on power up. Port pins in random state until oscillator starts and algorithm write 1's to them. Reset sets PC to 0000. Typical circuits:
RE SE T
+5V
+5V
8051
10uF 2.2uF
80C51
RST
RST
8.2K
addressable as 80 to FF hex.
- 16 addresses are bit addressable: Set, Clear, AND, OR, MOV (those ending in 0 or 8). - This space contains: Special purpose CPU registers. I/O control registers. I/O ports.
Bit Addressable
F8 F0
E8
E0 D8 D0 C8 C0 B8 B0 A8 A0 98 90 88 80 IP P3 IE P2 SCON P1 TCON P0 TMOD TL0 SP DPH TL1 DPL TH0 TH1 PCON SBUF
- TCON
- TH0 - TL0 - TH1 - TL1 Serial I/O: - SCON - SBUF
: Timer control.
: Timer 0 high byte. : Timer 0 low byte. : Timer 1 high byte. : Timer 1 low byte.
Other:
- PCON : Power control & misc.
F0
RS1
: Carry Flag.
RS0
OV
----
- AC : Auxiliary Carry Flag. - F0 : Flag 0 (available for user). - RS1 : Register Select 1. - RS0 : Register Select 0. - OV : Arithmetic Overflow Flag. -P : Accumulator Parity Flag.
RS1 0 0
RSO 0 1
Register Bank 0 1
1
1
0
1
2
3
10h - 17h
18h - 1Fh
CONFIGURAT ION
CMOS
2 OSC. PERIODS
V C C
VC C
VC C
P1
P2
P3 PORT PIN
NMOS
2 OSC. PERIODS
- As an I/O port:
open drain.
PO RT 0
- As a multiplexed data bus: Tristate bus with strong pull-ups. 8-bit instruction bus, strobed by PSEN. Low byte of address bus, strobed by ALE. 8-bit data bus, strobed by WR and RD. - 3.2 mA outputs (about 8 LSTTL loads).
As an I/O port:
PO RT 1
Standard quasi-bidirectional.
- Alternate functions:
Only on some derivatives.
- As an I/O port:
PO RT 2
Standard quasi-bidirectional.
- Alternate functions: High byte of address bus for external program and data memory accesses.
PO RT - As an I/O port: 3
Standard quasi-bidirectional. - Alternate functions: Serial I/O Timer clocks Interrupts Data memory - TXD, RXD - T0, T1 - INT0, INT1 - RD, WR
COUNTER / TIMERS
- Two 16-bit Counter/Timers:
Up counters, can interrupt on overflow. - Counts:
8-bit auto-reload. Counter in TL0 or TL1. Reload value in TH0 or TH1. Provides a periodic flag or interrupt.
(CONT'D)
Splits timer 0 into two 8-bit counter/timers. First counter (TLO) acts like mode 0, without prescaler. Second counter (TH0): Counts CPU cycles. Uses TR1 (timer 1 run bit) as enable. Uses TF1 (timer 1 overflow bit) as flag. Uses Timer 1 interrupt. Timer 1 (when timer 0 is in mode 3 ): Counter stopped if in mode 3. Running in mode 0, 1, or 2. Has gate (INT1) and external input (T1), but no flag or interrupt. May be used as a baud rate generator.
TF1
Cont r ol
Gat e
I NT1 (Pi n)
The Gate input controls whether the Counter runs while gated by the interrupt signal or not.
- GATE : Permits INTx pin to enable/disable counter. - C/T : Set for counter operation, reset for timer operation.
- M1, M0 :
00 : Emulate 8048 counter/timer (13-bits). 01 :16-bit counter/timer. 10 : 8-bit auto-reload mode 11 :Timer 0 = two 8-bit timers.
Timer 1 Counting disabled. Timing function allowed. Can be used as Baud Rate generator.
- IE1, IE0 : Edge flag for external interrupts 1 and 0. * Set by interrupt edge, cleared when interrupt is processed. - IT1, IT0 : Type bit for external interrupts. * Set for falling edge interrupts, reset for 0 level interrupts. * = not related to counter/timer operation.
- Four modes of operation: Synchronous serial I/O expansion. Asynchronous serial I/O with variable baud rate. Nine bit mode with variable baud rate. Nine bit mode with fixed baud rate. - 10 or 11 bit frames. - Interrupt driven or polled operation. - Registers: SCON - Serial port control register. SBUF - Read received data. - Write data to be transmitted. PCON - SMOD bit.
Mode 0: Shift Register Mode. Serial data is transmitted/received on RXD. TXD outputs shift clock. Baud
Rate is 1/12 of clock frequency. Mode 1: 10-bits transmitted or received. Start (0), 8 data bits (LSB first), and a stop bit (1). Baud Rate Clock is variable using Timer 1 overflow or external count input. Can go up to 104.2KHz (20MHz osc.). Mode 2: 11-bits transmitted or received. Start (0), 8 data bits (LSB first), programmable 9th bit, and stop bit (1). Baud Rate programmable to either 1/32 or 1/64 oscillator frequency (625KHz for 20MHz osc.). Mode 3: 11-bit mode. Baud Rate variable using Timer 1 overflow or external input. 104.2 KHz max. (20 MHz osc.).
MULTI-DROP COMMUNICATION
Serial Communication Modes 2 and 3 allow one "Master" 8051 to control several "Slaves": The serial port can be programmed to generate an interrupt if the 9th data bit = 1. The TXD outputs of the slaves are tied together and to the RXD input of the master. The RXD inputs of the slaves are tied together and to the TXD ouput of the master. Each slave is assigned an address. Address bytes transmitted by the master have the 9th bit = 1. When the master transmits an address byte, all the slaves are interrupted. The slaves then check to see if they are being addressed or not. The Addressed slave can then carry out the master's commands.
INTERRUPT SYSTEM
- 5 Interrupt Sources (in order of priority): External Interrupt 0. Timer 0. External Interrupt 1. Timer 1. Serial Port. - Each interrupt type has a separate vector address. - Each interrupt type can be programmed to one of two priority levels.
- PS
: Serial interface.
- PT1
- PX1 - PT0 - PX0
: Timer 1.
: External interrupt 1. : Timer 0. : External interrupt 0. 0 = Low priority. 1 = High priority.
- POWER DOWN OPERATION Setting PD bit stops oscillator. RAM contents are saved. Exit via Reset. Some (newer) 80C51 derivatives allow Power-Down wakeup via Interrupt.
Interrupt sources remain active: Serial interface. External interrupts. Timers. Exit with any enabled interrupt or Reset. - GF0, GF1 are general purpose software flags. - SMOD serial interface control bit. Doubles baud rate in modes 1,2, and 3. - Only SMOD available on NMOS parts.
POWER CONSUMPTIO N
Example : for 80C51 at Vcc = 5V.
Mode / Freq.
Operating Idle Power Down
0.5 MHz
2.2 mA 0.9 mA 50 uA
16 MHz
20.5 mA 5.0 mA 50 uA
There are basically 5 ways of specifying source/destination operand addresses: 1. Particular On-chip Resources: This includes the Accumulator (A), the Stack Pointer (SP), the Data Pointer (DP), the Program Counter (PC), and the Carry (C). Other On-chip Registers are Memory-mapped while these have special Op-codes. 2. Immediate operands: The # sign is the designator. These are 8-bits except for DPTR contents (16-bits). 3. Register operands: Designated as Rn, where n is 0..7. One of the four Register Banks is used (PSW selected). 4. Direct Operands: From 00 to FF Hex, specifies one of the internal data addresses. 5. Indirect Address: Designated as @Ri, where i is 0 or 1, uses the contents of R0 or R1 in the selected Register Bank to specify the address. Other form is @A, using Accumulator contents.
ADDRESSING MODES
Bytes/Cycles
SUBB
A, @Ri
A, #data
1/1
2/1 1/1 1/1
INC DEC
A Rn
direct
@Ri INC MUL DPTR AB
2/1
1/1 1/2 1/4
DIV
DA
AB
A
1/4
1/1
XRL
A, @Ri
A, #data direct, A direct, #data C, bit
1/1
2/1 2/1 3/2 2/2
C, /bit
CLR CPL A C bit
2/2
1/1 1/1 2/1
Bytes/Cycles
A, @Ri
A, #data Rn, A Rn , direct Rn, #data direct, A direct, Rn direct, direct direct, @Ri direct, #data
1/1
2/1 1/1 2/2 2/1 2/1 2/2 3/2 2/2 3/2
AJMP
SJMP JMP JZ JNZ
addr11
rel @A+DPTR rel rel
2/2
2/2 1/2 2/2 2/2
DJNZ
Rn, rel
direct, rel
2/2
3/2 1/1 2/2
NOP JC
rel
JNC
JB JNB JBC
rel
bit, rel bit, rel bit, rel
2/2
3/2 3/2 3/2
THE I 2C BUS
(INTER - INTEGRATED CIRCUIT) A 2 wire serial data and control bus
Implemented with one serial data (SDA) and one clock (SCL) line.
Unique start and stop conditions. Slave selection protocol uses a 7-Bit slave address. Bi-directional data transfer. Acknowledgement after each byte transferred. No limit on the number of bytes transferred. Real multimaster capability.
Arbitration procedure.
Transmission speed up to 100Khz Maximum bus length of 4 meters. Maximum drive capacity of 400pF.
I2C DEFINITIO NS
MASTER:
Initiates a transfer by generating start and stop conditions. Generates the clock. Transmits the slave address. Determines data transfer direction. SLAVE: Responds only when addressed.
I2C HARDWARE DETAILS Devices connected to the bus must have an open drain or
open collector output for serial clock and data. The device must also be able to sense the logic level on these pins. All devices must have a common reference ground.
The serial clock and data lines are connected to VCC through pull up resistors.
At any given moment the I2C bus is: Idle, in Master transmit mode, or in Master receive mode.
SCLK1 OUT
DATA1 OUT
SCLK2 OUT
DATA2 OUT
SCLK IN
DATA IN
SCLK IN
DATA IN
DEVICE 2
Both start and stop conditions are generated by bus master. The Bus is busy after a start condition.
SDA SCL
S Start Condition
SDA
SCL
I2C ADDRE SS
Each node has a unique 7 bit address. Peripherals usually have fixed and programmable address portions.
ACKNOWLE DGEMENT
Master/slave receivers pull data line low for one clock pulse after reception of a byte. Master receiver leaves data line high after receipt of the last byte requested. Slave receiver leaves data line high on the byte following the last byte it can accept.
SDA MSB SCL S START CONDITION 1 2 7 8 9 ACK 1 2 3-8 9 ACK P STOP CONDITION
Master Read:
S SLAVE ADDRESS R A DATA A DATA NA P
SCL
DATA1
DATA2
SDA
SCL
ACCESS.B US .1
DEC has invented an interconnect method for connecting a PC or Workstation to low speed I/O devices such as:
ACCESS.B US .2
ACCESS.bus features:
Up to 14 devices
Up to 8 Meters (26.4 feet) in length Serial, daisy-chained 4-pin cable (2 pins are power and ground). Only ONE device port needed on computer.
ACCESS BUS .3
ACCESS.Bus features: Layered 3-layer protocol defined by DEC: Physical layer is I2C. Base Protocol over I2C defines the structure of I2C messages and defines Control and Status Messages. Also supports auto-addressing and hot plugging. Applications Protocol defines message semantics for particular device types. Extremely low cost implementation based on off-the-shelf Microcontrollers with I2C such as the Signetics 83/87C751 (used in new DEC workstation ).
ACCESS.B US .4 Device address and type recognition is automatic. No drivers have to be loaded.
Concise protocol. Only 7 standard message types. Fully implemented in the 87C751 with 2K of Program memory. ACCESS.Bus is part of DEC's ARC and ACE platforms.
ACCESS.B US is Apple's ADB (Apple Desktop Bus). The .5 The closest thing to the ACCESS.bus
following is a comparison between ADB and ACCESS.bus: ADB not recommended ACCESS.bus fully supported Hot-Plugging
10 KBits/sec
3 devices
80 KBits/sec
14 devices
Proprietary
5 meters
Open. No royalties
8 meters
Extended I/O
80C51
Memory 2 to 32 K
CEPROM 4K CEPROM 4K CEPROM 8K CEPROM 8K CEPROM 8K CEPROM 8K CEPROM 16K CEPROM 16K CEPROM 32K
80C51 CODING:
IDEAS AND EXAMPLES
COMP ARE
The 80C51 has no basic compare instruction. However, the CJNE (compare and jump if not equal) instruction leaves the carry flag set after execution, allowing further magnitude comparisons to be made. This method works for all variations of CJNE: CJNE CJNE CJNE A,direct,rel A,#data,rel Rn,#data,rel
CJNE
@Ri,#data,rel
Examples of four variations of magnitude comparison: CJNE Test: JC CJNE Test: JNC CJNE SJMP Test: Else: JNC ----CJNE SJMP Test: JNC A,Value,Test LTE LTE ;Branch if A <= Value. A,Value,Test LT A,Value,Test GTE A,Value,Test Else GT ;Branch if A > Value. ;Branch if A >= Value. ;Branch if A < Value.
COMP ARE
Most instructions that reference 80C51 port data read the value on the port pins rather than the value in the port latch. However, some instructions read the port latch instead. 1) Arithmetic or logical operations that may alter port values: ANL port,src ORL port,src XRL port,src INC port DEC port DJNZ port,label 3) Instructions that may alter port bits: MOV bit,C JBC bit,label CPL bit CLR bit SETB bit
READMODIFYWRITE
This example uses external interrupt 0 as the means to accomplish the single step. This interrupt is a good choice because it is the highest priority interrupt . Note: the user program must not write to the IE or IP registers or make use of other interrupt related functions. ;Set up for single step of some user routine: StartSS: SETB SETB JNB JB MOV LJMP . . JNB JB RETI PX0 IT0 INT0,$ INT0,$ IE,#81h UserProg . . INT0,$ INT0,$ ;Set INT0 as high priority. ;Set INT0 to edge triggered mode. ;Wait for a "single step" interrupt to come, ; and go. ;Enable INT0 and insure that we are not ; interrupted during the following jump. ;Code to dump registers, user program ; address, etc. ;Wait for a "single step" interrupt to come, ; and go. ;This RETI will allow one user program instruction to ; execute, after which we will return to the INT0 service ; routine.
ExInt0:
start timer
stop timer
Assumption: use external interrupt 0 for the pulse input. Use timer 0 in gated mode.
Note: to measure pulse low time in this manner, the input must be inverted externally.
;External interrupt 0 service routine. ExInt0: CLR TR0 MOV ValH,TH0 MOV ValL,TL0 MOV TH0,#0 MOV TL0,#0 SETB TR0 RETI
;Stop timer. ;Save timer value. ;Clear timer 0 for measurement. ;Restart timer.
start timer
stop timer
Note: this method may entail some loss of precision due to the possibility of variable interrupt latency.
;External interrupt 0 service routine. ExInt0: CPL JB MOV MOV MOV MOV INT0EX: RETI TR0 TR0,INT0EX ValH,TH0 ValL,TL0 TH0,#0 TL0,#0 ;Clear timer so another sample can ; be taken. ;Complement the timer run flag. This starts ; and stops the timer on alternate interrupts. ;Exit if timer is running. ;Otherwise sample the timer value.
timer
stop pulse
Note: the precision of pulses generated using this method will vary depending on the interrupt latency of the timer interrupt.
;Tiimer 0 interrupt routine. T0INT: CLR CLR RETI P2.0 TR0 ;End of pulse (use SETB for a low going pulse). ;Stop timer.
repeat
Note: the precision of pulses generated using this method will vary depending on the interrupt latency of the timer interrupt.
Note: for higher frequency pulses, it may be possible to use the timer reload feature (mode 2) to obtain more accurate pulse durations.
{
{
0000
BlockMove: MOV MOV MOV MOV MOV Loop: MOVX MOVX INC INC MOV JNZ INC DJNZ DJNZ RET
R0,#LOW(FromAddr) P2,#HIGH(FromAddr) DPTR,#ToAddr R1,#LOW(ByteCount) R2,#HIGH(ByteCount) A,@R0 @DPTR,A DPTR R0 A,R0 L1 P2 R1,Loop R2,Loop
;Initialize 'from' memory pointer. ;Initialize 'to' memory pointer. ;Initialize byte count.
;Read in a source block byte. ;Write byte out to destination block. ;Increment 'to' memory pointer. ;Increment 'from' memory pointer.
L1:
Exit
Exit
Receive?
Done? N
Exit
First bit?
Y
Done? N
Look for STOP (set error flag and abort if not found).
Look for START (set error flag and abort if not found).
Exit
(CONTINUED)
Fully documented. User's manual includes experiments for learning the development board and the 80C51 architecture. Switches, LEDs, and a potentiometer are included to allow simple experiments to be performed without additional circuitry.
SUPPORTED MICROCONTROLLE RS
Type 1 (full support via RS-232 to PC) 8031/51 8032/52 8xC31/51 8xC32/52 8xC451 8xC550 8xC552 8xC528 8xC652/654 8xC851
SUPPORTED MICROCONTROLLERS
(CONTINUED) Type 2 (limited support via I2C bus to type 1 device and PC) 8xC410 8xC751 8xC752
The 8xC751 and 8xC752 have no external program memory capability and do not support user program loading on the DB-51. The 8xC410 does not have an on-chip UART and must be communicated with via its I2C port. The current version of the DB-51 does not support user program loading on the 8xC410.
RS -232 Interface
Type 1 Microcontroller
I2C bus
Type 2 Microcontroller
EMULA TORS
Nohau Corp. 51 E. Campbell Ave. Campbell, CA 95008 (408) 866-1820 MetaLink Corp. 325 E. Elliot Road, Suite 23 Chandler, AZ 85225 (602) 926-0797 Ceibo Ltd. 105 Gleason Rd. Lexington, MA 02173 (617) 863-9927 And others... Signum Systems 171 E. Thousand Oaks Blvd., #202 Thousand Oaks, CA 91360 (805) 371-4608 BSO/Tasking 128 Technology Center P.O.Box 9164 Waltham, MA 02254-9164 (617) 894-7800
8051 C COMPILERS
2500 AD Software, Inc. 109 Brookdale Avenue P.O. Box 480 Buena Vista, CO 81211 (800) 843-8144
Franklin Software, Inc. 888 Saratoga Ave., #2 San Jose, CA 95129 (408) 296-8051 And others...
Download software:
- Public Domain 80C51 support tools - Demonstration code Send messages to Signetics applications engineers (800) 451-6644 or (408) 991-2406