Embedded C query

dear all,

I am facing a compilation problem for my C code on a 8 bit micro controller (Motorola HC11).

I use cosmic software's compiler which allows me to directly refrence addresses, using the @ variable.

***************************************************************************************************************** So the undermentioned C statement,

unsigned char portA @ 0xD300;

will enable me to use portA to access the memory location D300.

I can now read and write to the memory location D300 using simple C statements like

portA= 10; (will write 10 at location D300 refrenced by portA)

similarly to read i can say....

c= portA; (this will copy the value inside memory location D300 referenced by variable portA to c).

*********************************************************************************************************************

Now the problem starts... I made a declaration like this,

******************************************************************************************** unsigned char register_data @ 0x00FE; /* declare a register whose address is 00FE*/ unsigned char *ptr_db; unsigned char ram_packet_header @ 0xD300; /* declare a memory address, 0xD300*/ ptr_db = &ram_packet_header; /* Now ptr_db contains the address D300*/

untill here therr is no problem and the code compiles fine.

Now I want to store the content of ptr_db( which is 0xD300, an address) in the variable register_data. .

so I say,

register_data = (unsigned char) ptr_db;

which doesnt compile and gives me incompatible pointer type assingment.

Is there a way to get the value stored inside a pointer type variable (which is an address) and store it in another unsigned char variable ?

Thanks ashu

Reply to
ashu
Loading thread data ...

** **************************************
** ******************************************
** *****************

You're trying to stuff a 16-bit value (an address) into an 8-bit variable. I doubt that's what you really want.

Mike

Reply to
Mike Silva

*****************************************************************************************************************
*********************************************************************************************************************
********************************************************************************************

I think you're missing an asterisk.

register_data = *ptr_db;

This tells the compiler you want the data that the pointer is pointing to. The way you did it, you are telling it to put the pointer into the 8bit value.

Scott

Reply to
Not Really Me

**** **************************************
**** ******************************************
**** *****************

Hi Mike,

you noticed it :)

Actually since my pointer can hold 16 bits, i want to extract lower 8 bits of the value contained in pointer and store it in a 8 bit register.

I also need the higher byte, which i will store in some other 8 bit register(which i havent mentioned in the code).

thanks ashu

Reply to
ashu

****** **************************************
****** ******************************************
****** *****************
,

Hi scot,

No, I am not missing an astreik, an astriek, would get me the value "pointed to" by the pointer, and not the address contained in the pointer type variable.

I want to store the address stored in the pointer type variable in some variable, How to do that ?

regs ashu

?
Reply to
ashu

********************************************************************************************

The nature of the error is probably that register_data is an array at a fixed location (a constant pointer), and C won't allow you to assign it that way.

You would have to say something like:

register_data[0] = (unsigned char) ptr_db;

or perhaps

*register_data = (unsigned char) ptr_db;

As the other poster pointed out, this type cast causes you to lose information (you are truncating 16 bits).

If you want to start playing around with bytes of address and so on, the most efficient way tends to be with unions, i.e.

typedef union { unsigned char *p; unsigned char b[2]; } PICKAPART16;

where there is always the possibility of porting the code to a different-endian machine or a development environment with different data sizes and causing grief.

That being said, one can always do:

PICKAPART16 ptr_db;

ptr_db.p = &ram_packet_header;

ram_packet_header[0] = ptr_db.b[0]; ram_packet_header[1] = ptr_db.b[1];

The Lizard

Reply to
Jujitsu Lizard

****** **************************************
****** ******************************************
****** *****************
,
?

Try something like

unsigned short addr =3D (unsigned short) ptr_db; /* stuff a 16 bit variable into another 16 bit variable */ register_data =3D addr & 0xFF; /* mask helps explain your intentions */ hi_byte =3D addr >> 8; /* here's the high byte when you need it */

BTW, your code did compile (with warnings) in the first compiler I tried it in (AVR GCC). So the added 16-bit variable above is just an attempt to make your compiler happy.

Reply to
Mike Silva

Since you already know what the actual address value is, since you put the variable at a specific address, then you could try.

#define RAM_P_HEADER 0xD300 unsigned char ram_packet_header @ RAM_P_HEADER;

low_byte=(RAM_P_HEADER&0xFF); high_byte=(RAM_P_HEADER>>8);

Regards Anton Erasmus

Reply to
Anton Erasmus

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.