Searching \ for 'Long Variables in MPC or MPLABC' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=mplab
Search entire site for: 'Long Variables in MPC or MPLABC'.

Truncated match.
PICList Thread
'Long Variables in MPC or MPLABC'
1997\02\03@090021 by tjaart

flavicon
face
Hi All!

Here's my problem :
I want to copy a 'long' variable to into two useable bytes (high
and low). MPLABC and MPD doesn't allow access to the byte at the
higher address, so I am forced to do it in assembly. The problem,
however comes in when the variable moves into another bank, so I
have to re-check the validity of all the assembly sections after
compilation. Yuck.

Is there an easier way?

--
Friendly Regards

Tjaart van der Walt
______________________________________________________________
|  Another sun-deprived R&D Engineer slaving away in a dungeon |
|                      WASP International                      |
|              GSM and GPS value-added applications            |
|+27-(0)11-622-8686 |  http://wasp.co.za   | spam_OUTtjaartTakeThisOuTspamwasp.co.za |
|______________________________________________________________|

1997\02\03@153933 by Andrew Warren

face
flavicon
face
Tjaart van der Walt <.....tjaartKILLspamspam@spam@wasp.co.za> wrote:

> I want to copy a 'long' variable to into two useable bytes (high and
> low). MPLABC and MPD [sic] doesn't allow access to the byte at the higher
> address, so I am forced to do it in assembly. The problem, however
> comes in when the variable moves into another bank, so I have to
> re-check the validity of all the assembly sections after
> compilation. Yuck.
>
> Is there an easier way?

Tjaart:

Yeah, there is.  You can even arrange things so that each BIT in a
long (16-bit) variable is accessible.

First, to make the code easy to read, do a couple of typedefs:

   typedef unsigned char byte;
   typedef unsigned long word;

Next, define two structures... One is a pair of bytes; the other is a
pair of what MPC calls "bits" (newer versions of MPC may make the
"bits" thing unnecessary).

   struct both_bytes
   {
       byte hi;
       byte lo;
   };

   struct bits_16
   {
       bits hibits;
       bits lobits;
   };

Finally, set up unions of the various structures.  The first one
allows access to a long variable either as a 16-bit or an 8-bit
value; the second one adds access to each of the 16 bits, too.

   union w_and_b
   {
       word w;
       struct both_bytes b;
   };

   union all_three
   {
       word w;
       struct both_bytes b;
       struct bits_16 b16;
   };

Ok... When you allocate space for your variables, do it like this:

   union w_and_b two_way;        // "two_way" can be accessed as
                                 // either a 16-bit word or two
                                 // individual bytes.

   union all_three three_way;    // "three_way" can be accessed as a
                                 // 16-bit word, two individual
                                 // bytes, or 16 individual bits.

To use the variables, do this:

   two_way.w = 65000;            // To access two_way as a 16-bit
                                 // word, append ".w".

   if (two_way.b.hi) {};         // To access two_way's high byte,
                                 // append ".b.hi".

   two_way.b.lo &= 0xF0;         // To access two_way's low byte,
                                 // append ".b.lo".

   // three_way works the same as above, but adds the following:

       three_way.b16.lobits.4 = 1;   // To access a bit in three_way's
                                 // low byte, append
                                 // ".b16.lobits.x", where "x" is
                                 // the bit number [0-7].

   if (three_way.b16.hibits.7)   // To access a bit in three_way's
       { };                      // low byte, append
                                 // ".b16.hibits.x", where "x" is
                                 // the bit number [0-7].

Of course, you may want to change the names of the structures and
unions.

The bit-access union (used for "three_way" in the above examples) is
kinda clunky... I expect that newer versions of MPC/MPLABC will
provide better built-in bit access, so the multi-level structures
that I've shown won't be necessary.

-Andy

=== Andrew Warren - fastfwdspamKILLspamix.netcom.com                 ===
=== Fast Forward Engineering - Vista, California          ===
===                                                       ===
=== Did the information in this post help you?  Consider  ===
=== contributing to the PICLIST Fund.  Details are at:    ===
=== http://www.geocities.com/SiliconValley/2499/fund.html ===

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...