Keil Logo

C51: Offsets with Far Memory Access


Information in this article applies to:

  • C51 Version 7

QUESTION

I need to manage an EEPROM on an Atmel device, and I am using the 'far' memory type as explained in Keil\C51\EXAMPLES\FarMemory\E2PROM on 80C51RD2. Within the EEPROM, I store offsets to other elements using WORD variables. However, when I try to access these objects, the code fails.

My access code looks like:

unsigned int far offset;    // an offset in EEPROM that points to other elements
unsigned char far *p, c;
  :
  p = (unsigned char far *) offset;
  c = *p;                        // this access fails

It seems to be impossible to access EEPROM space by just using an offset.

Do you have a solution to that problem?

ANSWER

Yes, there is a solution. You can use the FARRAY macros to define an EEPROM memory. The following example shows you how to do that.

#include

unsigned int far i[3];

unsigned char far *p;
unsigned char uc;

// macro that allows easy EEPROM access
#define EEPROM FARRAY (unsigned char, 0x20000)

void test (void)  {
// construct without macro
  p = (unsigned char far *) &(FARRAY (unsigned char, 0x20000)[i[0]]);

// using macros
  p  = &EEPROM [5];
  uc =  EEPROM [i[3]];
  p  = &EEPROM [uc];
}

MORE INFORMATION

  • Refer to FARRAY in the Cx51 User's Guide.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.