#include"mbed.h”SPIser_port(A6,A5,A4);// mosi, miso, sclk
DigitalOutred_led(D11);//red led
DigitalOutchip_select(D3);//this acts as “slave select”
charswitch_word;//word we will send
charrecd_val;//value return from slave
intmain(){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;}}
#include"mbed.h”SPISlaveser_port(A6,A5,A4,D3);// mosi, miso, sclk, ssel
DigitalOutred_led(D11);//red led
charslave_word;//word the slave will send
charrecd_val;//value received from the master
intmain(){//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
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.
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.
#include"mbed.h"I2Ci2c_port(D0,D1);//Configure a serial port, SDA,SCL
DigitalOutred_led(p25);//red led
charmaster_word;//word we will send
charrecd_val;//value return from slave
constintdevice_addr=0x52;//I2C slave address
intmain(){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;}}
#include"mbed.h”I2CSlaveslave(D0,D1);// SDA, SCL
DigitalOutred_led(D11);//red led
charslave_word;//word the slave will send
charrecd_val;//value received from the master
intmain(){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
inti=slave.receive();if(i==3)recd_val=slave.read();}if(recd_val==0xA0)red_led=1;}}
SPI与I2C对比
SPI 更快,更节能,双工(更高效),不受限于8位字
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"AnalogInAin(D3);Serialpc(USBRX,USBTX);intmain(){doubleV;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.