/* PORT_WR() performs a buffered write of the "mask"ed bits of "val" to
 * "port" (P1, P2, etc.) through the port's output latch register.  This
 * technique is especially useful to deal with any read-modify-write
 * issues a port may have.
 */
#define PORT_WR(port, val, mask) 
        (port = (port##_OLR ^= (port##_OLR ^ (val)) & (mask)))

unsigned char P2_OLR;   /* OLR: Output Latch Register */

void main(void)
{
    unsigned char i;

    P2_OLR = 0xF0;
    P2     = P2_OLR;

    for (i = 0; /* forever */ ; i++)
        PORT_WR(P2, i, 0x0F);
}