In a recent project, I learned about the external Ram Extension of the AVR microcontroller, which is recorded here.
In this paper, we use a single-chip microcomputer with a single-chip microcomputer as the testing platform and expanded 74hc573 (AHC series are used in the data section. However, I used the HC series to configure the fastest access speed under the 16 m crystal oscillator and did not find anything wrong, of course this is not a rigorous design) and 62256 chip.
I use the GCC compiler and C ++ for programming in the project. Theoretically, as long as the external memory is enabled and configured, the variable Address allocation can be done by the compiler, but the premise is that the memory enabling function and configuration code should be executed first after the power-on reset, and C or C ++ should be used to compile the program, after power-on reset, the first code to be executed is the startup code automatically added by the compiler and the constructor code. I don't know how to make the memory performance and configuration code run first after the power-on reset, so I wrote the following macro to control access to the external memory.
The Code is as follows:
// Extern_ram.h
# Ifndef _ h_extern_ram_h _
# DEFINE _ h_extern_ram_h _
# Include "type. H"
# Define def_ex_var (name) ex_var name;
# Define init_ex_var (name, ele_size, ele_n) do {\
Name. size_ele = ele_size ;\
Name. n_ele = ele_n ;\
Name. ADDR = P ;\
P + = (ele_size) * (ele_n ));\
} While (0)
# Define wr_ex_var (name, POs, pdat) do {\
For (uint8 IIIII = "0"; IIIII <name. size_ele; IIIII ++ )\
(Name. ADDR + (POS) * name. size_ele) [IIIII] = * (int8 *) pdat + iiiii );\
} While (0)
# Define rd_ex_var (name, POs, pdat) do {\
For (uint8 IIIII = "0"; IIIII <name. size_ele; IIIII ++ )\
* (Int8 *) pdat + iiiii) = (int8 *) (name. ADDR + (POS) * name. size_ele) [IIIII]; \
} While (0 );
# Define get_p_ex_var (name, POS) (name. ADDR + (POS) * name. size_ele)
Typedef struct _ tag_ex_var
{
Uint8 size_ele;
Uint16 n_ele;
Int8 * ADDR;
} Ex_var;
# Endif
Application Example
// Main. cpp
# Include "des. H"
Int8 * P = (int8 *) 0x3100; // The external RAM address starts from 0x3100
Def_ex_var (TEST); // defines an external variable
Int16 main (void)
{
Uint8 I;
Int16 TMP;
Int16 * p_ele;
Mcucr | = 0x80; // enables xram and configures the fastest access speed.
Init_ex_var (test, sizeof (int16), 100); // Initialize an external variable with 100 elements. The element size is sizeof (int16)
TMP = "0 xaaaa ";
For (I = 0; I <100; I ++)
{
Wr_ex_var (test, I, & TMP); // write example. All elements of the test variable are assigned 0 xaaaa.
}
For (I = 0; I <100; I ++)
{
Rd_ex_var (test, I, & TMP); // read example
}
P_ele = (int16 *) get_p_ex_var (test, 32); // gets the pointer to the first element of the test variable.
While (1 );
Return 0x00;
}
Postscript:
1. Modify the header file.
2. If you want to use it in other compilers, pay attention to the memory layout of the variables. This article describes how to use the big-end mode.
3. Do not use IIIII for the naming of local variables, otherwise they will be overwritten during macro expansion.
4. The article is rough, but in order to show respect for the work of the author, please indicate the source and retain the copyright information.