一、电源
可以通过USB口或者直流电源座给Arduino Mega供电。Arduino Mega带有自动切换电源功能。
电源引脚如下:
Vin:电源输入引脚。当使用外部电源通过DC电源座供电时,这个引脚可以输出电源电压。
5V:使用USB供电时,直接输出USB提供的5V电压;使用外部电源供电时,输出稳压后的5V电压。
3V3:最大输出能力为50 mA。
GND:接地引脚。
二、串口
通常我们将Arduino 自带的串口称为硬件串口;
而使用SoftwareSerial类库模拟成的串口,称为软件模拟串口(简称软串口)。
在Arduino提供了0(RX)、1(TX)一组硬件串口,可与外围串口设备通信,如果要连接更多的串口设备,可以使用软串口。
https://www.arduino.cn/thread-47262-1-1.html
硬串口就是自带的rx,tx
软窗口可以使用库:#include"SoftwareSerial"
三、中断
attachInterrupt(中断通道, 中断函数, 触发方式);
实例:
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);//当int.0电平改变时,触发中断函数blink
}
void loop()
{
digitalWrite(pin, state);
}
void blink()//中断函数
{
state = !state;
}
可以看这个网址的介绍:中断
四、Arduino MEGA介绍
https://www.arduino.cn/thread-81898-1-1.html
五、自己写类和库
主要就是写声明用的头文件(.h)和写定义的源文件(.cpp)
头文件中:通常一个类可以包含两个部分——public和private。public中声明的函数和变量可以被外部程序所访问,而private中声明的函数和变量,只能从这个类的内部访问。
//SR04.H
#ifndef SR04_H
#define SR04_H
#include "Arduino.h"
class SR04 {
public:
SR04(int TrigPin,int EchoPin);
float GetDistance();
private:
int Trig_pin;
int Echo_pin;
float distance;
};
#endif[
源文件用来定义上面提到的函数
//SR04.cpp
#include "Arduino.h"
#include "SR04.h"
SR04::SR04(int TP, int EP)
{
pinMode(TP,OUTPUT);
pinMode(EP,INPUT);
Trig_pin=TP;
Echo_pin=EP;
}
float SR04::GetDistance()
{
digitalWrite(Trig_pin, LOW);
delayMicroseconds(2);
digitalWrite(Trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(Trig_pin, LOW);
float distance = pulseIn(Echo_pin, HIGH) / 58.00;
return distance;
}
在SR04.h文件中声明了SR04类及其成员,在SR04.cpp中定义该函数的实现方法。在类声明以外定义成员函数时,需要使用域操作符::,说明该函数作用于SR04类。
https://www.arduino.cn/thread-31722-1-1.html
五、教程汇总贴
网址:教程汇总