1

Gostaria de saber se existe algum modo de criar um define para uma "variável" em C que tenha 16 bits chamada PORTAB, de tal modo que escrever:

PORTAB = 0xAAFF;

Seja equivalente à:

PORTA = 0xAA;
PORTB = 0xFF;

Obrigado.

2 Respostas 2

0
#define PORTA (0xAA)
#define PORTB (0xFF)
#define PORTAB (((PORTA) << 8) | (PORTB))

Ou então:

#define JUNTAR_16_BITS(a, b) (((a) << 8) | (b))

#define PORTA (0xAA)
#define PORTB (0xFF)
#define PORTAB JUNTAR_16_BITS(PORTA, PORTB)
0

Sugiro que você use duas macros diferentes, uma para leitura e outra para gravação.

Leitura:

#define PORTAB ((unsigned short)(((PORTA) << 8) | (PORTB)))

Gravação:

#define PORTAB( value ) do{ unsigned short v = value; PORTA = ((unsigned char*)&v)[1]; PORTB = ((unsigned char*)&v)[0]; }while(0)

Exemplo de Uso:

#include <stdio.h>

#define PORTAB ((unsigned short)(((PORTA) << 8) | (PORTB)))

#define SET_PORTAB( value ) do{ unsigned short v = value; PORTA = ((unsigned char*)&v)[1]; PORTB = ((unsigned char*)&v)[0]; }while(0)

int main( void )
{
    /* Gravação */

    PORTA = 0xAA;
    PORTB = 0xFF;

    SET_PORTAB( 0xAAFF );

    /* Leitura */

    printf( "PORTA  = 0x%X\n", PORTA );
    printf( "PORTB  = 0x%X\n", PORTB );
    printf( "PORTAB = 0x%X\n", PORTAB );

    return 0;
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .