Using variables larger than 256 bytes
Several applications require the use of variables larger than 256 bytes. For example, if you want to build a robot with a camera and process images, or add cool effects to an audio stream. But while the PIC18F4550 of the Dwengo Board contains 2048 bytes of data memory, the default linker script does not allow one to create variables larger than 256. This is fairly easy to fix though.
Creating larger variables is possible in 4 steps:
Step 1. Assign your own section,
DATA, to an object that is larger than 256 bytes. In the following example, we declare an array
large_table of 768 integers (1536 bytes).
- #pragma udata DATA // section DATA
- int large_table;
Step 2. You can access the object
large_table through a pointer
table_ptr points to the first element of the array. You can read and modify elements of the array using
- #pragma udata // return to default section
- int *table_ptr = &large_table;
Step 3. In the linker script, the total memory space of the PIC is divided into blocks (
DATABANK) that can each be assigned to a specific section (
SECTION). Edit the linker script (.lkr file) to assign a sufficiently large memory block. This is done using the keyword
DATABANK which you pass the
END parameters. In our example,
NAME=data starting at address 0x200h until 0x7FFh.
Note that in this example, the memory space we want to use is that large that we need to claim the memory originally used for the USB buffers. This means that, in this program, you can no longer use the USB functionality. Therefore, remove the
DATABANK lines in the linker script for USB (addresses 0x400h until 0x7FFh).
Step 4. Finally, the memory space we just created, which is called
data (in lowercase), can be assigned to the
DATA section (uppercase).
Here the code for the linker script (.lkr file).
- // File: 18f4550_bigMem.lkr
- // Linker scrip for PIC18F4550 and storing a large array of 1536 bytes
- LIBPATH .
- FILES c018i.o
- FILES clib.lib
- FILES p18f4550.lib
- CODEPAGE NAME=page START=0x0 END=0x7FFF
- CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
- CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
- CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
- CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
- ACCESSBANK NAME=accessram START=0x0 END=0x5F
- DATABANK NAME=gpr0 START=0x60 END=0xFF
- DATABANK NAME=gpr1 START=0x100 END=0x1FF
- DATABANK NAME=data START=0x200 END=0x7FF PROTECTED
- ACCESSBANK NAME=accesssfr START=0xF60 END=0xFFF PROTECTED
- SECTION NAME=CONFIG ROM=config
- SECTION NAME=DATA RAM=data
- STACK SIZE=0x100 RAM=gpr1