cancel
Showing results for 
Search instead for 
Did you mean: 

C51: Memory accesses providing strange results

dtylski
Associate
Posted on July 29, 2014 at 16:27

Hi,

I would like to share some knowledge about a point that puzzled me for 3 days.

In Keil µVision (C51), if you write the following:

    char *ptr = (char *)0x8000;

for accessing data located in some Flash memory for example... You won't get the expected results: the pointer will read elsewhere.

Actually the pointer does not hold on 2 bytes as expected, but on 3 (generic pointer), however the explicit cast does not provide you the 3rd byte necessary to access XDATA (for example).

Some pointers hold on 1 byte: for accessing IDATA.

In such cases, advice is to _always_ specify which address space it is pointing to:

    char xdata *ptr = (char xdata *)0x8000;

so that the generated assembly code makes use of the appropriate opcodes (DPTR in the case of XDATA). This pointer is really 2-byte long and will allow you to read/write at the right location.

Note that any function prototype taking a pointer as argument is concerned with this issue.

If the destination is not known at compile-time (because of a generic function), use the keyword ''generic'' (but don't try to cast an integer to a generic pointer unless you really know how it will be decoded).

_____

References:

http://www.keil.com/support/man/docs/c51/c51_le_ptrs.htm

http://www.keil.com/support/man/docs/c51/c51_le_memspecificptrs.htm

http://www.keil.com/support/docs/1012.htm

0 REPLIES 0