gpt4 book ai didi

c++ - C++ 基础知识与 Arduino

转载 作者:行者123 更新时间:2023-11-28 08:00:02 25 4
gpt4 key购买 nike

我读过有关 Arduino 的文章,以及它如何使用一种类似于但不等于 C 的语言。我非常熟悉 C++,我想知道如何使用 Arduino 完成基本任务,例如与输入/输出引脚。我认为需要引脚的内存地址,然后为“闪烁的 LED”做这样的事情:

int main()    {
while (1) {
bool * out_pin = /* Whatever that memory address was for that pin */;
*out_pin = 1;
// Some sort of sleep function? (I only know of "windows.h"'s "Sleep" function)
*out_pin = 0;
}
return 0; // Kind of unneeded, I suppose, but probably compiler errors otherwise.
}

我可能真的错了:这就是我问这个问题的原因。

最佳答案

这是从我的问题下面的评论中复制的。 David Schwartz 回答了我的问题:

Close. The pins don't have memory addresses (they're register mapped, not memory mapped). Generally, the compiler already maps them to variables for you. So you just do pin_name = pin_value; (like PORTD = 7;) and the compiler does the magic. – David Schwartz 7 mins ago

[PORTD's] a keyword for a register. It behaves like a variable. When the compiler sees PORTD = 7; is compiles it to the necessary assembly code to load a 7 into the PORTD register. When it sees i = PORTD; is loads the value from the PORTD register and stored it in the variable i. The compiler just makes it work. – David Schwartz 2 mins ago

谢谢!

关于c++ - C++ 基础知识与 Arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11714319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com