可以的,但是一般性不这么用,不是所有的编译器都支持sfr(本质是地址)的传递。
你可以使用这样的东西:(这是我写的AVR类库)
void SETB(PORT port,unsigned char bit);//其他的源代码举一反三吧
记得定义:
#define PORT enum PORT
//方便输入
#define setb SETB
#define clrb CLRB
#define getb GETB
PORT{
PA,
PB,
PC,
PD
};
Port和bit的本质都是寄存器,一个是byte寻址一个是bit寻址而已,千万不要以为bit寻址就一定快,往往是byte要快一些。
附上源代码:
gpio.h
#ifndef AVR_GPIO_H
#define AVR_GPIO_H
#define PORT enum PORT
//方便输入
#define setb SETB
#define clrb CLRB
#define getb GETB
PORT{
//PA,
PB,
PC,
PD
};
extern void SETB(PORT port,unsigned char bit);
//相应端口置1,并设置为输出
extern void CLRB(PORT port,unsigned char bit);
//相应端口置0,并设置为输出
extern unsigned char GETB(PORT port,unsigned char bit);
//相应端口设置为输入并读取,返回0xff(1)与0x00(0),异常返回0xf0
extern void SETD(PORT port,unsigned char bit);
//相应端口设置为输出
extern void CLRD(PORT port,unsigned char bit);
//相应端口设置为输入
#endif
gpio.c
#include"inc.h"
void SETB(PORT port,unsigned char bit){
if(bit<=7){
switch(port){
//case PA:
//DDRA|=(1<<bit);
//PORTA|=(1<<bit);
//break;
//use in Mega16
case PB:
DDRB|=(1<<bit);
PORTB|=(1<<bit);
break;
case PC:
DDRC|=(1<<bit);
PORTC|=(1<<bit);
break;
case PD:
DDRD|=(1<<bit);
PORTD|=(1<<bit);
break;
}
}
}
void CLRB(PORT port,unsigned char bit){
if(bit<=7){
switch(port){
//case PA:
//DDRA&=(1<<bit);
//PORTA&=(0xff-(1<<bit));
//break;
//use in Mega16
case PB:
DDRB|=(1<<bit);
PORTB&=(0xff-(1<<bit));
break;
case PC:
DDRC|=(1<<bit);
PORTC&=(0xff-(1<<bit));
break;
case PD:
DDRD|=(1<<bit);
PORTD&=(0xff-(1<<bit));
break;
}
}
}
unsigned char GETB(PORT port,unsigned char bit){
unsigned return_value=0;
if(bit<=7){
switch(port){
//case PA:
//DDRA|=(0xff-(1<<bit));
//return_value=PINA&(1<<bit);
//break;
//use in Mega16
case PB:
DDRB|=(0xff-(1<<bit));
return_value=PINB&(1<<bit);
break;
case PC:
DDRC|=(0xff-(1<<bit));
return_value=PINC&(1<<bit);
break;
case PD:
DDRD|=(0xff-(1<<bit));
return_value=PIND&(1<<bit);
break;
}
if(return_value==0){
return(0x00);
}else{
return(0xff);
}
}
return(0xf0);
}
void SETD(PORT port,unsigned char bit){
if(bit<=7){
switch(port){
//case PA:
//DDRA|=(1<<bit);
//break;
//use in Mega16
case PB:
DDRB|=(1<<bit);
break;
case PC:
DDRC|=(1<<bit);
break;
case PD:
DDRD|=(1<<bit);
break;
}
}
}
void CLRD(PORT port,unsigned char bit){
if(bit<=7){
switch(port){
//case PA:
//DDRA|=(0xff-(1<<bit));
//break;
//use in Mega16
case PB:
DDRB|=(0xff-(1<<bit));
break;
case PC:
DDRC|=(0xff-(1<<bit));
break;
case PD:
DDRD|=(0xff-(1<<bit));
break;
}
}
}