使用C中的宏控制AVR芯片中某个端口上的单个位



我有使用CodeVisionAVR进行AVR编程的经验。最近,我转而使用Atmel Studio,发现了一个小但令人讨厌的问题:我无法像在CodeVisionAVR中那样轻松地控制每个PIN/PORT。

首先,让我们初始化一个特定的端口:

// Initialzing one port, for example PORT D:
// Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=Out 
DDRD=0x01;
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=1 
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (1<<PORTD0);
// After initializing, Port D bit 0 should be the only pin set to out.

在CodeVisionAVR中,我可以做到:

PORTD.0 = 0; // Set Port D bit 0 to low
PORTD.0 = 1; // Or to high

但是在AtmelStudio中,将端口D位0寻址为PORTD.0会给我一个错误。我不得不这样做:

PORTD = (1<<PORTD0); // Set Port D bit 0 to high
PORTD = (0<<PORTD0); // and low

正如您所看到的,通过移位位进行寻址不那么干净,也更难读/写。我以为CVAVR使用了类似结构的东西来模仿点(.(寻址方法(在C中,我们没有类(,或者一些重载运算符,或者一些宏,但在挖掘了CVAVR中包含的头文件后,我只能找到:

sfrb PORTD=0x12;
// ... other ports ...
sfrb PIND=0x10;

我的问题是,我可以模仿Atmel Studio中CVAVR的位寻址方法吗?如果是,如何?或者这是IDE独有的功能?

您对CodeVisionAVR 的看法

PORTD.0 = 0; // Set Port D bit 0 to low
PORTD.0 = 1; // Or to high

不是一个有效的C语义,所以如果你在谓词上没有犯错误,它一定是一个代码视觉编译器扩展。

C中的这种赋值表示结构访问,但不能声明以数字开头的结构成员(或任何其他标识符(,因此PORTD.0将产生错误。

当你这样做的时候:

PORTD = (1<<PORTD0); // Set Port D bit 0 to low
PORTD = (0<<PORTD0); // and high

您不是在做注释,而是在第一个表达式中将0x1赋值给PORTD,在第二个表达式中将0x0赋值(假设PORTD0==1(。如果你的意图是只操纵一个比特,这就是你应该做的:

PORTD |= (1<<PORTD0); //sets bit PORTD0 high in PORTD 
PORTD &= ~(1<<PORTD0); //sets bit PORTD0 low in PORTD 
PORTD ^= (1<<PORTD0); //toggles PORTD0 in PORTD 

你应该读一下C中的比特操作,这里有一篇文章,里面有更多的例子

有时这些操作被封装在一个宏中,这是一个如何做到这一点的例子:

#define BitSet(Port,Bit) (Port|=(1<<Bit))
#define BitClear(Port,Bit) (Port&=~(1<<Bit))
#define BitToggle(Port,Bit) (Port^=(1<<Bit))
#define SetBits(Port,BitMask) (Port|=BitMask)
#define ClearBits(Port,BitMask) (Port&=~BitMask)
#define ToggleBits(Port,BitMask) (Port^=BitMask)

//then you can use it
BitSet(PORTD,0) ; //Sets the bit0
BitSet(PORTD,1) ; //Sets the bit1
BitSet(PORTD,2) ; //Sets the bit2
// or
BitSet(PORTD,PORTD0) ; //Sets the bit0
BitSet(PORTD,PORTD1) ; //Sets the bit1
BitSet(PORTD,PORTD2) ; //Sets the bit2
...
SetBits(PORTD,0x55) ; //Sets the bits 0,2,4, and 6. Leaves the other unchanged
ClearBits(PORTD,0x55) ; //Clear the bits 0,2,4, and 6. Leaves the other unchanged

最新更新