Searching \ for 'Tables and flags with C.' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/mems.htm?key=table
Search entire site for: 'Tables and flags with C.'.

Truncated match.
PICList Thread
'Tables and flags with C.'
2000\04\22@210039 by netquake

flavicon
face
Hello!

I'm new to embedded mcu programming using C and I'm lost on
how to implement tables (I used retlw for Pic and lpm for Avr)
and a flag register (I used an internal 8 bit register as
8 flags which I toggled when necessary).
I assume that there must be variations whithin each compiler
but I just want to know what is the general approach.

Thanx!
------------------------------------
netQ <spam_OUTnetquakeTakeThisOuTspaminnocent.com>
http://virtuaweb.com/picprog
"Home of amateur PIC programmers..."

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6.2

mQCNAzV38BkAAAEEALfWv9j3f+tZ+z2IW+2o9Ebx4bUGnHjHPqIe0a6yVKawaeV/
Y4I6L2/A0ddbVMG8+qJ0MvHNkr3DzYkpW+hTl9zAzXkKBdZ3GA5bbvot7entl/O2
YDtRWUV730koxBo5iFFUbJH5kbmkox+h3znj34zPnZNWzNaqOAwol3wABfBNAAUT
tBxOZXRRIDxuZXRxdWFrZUBpbm5vY2VudC5jb20+
=0bVU
-----END PGP PUBLIC KEY BLOCK-----

2000\04\23@022354 by William Chops Westfield

face picon face
   I'm new to embedded mcu programming using C and I'm lost on
   how to implement tables (I used retlw for Pic and lpm for Avr)
   and a flag register (I used an internal 8 bit register as
   8 flags which I toggled when necessary).

Ok, here's "in general."

unsigned char flags;
#define FLAG_EOL   0x01
#define FLAG_EOF   0x02
#define FLAG_FULL  0x04
#define FLAG_EMPTY 0x08
#define FLAG_unused1 0x10
#define FLAG_unused2 0x20
#define FLAG_unused3 0x40
#define FLAG_unused4 0x80

main ()
{
   unsigned char c;
       :
   if (c == 13)
       flags |= FLAG_EOL;      /* Set End of line flag when we see CR */
   else if (c == 'Z'-64)
       flags |= FLAG_EOF;      /* Set End of File flag when we see ^Z */
   else
       flags &= ~(FLAG_EOL|FLAG_EOF);  /* Otherwise clear flags */
}

unsigned char translate (unsigned char inchar)
{
   static const unsigned char transtab[32] = {
       123, 12, 14, 1,    1, 1, 12, 65,        /* 0-7 */
       123, 12, 14, 1,    1, 1, 12, 65,        /* 8-15 */
       123, 12, 14, 1,    1, 1, 12, 65,        /* 16-23 */
       123, 12, 14, 1,    1, 1, 12, 65 };      /* 24-31 */

   if (inchar < 32)
       return(transtab[inchar]);
   return(inchar);
}

As for specifics...  transtab is likely to be defined outside of the
function translate() (as a global array) in many cases.  The code OUGHT to
look about the same regardless (the "static const" should put the actual
data initialization outside the function.)

The translation function is a good example of something that should get
special handing on something like a PIC.  A "simplistic" translation of the
code shown might result in the use of a real array rather than the retlw
instruction (and that might certainly happen if you're not careful about
those extra declaration keywords, and I'm not sure I got them all right.)
The following might be more likely to produce the retlw code, but it would
be "weird looking" as a C program:

unsigned char translate (unsigned char inchar)
{
   switch (inchar) {
       case 0: return(123);
       case 1: return(12);
       case 2: return(14);
          :
       case 31: return(65);
       default: return(inchar);
   }
}

Likewise, a microcontroller will bit set instructions might create
suboptimal code for:
       flags &= ~(FLAG_EOL|FLAG_EOF);  /* Otherwise clear flags */
because it noticed there was more than one bit being cleared, making
bit clear instructions "unusable" and creating genuine boolean math
code, when two bit clear instructions would have sufficed.
       flags &= ~FLAG_EOL;
       flags &= ~FLAG_EOF;
might result in better code.  (eventually, it pays off to have paid for a
good compiler, and trust it to do the "right" optimizations, rather than
to optimize your code in weird ways to suit a particular compiler, but there
are "obvious" things to watch out for.

BillW

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