avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Preprocessor question


From: Paulo Marques
Subject: Re: [avr-gcc-list] Preprocessor question
Date: Mon, 05 May 2008 11:31:04 +0100
User-agent: Thunderbird 1.5.0.14 (X11/20071210)

Thomas D. Dean wrote:
Thanks,

I re-read 'info cpp' and think I may actually understand it.

BTW, I use a different trick for pin access macros that has been posted before on this list (can't find the reference now, sorry).

I use the attached include file. Then I define my hardware pins like this:

#define LED_RED         A,2
#define SWITCH_1        B,4
#define SWITCH_2        B,5

With this definitions, on the main program I can simply do:

port_bit_set(SWITCH_1);
dir_bit_output(SWITCH_1);

and it takes care of both pin number and port address.

I just thought I'd publish it here in case someone else finds this to be useful.

--
Paulo Marques
Software Development Department - Grupo PIE, S.A.
Phone: +351 252 290600, Fax: +351 252 290601
Web: www.grupopie.com

"Very funny Scotty. Now beam up my clothes."
#ifndef _BIT_TOOLS_H
#define _BIT_TOOLS_H    1

#include <inttypes.h>

// bit manipulation macros

#define bits_set_8(var, mask)    ((var) |= (uint8_t)(mask))
#define bits_clear_8(var, mask)  ((var) &= (uint8_t)~(mask))
#define bits_toggle_8(var, mask) ((var) ^= (uint8_t)(mask))
#define bits_read_8(var, mask)   ((var) & (uint8_t)(mask))
#define bits_write_8(var, mask)  ((var) = (uint8_t)(mask))

#define bits_set_16(var, mask)    ((var) |= (uint16_t)(mask))
#define bits_clear_16(var, mask)  ((var) &= (uint16_t)~(mask))
#define bits_toggle_16(var, mask) ((var) ^= (uint16_t)(mask))
#define bits_read_16(var, mask)   ((var) & (uint16_t)(mask))
#define bits_write_16(var, mask)  ((var) = (uint16_t)(mask))

#define bit(x)        ((uint8_t)1 << (x))
#define bit_long(x)   ((uint32_t)1 << (x))

// pin manipulation macros

#define BIT(p,b)                (b)

#define PORT(p,b)               (PORT ## p)
#define PIN(p,b)                (PIN ## p)
#define DDR(p,b)                (DDR ## p)

#define port_bit_set(io)        bits_set_8(PORT(io), BIT(io))
#define port_bit_clear(io)      bits_clear_8(PORT(io), BIT(io))
#define port_bit_toggle(io)     bits_toggle_8(PORT(io), BIT(io))
#define port_bit_read(io)       bits_read_8(PORT(io), BIT(io))

#define pin_bit_read(io)        bits_read_8(PIN(io), BIT(io))

#define dir_bit_clear(io)       bits_clear_8(DDR(io), BIT(io))
#define dir_bit_set(io)         bits_set_8(DDR(io), BIT(io))
#define dir_bit_input(io)       bits_clear_8(DDR(io), BIT(io))
#define dir_bit_output(io)      bits_set_8(DDR(io), BIT(io))

#define pin_number(io)          (BIT(io))
#define port_name(io)           (PORT(io))
#define pin_name(io)            (PIN(io))

#endif

reply via email to

[Prev in Thread] Current Thread [Next in Thread]