Re: [Hampshire] [Slightly OT] Arduino coding help - pointers…

Top Page

Reply to this message
Author: Stephen Pelc
Date:  
To: hampshire
Subject: Re: [Hampshire] [Slightly OT] Arduino coding help - pointers to structs
Paul said:

> I'm writing a piece of code for an Arduino to process
> short data blocks received over a serial link.
>
> I've made a struct to hold the input data block which is
> something like:
>
> struct received_data {
>     unsigned int start:1;
>     unsigned int address:4;
>     unsigned int command:5;
>     unsigned int flags:4;
>     unsigned int padding:1;
>     unsigned int parity:1;
>     }

...

> I want to be able to do something like:
> decodedaddress=messagedata.address;
> but I can't seem to make it work. Every possible version
> of this I try either throws a compile error or the first
> test I need to perform says that the first bit of the
> struct (which must be 1) is 0.


If it's a real Arduino, it's probably using an AVR. RAM consists
of 8 bit bytes. The serial line will be processing 7 or 8 bit
data units. The C standard doesn't say what bitfields are, or
what order they occur in. What you want to do is extract bits
from bytes. Masks and AND operations are the way to go.

Bitfields in embedded apps are a common source of chaos when
associated with external data.

Stephen


--
Stephen Pelc, stephen@???
MicroProcessor Engineering Ltd - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)23 8063 1441, fax: +44 (0)23 8033 9691
web: http://www.mpeforth.com - free VFX Forth downloads


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.