简单的 arduino 代码,无需在 avr-libc 中使用 avr 标头
我有这个简单的代码
,我正在尝试为 arduino 微控制器编译,而不使用 avr 标头。我刚刚在源程序文件中定义了所有宏
,但我的 gcc-avr 说
led.c:15:8: error: lvalue required as left operand of assignment
DDRB |= 0B100000; // PORTB5 1010
^
现在我可以预期在某些 cpu 上出现此错误,io 区域不是此进程的虚拟内存空间,但我正在必须具有执行位的 mocrocontroller 上运行我的代码。如何摆脱这条消息并编译它并能够在 arduino 上运行
但是 gcc-avr 会抛出错误
#define F_CPU 16000000
#define BLINK_DELAY_MS 5000
#include <util/delay.h>
#define __SFR_OFFSET 0x20
#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)
#define DDRB _SFR_IO8(0x04)
#define PORTB _SFR_IO8(0x05)
int main (void)
{
// Arduino digital pin 13 (pin 5 of PORTB) for output
DDRB |= 0B100000; // PORTB5 1010
while(1) {
// turn LED on
PORTB |= 0B100000; // PORTB5
// _delay_ms(BLINK_DELAY_MS);
int x=0;
while(x<25)
{
x++;
}
x=0;
// turn LED off
PORTB &= ~ 0B100000; // PORTB5
//hello
while(x<25)
{
x++;
}
//_delay_ms(BLINK_DELAY_MS);
}
}
I have this simple code
That I am trying to compile for arduino microcontroller and without using avr headers. I just defined all macros in my source program file
but my gcc-avr says
led.c:15:8: error: lvalue required as left operand of assignment
DDRB |= 0B100000; // PORTB5 1010
^
Now I can expect this error on some cpu that io area is not virtual memory space of this process but I am running my code on mocrocontroller that must have execution bit. how to get rid of this message and compile it and able to run on arduino
But the gcc-avr throws error that
#define F_CPU 16000000
#define BLINK_DELAY_MS 5000
#include <util/delay.h>
#define __SFR_OFFSET 0x20
#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)
#define DDRB _SFR_IO8(0x04)
#define PORTB _SFR_IO8(0x05)
int main (void)
{
// Arduino digital pin 13 (pin 5 of PORTB) for output
DDRB |= 0B100000; // PORTB5 1010
while(1) {
// turn LED on
PORTB |= 0B100000; // PORTB5
// _delay_ms(BLINK_DELAY_MS);
int x=0;
while(x<25)
{
x++;
}
x=0;
// turn LED off
PORTB &= ~ 0B100000; // PORTB5
//hello
while(x<25)
{
x++;
}
//_delay_ms(BLINK_DELAY_MS);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是宏,您将寄存器定义为整数,而不是整数的地址。
DDRB
扩展为0x04 + 0x20
,因此您最终会得到类似(0x04 + 0x20) |= 0B100000;
的代码。您应该能够通过强制转换然后取消引用来解决此问题:有关详细信息,请参阅如何访问硬件从固件注册?
另请注意,以双下划线
__
开头的宏是为编译器保留的,因此我们永远不应该使用它,否则可能会导致命名冲突。The problems are the macros, you are defining the register as an integer, not as an address to an integer.
DDRB
expands to0x04 + 0x20
so you end up with code like(0x04 + 0x20) |= 0B100000;
. You should be able to fix this with a cast and then de-reference:For details see How to access a hardware register from firmware?
Please also note that macros starting with double underscore
__
are reserved for the compiler, so we should never use that or we might end up with naming collisions.