Hello,
LethalCorpse wrote:
...
I'd really like it to be able to use the smallest number of bits for an enum, and hence stack multiple boolean variables in a bitfield, but I suspect that's a bridge too far
.
If the requirement is simply to handle single bit flags in a memory efficient, and code efficient manner, there is an alternative approach is to directly utilize a bit field. With single-bit elements, I do not anticipate portability issues. But maybe others will disagree.
Using CW, this method appears to work as anticipated, for both 8-bit and 16-bit variables. For code efficiency on 8-bit MCUs, the variable should be placed in zero page RAM.
Within a "common.h" header file, the generic bit fields are defined for both 8-bit and 16-bit types.
// File: common.h...// Single bit flags:union FLAGSTR8 { byte B; struct { unsigned flag0 :1; unsigned flag1 :1; unsigned flag2 :1; unsigned flag3 :1; unsigned flag4 :1; unsigned flag5 :1; unsigned flag6 :1; unsigned flag7 :1; } b;};typedef union FLAGSTR8 FLAGSTR8_;union FLAGSTR16 { word W; struct { unsigned flag0 :1; unsigned flag1 :1; unsigned flag2 :1; unsigned flag3 :1; unsigned flag4 :1; unsigned flag5 :1; unsigned flag6 :1; unsigned flag7 :1; unsigned flag8 :1; unsigned flag9 :1; unsigned flag10 :1; unsigned flag11 :1; unsigned flag12 :1; unsigned flag13 :1; unsigned flag14 :1; unsigned flag15 :1; } b;};typedef union FLAGSTR16 FLAGSTR16_;
Then within a specific .c file, the zero page variable is defined.
#include "common.h"// Global/static variables:#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGEvolatile FLAGSTR16_ flag; // Single bit control flags...#pragma DATA_SEG DEFAULT
The corresponding .h header file would then #define the bit usage for the variable. The following example is from one of my projects. Of course, this approach is based on that used by CW to define the various bits of the hardware registers.
#include "common.h"/***********************************************************************/// Global variables:#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGEextern volatile FLAGSTR16_ flag; // Single bit control flags...#pragma DATA_SEG DEFAULT/***********************************************************************/// Single bit flags:#define flag_COMMAND flag.b.flag1 // Local command entry mode#define flag_NEWLINE flag.b.flag2 // New line flag for command entry#define flag_LO_BATT flag.b.flag3 // Low battery state#define flag_NO_REC flag.b.flag4 // No record for client code#define flag_WAC_MODE flag.b.flag5 // Paging message format control#define flag_TSYS flag.b.flag6 // System timeout flag#define flag_PROMPT flag.b.flag7 // Previous prompt character#define flag_NORM_S100 flag.b.flag8 // System 100 normal mode#define flag_DFLTPAGE flag.b.flag9 // Default paging mode flag#define flag_EVPROC flag.b.flag10 // Default paging event in progress#define flag_DOWNLOAD flag.b.flag11 // Download mode is current
Regards,
Mac