AVR读写IO口右一方法(借用ATmel)
扫描二维码
随时随地手机看文章
//头文件
#ifndef__always_inline__
#define__always_inline___Pragma("inline=forced")inline
#endif
#defineAVR_GPIO_ASSIGN(name, port, bit)
__always_inline__staticvoid GPIO_##name##_Set(void) {PORT##port |= (1<< bit);}
__always_inline__staticvoid GPIO_##name##_Clr(void) {PORT##port &= ~(1<< bit);}
__always_inline__staticvoid GPIO_##name##_Toggle(void) {PORT##port ^= (1<< bit);}
__always_inline__staticuint8GPIO_##name##_Read(void) {return(PIN##port & (1<< bit)) !=0;}
__always_inline__staticuint8GPIO_##name##_DdrRead(void) {return(DDR##port & (1<< bit)) !=0;}
__always_inline__staticvoid GPIO_##name##_MakeOut(uint8 x) {(x)?(PORT##port |= (1<< bit)):(PORT##port &= ~(1<< bit)); DDR##port |= (1<< bit);}
__always_inline__staticvoid GPIO_##name##_MakeIn(uint8 x) {DDR##port &= ~(1<< bit); (x)?(PORT##port |= (1<< bit)):(PORT##port &= ~(1<< bit));}
__always_inline__staticvoid 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文件
volatileuint8 key;
intmain()
{
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);
}