The basis of 1-Wire technology is a serial protocol using a single data line plus ground reference for communication. 1-Wire protocol uses CMOS/TTL logic and operates at a supply voltage ranging from 2.8V to 6V.
1-Wire provides low-speed data, signaling, and power over a single conductor. It is similar in concept to I2C
There are two types of 1-Wire protocol:
I2C (Inter-Integrated Circuit), pronounced I-squared-C, is also called IIC (pronounced I-I-C), or I2C (pronounced I-two-C). The I2C was designed by Philips in the early '80s, and It is typically used for lower-speed communication between peripheral devices and microcontrollers in short-distance. For example, EEPROM, ADC, and RTC etc.
I2C is a multi-master and multi-slave serial communication protocol means that we have the freedom to attach multiple IC at a time with the same bus. In I2C protocol, communication always started by the master and in the case of multi-master, only one master has the ownership of the bus.
I2C is basically two-wire communication protocol. It uses only two wire for the communication. In which one wire is used for the data (SDA) and other wire is used for the clock (SCL). In I2C, both buses are bidirectional, which means master able to send and receive the data from the slave. The clock bus is controlled by the masters, but in some situations slave devices may force the clock low at times to delay the master sending more data, or to require more time to prepare data before the master attempts to clock it out. This is called "Clock Stretching" and is described on the protocol part.
All slave and master are connected with same data and clock bus, here important thing is to remember these buses are connected to each other using the WIRE-AND configuration which is done by to putting both pins is "open drain". Open drain means that device can pull the corresponding signal line low, but cannot drive it high. Thus, the open-drain configuration allows in I2C to connect multiple nodes to the bus without any short circuits from signal contention.
In the other word, the open-drain allows the master and slave to drive the line low and "release" to high impedance state. In that situation, when master and slave release the bus, need a pull-up resistor to pull the line high. Each signal line has a pull-up resistor on it, to restore the signal to high when no device is asserting it low. The value of the pull-up resistor is very important as per the perspective of the design of I2C system because the incorrect value of pull-up resistor can lead to signal loss. But a good rule of thumb is to start with 4.7 KΩ and adjust down if necessary.
Figure 1: Basic Internal Structure of the Slave and Master Devices
Figure 1 shows a simplified view of the internal structure of the slave and master devices on the SDA/SCL lines, consisting of a buffer to read input data, and a pull-down FET to transmit the data. A device is only able to pull the bus line low (provide short to ground) or release the bus line (high impedance to ground) and allow the external pull-up resistor to raise the voltage. This is an important concept to realize when dealing with I2C devices, since no device may hold the bus high. This property is what allows bidirectional communication to take place.
I2C is a fairly robust protocol, and can be used with short runs of wire (2 ~ 3 m). For long runs, or systems with lots of devices, smaller resistors are better.
The above diagram shows a simplified equivalent circuit for an I2C connection between the masters and slaves. All I2C master and slave devices are connected with only two wires: serial data (SDA) and serial clock (SCL). Each device can be a transmitter, a receiver or both. Some devices are masters – they generate bus clock and initiate communication on the bus, other devices are slaves and respond to the commands on the bus. In order to communicate with specific device, each slave device must have an address which is unique on the bus. I2C master devices (usually microcontrollers) don't need an address since no other (slave) device sends commands to the master.
We know that I2C communication protocol supports the multiple masters and multiple slaves, but most system designs include only one master.
The I2C uses two signals: SDA and SCL. Both signals are bidirectional, and connected via resistors to a positive power supply voltage. All the devices on the bus must use open-collector or open-drain pins. Therefore, when the bus is free, both signals are logic high. Activating the signal means pulling it down (wired AND) to logic 0. The number of the devices on a single I2C bus is almost unlimited — the only requirement is that the bus capacitance does not exceed 400 pF.
The I2C is a serial synchronous communication protocol. Each clock just transfers one bit of data. The SDA signal can only be changed when the SCL signal is low, this means the data should be stable when the clock is high.
Data on the I2C bus is transferred in 8-bits. There are no limitation on the number of bytes, however, each byte must be followed by an acknowledge (ACK) bit. This bit signals whether the device is ready to proceed with the next byte. For all data bits including the ACKnowledge bit, the master must generate clock pulses. If the slave device does not acknowledges transfer, this means that there is no more data or the device is not ready for the transfer yet. The master device must either generate a STOP or REpeated START condition bit.
The default state of SDA and SCL line is high. A master issuing the Start condition first pulls the SDA line low, and next pulls the SCL line low. This puts all slave devices on notice that a transmission is about to start. The I2C bus is considered busy after the assertion of the START bit. If two master devices wish to take ownership of the bus at one time, whichever device pulls SDA low first wins the race and gains control of the bus.
Once all the data frames have been sent, the master will generate a stop condition. The bus master first releases the SCL and then the SDA line. The I2C bus is considered free after the assertion of the STOP bit.
A Stop condition ALWAYS denotes the END of a transmission. Even if it is issued in the middle of a transaction or in the middle of a byte. It is "good behavior" for a master that, in this case, it disregards the information sent and resumes the "listening state", waiting for a new start condition.
The repeated start condition similar to the START condition but both are different to each other. The repeated start is asserted by the master before the stop condition (When the bus is not in an idle state).
A Repeated Start condition is asserted by the master when bus master does not want to lose their control from the bus. The repeated start is beneficial for the master when it wants to start a new communication without the asserting the stop condition.
Repeated start is beneficial when more than one master connected with the I2c Bus.
Sometimes, the master data rate will exceed the slave's ability to provide data. The slave is not ready to sent the data, for example, the A/D conversion has not been completed, or the previous operation has not been completed.
In this case, the slave device will execute what is referred to as "clock stretching". Typically, all clocking is driven by the bus master. The slave simply places the data on the bus or fetches data off the bus in response to the master's clock pulse. At any time during the data transfer processing, an addressed slave can pull the SCL line low to pause the transaction. After the processing of the received data, slave will release the SCL line high to resume the communication.
The clock stretching is the way in which slave drive the SCL line but it is the fact, most of the slave do not drive the SCL line
In I2c communication protocol, most of the I2C slave devices do not use the clock stretching feature, but every master should support the clock stretching.
The arbitration is required in case of multi-master, where more than one master is tried to communicate with a slave simultaneously. In I2C arbitration is achieved by the SDA line.
For example, if two masters in the I2C bus are tried to communication with a slave simultaneously, then they will assert a START condition on the bus. The SCL clock line would be already synchronized by the wired and logic.
In the above case, everything will be good till the state of SDA line will same what is the masters driving on the bus. If any master sees that the state of SDA line differs, what is it driving then they will exit from the communication and lose their arbitration.
Master which is losing their arbitration will wait till bus become free.
Each slave device on the I2C bus should have an unique 7-bit address, called slave address. The master device generates all the serial clock pules and the START and STOP conditions bit. Data is transferred with the most significant bit (MSb) first.
Data transfer from a master transmitter to a slave receiver (data write mode)
Data transfer from a slave transmitter to a master receiver (data read mode)
Sometimes the master needs to write some data and then read from the slave device. In such cases it must first write to the slave device, change the data transfer direction and then read from the slave device. This means sending the I2C address with the R/W bit set to write and then sending some additional data like register address. After writing is finished, the master device generates REpeated START (RESTART) condition and sends the I2C address with the R/W bit set to read. After this, the data transfer direction is changed and the master device starts reading the data.
Two slave addressing formats are defined in the I2C specification: 7-bit and 10-bit addressing. Standard Mode I2C makes use of 7-bit addressing. 10-bit addressing was later added as an extension to standard mode I2C.
In 7-bit addressing format, the slave address is transferred in the first byte after the START condition bit. The bit[7:1] of the byte comprise the slave address, followed by a direction bit at bit. The format is shown as the following diagram.
A seven bit wide address space theoretically allows 128 I2C addresses – however, some addresses are reserved for special purposes. Thus, only 112 addresses are available with the 7 bit address format.
Two groups of addresses are reserved for special functions:
The following table shows I2C addresses reserved for special purposes:
|Slave Address||R/W bit||Description|
|1||000 0000||0||General call address|
|2||000 0000||1||START byte|
|3||000 0001||X||CBUS address|
|4||000 0010||X||Reserved for different bus format|
|5||000 0011||X||Reserved for feture purposes|
|6||000 01XX||X||Hi-Speed Master code|
|7||111 10XX||X||10-bit slave addressing format|
|8||111 11XX||1||Device ID|
10-bit addressing expands the number of possible addresses. Devices with 7-bit and 10-bit addresses can be connected to the same I2C-bus, and both 7-bit and 10-bit addressing can be used in all bus speed modes. Currently, 10-bit addressing is not being widely used.
The Serial Peripheral Interface bus (SPI) is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems.The SPI bus is commonly used for communication with flash memory, sensors, real-time clock (RTCs), analog-to-digital converters, and mode.
SPI features include:
There is no pre-defined protocol in SPI, the master and slave need to agree about the data frame for the exchange. The data frame is described by two parameters: clock polarity (CPOL) and clock phase (CPHA). CPOL parameter is used to define whether the clock is idle when high or low. CPHA parameter is used to shift the sampling phase. If CPHA = 0 the data are sampled on the leading (first) clock edge. If CPHA = 1 the data are sampled on the trailing (second) clock edge, regardless of whether that clock edge is rising or falling. The 4 modes combine polarity and phase according to following table:
Table: SPI Clock Phase (CPHA) and Clock Polarity (CPOL) Operation
|SCLK Transmit Edge||SCLK Receive Edge||SCLK Idle State|
CPOL: used for clock idle state
CPHA: used to shift the sampling phase
Timing Diagram for Clock Polarity and Phase
The following diagram shows the four possible states for these parameters and the corresponding mode in SPI.
SPI bus connection can be configured on 4-wire or 3-wire mode.
4-wire bus mode is SPI standard connection mode. It supports full-duplex data transaction, which means SPI allows to transmit and receive data simultaneously on two data lines (MOSI and MISO),
In 3-wire serial bus mode, MOSI abd MISO lines are combined to a single bidirectional data line (SDAT). Transaction are half-duplex to allow for bidirectional communication. Reducing the number of data lines and operating in half-duplex mode also decreases maximum possible throughput; many 3-wire devices have low performance requirements and are instead designed with low pin count in mind.
SPI devices communicate in full duplex mode using a master-slave architecture with a signal master. There are two ways to connect multiple slaves with a master:
Individual Slave Select Configuration
In this configuration, the SCLK, MOSI and MISO are shared by all devices. Each device has an individual slave select (/SS) lines. The master will pull low on a slave /SS line to select a device for communication. A pull-up resistor on the /SS line is highly recommended for each individual device to reduce cross-talk between devices. The data lines (MOSI and MISO) are connected with slaves in parallel connection.
Daisy Chain Configuration
SPI devices may be connected in a daisy chain configuration. The data lines (MOSI and MISO) are coonected with slaves in serial connection — the first slave data output being connected to the second slave data input, etc. The whole chain acts as a communication shift register; daily chaining is often done with shift registers to provide a block of inputs or outputs through SPI. This configuration only requires a single /SS line from the master, rather than a separate /SS line for each slave.
To write code for a new SPI device, you need to note a few things:
The good definition of Pulse-Width Modulation (PWM) is already in the name itself. It means modulating/varying the width of the pulse only, not the frequency. In the digital control system, PWM is used to control the amount of power sent to a device. Therefore, the PWM is a very effective technique for controlling analog devices using the digital outputs of microcontroller. It is widely used in many fields, such as measurement, communication, power control and transformation.
A PWM signal consists of two main components that define its behavior: a duty cycle and a frequency.
In the digital system, PWM is the method to produce variable voltage using digital means. Typically, digital system only has two output voltages, the high (5V, 3.3V … etc.) or low (0V). But, how it is possible that digital system can produce a voltage that is between the high and low voltages? Here, we use mathematic to analyze the PWM signal and the average output.
PWM uses a rectangular pulse wave whose pulse width is modulated resulting in the variation of the average value of the waveform. Consider a waveform such as shown in figure 2.
Figure 2: A Pulse Wave
The pulse signal is f(t) with period T and a duty cycle D (0.0~1.0). The average voltage of the waveform is given by:
Just focus on one cycle T, as f(t) is a pulse wave, its positive pulse is for 0 < t < D×T and zero pulse is for D×T < t < T. The above expression then becomes:
From this, it is obvious that the average value of the signal is directly dependent on the duty cycle D.
For example, if a digital signal produces pulse high (5V) and low (0V) equally, let us assume the signal in high state for 1 microsecond and in low state for 1 microsecond. The duty cycle is 50%, so the average voltage would be 2.5 volts. Now, change the high voltage in high state for 7 microseconds and in low state for 3 microseconds. The duty cycle now is became to 70%, the average voltage would measure 70% of 5 volts, or 5v x 0.7 = 3.5 volts.
There are a couple types of PWM, and it can be classified in different ways.
There are two kinds of PWM signals: Symmetric and Asymmetric.
Classify PWM Signal by Signal-Alignment
The PWM signal can be classified by signal-alignment in four different types:
The basic idea to generate PWM signal is using a counter (or timer), a CMP (compare) value, and a digital output pin. The counter continuously counts to up or down, and is compared with CMP value. The digital output (PWM) will be changed when the counter matches the CMP value, or when counter resets. It can be implemented by software or hardware. Most of microcontrollers already have hardware modules that can generate PWM signal after initialize the registers.
The PWM timer in the microcontroller runs in one of two modes: Count-Down mode or Count-Up/Down mode.
Generally, Count-Down mode is used for generating left- or right-aligned PWM signals, while the Count-Up/Down mode is used for generating center-aligned PWM signals.
Check the microcontroller's datasheet about PWM timer. Most of ARM microcontrollers use Count-Down mode.
A center-aligned PWM implements the PWM differently from all of the other modes. The PWM timer is configured counting-up and -down mode. The counter starts at zero and count up to the Period (LOAD) vlaue, and when the Period (LOAD) value is reached, the counter starts counting back down to zero. In this mode, the Period (LOAD) value is actually half of the period of the final PWM output.
A Universal Asynchronous Receiver/Transmitter, abbreviated UART (pronounced ' you-art '), is an asynchronous serial communication protocol. The universal designation indicates that the data format and transmission speeds are configurable. UART is very useful interface which can transmit signals without needing to synchronize with a clock signal. This method of transmission is extremely useful for reducing wires and I/O pins compared to parallel systems which transmit data faster bus are far more complex. It provides a cost effective simple and reliable communication between one controller to another controller or between a controller and host computer.
In the UART system, the parallel data is reformatted into a serial stream, and then sends it out through the transmitting wire. In a simplest connection form, a UART can be thought of as a two wire communication system where one line is transmitting and the other is receiving. UART can be configured for Full Duplex, Half Duplex, RX only, or TX only version.
UARTs are commonly used in conjunction with communication standard such as RS-232 or RS-485. The voltage levels of RS-232 are -12V and +12V. Usually all the digital systems work on TTL or CMOS voltage levels, which cannot directly compatible with those of RS-232, a level transition buffer such as MAX232 must be used.
UART transmitted data is organized into packets. Each packet contain [1 START bit ] + [ DATA frame (5 ~ 9 bits) ] + [ PARITY bit (optional) ] + [ STOP bit (1, 2) ], as shown in the following diagram.
The baud rate specifies how fast data is sent over a serial line. The units of the baud rate are bit-per-second (bps). The higher a baud rate goes, the faster data is sent and received, but the speed of data transfer is limited. Since there is no clock signal in the UART communication, the higher transmitting speed will easy leading to get errors on the receiving end. Therefore, each of the two UARTs must be communicating using a known baud rate, or bit frequency. The receiver and transmitter clock rates must be within 5% to avoid errors.
There are different sets of standard baud dates in used depending on the application. Usually the baud rates use 1200, 2400, 4800, 9600,19200, 38400, 57600 and 115200 bps. Both the local and remote UARTs must be configured for the same baud rate.
UARTs transmit data asynchronously, which means there is no clock signal to synchronize the data bits from the transmitting UART to the receiving UART. Typically, an asynchronous communication needs handshaking signals to ensure sender and receiver to coordinate data transfers. In the UART, there are no extra handshaking signals between the transmitter and receiver. The transmitter generates a bit stream based on its clock signal, and then the receiver's goal is to use its internal clock signal to sample the incoming data. Therefore, the transmitter and receiver both need to have same transfer speed and data packet format. Then, the receiver's UART resynchronize the internal clocks on the falling edge of the start bit,and then read the center of each expected data bit based on the baud rate that is set.
UART Flow Control is a method for slow and fast devices to communicate with each other over UART without the risk of losing data.
Consider the case where two units are communicating over UART. A transmitter T is sending a long stream of bytes to a receiver R. R is a slower device than T, and at some point R cannot keep up. It needs to either do some processing on the data or empty some buffers before it can keep receiving data.
R needs to tell T to stop transmitting for a while. This is where flow control comes in. Flow control provides extra signaling to inform the transmitter that it should stop (pause) or start (resume) the transmission.
Several forms of flow control exist. For example, hardware flow control uses extra wires, where the logic level on these wires define whether the transmitter should keep sending data or stop. With software flow control, special characters are sent over the normal data lines to start or stop the transmission.
When hardware flow control is enabled, two extra wires are needed in addition to the data lines. They are called RTS (Request to Send) and CTS (Clear to Send). The CTS signal is an input to the UART that is set by the other UART in the system when it is OK to send data on the bus. The RTS signal is an output of the UART informing the other UART on the bus that it is ready to receive data. The RTS line of one UART is connected to the CTS line of the other UART and vice versa. These lines are only valid before a transmission is started. If the signal is set or cleared after a transfer is started the change will only affect the next transfer.
Figure 2: The Connection for Hardware Flow Control
Software flow control does not use extra wires. Only 3 wires are required (RX, TX, and GND). Transmission is started and stopped by sending special flow control characters. The flow control characters are sent over the normal TX and RX lines. The flow control charac-ters are typically the ASCII codes XON and XOFF (0x11 and 0x13). If device A sends XOFF to device B it means that B should halt transmission to A until B receives an XON character from A.
Figure 3: The Connection for Software Flow Control
Both UART communicating devices must operate at the same baud rate and have same configuration for successful communication:
The most common configuration for UART is often listed as "8N1", which is shorthand for eight data bits, no parity, and one stop bit. This is the default configuration for the UART communication protocol. Therefore, in most of applications you only need to set the baud rate.