cs-280 dr. mark l. hornick 1 storing data in program memory special-purpose load and store...
TRANSCRIPT
CS-280Dr. Mark L. Hornick
1
Storing data in Program Memory
Special-purposeLoad and Store Instructions
CS-280Dr. Mark L. Hornick
2
Atmega32 Memory
Address bus (16-bit in Atmega32) A unique 16-bit address
references each memory byte.
Data bus (8-bit)
Volatile – RAM (fast RW) SRAM (temp data store) DRAM
Nonvolatile – ROM (fast R – slow W) ROM PROM EPROM EEPROM (permanent data
store) Flash ROM (program store)
CS-280Dr. Mark L. Hornick
3
SRAM is volatile memory
Power must be supplied to maintain values stored in SRAM
SRAM values disappear when power is shut off
SRAM is cleared when power is first applied
CS-280Dr. Mark L. Hornick
4
Review: Program vs. Data Memory Addressing
Program Memory is organized and accessed in words A word is 2 bytes Each word has a unique
address
Data Memory is organized and accessed in bytes Each byte has a unique
address
Byte 0
Byte 2
Byte 4
Byte 6
Byte 8
0x0000
0x0001
0x0002
0x0003
0x0004
Byte 1
Byte 3
Byte 5
Byte 7
Byte 9
Byte 0
Byte 1
Byte 2
Byte 3
Byte 4
0x0060
0x0061
0x0062
0x0063
0x0064
CS-280Dr. Mark L. Hornick
5
Review: Allocating SRAM Data memory.DSEG ; subsequent directives refer to data segment.ORG SRAM_START ; address where Data Memory starts (0x60)x1: .byte 1 ; reserve 1 byte of SRAM, assign label x1=0x60x2: .byte 2 ; reserve 2 bytes of SRAM, assign label x2=0x61x3: .byte 1 ; reserve 1 byte of SRAM, assign label x3=0x63.CSEG ; switch further directives to code segment.ORG 0x2A ; set addr for start of program instructions
LDS R20, x1 ; load value at data addr specified by x1STS x2, R20 ; store value in R20 to data addr x2
The addresses are assigned automatically by the Assembler The .byte n directive tells the assembler to allocation n bytes Be very careful about using .ORG for addresses < 0x60
CS-280Dr. Mark L. Hornick
6
Review: The X, Y, and Z Registers
The X, Y, and Z 16-bit registers overlap the last six 8-bit registers R26 through R31
CS-280Dr. Mark L. Hornick
7
Review: Indirect Addressing
Accesses a 8-bit (byte) value from SRAM data memory at the address specified indirectly via the 16-bit X, Y, or Z index-registers
Example: LD R20, X ; load value at data addr held in X to R20 ST Y, R20 ; store value in R20 to data addr held in Y Note X, Y, Z hold data address values that are 16-bits
CS-280Dr. Mark L. Hornick
8
Data values can be stored permanently by allocating them to non-volatile Program Memory
.DSEG ; subsequent directives refer to data segment
.ORG SRAM_START ; address where Data Memory starts (0x60)x1: .byte 5 ; reserve 5 bytes of (uninitialized) volatile SRAM
.CSEG ; switch further directives to code segment
.ORG 0x2A ; set addr for start of program instructionsy1: .db 1,5 ; alloc 2 bytes in Prog Memory with initial values of 1 and 5title: .db ‘c’,’s’,’2’,’8’,’0’,0 ; allocate 6 bytes in Program Memorycourse: .db “CS-280”, 0 ; allocate 7 bytes in Program MemoryNote assembler does NOT automatically insert a NULL char at the end of the string
The .byte n directive tells the assembler to allocate n bytes in Data Memory No initial value can be specified to SRAM data memory values
The .db n,m,… (“define byte”) directive tells the assembler to allocate and store the bytes n,m… in Program Memory The initial values of the memory are specified The assembler always starts data on a word boundary in memory
CS-280Dr. Mark L. Hornick
9
Review: ASCII Encoding
For example, character 'O' is 79 (row value 70 + col value 9 = 79).
For example, character 'O' is 79 (row value 70 + col value 9 = 79).
O
9
70
CS-280Dr. Mark L. Hornick
10
Accessing (reading) data from Program Memory
There are three instructions to load (read) data from Program Memory to a register
LPM loads from byte address in Z register to R0,
where Z and R0 are implied arguments
LPM Rn, Z loads from byte address in Z to Rn (R0-R31); note
that X and Y are not allowed
LPM Rn, Z+ Same as above, but increments Z after loading
CS-280Dr. Mark L. Hornick
11
Big Red Flag
When using LPM, the Z register must contain the data’s byte address
Recall: Program Memory is organized by words – each word has an address Byte address is 2*word address
CS-280Dr. Mark L. Hornick
12
How do you load a byte address value into Z?
.CSEG ; switch further directives to code segment
.ORG 0x2A ; set addr for start of program instructionsLDI ZL, LOW(2*values) ; load ZL with the low 8 bits of the addressLDI ZH, HIGH(2*values) ; load ZH with the high 8 bits of the addressLPM R20, Z ; load Program Memory byte addr contained in Z
values: .db 5, 10, 11, 12, 13 ; define 5 bytes of Program Memory
Use LOW() and HIGH() are Assembler functions “values” label contains the word address of the
location of the first reserved byte The assembler expression “2*values” correctly
computes the associated byte address
CS-280Dr. Mark L. Hornick
13
There are certain conventions for defining Program Memory
The .db directives are placed after program instructions, so that the data itself is placed in memory after the instruction opcodes
When defining numeric data values, the first value specifies the number of values to follow:
y1: .db 1,5 ; alloc 2 bytes in Prog Memory with initial values of 1 and 5
When defining character or string data values, the last value speicified should always be zero, indicating that the character data is terminated with a null-character:
title: .db ‘c’,’s’,’2’,’8’,’0’,0 ; allocate 6 bytes in Program Memorycourse: .db “CS-280”, 0 ; allocate 7 bytes in Program Memory
CS-280Dr. Mark L. Hornick
14
Note: The Z register can also be incremented explicitly
ADIW ZH:ZL, 5 ; add immediate value (to Z) The value may be 0-63 (decimal)
So you can use ADIW in conjuction with LPM and LPM Rn, Z instructions to post-increment the Z register to point to the address of the next character in program memory
CS-280Dr. Mark L. Hornick
15
There is also an SPM instruction….
But due to the nature of Flash memory, writing to it is more complex Before writing, an entire page of memory must be erased Page size is device-specific On some devices, only entire pages can be written
In CE2800, use the .DB directive to write to Flash memory Only writes once, when the program is downloaded to the
device