|
|
View previous topic :: View next topic |
Author |
Message |
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
Constant Within a Structure? |
Posted: Sun Nov 04, 2007 10:53 am |
|
|
Hello All,
I'm sure there is a better way to try to do this, but my C experience keeps from understanding all the possibilities.
I have a specific packet structure for ACKs and NACKs. The only difference between the two is the command byte that delineates whether the packet is an ACK or NACK. How would be the best way to define them. I've tried this:
Code: | //////////////////////////////////////////////////////////////////////
struct packet_ack {
int8 to_lo; //id of rx'er 1 byte
int8 to_hi; //id of rx'er 1 byte
int8 from_lo; //id of tx'er 1 byte
int8 from_hi; //id of tx'er 1 byte
int8 packet_num; //packet# 1 byte
//packet number will be generated by sender
//valid 1-255, 00 denotes no packet flag
int8 cmd = 0xFF; //command 1 byte
int8 checksum; //check Sum 1 bytes
// ---------------
} ack_packet; // TOTAL 7 bytes
//////////////////////////////////////////////////////////////////////
struct packet_nack {
int8 to_lo; //id of rx'er 1 byte
int8 to_hi; //id of rx'er 1 byte
int8 from_lo; //id of tx'er 1 byte
int8 from_hi; //id of tx'er 1 byte
int8 packet_num; //packet# 1 byte
//packet number will be generated by sender
//valid 1-255, 00 denotes no packet flag
int8 cmd = 0x00; //command 1 byte
int8 checksum; //check Sum 1 bytes
// ---------------
} nack_packet; // TOTAL 7 bytes
////////////////////////////////////////////////////////////////////// |
But, of course I've learned that initializing values within the structure is not allowed.
A tip or two on the appropriate concept to get me going would be a great help.
Thanks,
John
EDIT:
I've tried this and the compiler doesn't like the assignment after declaring the structure. Why?
Code: | //////////////////////////////////////////////////////////////////////
struct packet_ack_nack {
int8 to_lo; //id of rx'er 1 byte
int8 to_hi; //id of rx'er 1 byte
int8 from_lo; //id of tx'er 1 byte
int8 from_hi; //id of tx'er 1 byte
int8 packet_num; //packet# 1 byte
//packet number will be generated by sender
//valid 1-255, 00 denotes no packet flag
int8 cmd; //command 1 byte
int8 checksum; //check Sum 1 bytes
// ---------------
} ack_packet, nack_packet; // TOTAL 7 bytes
ack_packet.cmd = ACK_CMD;
nack_packet.cmd = NACK_CMD; |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 04, 2007 1:52 pm |
|
|
Quote: | But, of course I've learned that initializing values within the structure is not allowed. |
See the program below.
Program output:
Quote: |
ram_data.a = 55
ram_data.b = 1234
ram_data.c = 89abcdef
ram_data.d = 1.234
const_data.a = aa
const_data.b = abcd
const_data.c = 12345678
const_data.d = 3.456
|
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
struct
{
int8 a;
int16 b;
int32 c;
float d;
}ram_data = {0x55, 0x1234, 0x89ABCDEF, 1.234};
struct
{
int8 a;
int16 b;
int32 c;
float d;
}const const_data = {0xAA, 0xABCD, 0x12345678, 3.456};
//======================================
void main()
{
printf("ram_data.a = %x \n\r", ram_data.a);
printf("ram_data.b = %lx \n\r", ram_data.b);
printf("ram_data.c = %lx \n\r", ram_data.c);
printf("ram_data.d = %7.3f \n\r", ram_data.d);
printf("\n\r");
printf("const_data.a = %x \n\r", const_data.a);
printf("const_data.b = %lx \n\r", const_data.b);
printf("const_data.c = %lx \n\r", const_data.c);
printf("const_data.d = %7.3f \n\r", const_data.d);
while(1);
}
|
|
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Sun Nov 04, 2007 4:36 pm |
|
|
PCM,
Thanks. That led me to this:
Code: | //////////////////////////////////////////////////////////////////////
struct packet_ack_nack {
int8 to_lo; //id of rx'er 1 byte
int8 to_hi; //id of rx'er 1 byte
int8 from_lo; //id of tx'er 1 byte
int8 from_hi; //id of tx'er 1 byte
int8 packet_num; //packet# 1 byte
//packet number will be generated by sender
//valid 1-255, 00 denotes no packet flag
int8 cmd; //command 1 byte
int8 checksum; //check Sum 1 bytes
// ---------------
} // TOTAL 7 bytes
ack_packet = {0x00, 0x00, 0x00, 0x00, 0x00, ACK_CMD},
nack_packet = {0x00, 0x00, 0x00, 0x00, 0x00, NACK_CMD}; |
Is there any way to declare a constant inside the structure?
Code: | struct the_structure {
int8 item1;
const int8 item2 = 0x00;
} my_struct; |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 04, 2007 4:49 pm |
|
|
I don't think the compiler allows mixing of RAM and ROM (const) elements
in the same structure. |
|
|
Ttelmah Guest
|
|
Posted: Sun Nov 04, 2007 4:51 pm |
|
|
No.
Remember that constants, are stored in the ROM, as opposed to normal variables being in RAM. To declare a constant 'inside' a structure, would require a complete change of access mode for just the one value...
What you can do, is have a constant data array, and store the index used to access a particular value inside a structure.
A structure is a single entity, and the whole entity must either be 'constant', or variable.
Best Wishes |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Sun Nov 04, 2007 5:21 pm |
|
|
Ahhhh. That makes sense.
Thanks for the explanation. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|