简体   繁体   English

Arduino的C ++基础知识

[英]Basics of C++ with Arduino

I have read about Arduino, and how it uses a language that is similar but not equal to C. I am very familiar with C++, and I was wondering how one would do basic tasks with the Arduino, such as communicating with the I/O pins. 我已经阅读了有关Arduino的信息,以及它如何使用类似于但不等于C的语言。我对C ++非常熟悉,我想知道人们如何使用Arduino做基本任务,例如与I / O通讯。针脚。 I figure that one would need the memory address to the pins, and then do something like this for a "flashing led": 我认为人们需要将内存地址连接到引脚,然后对“闪烁的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.
}

I'm probably really wrong: that's why I'm asking this question. 我可能真的错了:这就是为什么我问这个问题。

This is copied from the comments below my question. 这是从我的问题下面的评论中复制的。 David Schwartz answered my questions: 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; 所以你只要做pin_name = pin_value; (like PORTD = 7;) and the compiler does the magic. (例如PORTD = 7;),而编译器则发挥了魔力。 – David Schwartz 7 mins ago – David Schwartz 7分钟前

[PORTD's] a keyword for a register. [PORTD's]注册的关键字。 It behaves like a variable. 它的行为就像一个变量。 When the compiler sees PORTD = 7; 当编译器看到PORTD = 7时; is compiles it to the necessary assembly code to load a 7 into the PORTD register. 是将其编译为必要的汇编代码,以将7装入PORTD寄存器。 When it sees i = PORTD; 当看到i = PORTD时; is loads the value from the PORTD register and stored it in the variable i. 是从PORTD寄存器加载值并将其存储在变量i中。 The compiler just makes it work. 编译器使其工作。 – David Schwartz 2 mins ago – David Schwartz 2分钟前

Thank you! 谢谢!

You need to use pinMode(your_pin) to activate the IO pin. 您需要使用pinMode(your_pin)激活IO引脚。 Then you can use digital/analog write/read to communicate with them 然后,您可以使用数字/模拟写入/读取与他们进行通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM