Arduino study notes (1): basics

Arduino program structure

Includes two main functions:

  1. void setup(){} The
    setup function will be called once when the program is running, which is mainly used to initialize variables and set the pin working mode.
  2. void loop(){} is
    equivalent to an infinite loop while(1){}.
    Of course, you can customize the function and be called in the above two functions. Note that the setup function and loop function are essential.

type of data

Insert picture description herebyte: One byte, stores an 8-bit unsigned number.
word: On Uno and other ATMEGA-based boards, a 16-bit unsigned number is stored; while on Due and Zero, a 32-bit unsigned number is stored.
array: It is not a data type, it just means that an array is supported.
String object (String): It can be created and assigned a value or string just like a normal variable. The assignment operator cannot be used for character arrays, but it can be used for String objects. The main disadvantage is that it takes up a lot of memory.

Time function

  1. The delay() function, in milliseconds, blocks the delay.
  2. The delayMicroseconds() function, in microseconds, blocks the delay.
  3. The millis() function, in milliseconds, returns the time since the Arduino board is powered on.
  4. The micros() function, in microseconds, returns the time since the Arduino board is powered on.

I/O function

The Arduino pins are in input mode by default, showing a high impedance state. The pin can be set to INPUT_PULLUP through the pinMode() function, and the pull-up resistor is enabled, and the pin level is high in the idle state. The pull-up resistor can provide enough current to drive the LED light to brighten.
When the pin is set to output mode, it will present a low impedance state.

pinMode() function

Function prototype: pinMode(pin, mode). The specified pin can be set to input or output mode. Use INPUT_PULLUP mode to enable the internal pull-up resistor, while INPUT mode disables the internal pull-up.

digitalWrite() function

Function prototype: digitalWrite(pin, value). The HIGH or LOW value can be written to the designated digital pin. If a pin is set to INPUT mode, call the digitalWrite() function to enable (HIGH) or disable (LOW) the internal pull-up resistor of the pin.

digitalRead() function

Function prototype: digitalRead(pin). The level of the specified digital pin can be read.

analogWrite() function

Function prototype: analogWrite(pin, value). It is used to output a PWM wave with a certain duty cycle to the specified pin. Value: 0~255 (on-off). On most Arduino boards, this function supports digital pins 3, 5, 6, 9, 10 and 11, which are digital pins with "~".

analogRead() function

Function prototype: analogRead(pin). The voltage of the designated analog pin can be read, and the corresponding AD value (0~1023) can be returned, which corresponds to 0~5V.

analogReference() function

Function prototype: analogReference(type).
The type is acceptable:

  1. DEFAULT
    default analog reference value of 5 or 3.3V
  2. INTERNAL (not applicable to Arduino Mega)
    built-in reference, equal to 1.1V on ATmega168 or ATmega328, and equal to 2.56V on ATmega8
  3. INTERNAL1V1 (for Arduino Mega)
    built-in reference 1.1V
  4. INTERNAL2V56 (for Arduino Mega)
    built-in reference 2.56V
  5. EXTERNAL
    reference value is the external voltage applied to the AREF pin (limited to the voltage within 0 to 5V)

Character function

Insert picture description here

random number

randomSeed() function

Function prototype: randomSeed(seed). Pseudo-random number generators, in essence, the generated numbers are randomly distributed, but their order is predictable, that is, they will recur. Use case: randomSeed(analogRead(5)); //Pick up the random noise of analog pin 5 (unused) and return its AD value.

random() function

Function prototype:

  1. long random(max). Returns a random number between 0 and max.
  2. long random(min, max). Returns a random number ranging from min to max.

Interrupt

  1. Hardware interrupt: Most Arduino designs have two hardware interrupts, the corresponding pins are digital pins 2 and 3.
  2. Software interrupt: Arduino supports attachInterrupt() function, and its function prototype is: attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
    or attachInterrupt(pin, ISR, mode), only supports Arduino Due, Zero board.
    Among them, ISR is the designated interrupt processing function. Value of
    mode :
    1) LOW: low level trigger
    2) CHANGE: transition edge trigger
    3) FALLING: falling edge trigger

Arduino communication

UART

  1. Serial port initialization setting: Serial.begin(baund). Set the baud rate.
  2. Serial port sends data: Serial.print(data) or Serial.println(data), Serial.write(data).
  3. The serial port receives data: Serial.read().

I2C

Use SDA (data) and SCL (clock) pins. Need to use the Wire library, that is, the program needs to include the Wire.h header file.

Send (host)

  1. Initialize, set as the host: Wire.begin().
  2. Start, specify the I2C slave address: Wire.beginTransmission(addr).
  3. Data cache: Wire.write(value). Use queues to cache data.
  4. Transmit data and then close: Wire.endTransmission().
  5. Data request: Wire.requestFrom(addr, bytes). Request a certain number of bytes of data from the slave at the specified address.
  6. Response to data request: Wire.onRequest(func). When a data request is received, the specified function is called for processing.

Receive (slave)

  1. Initialize, set as a slave, and specify the address: Wire.begin(addr).
  2. Specify the receive data processing function: Wire.onReceive(func).
  3. Monitor whether there is data: Wire.available().
  4. Receive data on the I2C bus: Wire.read().

SPI

The Serial Peripheral Interface (SPI) bus can be used for serial communication, full duplex. Use SCK (clock), MOSI (master out and slave in) and MISO (master in and slave out). Need to use the SPI library, that is, the program should include the SPI.h header file.

  1. Initialization: SPI.begin().

Guess you like

Origin blog.csdn.net/qq_42386127/article/details/98771322