Application Note AT89S8253 Primer 8051 Flash Microcontroller
Application Note AT89S8253 Primer 8051 Flash Microcontroller
1. Introduction Flash
The Atmel® AT89S8253 microcontroller is a low-power, high-performance device fea-
turing 12K bytes of Flash memory, 2K bytes of EEPROM, and a Serial Peripheral Microcontrollers
Interface (SPI). The Flash and EEPROM memories may be reprogrammed in-system
via the SPI. The EEPROM provides applications with re-writable, nonvolatile data
AT89S8253 Primer
storage. These features, and others, are described in this application note. Code sam-
ples are provided. Additional information on the AT89S8253 microcontroller can be
found in the datasheet. The AT89S8253 device is meant as a replacement for the Application Note
AT89S8252 and AT89S53 devices.
2. Memory Organization
52K BYTES
EXTERNAL
64K BYTES
EXTERNAL
OR
3000
AND
2FFF
12K BYTES
INTERNAL
0000 0000
(EA PIN HIGH) (EA PIN LOW)
3655B–MICRO–3/07
2.2 Data Memory
Figure 2-2 shows a map of AT89S8253 data memory, which consists of 256 bytes of internal
RAM, the Special Function Registers (SFRs), 2K bytes of on-chip EEPROM and, optionally, up
to 64K bytes of external memory.
To the left in Figure 2-2 are shown the 256 bytes of internal RAM and the SFRs, which shadow
the upper 128 bytes of internal RAM. The lower 128 bytes (00H - 7FH) of internal RAM are
accessible by both direct and indirect addressing, while the upper 128 bytes (80H - FFH) are
accessible by indirect addressing only. The SFRs (80H - FFH) are accessible by direct address-
ing only. The addressing mode of an instruction distinguishes accesses to the upper 128 bytes
of internal RAM from accesses to the overlapping SFRs.
The stack, which grows upward, may reside anywhere in the 256 bytes of internal RAM.
To the right in Figure 2-2 are shown the 2K bytes of on-chip EEPROM and the optional 64K
bytes of external data memory. Although the EEPROM is internal, it is shown in the diagram
shadowing the lower 2K bytes of external data memory because some of the same instructions
are used to access EEPROM as are used to access external data memory.
FF
64K BYTES
INDIRECT EXTERNAL
ADDRESSING 80
ONLY
80
7F
AND
DIRECT
AND INDIRECT 7FF
ADDRESSING
00 0000
EEPROM (MOVX
INDIRECT ADDRESSING)
000
3. EEPROM
The AT89S8253 includes 2K bytes of on-chip EEPROM for nonvolatile data storage. EEPROM
and external data memory are accessible by indirect addressing only, utilizing the MOVX
instructions, which come in two modes: 8-bit and 16-bit. Only the 16-bit MOVX instructions
(those utilizing DPTR) may be used to access internal EEPROM. The 2K bytes of EEPROM are
accessed at addresses 000H - 7FFH.
Accesses to EEPROM are distinguished from accesses to external data memory by the state of
the EEMEN bit in SFR EECON (96H). To access EEPROM, EEMEN is set; to access external
data memory, EEMEN is cleared. Reset clears EEMEN. To enable write accesses to EEPROM,
bit EEMWE in SFR EECON must also be set. Reset clears this bit, disabling EEPROM writes. It
is not necessary to explicitly erase any portion of EEPROM before writing new data.
Sample code showing EEPROM reads and writes is presented in Section 3.3.
Reset
X X 0 0 0 0 X X
Value
EEWEN
EELD
tWC tWC
RDY/BSY
MOVX
EEWEN
EELD
tWC
RDY/BSY
MOVX
3
3655B–MICRO–3/07
3.2.1 DATA Polling
The end of an EEPROM programming cycle may also be determined utilizing the DATA Polling
method, in which the location written is read repeatedly. During programming, the most signifi-
cant bit of the data read is the complement of the data bit written. When programming is
complete, true data is returned. The return of true data also serves as verification of the write
operation.
; EEPROM byte write example, utilizing fixed delay for write cycle.
; Delay is worst case (10 ms). Code for delay is not shown.
; Write is preceded by a write inhibit check, but code to handle an
; inhibit condition is not shown. Write is followed by verify
; (read and compare), but code to handle verification failure
; is not shown.
; EEPROM byte write example, utilizing DATA Polling to determine the end of
; the write cycle. Write is preceded by a write inhibit check, but code to
; handle an inhibit condition is not shown. After data is loaded, the code
; loops on read until data is returned true. Write verification is implicit
; in this method. Needs timeout to prevent write error from causing an
; infinite loop.
5
3655B–MICRO–3/07
mov dptr, #ADDRESS ; address to write
mov a, #DATA ; data to write
movx @dptr, a ; write EEPROM
loop:
movx a, @dptr ; read EEPROM
cjne a, #DATA, loop ; jump if data compare fails (busy)
7
3655B–MICRO–3/07
copy:
movx a, @dptr ; read external data memory
inc dptr ; advance external data memory pointer
xrl EECON, #DPS ; switch data pointers
movx @dptr, a ; write EEPROM
inc dptr ; advance EEPROM pointer
xrl EECON, #DPS ; switch data pointers
wait1:
mov a, EECON ; get EEPROM write status
anl a, #RDY ; check RDY/BSY
jnz wait1 ; jump if not busy
wait2:
mov a, EECON ; get EEPROM write status
anl a, #RDY ; check RDY/BSY
jz wait2 ; jump if busy
djnz r7, copy ; continue until done
6. Watchdog Timer
The AT89S8253 features a watchdog timer which allows control of the microcontroller to be
regained, should it be lost. When enabled, the timer will reset the microcontroller after a speci-
fied period has elapsed, unless prevented from doing so by the intervention of the firmware. The
AT89S8253 watchdog has two operating modes: software mode (from AT89S8252 and
AT89S53) and hardware mode (from AT89S52). Note that the watchdog on AT89S8253 is con-
trolled by SFR WDTCON at address A7H as compared with WMCON (96H) on AT89S8252 and
AT89S53.
To enable the watchdog timer in software mode, the WDTEN bit in SFR WDTCON (A7H) must
be set and the HWDT bit must be cleared; to disable the timer, WDTEN should be cleared. Once
the timer is enabled, the firmware must set the WSWRST bit in SFR WDTCON (or disable the
timer) before the reset period elapses to prevent the timer from resetting the microcontroller.
Each time WSWRST is set, a new reset period begins, requiring another response from the firm-
ware. The firmware does not need to clear WSWRST after setting it; WSWRST is automatically
cleared by the microcontroller.
To enable the watchdog timer in hardware mode, the HWDT bit in WDTCON must be set and
the sequence 1EH/E1H must be written to SFR WDTRST (A6H). In hardware mode, the watch-
dog can only be disabled by a device reset. The WDTEN bit will be forced high when the
hardware watchdog mode is enabled. Once the hardware watchdog timer is enabled, the firm-
ware must write the 1Eh/E1h sequence to WDTRST before the reset period elapses to prevent
the timer from resetting the microcontroller. Each time WDTRST is written with the correct
sequence, a new reset period begins, requiring another response from the firmware.
The watchdog timer reset period varies from 16K to 2048K system clock cycles, as specified by
bits PS0, PS1 and PS2 in WDTCON. Refer to the AT89S8253 datasheet for the nominal reset
periods corresponding to the bit settings. The timer reset period of the AT89S8253 depends on
the frequency of the clock source driving the microcontroller. If the watchdog timer times out, it
will generate a reset pulse lasting 96 clock periods. The RST pin will also be pulled high for the
duration of the reset, unless the DISRTO bit in WDTCON was set prior to the time-out. Reset
(including reset generated by the watchdog timer) clears WDTEN, WSWRST, HWDT, WDIDLE,
DISRTO, SP0, SP1 and SP2, disabling the watchdog timer.
When WDIDLE = 0, the watchdog timer continues to operate even when the microcontroller is in
Idle mode. To prevent the watchdog from timing out during Idle, an interrupt such as a timer
overflow must be used to wake up the device periodically and reset the watchdog, or the WDI-
DLE bit may be set to disable the watchdog during Idle. The watchdog is always disabled during
Power-down mode. To prevent the watchdog from timing out immediately upon exit from
Power-down (or Idle with WDIDLE set), the watchdog should be reset just before entering
Power-down (or Idle).
Reset
0 0 0 0 0 0 0 0
Value
9
3655B–MICRO–3/07
6.1 Listing 4: Watchdog Timer Example
; Use the software watchdog timer to regain control of the microcontroller
; if an operation takes longer than expected. The details of the operation
; are not shown. The operation is expected to take less than 20 ms to
; complete and the reset period chosen is 32 ms. Adequate margin must be
; allowed between the desired reset period and the selected period to allow
; for the slop present in the timer.
; The EECON register is not bit-addressable, so Boolean operations are used.
loop:
; Do something which normally takes less than 20 ms.
.
.
.
8. Enhanced UART
The serial port (UART) of the AT89S8253 includes the enhanced features of framing error
detection and automatic address recognition.
11
3655B–MICRO–3/07
To use framing error detection with Modes 2 or 3, first set SM0 while SMOD0 = 0 and then set
SMOD0 to map FE into SCON.
13
3655B–MICRO–3/07
To enable the SPI feature, the SPE bit in SFR SPCR must be set; to disable the SPI, SPE is
cleared. When the SPI is enabled, microcontroller pins P1.4, P1.5, P1.6 and P1.7 become SS,
MOSI, MISO, and SCK, respectively. The SPI may not operate correctly unless pins P1.4 - P1.7
are first programmed high. Reset sets pins P1.4 - P1.7 high and clears SPE, disabling the SPI.
Note that because P1.7 starts high, enabling the SPI in master mode with CPOL = 0 will result in
a falling edge on SCK and clearing either MSTR or SPE will generate a rising edge on SCK. To
prevent the slaves from being clocked by these edges, all slaves should be disabled with either
SS = 1 or SPE = 0 when enabling/disabling the master.
The MSTR bit in SFR SPCR configures the microcontroller as a SPI master when set, and as a
slave when cleared. Reset clears MSTR. When the microcontroller is configured as a SPI mas-
ter, SS (P1.4) is not utilized and may be used as a general-purpose, programmable output.
When the microcontroller is configured as a SPI master, the frequency of the serial clock is
determined by bits SPR0 and SPR1 in SFR SPCR. The frequency of the serial clock is the fre-
quency of the microcontroller’s clock source divided by the selected divisor. The divisor must be
selected to produce a serial clock frequency which is compatible with the master’s slaves. Refer
to the AT89S8253 datasheet for the divisors corresponding to the settings of bits SPR0 and
SPR1.
The DORD bit in SFR SPCR determines the order in which the bits in the serial data are trans-
ferred. Data is transferred least-significant bit (LSB) first when DORD is set; most-significant bit
(MSB) first when DORD is cleared. Reset clears DORD. Note that only MSB-first data transfers
are shown in the diagrams in the AT89S8253 datasheet.
The polarity of the SPI serial clock is determined by the CPOL bit in SFR SPCR. Setting CPOL
specifies serial clock high when idle; clearing CPOL specifies serial clock low when idle. Reset
clears CPOL.
The CPHA bit in SFR SPCR controls the phase of the SPI serial clock, which defines the rela-
tionship between the clock and the shifting and sampling of serial data. Setting CPHA specifies
that data is to be shifted on the leading edge of the clock and sampled on the trailing edge.
Clearing CPHA specifies that data is to be sampled on the leading edge of the clock and shifted
on the trailing edge. Reset sets CPHA. Examples of SPI serial clock phase and polarity are
shown in the diagrams in the AT89S8253 datasheet.
Only an AT89S8253 configured as an SPI master may initiate a data transfer. A data transfer is
triggered by a byte written to SFR SPDR (86H), the SPI Data Register. As data is shifted out of
the master, data from the selected slave is simultaneously shifted in, replacing the data in
SPDR. When a data transfer is complete, the SPIF bit is set in SFR SPSR (AAH), the SPI Status
Register. The data received from the slave may then be read from SPDR. Writing SPDR during
a data transfer sets the Write Collision bit (WCOL) in SPSR. The progress of the data transfer is
not affected by a collision. The SPIF and WCOL bits cannot be cleared directly by software. To
clear bits SPIF and WCOL, first read SPSR and then read or write SPDR. This operation gener-
ally occurs within the normal operation of the SPI routine, e.g., polling the status of SPIF reads
the SPSR register and then reading the received value from SPDR or sending the next value by
writing SPDR automatically clears the flags.
An interrupt may be generated as an alternative to polling SPIF to determine the end of a SPI
data transfer. To enable the SPI interrupt, three bits must be set. The first is the SPIE bit in
SPCR, which causes an interrupt to be generated when SPIF is set. The second and third are
the EA and ES bits in SFR IE (A8H). EA is the global interrupt enable bit. The SPI shares an
interrupt vector with the UART, so the UART enable ES must also be set. When an SPI interrupt
occurs, the SPI/UART interrupt service routine must determine the source of the interrupt. An
SPI interrupt is indicated when the SPIF bit in SPSR is set.
SCK
MOSI 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
MISO 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
Write
SPDR
WCOL
LDEN
SPIF
Read
SPDR
15
3655B–MICRO–3/07
9.2 Listing 6: SPI Example
In the application shown below, the AT89S8253 is configured as an SPI master and interfaces to
an Atmel AT25040 SPI-compatible EEPROM. The EEPROM provides 512 bytes of re-writable,
nonvolatile storage while requiring only a four-pin interface to the microcontroller. The microcon-
troller and EEPROM are wired as shown in Figure 9-2. Note that the microcontroller’s SS pin is
used as a slave select, since it is unused when the microcontroller is configured as a SPI mas-
ter. Additional EEPROMs may be connected to the microcontroller’s SCK, MISO and MOSI pins,
but each device must have its own select line.
Sample code for the application is shown in Section 9.2. A SPI master must be configured to
meet the requirements of its slaves. The AT25040 datasheet states that the maximum clock rate
for the device is 2 MHz. The microcontroller’s clock source is a 24-MHz crystal (Figure 9-2), so a
SPI serial clock divisor of 16 was chosen to produce a serial clock of 1.5 MHz. As shown in the
AT25040 datasheet, the device’s chip select (CS) input must remain active (low) for the duration
of an operation, which may include multiple data transfers. Also, the serial clock must be low
when idle and data is transferred most-significant bit first. Therefore, CPHA = 1, CPOL = 0 and
DORD = 0. In the example, SPI interrupts are not used.
VCC VCC
C2
U1
5 pF 31 39
EA/VPP P0.0
+ C1 38
P0.1
1 uF Y1 19 XTAL1 37
24 MHz P0.2
P0.3 36
C3 P0.4 35
18 34
XTAL2 P0.5
P0.6 33
5 pF 9 RST 32
P0.7
10 21
P3.0/RXD P2.0
11 22
P3.1/TXD P2.1
12 23
SPI Master Mode P3.2/INT0 P2.2
13 24
P3.3/INT1 P2.3
14 P3.4/T0 25
P2.4
15 P3.5/T1 P2.5 26
27
P2.6
1 P1.0/T2 28
P2.7
2 P1.1/T2EX
VCC U2 3 P1.2 29
PSEN
CS 1 4 P1.3 ALE/PROG
30
7 HOLD
SCK 6 5 P1.4/SS
3
WP SI 5 6 P1.5/MOSI
2 7
SO P1.6/MISO P3.6/WR 16
8 P1.7/SCK P3.7/RD 17
AT25040
AT89S8253
main:
; SPI master mode initialization code.
17
3655B–MICRO–3/07
; Write one byte to AT25040 and verify (read and compare).
; Code to handle verification failure is not shown.
; Needs timeout to prevent write error from causing an infinite loop.
enable_write:
; Enable write.
; Does not check for device ready before sending command.
; Returns nothing. Destroys A.
read_byte:
write_byte:
masterIO:
; Send/receive data through the SPI port.
; A byte is shifted in as a byte is shifted out,
; receiving and sending simultaneously.
; Waits for shift out/in complete before returning.
; Expects slave already selected.
; Called with data to send in A. Returns data received in A.
19
3655B–MICRO–3/07
Headquarters Operations
Atmel Corporation Memory RF/Automotive
2325 Orchard Parkway 2325 Orchard Parkway Theresienstrasse 2
San Jose, CA 95131, USA San Jose, CA 95131, USA Postfach 3535
Tel: 1(408) 441-0311 Tel: 1(408) 441-0311 74025 Heilbronn
Fax: 1(408) 487-2600 Fax: 1(408) 436-4314 Germany
Tel: (49) 71-31-67-0
Microcontrollers Fax: (49) 71-31-67-2340
International 2325 Orchard Parkway
San Jose, CA 95131, USA 1150 East Cheyenne Mtn. Blvd.
Atmel Asia Tel: 1(408) 441-0311 Colorado Springs, CO 80906, USA
Room 1219 Fax: 1(408) 436-4314 Tel: 1(719) 576-3300
Chinachem Golden Plaza Fax: 1(719) 540-1759
77 Mody Road Tsimshatsui La Chantrerie
East Kowloon BP 70602 Biometrics
Hong Kong 44306 Nantes Cedex 3 Avenue de Rochepleine
Tel: (852) 2721-9778 France BP 123
Fax: (852) 2722-1369 Tel: (33) 2-40-18-18-18 38521 Saint-Egreve Cedex
Fax: (33) 2-40-18-19-60 France
Atmel Europe Tel: (33) 4-76-58-47-50
Le Krebs ASIC/ASSP/Smart Cards Fax: (33) 4-76-58-47-60
8, Rue Jean-Pierre Timbaud Zone Industrielle
BP 309 13106 Rousset Cedex
78054 Saint-Quentin-en-Yvelines Cedex France
France Tel: (33) 4-42-53-60-00
Tel: (33) 1-30-60-70-00 Fax: (33) 4-42-53-60-01
Fax: (33) 1-30-60-71-11
1150 East Cheyenne Mtn. Blvd.
Atmel Japan Colorado Springs, CO 80906, USA
9F, Tonetsu Shinkawa Bldg. Tel: 1(719) 576-3300
1-24-8 Shinkawa Fax: 1(719) 540-1759
Chuo-ku, Tokyo 104-0033
Japan Scottish Enterprise Technology Park
Tel: (81) 3-3523-3551 Maxwell Building
Fax: (81) 3-3523-7581 East Kilbride G75 0QR
Scotland
Tel: (44) 1355-803-000
Fax: (44) 1355-242-743
Literature Requests
www.atmel.com/literature
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any
intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND CONDI-
TIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY
WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDEN-
TAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT
OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no
representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications
and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided
otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel’s products are not intended, authorized, or warranted for use
as components in applications intended to support or sustain life.
© 2007 Atmel Corporation. All rights reserved. Atmel ®, logo and combinations thereof, Everywhere You Are® and others are registered trade-
marks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.
3655B–MICRO–3/07