这以前的教程包括讨论开始的工具和组件Arduino. However, before starting with Arduino UNO (or any other Arduino board) — and experimenting with hardware projects on various sensors, actuators, and modules — it’s important to get through the basics of Arduino sketches and the embedded C for Arduino-compatible coding.
这term “Arduino-compatible coding” refers to all Arduino and Arduino-compatible microcontroller boards that can be programmed and uploaded using Arduino IDE.
Arduino boards are programmed in “C.” C is a popular system programming language that has minimal execution time on hardware in comparison to other high-level programming languages. It’s the reason most of the operating systems and several programming languages are built on C.
Much like other microcontrollers, the AVR microcontrollers housed in Arduino boards are programmed in a subset of C. A general term for such subsets is “Embedded C” because they apply to programming embedded controllers. The language in which Arduino is programmed is a subset of C and it includes only those features of standard C that are supported by the Arduino IDE.
这并不意味着Arduino C滞后在任何地方,因为它是C的一个子集。标准C的大多数缺失功能都可以轻松地处理。相反,Arduino C是C和C ++的混合体,这意味着它具有功能性且面向对象。
这structure of sketches
本质上,空白的Arduino草图具有两个功能:
1。Setup()
2。环形()
当Arduino Sketch开始执行时,Setup()函数首先称为。它仅执行一次,必须用于初始化变量,设置pinmodes,为硬件组件制作设置,使用库等。
这环形()function is next to the setup() function and it is iterated infinitely. Any other user-defined functions must be called inside the loop function. This is how microcontrollers execute their firmware code by repeating their code for an infinite number of times while they remain powered on.
If users have programmed other microcontrollers (such as 8051, AVR, PIC, or RX), it’s possible to compare the code inside the setup() function with the one outside of the main() loop of an embedded C program — which may have been written to initialize variables and make hardware settings. The setup() and loop() functions have void return types.
A program for a microcontroller must be structured in the same manner as it functions. A microcontroller must be “aware” of its hardware environment and know how to interact with it.
A microcontroller can interact with other hardware components or devices only through these five ways:
1。Digital Input.this may be received in digital LOW or HIGH from other devices. These will be TTL logic levels or voltages converted to TTL logic levels before being applied to the GPIO.
2。Digital Output.与其他设备相比,这可能是数字低或高的输出。同样,输出将是TTL逻辑级别。
3。Analog Input.它可能会从其他设备“感知”模拟电压。使用内置的模数转换器将感知的电压转换为数字值。
4.模拟输出。It may output analog voltage to other devices. This analog output is not analog voltage but a PWM signal that approximates analog voltage levels.
5. Serial Communication.根据标准串行数据协议,例如UART,USART,I2C,SPI,Microwire,1-Wire和Can等,它可能会在串行中传输,接收或接收数据。与其他设备的串行通信可以成为点对点(UART/USART),半双链(I2C)或全双工(SPI)。
知道如何执行这五种类型的MicroController交互的用户可以将任何硬件连接在一起。
An Arduino program or any microcontroller program must first have code for initialization. This may include:
- Defining variables and constants
- Setting up pinModes
- Setting up ADC/PWM channels
- Initializing settings for serial communications
A microcontroller simply intercepts incoming data, processes it according to programmed instructions, and outputs data through its I/O peripherals. This means the program must be organized in specific sections that can handle input data, process data, and control output.
与桌面应用程序不同,µC程序不是为终止设计的。这些程序一直迭代无限的次数,直到系统关闭或达到故障为止。电源关闭后,Arduino或任何微控制器在“电源简历”上重置,并从一开始就开始执行程序。
这个项目包括代码来处理失败时possible. So, any Arduino program can be visualized as a four-step program as follows:
1。Initialization
2。Input- 这应该包括用于数据验证的代码,并处理不正确或意外传入的数据
3。Processing– this should include code for unexpected failures or exceptions raised while data processing
4。Output– this may include code for verification of expected results if the interfaced device can also communicate back to the microcontroller
注释
Arduino C中的评论类似于标准C中的评论。单行评论以一对斜杠(//)开头,并在行末尾(EOL)结束。多行注释以Slash-asterisk对(/*)开头,并以星号斜线对(*/)结束。
这些是单行评论的示例:
//这是单线评论
/* 这个
is
一种
multi-line
comment */
Arduino C数据类型
这se data types are supported in Arduino C.
值得注意的是,“字符串”和“字符串对象”是不同的。字符串数据类型定义了一个简单的字符数组,而字符串数据类型定义了字符串对象。
Arduino C支持这些内置功能以操纵字符串对象:
身份标识
标识符是程序中的变量,函数,常数,类,方法和其他对象的名称。在Arduino C中,标识符应仅包含字母数字字符,DASH( - )或下划线(_)。标识符只能从下划线或字母开始。
Keywords
Keywords are constants, variables, or function names that cannot be used as identifiers.
Arduino C有以下关键字:
变量
变量一种re references in a program with values that can change during the execution of the program. Variables can have names that must be identifiers.
For example, in Arduino C, each variable must be explicitly defined with a specified data type before it’s used in the code.
- If, in a code statement, a variable has been instantiated by a data type but there’s no value assigned to it, the variable is said to be defined but not declared.
- If it’s also assigned a value in the same statement or another statement, it’s said to be declared.
这memory location where the value of a variable is stored at runtime is called its “lvalue” or location value. The value stored in the memory location of the variable is called its “rvalue” or register value.
A defined variable has a lvalue but no rvalue. A declared variable has a lvalue and rvalue.
this is a valid definition of a variable:
int num1;
this is a valid declaration of a variable:
int num1 = 0;
要么…
int num1;
num1 = 0;
Constants
Constants are references in a program with a value that does not change during the execution of the program. The integer and floating-point constants can be declared in Arduino C usingconst关键字或#Define指令。t
这是整数常数有效声明的一个示例:
const int rxpin = 0;
一些内置的常量高,低,输入,OUTPUT, INPUT_PULLUP, LED_BUILTIN, true, and false. The #define directive allows for declaring constants before the compilation of the program.
this is a valid declaration of a constant using #define directive:
#Define Ledpin 3
Operators
这se operators are available in Arduino C:
1。Arithmetic- 加法(+),乘法(*),减法( - ),划分(/)和模块化除法(%)
2。Assignment(=)
3。比较– equal to (==), not equal to (!=), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=)
4。钻头– bitwise and (&), bitwise or (|), bitwise xor (^), bitwise not (~), left bitshift (<<), and right bitshift (>>)
5.Boolean– and (&&), or (||) and not (!)
6.化合物– increment (++), decrement (–), compound addition (+=), compound subtraction (-=), compound multiplication (*=), compound division (/=), compound bitwise and (&=), and compound bitwise or (|=)
7.Cast- 这些操作员将变量的当前类型转换为另一种类型。类型铸件可以通过在变量出现之前指示括号中的新数据类型来应用于变量。
例如:
i = (int) f
8.sizeof– The sizeof operator returns the size of an array in the number of bytes.
9. Ternary(:?)
10。Pointer– dereference operator (*) and reference operator (&)
Statements和statement blocks
声明是处理器的完整C指令。所有c语句以半隆(;)结尾。语句块是包含在括号内({,})内的一组语句。编译器还将语句块视为单个语句。
Operator precedence
this table shows the precedence of operators in Arduino C in descending order:
Control Structures
Arduino C支持这些控制结构:
- if
- 如果别的…
- for
- 开关情况
- while
- do… while…
- break
- continue
- goto
- return
User-defined functions
功能是语句的可叫块。程序员还可以编写自己的功能。功能是根据程序中语句块的功能组织代码的理想方式。
函数定义具有此语法:
function_type function_name(gragments){
function_body
}
函数的类型可以是包括void在内的任何数据类型。该函数有望通过返回语句返回相同类型的值。该语句应是功能主体中的最后一个(返回语句后的任何语句都将无法执行)。
这function exits after the return statement. If the type of a function is void, it should not return any value. The function name can be any identifier, and may or may not need arguments. The arguments are variables that are bound to the function.
功能的主体是语句块。每当调用函数时,执行此块语句块。
this is a valid example of user-defined C function:
int add_inputs(int a,int b,int c){
return a+b+c;
}
A function is called by its name and followed by a parenthesis. Any positional arguments must be passed within parenthesis.
this is a valid example of calling a function:
一种dd_inputs(5, 2, 14)
内置功能
Arduino支持几个内置功能,使编程Arduino板变得更加容易。此表中列出了常用的内置Arduino功能。
可变范围
这scope of a variable refers to visibility and lifetime of a variable in the program. For example, variables that are:
- Only visible inside a function have a local scope.
- 该程序的所有功能都可以看到全球范围。
必须在任何函数之外定义或声明具有全局范围的变量,包括设置()和loop()函数。如果将局部变量定义为静态(使用静态关键字),则仅一个函数可见。但是,它没有被破坏,并且将持续超出函数调用,从而在函数调用之间保留其数据。
If a variable (local or global) is defined as volatile, it’s stored in RAM instead of storage registers. A variable must be defined as volatile if it’s likely to be changed beyond the control of the code (such as in the case of an interrupted service routine).
In the下一个教程,,,,we will discuss how to perform digital output. Also, through the digital output from Arduino, we will build an LED driver.
Filed Under:Arduino,,,,354manbetx
Questions related to this article?
Ask and discuss onEDAboard.com和Electro-Tech-Online.comforums.
告诉我们你的想法!!
你一定是logged in发表评论。