char=int

B

bvermeersch

Hello,

I found this in some else's code:
....
void write_E2(unsigned int address, unsigned char data)
{
EEADR = address;
EEADRH = (address >> 8);
//The EEADRH:EEADR register pair is used to address
//the data EEPROM for read and write operations.
//EEADRH holds the two MSbits of the address; the
//upper 6 bits are ignored. The 10-bit range of the pair
//can address a memory range of 1024 bytes (00h to
//3FFh).
....
Is this a correct way of doing if you know:
....
extern volatile near unsigned char EEADR;
....

This means putting an int into a char.
will it choose the right byte, where can i find info about this?

Kind regards, Jef
 
N

Neil Kurzman

Hello,

I found this in some else's code:
...
void write_E2(unsigned int address, unsigned char data)
{
EEADR = address;
EEADRH = (address >> 8);
//The EEADRH:EEADR register pair is used to address
//the data EEPROM for read and write operations.
//EEADRH holds the two MSbits of the address; the
//upper 6 bits are ignored. The 10-bit range of the pair
//can address a memory range of 1024 bytes (00h to
//3FFh).
...
Is this a correct way of doing if you know:
...
extern volatile near unsigned char EEADR;
...

This means putting an int into a char.
will it choose the right byte, where can i find info about this?

Kind regards, Jef

No

AChar = (char)AnInt; // It is called a "Cast"

The code you have says it writes to an EEPROM (Memory chip)
 
C

Chris Torek

C does not have "near", but we can solve that problem with:

#define near /* nothing */

Of course, C (standard C anyway) does not define any methods of
talking to Real Hardware either. (Presumably EEADR is defined via
"linker magic", in the above code.)

What we *can* talk about here is portable C code:

volatile unsigned char var;

void write_E2(unsigned int address, unsigned char data) {
var = address;
var = address >> 8;
}

This has a well-defined effect in the C "abstract machine": the
first statement writes the low bits of the value in "address" to
"var", as if we had written:

var = address & UCHAR_MAX;

The second statement writes the low bits of the second value to
"var", as if we had written:

var = (address >> 8) & UCHAR_MAX;

The actual value of UCHAR_MAX is implementation-defined and
appears in <stdlib.h>. If CHAR_BIT is 8, UCHAR_MAX will be 255
(these values are typical, but they can be larger -- they cannot
be smaller though).

If CHAR_BIT is 8 (and I would bet that it is), and the comments
above are accurate, the code is guaranteed to work as desired on
any Standard C implementation (provided we remove the "near" keyword
so that the code will compile in the first place, of course).

Presumably the "near" is needed for this particular compiler, which
perhaps compiles a language almost but not entirely unlike C. :)
Whether the code works in this non-C language depends on the rules
of that language. The more it is like C, the more likely the code
is to work as is.

No

AChar = (char)AnInt; // It is called a "Cast"

In C, the cast is unnecessary. Writing to an "unsigned char"
variable always has the effect of discarding "unneeded" high-order
bits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top