tirithen
tirithen

Reputation: 3517

Arduino: union/struct attribute returns the wrong value

I'm trying to use a union and struct to represent a 32-bit signal that is going to be sent via a 433Mhz radio transmitter. I have a problem with getting the Arduino to store the 26-bit remote id on the signal.parts.remote attribute. When I fetch the value after setting it to 23607301 (decimal) I get 14341 (decimal) instead. How should I construct this union to have it return the correct value?

Signal.h

union signal_union
{
    struct
    {
        unsigned unit   :2;
        unsigned channel:2;
        unsigned status :1;
        unsigned group  :1;
        unsigned remote :26;
    } parts;
    unsigned long data;
};

typedef union signal_union Signal;

structtest.ino

#include "Signal.h"

Signal signal1;
Signal signal2;

void testPassingStruct(Signal *variable)
{
    variable->parts.status = 1;

    Serial.print("Unit: ");
    Serial.println(variable->parts.unit);
    Serial.println("Should be: 2");
    Serial.println("");
    Serial.print("Status: ");
    Serial.println(variable->parts.status);
    Serial.println("Should be: 1");
    Serial.println("");
    Serial.print("Remote: ");
    Serial.println(variable->parts.remote);
    Serial.println("Should be: 23607301");
    Serial.println("");
    Serial.print("Data: ");
    Serial.println(variable->data, BIN);
    Serial.println("Should be: 01011010000011100000000101110010");
    Serial.println("");
}

void setup() 
{
    Serial.begin(115200);

    signal1.parts.remote = 23607301;
    signal1.parts.unit = 2;
    signal1.parts.group = 1;
    testPassingStruct(&signal1);
}

void loop() 
{
}

Output (from Arduino):

Unit: 2
Should be: 2

Status: 1
Should be: 1

Remote: 14341
Should be: 23607301

Data: 1110000000010100110010
Should be: 01011010000011100000000101110010

This is a follow up question on Arduino: cannot pass union struct as pointer ac I can with gcc compiler

Upvotes: 1

Views: 2875

Answers (1)

NPE
NPE

Reputation: 500377

I suspect the issue has to do with the fact that unsigned (aka unsigned int) is 16 bits wide. Try changing the remote field to unsigned long:

unsigned long remote :26;

Upvotes: 3

Related Questions