//头文件
#ifndef __always_inline__
#define __always_inline__ _Pragma("inline=forced")inline
#endif
#define AVR_GPIO_ASSIGN(name, port, bit) \
__always_inline__ static void GPIO_##name##_Set(void) {PORT##port |= (1 << bit);}\
__always_inline__ static void GPIO_##name##_Clr(void) {PORT##port &= ~(1 << bit);}\
__always_inline__ static void GPIO_##name##_Toggle(void) {PORT##port ^= (1 << bit);}\
__always_inline__ static uint8 GPIO_##name##_Read(void) {return (PIN##port & (1 << bit)) !=0;}\
__always_inline__ static uint8 GPIO_##name##_DdrRead(void) {return (DDR##port & (1 << bit)) !=0;}\
__always_inline__ static void GPIO_##name##_MakeOut(uint8 x) {(x)?(PORT##port |= (1 << bit)):(PORT##port &= ~(1 << bit)); DDR##port |= (1 << bit);}\
__always_inline__ static void GPIO_##name##_MakeIn(uint8 x) {DDR##port &= ~(1 << bit); (x)?(PORT##port |= (1 << bit)):(PORT##port &= ~(1 << bit));}\
__always_inline__ static void GPIO_##name##_MakePullup(void) {GPIO_##name##_MakeIn(1);}
AVR_GPIO_ASSIGN(LED_1,B,0); //LED_1-------PORTB0
AVR_GPIO_ASSIGN(LED_2,B,1); //LED_2-------PORTB1
AVR_GPIO_ASSIGN(KEY,B,2); //KEY---------PORTB2
//C文件
volatile uint8 key;
int main()
{
GPIO_LED_1_MakeOut(0); //LED_1设为输出,初始输出低
GPIO_LED_2_MakeOut(0); //LED_2设为输出,初始输出低
GPIO_KEY_MakeIn(1); //KEY设为输入,初始上拉
GPIO_LED_1_Set(); //LED_1输出高
GPIO_LED_1_Clr(); //LED_1输出低
GPIO_LED_2_Set(); //LED_2输出高
GPIO_LED_2_Clr(); //LED_2输出低
key=GPIO_KEY_Read(); //读取KEY
while(1);
}
源程序下载地址:http://download.csdn.net/detail/dearwind153/4196937