//--------------------------------------------------------------- // EXAMPLE EX17.C : Demonstrate use of a UNION and STRUCTURE. // // C.P. Diduch, Dec 1999. //--------------------------------------------------------------- #define KEY_PA 0x138 // 82C55A port addresses for PA, #define KEY_PB 0x13A // PB, #define KEY_PC 0x13C // PC and the #define KEY_CON 0x13E // Control Register. #include // conio.h contains prototypes for // inportb() and outportb(). //--------------------------------------------------------------- union IO { struct { char LSB; char MSB; } ABYTE; int AWORD; }; //--------------------------------------------------------------- void main() { union IO Y; // Y is a variable that may be accessed as a word // (16-bits) by Y.AWORD. The high and low bytes // that comprise Y may be accessed as Y.ABYTE.LSB // and Y.ABYTE.MSB ! unsigned X; outportb(KEY_CON, 0x82); // Initialize 82C55A PPI with ports // KEY_PC and KEY_PA as OP ports and // port PB as an IP port. for (X = 0; X < 256; X++) { - Y.AWORD = X * X; outportb(KEY_PA, Y.ABYTE.LSB); outportb(KEY_PC, Y.ABYTE.MSB); } }