4. I2C,SPI,ADC,DAC

SPI与I2C

SPI – Serial Peripheral Interface

原理

Serial Communication Protocol

Master-Slave Protocol

  • Master controls all activities

Full Duplex

Maximum Operating Frequency

组成

MOSI
MISO
SCLK Serial Clock
SSEL Slvae Select

Function

  1. Master
名称 实例/功能
SPI SPI ser_port(A6, A5, A4); // mosi, miso, sclk
write recd_val=ser_port.write(0x88|0x66|0x4A); 写入slave,并接受返回信息
format ser_port.format(8,0); //8-bit data, Mode 0
frequency ser_port.frequency(1000000); // Clock frequency is set to 1MHz
  1. Slave
SPISlave SPISlave ser_port(A6, A5, A4, D3); // mosi, miso, sclk, ssel
format ser_port.format(8,0); //8-bit data, Mode 0
frequency ser_port.frequency(1000000); // Clock frequency is set to 1MHz
receive Polls the SPI to whether the data has been received
read ser_port.read(); //read from master
reply ser_port.reply(switch_word); //send reply to master
  • Mode 0:时钟极性为0,时钟相位为0。

  • Mode 1:时钟极性为0,时钟相位为1。

  • Mode 2:时钟极性为1,时钟相位为0。

  • Mode 3:时钟极性为1,时钟相位为1。

Example

  1. SPI Master
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "mbed.h”

SPI ser_port(A6, A5, A4); // mosi, miso, sclk
DigitalOut red_led(D11); //red led
DigitalOut chip_select(D3); //this acts as “slave select”
char switch_word; //word we will send
char recd_val; //value return from slave

int main()
{
    ser_port.format(8,0); //8-bit data, Mode 0
    ser_port.frequency(1000000); // Clock frequency is set to 1MHz
    while (1)
    {
        switch_word=0xA0;
        chip_select = 0; //select slave
        recd_val=ser_port.write(switch_word); //send switch_word
        //and rec. data
        chip_select = 1;
        wait(0.01); //wait 10 ms
        red_led=0;
        if (recd_val==0xA0)
        red_led=1;
    }
}
  1. SPI Slave
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mbed.h”
SPISlave ser_port(A6, A5, A4, D3); // mosi, miso, sclk, ssel
DigitalOut red_led(D11); //red led
char slave_word; //word the slave will send
char recd_val; //value received from the master

int main()
{
//If no formatting is made then default is applied
while (1)
{
    slave _word=0xA0;
    if(ser_port.receive()) //test for data Tx 
    {
        recd_val = ser_port.read(); //read from master
        ser_port.reply(switch_word); //send reply
    }
}
    red_led=0; //preset LED to 0
    if (recd_val==0xA0)
        red_led=1;
}

I2C - Inter-Integrated Circuit

原理

Half-Duplex

Master-Slave Hierarchy

  • Master initials the data transimission

组成

SCK Clock
SDA Bi-Directional

Function

  1. Master
I2C I2C i2c_port(D0, D1); //Configure a serial port, SDA,SCL
frequency
read read from Slave or Master
write write to Slave or Master
start i2c_port.start(); // start condition
stop Stop or Reset the I2C Slave back into the known ready receiving state.
  1. Slave
I2CSlave I2CSlave slave(D0, D1); // SDA, SCL
frequency
receive Checks to see if this I2C Slave has been addressed. (0: Has not been addressed; 1: addressed to read; 3: addressed to write)
address slave.address(0x52); //set the slave address
read
write
stop Reset the I2C Slave back into the known ready receiving state.

Example

  1. Master
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "mbed.h"

I2C i2c_port(D0, D1); //Configure a serial port, SDA,SCL
DigitalOut red_led(p25); //red led
char master_word; //word we will send
char recd_val; //value return from slave
const int device_addr = 0x52; //I2C slave address

int main()
{
    while (1)
    {
        master_word=0xA0;
        //send a single byte of data, in a correct I2C package
        i2c_port.start(); // start condition
        i2c_port.write(device_addr); //send the device address
        i2c_port.write(master_word); //send 1-byte of data
        i2c_port.stop(); //stop condition

        wait(0.002);
        //receive a single byte of data, in correct I2C package
        i2c_port.start();
        i2c_port.write(addr|0x01);
        recd_val=i2c_port.read(addr);
        i2c_port.stop();
        if (recd_val==0xA0)
            red_led=1;
    }
}
  1. Slave
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mbed.h”

I2CSlave slave(D0, D1); // SDA, SCL
DigitalOut red_led(D11); //red led
char slave_word; //word the slave will send
char recd_val; //value received from the master

int main()
{
    slave.address(0x52); //set the slave address
    while (1)
    {
        slave _word=0xA0;
        slave.write(slave_word);//load the word that will be sent
        //check to see if slave was addressed
        int i = slave.receive();
        if (i == 3)
            recd_val = slave.read(); }
        if (recd_val==0xA0)
            red_led=1;
    }
}

SPI与I2C对比

  1. SPI 更快,更节能,双工(更高效),不受限于8位字

  2. I2C更简单,支持多主机(一次只能开一个)

ADC

端口 原理

  • $V_{ref}$: 参考电压

  • $SC$: Start Conversion

  • $EOC$: End of Conversion

$$ D = \frac{V_i}{V_r} 2^n \\\ \\ \frac{Input}{Resolution} = Output \\\ \\ V_i : Analogue \ Input \\\ \\ V_r: Reference \ Voltage \\\ \\ \frac{V_r}{2^n}: Resolution \\\ \\ 0 \le D \le 2^{n-1}: Digital \ Output \ Value $$

分类

Successive Approximation ADC
Delta-sigma ADC
Flash ADC
Dual-Slope Integrating ADC
Pipeline ADC

Successive Approximation ADC

定义

  • Generates the binary output by comparing the input to a factor of the reference voltage which is generated by the SAR register.

电路

处理过程

应用

创建 Digital Voltmeter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include "mbed.h"
AnalogIn Ain(D3);
Serial pc(USBRX,USBTX);

int main(){
    double V;
    while(1){
    pc.printf("----Digital Voltmeter----");
    V = 3.3f*Ain;
    pc.printf("The measured voltage is %.2f", V);
    }
}

Digital Thermometer

DAC

电路 解释

$$ V_o = \frac{D}{2^n}V_r $$

Binary Ladder or R-2R Ladder

配置

$$ V_o = \dfrac{\sum_{i=0}^n 2^i V_i}{2^n} $$

优缺点

Advantages Disadvantages
High Resolution Voltage levels must be exactly the same for all inputs in Weighted Resistors DAC.
High Accuracy Binary weighted Resistor circuit that require Op- Amps are expensive.
Simple to implement In R-2R Ladder converters, delay is caused as the circuit needs switching based on the inputs.
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy