Arduino Uno R3 communication

Introduction

Aim 目标

Objectives

Device 设备

Arduino Uno

Arduino Uno 是一款基于 微控制器 ATmega328P的开发板。它有14个数字输入/输出引脚(这些引脚中有6个引脚可以作为PWM输出引脚),6个模拟输入引脚,16 MHz石英晶振,USB接口,电源接口,支持在线串行编程以及复位按键。用户只需要将开发板与电脑通过USB接口连接就可以使用。

在Arduino开发板家族中,Uno开发板是最适合初学者的Arduino开发板。由于它简单易学、稳定可靠,Uno开发板也是应用最为广泛且参考资料最多的开发板。

The Arduino Uno is an open-source microcontroller board based on the Microchip ATmega328P microcontroller and developed by Arduino.
Each of the 14 digital pins and 6 analog pins on the Uno can be used as an input or output, under software control (using pinMode(), digitalWrite(), and digitalRead() functions). They operate at 5 volts. Each pin can provide or receive 20 mA as the recommended operating condition and has an internal pull-up resistor (disconnected by default) of 20-50K ohm. A maximum of 40mA must not be exceeded on any I/O pin to avoid permanent damage to the microcontroller. The Uno has 6 analog inputs, labeled A0 through A5; each provides 10 bits of resolution (i.e. 1024 different values). By default, they measure from ground to 5 volts, though it is possible to change the upper end of the range using the AREF pin and the analogReference() function.

Arduino Uno是Arduino公司开发的基于Microchip ATmega328P单片机的开源微控制器板。

Uno上的14个数字引脚和6个模拟引脚都可以在软件控制下用作输入或输出(使用pinMode()、digitalWrite()和digitalRead()函数)。它们在5伏电压下工作。每个引脚可以提供或接收20ma作为推荐的操作条件,并有一个内部上拉电阻(默认断开)20- 50k欧姆。不能超过任何I/O引脚的最大40mA,以避免对微控制器的永久性损坏。Uno有6个模拟输入,标记为A0到A5;每个提供10位的分辨率(即1024个不同的值)。默认情况下,它们从接地到5伏,尽管可以使用AREF引脚和analogReference()函数改变范围的上端。

在这里插入图片描述

Technical specifications 技术规格

微控制器 ATmega328P
工作电压 5V
输入电压(推荐) 7-12V
输入电压(限制) 6-20V
数字l/O引脚 14(其中6个提供PWM输出)
PWM数字I/0引脚 6
模拟输入引脚 6
每个I/O引脚的直流电流 20毫安
3.3v引脚的直流电流 50毫安
闪存 32 KB(ATmega328P),其中0.5 KB由引导加载程序使用
SRAM 2KB (ATmega328P)
EEPROM 1KB (ATmega328P)
时钟速度 16 MHz
LED_BUILTIN 13
长度 68.6毫米
宽度 53.4毫米
重量 25克
Microcontroller Microchip ATmega328P
Operating Voltage 5 Volts
Input Voltage 7 to 20 Volts
Digital I/O Pins 14 (of which 6 can provide PWM output)
PWM Pins 6 (Pin # 3, 5, 6, 9, 10 and 11)
UART 1
I2C 1
SPI 1
Analog Input Pins 6
DC Current per I/O Pin 20 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 32 KB of which 0.5 KB used by bootloader
SRAM 2 KB
EEPROM 1 KB
Clock Speed 16 MHz
Length 68.6 mm
Width 53.4 mm
Weight 25 g
ICSP Header Yes
Power Sources DC Power Jack & USB Port

General pin functions 通用引脚功能

  • Power 引脚: 开发板可提供3.3V和5V电压输出,Vin引脚可用于从外部电源为开发板供电。
  • Analog In引脚: 模拟输入引脚,开发板可读取外部模拟信号,A0~A5为模拟输入引脚。
  • **Digital 引脚:**UNO R3拥有14个数字I/O引脚,其中6个可用于PWM(脉宽调制)输出。数字引脚用于读取逻辑值(0或1),或者作为数字输出引脚来驱动外部模块。标有"~"的引脚可产生PWM。
  • TX和RX引脚: 标有TX(发送)和RX(接收)的两个引脚用于串口通讯。其中标有TX和RX的LED灯连接相应引脚,在串口通讯时会以不同速度闪烁。
  • **13引脚:**开发板标记第13引脚,连接板载LED灯,可通过控制13引脚来控制LED灯亮灭。一般拿到开发板上电板载灯都会闪烁,可辅助检测开发板是否正常。
  • LED: There is a built-in LED driven by digital pin 13. When the pin is high value, the LED is on, when the pin is low, it is off.

  • VIN: The input voltage to the Arduino board when it is using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.

  • 5V: This pin outputs a regulated 5V from the regulator on the board. The board can be supplied with power either from the DC power jack (7 - 20V), the USB connector (5V), or the VIN pin of the board (7-20V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage the board.

  • 3V3: A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA.

  • GND: Ground pins.

  • IOREF: This pin on the Arduino board provides the voltage reference with which the microcontroller operates. A properly configured shield can read the IOREF pin voltage and select the appropriate power source, or enable voltage translators on the outputs to work with the 5V or 3.3V.

  • Reset: Typically used to add a reset button to shields that block the one on the board

Special pin functions 特殊引脚功能

Each of the 14 digital pins and 6 analog pins on the Uno can be used as an input or output, under software control (using pinMode(), digitalWrite(), and digitalRead() functions). They operate at 5 volts. Each pin can provide or receive 20 mA as the recommended operating condition and has an internal pull-up resistor (disconnected by default) of 20-50K ohm. A maximum of 40mA must not be exceeded on any I/O pin to avoid permanent damage to the microcontroller. The Uno has 6 analog inputs, labeled A0 through A5; each provides 10 bits of resolution (i.e. 1024 different values). By default, they measure from ground to 5 volts, though it is possible to change the upper end of the range using the AREF pin and the analogReference() function.

  • Serial / UART: pins 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL serial chip.

  • External interrupts: pins 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value.

  • PWM (pulse-width modulation): pins 3, 5, 6, 9, 10, and 11. Can provide 8-bit PWM output with the analogWrite() function.

  • SPI (Serial Peripheral Interface): pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). These pins support SPI communication using the SPI library.

  • TWI (two-wire interface) / I²C: pin SDA (A4) and pin SCL (A5). Support TWI communication using the Wire library.

  • AREF (analog reference): Reference voltage for the analog inputs

Communication 通信

The Arduino Uno has a number of facilities for communicating with a computer, another Arduino board, or other microcontrollers. The ATmega328 provides UART TTL (5V) serial communication, which is available on digital pins 0 (RX) and 1 (TX). An ATmega16U2 on the board channels this serial communication over USB and appears as a virtual com port to software on the computer. The 16U2 firmware uses the standard USB COM drivers, and no external driver is needed. However, on Windows, a .inf file is required. Arduino Software (IDE) includes a serial monitor which allows simple textual data to be sent to and from the board. The RX and TX LEDs on the board will flash when data is being transmitted via the USB-to-serial chip and USB connection to the computer (but not for serial communication on pins 0 and 1). A SoftwareSerial library allows serial communication on any of the Uno’s digital pins.

Arduino Uno有许多与计算机、另一个Arduino板或其他微控制器通信的设施。ATmega328提供UART TTL (5V)串行通信,可在数字引脚0 (RX)和1 (TX)上使用。板子上的ATmega16U2通过USB通道进行串行通信,并作为虚拟串口出现在计算机上的软件上。16U2固件采用标准USB COM驱动,不需要外部驱动。然而,在Windows上,需要一个.inf文件。Arduino软件(IDE)包括一个串行监视器,允许简单的文本数据被发送到和从板。当数据通过USB到串行芯片和USB连接到计算机时,板上的RX和TX led将闪烁(但不用于在引脚0和1上的串行通信)。软件串行库允许在任何Uno的数字引脚上进行串行通信。

USART 通用同步/异步串行接收/发送器

A universal synchronous and asynchronous receiver-transmitter (USART) is a type of a serial interface device that can be programmed to communicate asynchronously or synchronously.
The USART’s synchronous capabilities were primarily intended to support synchronous protocols like IBM’s synchronous transmit-receive (STR), binary synchronous communications (BSC), synchronous data link control (SDLC), and the ISO-standard high-level data link control (HDLC) synchronous link-layer protocols, which were used with synchronous voice-frequency modems.
It is mainly composed of three parts: baud rate generator, receiving unit, and sending unit.The functions of each unit are all realized by hardware. At the same time, the configuration interface (control register) is opened to the user in the form of a register, and the process monitoring (status register) is opened to the user in the form of a register.

通用同步和异步收发器(USART)是一种串行接口设备,可以通过编程实现异步或同步通信。

USART的同步功能主要用于支持同步协议,如IBM的同步发送-接收(STR)、二进制同步通信(BSC)、同步数据链路控制(SDLC)和iso标准的高级数据链路控制(HDLC)同步链路层协议,与同步音频调制解调器一起使用。

它主要由三部分组成:波特率发生器、接收单元和发送单元。各单元的功能均由硬件实现。同时,配置界面(控制寄存器)以寄存器的形式向用户打开,进程监控(状态寄存器)以寄存器的形式向用户打开。

USART is a full-duplex universal synchronous/asynchronous serial transceiver module, the interface is a highly flexible serial communication device.

USART是一个全双工通用同步/异步串行收发模块,该接口是一个高度灵活的串行通信设备。

Structure 结构

The clock generator consists of a synchronous logic circuit (driven by an external clock input in synchronous slave mode) and a baud rate generator. The sending clock pin XCK is only used in synchronous sending mode,
The transmitter part is composed of a separate write buffer (transmit UDR), a serial shift register, a parity generator and a control logic circuit for processing different frame structures. The use of the write buffer realizes the communication without delay in the continuous transmission of multiple frames of data.
The receiver is the most complicated part of the USART module, the most important is the clock and data receiving unit. The data receiving unit is used to receive asynchronous data. In addition to the receiving unit, the receiver also includes a parity checker, control logic, shift register and two-stage receiving buffer (receiving UDR). The receiver supports the same frame structure as the transmitter, and supports the detection of frame errors, data overflow, and check errors.

时钟发生器由同步逻辑电路(在同步从模式下由外部时钟输入驱动)和波特率发生器组成。发送时钟引脚XCK仅用于同步发送模式下,
发送器部分由一个单独的写入缓冲器(发送UDR)、一个串行移位寄存器、校验位发生器和用于处理不同帧结构的控制逻辑电路构成。使用写入缓冲器,实现了连续发送多帧数据无延时的通信。
接收器是USART模块最复杂的部分,最主要的是时钟和数据接收单元。数据接收单元用作异步数据的接收。除了接收单元,接收器还包括校验位校验器、控制逻辑、移位寄存器和两级接收缓冲器(接收UDR)。接收器支持与发送器相同的帧结构,同时支持帧错误、数据溢出和校验错误的检测。

Clock generation The clock generation logic generates basic clocks for the transmitter and receiver. The USART supports 4 clock modes: normal asynchronous mode, double-speed asynchronous mode, master synchronous mode, and slave synchronous mode. The USART control bit UMSEL and status register C (UCSRC) are used to select asynchronous mode and synchronous mode. Double speed mode (only applicable to asynchronous mode) is controlled by U2X in UCSRA register. When using synchronous mode (UMSEL = 1), the data direction register (DDR_XCK) of XCK determines whether the clock source is generated internally (master mode) or externally (slave mode). XCK is valid only in synchronous mode.

时钟产生时钟产生逻辑为发送器和接收器产生基础时钟。USART 支持4 种模式的时钟: 正常的异步模式,倍速的异步模式,主机同步模式,以及从机同步模式。USART 控制位UMSEL和状态寄存器C (UCSRC) 用于选择异步模式和同步模式。倍速模式( 只适用于异步模式) 受控于UCSRA 寄存器的U2X。使用同步模式 (UMSEL = 1) 时,XCK 的数据方向寄存器 (DDR_XCK)决定时钟源是由内部产生(主机模式)还是由外部生产(从机模式)。仅在同步模式下XCK 有效。

On-chip clock generation-The internal clock of the baud rate generator is used in asynchronous mode and synchronous master mode.

片内时钟产生-波特率发生器内部时钟用于异步模式与同步主机模式。

The baud rate register UBRR of USART is connected with the descending counter to form a programmable prescaler or baud rate generator. The descending counter counts the system clock, and when it counts to zero or the UBRRL register is written, it will automatically load the value of the UBRR register. When the count reaches zero, a clock is generated. This clock is used as the output clock of the baud rate generator. The frequency of the output clock is fosc/(UBRR+1). The generator divides the output clock of the baud rate generator by 2, 8 or 16, depending on the working mode. The output of the baud rate generator is directly used in the receiver and data recovery unit. The data recovery unit uses a state machine with 2, 8 or 16 states. The specific number of states is determined by the working mode set by the UMSEL, U2X and DDR_XCK bits.

USART 的波特率寄存器UBRR 和降序计数器相连接,一起构成可编程的预分频器或波特率发生器。降序计数器对系统时钟计数,当其计数到零或UBRRL 寄存器被写时,会自动装入UBRR 寄存器的值。当计数到零时产生一个时钟,该时钟作为波特率发生器的输出时钟,输出时钟的频率为fosc/(UBRR+1)。发生器对波特率发生器的输出时钟进行2、8或16 的分频,具体情况取决于工作模式。波特率发生器的输出被直接用于接收器与数据恢复单元。数据恢复单元使用了一个有2、8或16个状态的状态机,具体状态数由UMSEL、U2X 与 DDR_XCK 位设定的工作模式决定。
在这里插入图片描述
.
在这里插入图片描述
.
在这里插入图片描述

时钟发生器的内部结构

USART Block Diagram

Register 寄存器

  • UDRn是收发数据寄存器,收(RXB)和发(TXB)使用不同的寄存器,但都通过UDRn来访问。向TXB写入一个字节,UART就开始发送;RXB保存接收到的数据,带有额外一个字节的缓冲(如同下一节要讲的缓冲区)。

  • UCSRnA包含UART状态位,如三个中断对应的标志,以及一些不常用的设置位。

  • UCSRnB主要用于使能,包括收发器与三个中断的使能位,以及9位帧格式相关的位。

  • UCSRnC是最主要的控制寄存器,可以配置USART的模式与格式。

  • UBRRnLUBRRnH(可以通过UBRRn来访问这个16位寄存器)用于设定波特率,在异步模式下:

    B

    A

    U

    D

    =

    F

    C

    P

    U

    16

    (

    U

    B

    B

    R

    n

    +

    1

    )

    BAUD=\frac{F_{CPU}}{16(UBBRn+1)}

    BAUD=16(UBBRn+1)FCPU
    在这里插入图片描述

UBRRn寄存器值Equations for Calculating Baud Rate Register Setting

interrept 中断

接收完成(RX)、数据寄存器空(UDRE)、发送完成(TX

Principle 原理

Serial Communication 串口通信

In telecommunication and data transmission, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus.

在通信和数据传输中,串行通信是在通信通道或计算机总线上按顺序一次发送一位数据的过程
在这里插入图片描述

串行接口是一种可以将接收来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接收的串行数据流转换为并行的数据字符供给CPU的器件。一般完成这种功能的电路,我们称为串行接口电路。输的数据格式由起始位、数据位、奇偶校验位和停止位组成。

Baud 波特率

即调制速率,指的是有效数据讯号调制载波的速率,即单位时间内载波调制状态变化的次数。波特率表示单位时间内传送的码元符号的个数,它是对符号传输速率的一种度量,它用单位时间内载波调制状态改变的次数来表示。

Data bit数据位

这是衡量通信中实际数据位的参数。当计算机发送一个信息包,实际的数据往往不会是8位的,标准的值是6、7和8位。如果数据使用简单的文本(标准 ASCII码),那么每个数据包使用7位数据。每个包是指一个字节,包括开始/停止位,数据位和奇偶校验位。Arduino默认使用8位数据位,即每次可传输1B数据.

Stop bit停止位

用于表示单个包的最后一位。典型的值为1,1.5和2位。由于数据是在传输线上定时的,并且每一个设备有其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。 Arduino默认是1位停止位.

Parity 奇偶校验位

在串口通信中一种简单的检错方式。有四种检错方式:偶、奇、高和低。对于偶和奇校验的情况,串口会设置校验位(数据位后面的一位),用一个值确保传输的数据有偶个或者奇个逻辑高位。高位和低位不真正的检查数据,简单置位逻辑高或者逻辑低校验。这样使得接收设备能够知道一个位的状态,有机会判断是否有噪声干扰了通信或者是否传输和接收数据是否不同步。Arduino默认无校验位.

RS-232标准

在串行通讯时,要求通讯双方都采用一个标准接口,使不同的设备可以方便地连接起来进行通讯。RS-232-C接口(又称EIARS-232-C)是目前最常用的一种串行通讯接口。该标准规定采用一个25个脚的DB-25连接器,对连接器的每个引脚的信号内容加以规定,还对各种信号的电平加以规定。

HardwareSerial 硬件串口通信

硬件串口的操作类为HardwareSerial,定义于 HardwareSerial.h 源文件中,并对用户公开声明了Serial对象,用户在Arduino程序中直接调用Serial, 就可实现串口通讯

  • begin();初始化串口,通常置于setup()函数中,该函数可配置串口的各项参数。
  • end();结束串口通信。该操作可以释放该串口所在的数字引脚,此时串口Rx和Tx可以作为数字IO引脚使用。
  • available( );判断串口缓冲区的状态,获取串口接收到的数据个数,即获取串口接收缓冲区中的字节数。
  • print();串口输出数据,写入字符数据到串口。将数据输出到串口。数据会以ASCII码形式输出。
  • println();将数据输出到串口,并回车换行。
  • read();读取串口数据,一次读一个字符,读完后删除已读数据。
  • readBytes();从接收缓冲区读取指定长度的字符,并将其存人一个数组中。
  • peek( );返回1字节的数据,但不会从接收缓冲区删除该数据。
  • write( );输出数据到串口。以字节形式输出到串口。

SoftwareSerial 软件串口通信

软串口的操作类为SoftwareSerial,定义于SoftwareSerial.h源文件中,但不像硬串口那样,源文件中并没有事先声明软串口对象,Arduino程序中需要手动创建软串口对象。在使用前需要先声明包SoftwareSerial.h 头文件。其中定义的成员函数与硬串口的类似,基本函数用法都与其类似。软串口是由程序模拟生成的,使用起来不如硬串口稳定,并且与硬串口一样,波特率越高越不稳定。

软串口通过AVR芯片的PCINT中断功能来实现,在Arduino UNO 上,所有引脚都支持PCINT中断,因此所有引脚都可设置为软串口的RX接收端。但在其他型号的Arduino上,并不是每个引脚都支持中断功能,所以只有特定的引脚可以设置为RX端。

  • SoftwareSerial();类构造函数,通过它可以指定软串口的RX和TX引脚。
  • listen();开启软串口监听状态。Arduino在同一时间仅能监听一个软串口。
  • isListening();监测软串口是否正处于监听状态。
  • overflow();检测缓冲区是否已经溢出。

System Design

设计两个arduino开发板之间进行双通道串口通信,输入模拟信号,输出数字信号。通道一利用开发板提供的硬件串口通信进行传输,通道二使用自定义类模板的软件串口通信进行传输。

int analogPin1=A0;          
int analogPin2=A1;
//Set the analog signal reading port

int val1=0;                 
int val2=0;
//The analog reading on the pin.

double a;                   
double b;
//Actual potentiometer voltage value

void setup()
{
	Serial.begin(9600);
	//Set the baud rate of the serial port and enable the serial port. 
    //Serial communication starts to work
}

void loop()
{
	val1=analogRead(analogPin1);    
	//Read the potentiometer voltage signal as an analog signal and pass it into the analog signal read-in port
  	a=val1*5.0/1023;                
  	//Conversion of analog signal mapping value to actual voltage value
	Serial.println(a);              
	//Send data through serial communication

	val2=analogRead(analogPin2);    
  	b=val2*5.0/1023;
	Serial.println(b);	
	//Same as above

	delay(200);                     
	//Add 200ms delay
}
#include <LiquidCrystal.h>
//Import the LCD external head file
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//Set the relevant pins of the LCD screen
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Set the relevant parameters of the LCD screen 

double a;
double b;
//Obtained from serial communication, record the actual voltage value

void setup()
{
	lcd.begin(16,2);
    //Activate the LCD screen
	Serial.begin(9600);
	//Set the baud rate of the serial port and enable the serial port. 
    //Serial communication starts to work
}

void loop()
{
  	a=Serial.parseFloat();
    //Get the first number conforming to the floating-point number format from the serial buffer and convert it to a floating-point number and return
    lcd.setCursor(0,0);
    //Set the output display of the LCD screen from the specified position
	lcd.print(a);
    //LCD display	

  	b=Serial.parseFloat();
  	lcd.setCursor(0,1);
	lcd.print(b);
    Same as above

  	delay(200);
    //Add 200ms delay
}

版权声明:本文为CSDN博主「ridiculous_dzx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36682445/article/details/121452151

生成海报
点赞 0

ridiculous_dzx

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

七、Arduino 串口的使用

Arduino与计算机通信最常用的方式就是串口通信 我们使用USB线连接Arduino Uno与计算机时,Arduino Uno会在计算机上虚拟出一个串口设备,此时两者之间便建立了串口连接。通过此连接,

Arduino与Proteus仿真实例-DHT11温度湿度传感器驱动仿真

DHT11温度湿度传感器驱动仿真 DHT11 是一款基本的超低成本数字温度和湿度传感器。 它使用电容式湿度传感器和热敏电阻来测量周围的空气,并在数据引脚上吐出数字信号(不需要模拟输入引脚)。 它使用起来相当简单,但需要仔细定时来抓取数据。

Arduino使用Esp32-cam开发版

首先你需要先准备一些硬件 1、Esp32-Cam开发版 2、TY-OV2640-v2.0摄像头 3、烧录底座,可以用USB-TTL,我用的是Esp8266的烧录底座 4、杜邦线母线x5,颜色不做要求 开始填坑 贴个大