atmega161-4pi ATMEL Corporation, atmega161-4pi Datasheet - Page 113

no-image

atmega161-4pi

Manufacturer Part Number
atmega161-4pi
Description
8-bit Microcontroller With 16k Bytes Of In-system Programmable Flash
Manufacturer
ATMEL Corporation
Datasheet
Fill the Temporary Buffer
Perform a Page Write
Addressing the FLASH during
Self-programming
1228D–AVR–02/07
To write an instruction word, set up the address in the Z-pointer and data in R1:R0, write
“0001” to SPMCR, and execute SPM within four clock cycles after writing SPMCR. The
content of Z6:Z1 is used to address the data in the temporary buffer. Z13:Z7 must point
to the page that is supposed to be written.
To execute a page write, set up the address in the Z-pointer, write “0101” to SPMCR,
and execute SPM within four clock cycles after writing SPMCR. The data in R1 and R0
are ignored. The page address must be written to Z13:Z7. During this operation, Z6:Z0
must be zero to ensure that the page is written correctly. When a page write operation is
completed, the Z-pointer will point to the first word in the successive page.
The Z-pointer is used to address the SPM commands.
Z15:Z14 always ignored
Z13:Z7
Z6:Z1
Z0
The only operation that does not use the Z-pointer is setting the Boot Loader Lock bits.
The content of the Z-pointer is ignored and will have no effect on the operation.
Note that the page erase and page write operation are addressed independently. There-
fore, it is of major importance that the Boot Loader software addresses the same page in
both the page erase and page write operations.
The LPM instruction also uses the Z-pointer to store the address. Since this instruction
addresses the Flash byte-by-byte, the LSB (bit Z0) of the Z-pointer is also used. See
page 16 for a detailed description.
Accidental writing into Flash program by the SPM instruction is prevented by setting up
an “SPM enable time window”. All accesses are executed by first setting I/O bits, and
then by executing SPM within four clock cycles. The I/O Register that controls the SPM
accesses is defined below.
Bit
$1F ($1F)
$1E ($1E)
Code Example
Wait:
sbrc r16,SPMEN
rjmp Wait
ldi
out
spm
page select, for page erase, page write
word select, for filling temp buffer (must be zero during page write operation)
should be zero for all SPM commands, byte select for the LPM instruction
Z15
in
r16,(1<<PGWRT) + (1<<SPMEN) ; The previous writing is completed,
SPMCR,r16
15
Z7
7
r16,SPMCR
Z14
14
Z6
6
; Wait for SPMEN to be cleared (indicates that previous
; if not cleared, keep waiting
; output to register
; start the erase operation
write operation is completed)
Z13
Z5
13
5
; read SPMCR Register
Z12
12
Z4
4
set up for next erase
Z11
Z3
11
3
Z10
Z2
10
2
ATmega161(L)
Z9
Z1
9
1
Z8
Z0
8
0
ZH
ZL
113

Related parts for atmega161-4pi