Microcontroller basics Lesson 4 Parallel communication Serial communication
Microcontroller basics
Lesson 4 • • Parallel communication Serial communication I 2 C communication SPI communication
Parallel vs. Serial Parallel Serial Conveying multiple binary digits (bits) simultaneously Conveys only a single bit at a time Many wires/traces (number of electrical conductors) Typically 1 wire/bit Less wires/traces Communication is based on rising and falling edges of a square wave Examples: ISA, ATA, SCSI, PCI Examples: UART, I 2 C, SPI
Parallel communication • Requires more traces and thus more space on the PCB • Can be faster than serial communication • Data transfer rate can be increased by increasing the number of connections • Used in part to part communication in computers RAM <-> CPU <-> GPU
Parallel LCD (4 -bit data bus) Hitachi HD 44780 • RS pin to digital pin 12 • Enable pin to digital pin 11 • D 4 pin to digital pin 5 • D 5 pin to digital pin 4 • D 6 pin to digital pin 3 • D 7 pin to digital pin 2 • R/W pin to ground • VSS pin to ground • VCC pin to 5 V • LED+ to 5 V (220Ω in series) • LED- to ground • CA to 0. . 5 V
Parallel LCD (4 -bit data bus) Hitachi HD 44780 https: //www. arduino. cc/en/Tutorial/Hello. World This example can also be found in the Arduino IDE examples
Serial communication • Point-to-point communication between two devices • Requires two wires minimum (RX and TX) • Also recommended to connect the ground wires of the devices together • TTL (transistor-logic) and RS 232 voltage levels are different!
Serial communication
Serial communication • Adjustable parameters: • • Baud rate - Communication speed Data bits - Number of actual data bits in one byte Stop bit - Length of the stop after each sent byte Parity - Used for error checking
Arduino C – More Serial functions Serial. available() Returns the number of bytes available in the serial input buffer. Serial. write() Write a single byte into the serial port Serial. read() Read a single byte from the input buffer MORE INFO: • http: //arduino. cc/en/reference/serial
Serial: I 2 C & SPI I 2 C (TWI) SPI Pins: SDA (A 4), SCL (A 5) Pins: MISO (11), MOSI (12), SCK (13), SS (10/any) Up to 127 devices. Devices have an address (0 x 00 to 0 x 7 F) which determines where data is sent/received from. SDA for both sending and receiving Devices are chosen based on SS (slave select pin). One line for sending (MOSI, master out, slave in) and one for receiving (MISO, master in, slave out). Data can go both ways at the same time Wire. h SPI. h Easier to daisy-chain Faster
I 2 C communication • Bus type communication (one transmits, multiple receives) • All the devices in the bus have a specific 7 -bit address • Requires 2 wires (SDA & SCL) • SDA = Serial Data Line • SCL = Serial Clock Line
I 2 C wiring
Example 1 – I 2 C communication Example of a generic I 2 C communication #define SENSOR_I 2 CADD 0 x 45 //Address for the sensor. Different for each device connected. #include <Wire. h> //I 2 C library char data. Byte; void setup(){ Wire. begin(); Serial. begin(9600); } void loop(){ Wire. begin. Transmission(SENSOR_I 2 CADD); //Begin transmission to address 0 x 45 Wire. write(0 x 22); //Register address within the sensor where the data is to be read from Wire. end. Transmission(); Wire. request. From(SENSOR_I 2 CADD, 1); //Get a byte(1) from the register address 0 x 22 if(Wire. available()) //If the buffer has data { data. Byte = Wire. read(); //Save the data to a variable } Serial. println(data. Byte); //print the received byte to Serial delay(100); }
SPI wiring
SPI communication • Bus type communication • The desired recipient is select individually with dedicated wire • Requires 2 or 3 wires for the communication +1 wire for each device in the bus • Slaves send data to master at the same time when master is sending data to them • Our example is the 3 wire spi (MISO, MOSI, SCLK pins) • Works using the SPI -library in Arduino IDE
Example 2 – SPI communication Example of a generic SPI communication #include <SPI. h> const int chip. Select. Pin = 4; byte data. Byte; void setup() { Serial. begin(9600); SPI. begin(); //Initializes the SPI pin. Mode(chip. Select. Pin, OUTPUT); //Pin for selecting the slave. Multiple slaves -> multiple pins digital. Write(chip. Select. Pin, HIGH); //Deselects the slave. Chip select pin is usually inverted } void loop() { digital. Write(chip. Select. Pin, LOW); //Selects the slave SPI. begin. Transaction(SPISettings(2000000, MSBFIRST, SPI_MODE 0)); //Starts communication //Using the given settings. SPI clock speed, bit order and SPI mode. SPI. transfer(1 << 7 | 0 << 6 | 0 x 12); //Example command for reading data from a register //A byte formed form = (Read. Bit Multiple. Read. Bit 6 -Bit. Register. Address) data. Byte = SPI. transfer(0 b 00); //Returned data is saved to a variable. Master sends nothing Serial. println(data. Byte); //Prints the data SPI. end. Transaction(); //Ends communication digital. Write(chip. Select. Pin, HIGH); //Deselects the slave }
Examples of I 2 C/SPI modules • A wide variety of sensors, drivers and modules use serial data to communicate with microcontrollers • PCD 8544, Nokia LCD (SPI) • Adafruit library with graphics • Smaller footprint library • Mini. OLED display (I 2 C) • Adafruit library (SSD 1306) 128 x 64 i 2 c • Wii Nunchuck (I 2 C) • Library • Example with Processing visualisation • VL 53 L 0 X Time-of-Flight Distance Sensor (I 2 C) • Description • Library
I 2 C and SPI libraries • I 2 C • https: //www. arduino. cc/en/Reference/Wire • SPI • https: //www. arduino. cc/en/Reference/SPI
In next lesson • Wireless communication • Bluetooth • Wi-Fi
- Slides: 20