Οι διεπαφές UART, I2C και SPI

Τα πάντα για υπολογιστές και πληροφορική.
Άβαταρ μέλους
Προέλλην
Right to Repair Champion
Right to Repair Champion
Δημοσιεύσεις: 20592

Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό Προέλλην » 25 Δεκ 2019, 16:53

Πρόκειται για τα βασικά πρωτόκολλα επικοινωνίας μεταξύ ασθητήρων και ελεγκτών, βασικά εργαλεία ενός maker.


Serial is an umbrella word for all that is "Time Division Multiplexed", to use an expensive term. It means that the data is sent spread over time, most often one single bit after another. All the following protocols are serial protocols.


UART (Universal Asynchronous Receiver Transmitter)

is one of the most used serial protocols. It's almost as old as I am, and very simple. Most controllers have a hardware UART on board.

Εικόνα

It uses a single data line for transmitting and one for receiving data. Most often 8-bit data is transferred, as follows: 1 start bit (low level), 8 data bits and 1 stop bit (high level). The low level start bit and high level stop bit mean that there's always a high to low transition to start the communication. That's what describes UART. No voltage level, so you can have it at 3.3 V or 5 V, whichever your microcontroller uses. Note that the microcontrollers which want to communicate via UART have to agree on the transmission speed, the bit-rate, as they only have the start bits falling edge to synchronize. That's called asynchronous communication.

For long distance communication (That doesn't have to be hundreds of meters) the 5 V UART is not very reliable, that's why it's converted to a higher voltage, typically +12 V for a "0" and -12 V for a "1". The data format remains the same. Then you have RS-232 (which you actually should call EIA-232, but nobody does.)

The timing dependency is one of the big drawbacks of UART, and the solution is USART, for Universal Synchronous/Asynchronous Receiver Transmitter. This can do UART, but also a synchronous protocol. In synchronous there's not only data, but also a clock transmitted. With each bit a clock pulse tells the receiver it should latch that bit. Synchronous protocols either need a higher bandwidth, like in the case of Manchester encoding, or an extra wire for the clock, like SPI and I2C.


SPI (Serial Peripheral Interface)

is another very simple serial protocol. A master sends a clock signal, and upon each clock pulse it shifts one bit out to the slave, and one bit in, coming from the slave.

Εικόνα

Signal names are therefore SCK for clock, MOSI for Master Out Slave In, and MISO for Master In Slave Out. By using SS (Slave Select) signals the master can control more than one slave on the bus. There are two ways to connect multiple slave devices to one master, one is mentioned above i.e. using slave select, and other is daisy chaining, it uses fewer hardware pins (select lines), but software gets complicated.


I2C (Inter-Integrated Circuit, pronounced "I squared C")

is also a synchronous protocol, and it's the first we see which has some "intelligence" in it; the other ones dumbly shifted bits in and out, and that was that. I2C uses only 2 wires, one for the clock (SCL) and one for the data (SDA). That means that master and slave send data over the same wire, again controlled by the master who creates the clock signal. I2C doesn't use separate Slave Selects to select a particular device, but has addressing.

Εικόνα

The first byte sent by the master holds a 7 bit address (so that you can use 127 devices on the bus) and a read/write bit, indicating whether the next byte(s) will also come from the master or should come from the slave. After each byte, the receiver must send a "0" to acknowledge the reception of the byte, which the master latches with a 9th clock pulse. If the master wants to write a byte, the same process repeats: the master puts bit after bit on the bus and each time gives a clock pulse to signal that the data is ready to be read. If the master wants to receive data it only generates the clock pulses. The slave has to take care that the next bit is ready when the clock pulse is given. This protocol is patented by NXP (formerly Phillips), to save licensing cost, Atmel using the word TWI (2-wire interface) which exactly same as I2C, so any AVR device will not have I2C but it will have TWI.

Two or more signals on the same wire may cause conflicts, and you would have a problem if one device sends a "1" while the other sends a "0". Therefore the bus is wired-OR'd: two resistors pull the bus to a high level, and the devices only send low levels. If they want to send a high level they simply release the bus.


https://www.rfwireless-world.com/Termin ... s-I2C.html
https://electronics.stackexchange.com/q ... -how-do-th
1 .
Hätt ich ne 2te Chance würd ich es genauso machen
Ich dreh mich nich um es geht nur darum wer du heute bist
Ich bin zufrieden, da scheint jemand auf mich aufzupassen
Ob es für Gott oder den Teufel is
Ich bereue nichts

Άβαταρ μέλους
Προέλλην
Right to Repair Champion
Right to Repair Champion
Δημοσιεύσεις: 20592

Re: Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό Προέλλην » 25 Δεκ 2019, 16:59

Εικόνα

παράδειγμα κατανομής pins για τα προαναφερθέντα interfaces στην πλακέτα ανάπτυξης Arduino Uno

(μπορείτε εύκολα να αντιστοιχήσετε τα ακρωνύμια των pins απο τα block diagrams του 1ου post στα pins της παραπάνω εικόνας)
1 .
Hätt ich ne 2te Chance würd ich es genauso machen
Ich dreh mich nich um es geht nur darum wer du heute bist
Ich bin zufrieden, da scheint jemand auf mich aufzupassen
Ob es für Gott oder den Teufel is
Ich bereue nichts

Άβαταρ μέλους
Προέλλην
Right to Repair Champion
Right to Repair Champion
Δημοσιεύσεις: 20592

Re: Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό Προέλλην » 25 Δεκ 2019, 17:15

Π.χ. για να συνδέσουμε στο Arduino Uno το GY-521 MPU6050 3-Axis Acceleration Gyroscope 6DOF Module μέσω I2C, (πέρα απο την 3.3V τροφοδοσία) συνδέουμε τα pins A4 (SDA) και A5 (SCL):

https://www.hotmcu.com/gy521-mpu6050-3a ... -p-83.html


Εικόνα

http://elektro.turanis.de/html/prj075/index.html
1 .
Hätt ich ne 2te Chance würd ich es genauso machen
Ich dreh mich nich um es geht nur darum wer du heute bist
Ich bin zufrieden, da scheint jemand auf mich aufzupassen
Ob es für Gott oder den Teufel is
Ich bereue nichts

Άβαταρ μέλους
itteithe
Extreme poster
Extreme poster
Δημοσιεύσεις: 2348
Τοποθεσία: Τρίπολη

Re: Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό itteithe » 25 Δεκ 2019, 17:31

:enamorado: εξαιρετικό θέμα! Όταν θα βρω χρόνο, θα γράψω μερικά πράγματα για τα interruptions, για τα σχετικά pin που τα υποστηρίζουν και πως μπορούμε να τα χειριστούμε προγραμματιστικά (και γιατί μας συμφέρει να τα χρησιμοποιούμε).
2 .
Δῶς μοι πᾶ στῶ καὶ τὰν γᾶν κινάσω

Habemus filium

Άβαταρ μέλους
Προέλλην
Right to Repair Champion
Right to Repair Champion
Δημοσιεύσεις: 20592

Re: Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό Προέλλην » 25 Δεκ 2019, 17:41

Aντίστοιχα, για να συνδέσουμε ένα αισθητήριο που υποστηρίζει SPI, λ.χ. το Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout https://cdn-learn.adafruit.com/download ... eakout.pdf

Pin 13 to SCK (Serial Clock)
Pin 12 to SDO (Serial Data Out)
Pin 11 to SDI (Serial Data In)
Pin 10 to CS (Chip Select)

Εικόνα
1 .
Hätt ich ne 2te Chance würd ich es genauso machen
Ich dreh mich nich um es geht nur darum wer du heute bist
Ich bin zufrieden, da scheint jemand auf mich aufzupassen
Ob es für Gott oder den Teufel is
Ich bereue nichts

vallon
Supreme poster
Supreme poster
Δημοσιεύσεις: 14177

Re: Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό vallon » 26 Δεκ 2019, 02:39

Very Interesting Topic

για να συμμετάσχω
https://www.instructables.com/id/Arduin ... -With-GPS/
https://www.mydronelab.com/blog/arduino-quadcopter.html
αν και δεν είναι αυτό που έχουμε φτιάξει εμείς με τα μαθητούδια ...λέει κάμποσα που αξίζουν
0 .
Αν ο κομπλεξισμός ήταν άθλημα, κάποιοι θα είχαν πάρει πανηγυρικά το πρωτάθλημα.

Άβαταρ μέλους
Προέλλην
Right to Repair Champion
Right to Repair Champion
Δημοσιεύσεις: 20592

Re: Οι διεπαφές UART, I2C και SPI

Δημοσίευσηαπό Προέλλην » 26 Δεκ 2019, 14:40

Πάρτε εδώ!:

Εικόνα

https://www.exp-tech.de/plattformen/tee ... teensy-3.5

120 MHz ARM Cortex-M4 with Floating Point Unit
512K Flash, 192K RAM, 4K EEPROM
Microcontroller Chip MK64FX512VMD12 (PDF link)
1 CAN Bus Port
16 General Purpose DMA Channels
5 Volt Tolerance On All Digital I/O Pins
62 I/O Pins (42 breadboard friendly)
25 Analog Inputs to 2 ADCs with 13 bits resolution
2 Analog Outputs (DACs) with 12 bit resolution
20 PWM Outputs (Teensy 3.6 has 22 PWM)
USB Full Speed (12 Mbit/sec) Port
Ethernet mac, capable of full 100 Mbit/sec speed
Native (4 bit SDIO) micro SD card port
I2S Audio Port, 4 Channel Digital Audio Input & Output
14 Hardware Timers
Cryptographic Acceleration Unit
Random Number Generator
CRC Computation Unit
6 Serial Ports (2 with FIFO & Fast Baud Rates)
3 SPI Ports (1 with FIFO)
3 I2C Ports (Teensy 3.6 has a 4th I2C port)
Real Time Clock

Λέω να μου το φέρει ο Αη Βασίλης :D
0 .
Hätt ich ne 2te Chance würd ich es genauso machen
Ich dreh mich nich um es geht nur darum wer du heute bist
Ich bin zufrieden, da scheint jemand auf mich aufzupassen
Ob es für Gott oder den Teufel is
Ich bereue nichts


Επιστροφή σε “Πληροφορική”