1

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

2
  • 1
    A hex calculator is your friend. 14341 = 0x3805, 23607301 = 0x1683805. When you are looking at numbers, try looking at their hex representation as well. Sometimes it helps in spotting such problems. Commented Jan 11, 2014 at 18:39
  • Thanks, that made it even clearer! :) Commented Jan 11, 2014 at 19:15

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks did it, so simple once you know it! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.