|
发表于 2010-9-10 02:09:31
|
显示全部楼层
I use a set of macros to do all of my port related work.
#define IO_SET(port, bits) port |= (bits) //set bits on port
#define IO_CLR(port, bits) port &=~(bits) //clear bits on port
#define IO_FLP(port, bits) port ^= (bits) //flip bits on port
#define IO_GET(port, bits) (port & (bits)) //return bits on port
#define IO_IN(ddr, bits) ddr &=~(bits) //set bits as input
#define IO_OUT(ddr, bits) ddr |= (bits) //set bits as output
and they are system independent so they are infinitely portable. all I do when I move my code to a different platform, is to check the corresponding datasheet to make sure that my IO_IN() and IO_OUT routines are correct.
when I need to use the routines, I do the following
#define LED_PORT PORTB
#define LED_DDR DDRB
#define LED1 (1<<0)
#define LED2 (1<<5)
so I would do
IO_OUT(LED_PORT, LED1 | LED2); IO_SET(LED_PORT, LED1); IO_FLP(LED_PORT, LED2);
to turn on LED1 and flip LED2.
if I want to move pins, all I do is to change the definition and recompile.
infinitely portable and fool proof. |
|