CCS News

Tech Note: Using addressmod() to allocate variables in a PIC® MCU access bank

Thursday 10 July, 2014

The CCS compiler addressmod() feature allows you to define custom memory spaces. This new function that replaces the older typemod() function now makes it easy to use the shared bank memory in many processors for specific variables your code uses. addressmod() allows for faster access to those variables.

addressmod(), supported in the CCS C Compiler, provides embedded C developers a way of declaring variable types with many custom parameters like specific read and write functions or memory ranges. This is useful in embedded applications because there are often several types of memory available in a design that need to be accessed, like RAM, ROM, EEPROM, Flash, and even LCDs. This article highlights a few details of addressmod(), for full details refer to the CCS C Compiler manual.

Recently the CCS C Compiler added a new optional parameters to addressmod() called 'share'. If this option is defined as TRUE, the CCS C Compiler will allow the addressmod() declared variable type to also be used by other variables but the addressmod() declared variables will have to be allocated in that region first. This is useful for the access of bank memory described above, as a developer may want to have some variables allocated in this region but leave it available for the compiler to use the remaining space for other variables.

The usage is:

addressmod(name, read_function, write_function, start_address, end_address, share);

Here is an example:

#if defined(__PCM__)
addressmod(near, , , 0x70, 0x76, TRUE);
#elif defined(__PCH__)
addressmod(near, , , 0x20, 0x5f, TRUE);
#elif defined(__PCD__)
addressmod(near, , , 0x1000, 0x1fff, TRUE);
#endif

near char flags;
char option;

The addressmod() code above can be copied and pasted into your projects to define a 'near' memory region that uses the PIC® MCU access bank.

'near' is an addressmod() created memory region that sits in the PIC® MCU access bank. Pre-processor #if / #else is used to make a 'near' for every applicable PIC® MCU architecture. Since the 'share' flag in addressmod() is set TRUE, the compiler will make this memory region available for general use once 'near' variables have been allocated. The variable 'flags' uses the 'near' memory region, meaning 'flags' is placed in the access bank. The 'option' variable does not have a memory region specified, so the compiler will use any RAM region available. If the user allocated more variables than can be saved in the 'near' region, the compiler will generate an 'Out of RAM' error message.

Like us on Facebook. Follow us on Twitter.

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit http://www.ccsinfo.com.

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.