View previous topic :: View next topic |
Author |
Message |
Stimpy
Joined: 05 Jan 2009 Posts: 4
|
Anonymous unions: working in 3.249 but broken in 4.071 |
Posted: Fri Nov 13, 2009 10:20 am |
|
|
I'm trying to port an old project from CCS version 3.249 to 4.071 but I'm running into a problem with anonymous unions. Here's a short example using pic 18F6680:
Code: | struct {
unsigned int8 update_interval;
unsigned int8 category;
union {
int16 pressure;
int16 temperature;
int16 humidity;
int16 windspeed;
};
} sensor;
void main() {
unsigned int8 x;
sensor.category=1;
sensor.update_interval=60;
sensor.pressure=1013;
x=sensor.update_interval;
printf("X equals %u\n\r",x);
}
|
In the older 3.249 version, the output is: X equals 60
The same code compiled with 4.071 produces: X equals 245
Has this been fixed in the latest 4.099 release? I would probably renew and upgrade if it has, otherwise does anyone know of any workarounds? The only method I've found so far is to move the union to the beginning of the struct but I don't know if this will be 100% effective in all cases.
I could also give the union a name instead of keeping it anonymous and this does appear to fix things but it's a LOT of code to change. Any other ideas?
Could somebody who has 4.099 try the above example on any PIC18 and see what output you get? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 13, 2009 1:14 pm |
|
|
I got this with vs. 4.099:
Test program:
Code: |
#include <18F6680.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
struct
{
unsigned int8 update_interval; // 60
unsigned int8 category; // 1
union {
int16 pressure; // 1013
int16 temperature;
int16 humidity;
int16 windspeed;
};
} sensor;
//====================================
void main()
{
unsigned int8 x;
sensor.category=1;
sensor.update_interval=60;
sensor.pressure=1013;
x=sensor.update_interval;
printf("X equals %u\n\r",x);
while(1);
} |
|
|
|
RossJ
Joined: 25 Aug 2004 Posts: 66
|
|
Posted: Sun Nov 22, 2009 2:51 am |
|
|
A similar problem with anonymous struct in a union (4.099 and 4.100). In this case, aaa is located in the same byte as bbb. If the struct is given a name, the problem goes away. I will email CCS support and refer them to this thread.
Code: |
union {
struct {
int1 aaa;
int8 bbb;
};
} xxx;
void main(void)
{
xxx.aaa = 0;
xxx.bbb = 1;
}
|
|
|
|
|