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).

  1. #pragma udata DATA // section DATA
  2. int large_table[768];

Step 2. You can access the object large_table through a pointer table_ptr. Here, table_ptr points to the first element of the array. You can read and modify elements of the array using table_ptr[ADDRESS].

  1. #pragma udata // return to default section
  2. int *table_ptr = &large_table[0];

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 NAME, START and 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).

  1. // File: 18f4550_bigMem.lkr
  2. // Linker scrip for PIC18F4550 and storing a large array of 1536 bytes
  3.  
  4. LIBPATH .
  5.  
  6. FILES c018i.o
  7. FILES clib.lib
  8. FILES p18f4550.lib
  9.  
  10. CODEPAGE NAME=page START=0x0 END=0x7FFF
  11. CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
  12. CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
  13. CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
  14. CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
  15.  
  16. ACCESSBANK NAME=accessram START=0x0 END=0x5F
  17. DATABANK NAME=gpr0 START=0x60 END=0xFF
  18. DATABANK NAME=gpr1 START=0x100 END=0x1FF
  19. DATABANK NAME=data START=0x200 END=0x7FF PROTECTED
  20. ACCESSBANK NAME=accesssfr START=0xF60 END=0xFFF PROTECTED
  21.  
  22. SECTION NAME=CONFIG ROM=config
  23. SECTION NAME=DATA RAM=data
  24.  
  25. STACK SIZE=0x100 RAM=gpr1

Comments

Thanks

This is reallly nice.

Altough C18 has a lot of power, i didn't realize about the trouble of mememory banks up to now.

Thanks for the help!!

Syndicate content