Download Analog And Digital Interfacing

Document related concepts

Islanding wikipedia , lookup

Stray voltage wikipedia , lookup

Fault tolerance wikipedia , lookup

Buck converter wikipedia , lookup

History of electric power transmission wikipedia , lookup

Voltage optimisation wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Power electronics wikipedia , lookup

Control system wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Alternating current wikipedia , lookup

Analog-to-digital converter wikipedia , lookup

Mains electricity wikipedia , lookup

Last mile wikipedia , lookup

Rectiverter wikipedia , lookup

Public address system wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Network analysis (electrical circuits) wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Big Picture
Other
Systems or
ICs
Physical
world
MSP430
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sensors
 A device that converts a physical phenomenon into an
electrical signal
 Physical phenomenon

light, temperature, humidity, pressure, and etc.
 Electrical signal

resistance, capacitance, current, voltage, and etc.
 Maps a physical phenomenon change to a electrical
signal change
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Produce a
proper output
voltage level
Signal Path
Sensors
• Light
• Temperature
• Acceleration
• Humidity
• Pressure
• etc.
• Resistance
• Capacitance
• Current
• Voltage
• etc.
Signal Conditioning
(if necessary)
• Amplification
• Filtering
Convert to
voltage
Analog to
Digital
Conversion
(ADC)
10011101
Converts
voltage to
digital number
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sensors We Have
 Raw sensors
 Produce raw electrical signal
 Signal conditioned sensors
 Have some signal conditioning circuit
 Produce analog output


Mostly Voltage
Sometime Current
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sensors We Have
 Digitalized sensors
 Have some signal conditioning circuit
 Convert analog to digital internally
 Provide digital output
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Produce a
proper output
voltage level
Our Focus
Signal Conditioning
Sensors
• Light
• Temperature
• Acceleration
• Humidity
• Pressure
• etc.
(if necessary)
Sensors
• Resistance
• Capacitance
• Current
• Voltage
• etc.
• Amplification
• Filtering
Convert to
voltage
• We are not going to talk about signal conditioning
• You can find many sensors that are signal conditioned and
provide a proper analog output, or even digitalized
•We will focus on how to understand these signal conditioned
sensors and get the output we want
Analog to
Digital
Conversion
10011101
Converts
voltage to
digital number
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Some Raw Sensors
 Temperature Sensors
 Thermistors

Temperature-sensitive resistor
 RTDs (resistive temperature devices)

Temperature-sensitive resistor
 Thermocouples

Temperature => voltage (mV)
 Pressure sensors
 Piezoresistive

Resistance change with applied pressure
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Photodiodes
 Light => current
 Generate current proportional to light density
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Photodiodes
 Two on Taroko
 S1087: for visible range
 S1087-01: for visible to IR(infrared) range
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Signal Conditioned Analog Output
 Proximity sensor
 Sharp GP2D120XJ00F
 Analog voltage output
 Accelerometer
 ADXL330 3-axis accelerometer
 Analog voltage output
 Industry standard analog output
 Flow sensors, pressure sensors, gas sensors, etc.
 4~20 mA, 0~5 V, 0~10 V
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Proximity Sensor
 Sharp GP2D120XJ00F
 Output voltage proportional to
the reflection distance
 Measure range: 30 cm
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Accelerometer
 Analog Device Inc. ADXL330
 Output voltage proportional to the acceleration
 Measurement range: +/- 3.6 g
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Industry standard analog outputs
 Many industrial instruments provide analog outputs
 4~20 mA current loop
 Most commonly used
 The instrument produce a current, range from 4 mA to
20 mA
 Physical measured quantity linearly maps to this
current range
 Others
 0~5V
 0 ~ 10 V
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
4~20 mA current loop
 Examples
 Measure temperature: 0 ~ 100 oC
Usually this
range is user
configurable
Measurements
0 oC
50 oC
100 oC
Current Output
4 mA
12 mA
20 mA
 Measure flow speed: 0 ~ 50 m3/h
Measurements
0 m3/h
25 m3/h
50 m3/h
Current Output
4 mA
12 mA
20 mA
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
How to Convert to Voltage
 Add a resistor
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Others Analog Output Sensors
 Ultrasound sensors

http://www.baumerelectric.com/en/shop/catalog/Ultraschall_Sensoren/page3.html?filter=all&sort=Familie&dir=ascending
 Gyroscopes

http://www.analog.com/en/subCat/0,2879,764%255F801%255F0%255F%255F0%255F,00.html
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Digitalized Sensors
 Ultrasound sensors
 SRF10
 Interface: I2C
 Temperature and Humidity Sensor
 SHT11
 Interface : manufacturer defined
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Digitalized Sensors
 Magnetometer
 SFE MicroMag 3-Axis
 Interface : SPI
 Applications: detecting vehicles
 Digital compass
 Hitachi HM55B
 Interface : UART
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Digitalized Sensors
 GPS
 Garmin GPS18
 Interface : UART
 Image sensor
 ST VS6451
 Interface: I2C
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Understand The Analog Sensors
 Most important goal
 Obtain the relationship between voltage and the
physical phenomenon quantity you want to measure

Transfer function
 Understand the characteristics and limitation of the
sensors
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sensor Characteristics Definitions
 Transfer Function
 Relationship between physical quantity and output voltage
 Sensitivity
 Ratio of change between physical quantity and output voltage
 Accuracy
 Largest expected error
 Linearity
 How linear the transfer function is
 Noise
 In real world, signal are usually coupled with noise
 Resolution
 minimum detectable signal fluctuation
 Bandwidth
 response times to an instantaneous change in physical signal
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Transfer Function
Ratiometric?
Output voltage is a ratio
of supply voltage
 ADXL330 accelerometer
From ADXL330 datasheet
It means when supply voltage Vs is 3 V, if the acceleration
increase by 1 g, the output voltage will increase 330 mV (typical)
It means when supply voltage Vs is 3 V, if the acceleration is
0 g, the output voltage will be 1.5 V (typical)
Transfer function: Voltage (V) = 1.5 + (0.3 * acceleration (g) )
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Transfer Function
 Proximity sensor: Sharp GP2D120XJ00F
Transfer function
Voltage (V) = f(Distance (cm))
Not a linear function
• Check datasheet
• if the manufacturer has provided
• Table mapping
• Approximations
• By a few straight lines
• Curve fitting
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Transfer Function
 Photodiode: S1087, S1087-01
From Taroko Schematic
Step 2: find the
relationship between
current and the
voltage at ADC4
(V = IR)
Step 1: find the
relationship between
light level and current
Transfer function
Voltage (V) = f(light level(lx))
Calculate by yourself
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sensitivity
 ADXL330 accelerometer
 Sharp GP2D120XJ00F
 Not a constanst
 Photodiode: S1087, S1087-01
 (Current)/(light level)
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sharp GP2D120XJ00F
Linearity
Photodiode: S1087, S1087-01
BAD
GOOD
ADXL330 accelerometer
GOOD
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Bandwidth
 How fast the next valid output ready
When power up, you have to wait for
52.9ms to get a first valid output. You
have to wait another 47.9ms to get the
second one.
Proximity sensor: Sharp GP2D120XJ00F
For the module we will use, the filter
capacitor is 0.1 uF. It means the
accelerometer module can have at most
50 different read-outs in one second
ADXL330 accelerometer
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Analog to Digital Conversion
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Analog to Digital Converter (ADC)
 ADC takes two inputs
 Voltage reference


Range of voltage that it can measure
It has a limited range
 Sensor signal
That’s why we need signal
conditioning to produce a
proper voltage output
 Resolution
 How many bits the ADC can output
 Sample Rate
 How many sample it can takes in one second
 For MSP430F1611, it is about 200 ksps
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Voltage reference
 Input voltage compares to the voltage reference
 Ratio of (input voltage)/(voltage reference) determines the output number
 Two voltage references
 Positive voltage reference (Vref+)
 Negative voltage reference (Vref-)
• In this figure, Vref+ =
1.6V; Vref- = 0 V
[0,0.1) V -> 0000
[0.8,0.9) V -> 1000
[1.5,1.6) V -> 1111
• If we change the Vref+ =
3.2V; Vref- remains 0 V,
then
[0,0.2) V -> 0000
[1.6,1.8) V -> 1000
[3.0,3.2) V -> 1111
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
About Voltage Reference
 Voltage reference can by internal or external
 Many ADCs have internal voltage reference integrated
 Unstable Voltage reference will affect the accuracy of
the ADC
 Minimum, Maximum
 If input voltage > Vref+, always output 1111
 If input voltage < Vref-, always output 0000
 Voltage Reference cannot exceed power supply voltage
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Resolution
 You heard 10-bit ADC, 12-bit ADC, 16-bit ADC
 What are they?
 Number of bits the ADC can output
• This is a 4-bit ADC
[0,0.1) V -> 0000
[0.1,0.2) V -> 0001
[0.2,0.3) V -> 0010
[1.5,1.6) V -> 1111
• If it is a 6-bit ADC
[0,0.025) V -> 000000
[0.025,0.05) V -> 000001
[0.05,0.075) V -> 000010
[1.575,1.6) V -> 111111
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Detectable Voltage Change
 Voltage reference + Resolution
 Define the detectable voltage change
 Detectable voltage change = ((Vref+) – (Vref-))/(2^resolution)
 Examples
 Vref = 1.6V, Resolution = 4-bit
1.6/(2^4) = 0.1 V
 Vref = 3.2V, Resolution = 4-bit
3.2/(2^4) = 0.2 V
 Vref = 1.6V, Resolution = 6-bit
1.6/(2^6) = 0.025 V
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Sample And Hold
 There is a sample-and-hold circuit before
A/D conversion
 Mostly integrated in the ADC chip
 When no conversion, switch S1 is open
 When a conversion start
 S1 closed
 Input signal charge C1
 S1 open, C1 holds the value of input signal
 A/D conversion
 Sample-and-hold time
 Time between S1 close and re-open
 If it is too short

C1 will not fully charged (error)
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
ADC Clock
 ADC needs a clock
 For sample-and-hold
and the A/D
conversion
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
ADC on Taroko
 12-bit on-chip ADC in MSP430
 Voltage reference
 Internal Vref: 1.5V, 2.5V
 No external Vref on Taroko
 User configurable combination for Vref+ and Vref Sample rate
 Approximate 200 ksps
 User configurable sample-and-hold time
 User configurable clock sources
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
An Example: ADXL330
 ADXL330 3-axis accelerometer
 Transfer function: V = 1.5 + (0.3 * g)
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
An Example: ADXL330
 Taroko ADC setting
 Resolution: 12-bit
 Voltage reference


Vref+ = 2.5 V
Vref- = 0 V
 Conversion formula

NADC is the output number

For our setting
N ADC  (2
n  bit
 Vin  Vref  
 1)  

V

V
 ref 
ref  

Vin  0 
Vin 
N ADC  (4095)  

(
4095
)

 2.5 
 2.5  0 
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
An Example: ADXL330
 Transfer function: V = 1.5 + (0.3 * g)
 Conversion formula
Vin  0 
Vin 
N ADC  (4095)  

(
4095
)

 2.5 
 2.5  0 
 When the acceleration is 0 g
 V = 1.5
 NADC = 2457
 When the acceleration is 1.5 g
 V = 1.95
 NADC = 3194
 When NADC = 1784, what is the acceleration?
 When NADC = 2635, what is the acceleration?
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Real World Design
 In practical design, there are always errors
 Noises




Power supply noise
Digital circuit noise
RF noise
Devices noise
 Devices Tolerances
 5% resistor, 1% resistor
 10% capacitor
 etc.
 Temperature drift
 Devices characteristics change when temperature change
 Two very simple methods to deal with two very common
errors
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
First Type of Errors
 Noise
 Dynamic
 ADXL330 at 0 g (1.5V)



Sample at S1 -> ok
Sample at S2 -> it is 0.333 g
Sample at S3 -> it is -0.333 g
1.6V
1.5V
1.4V
 Solution
 Average

(S1 + S2 + S3)/3 = (1.5+1.6+1.4)/3 = 1.5
S1
S2 S3
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
How Many Samples
 How many samples needed to average
 Depends on your requirement
 Suggestion: factor of 2 (2N)

Take 10 samples and average
Total += Si; //(i=1,…,10)
Result = Total/10;

Division => slow
Take 16 samples and average
Total += Si; //(i=1,…,16)
Result = (Total>>4);
Bit shift => fast
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Cautions!
 Is the Total large enough to hold the summation
 Max possible Total = 4096*16 = 65536
 Total should be at least 16-bit unsigned int
 IMPORTANT!!
 int in IAR for MSP430 is 16-bit long
Total += Si; //(i=1,…,16)
Result = (Total>>4);
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Second Type of Errors
 Offset
 Static
 For example: ADXL330


For A accelerometer, 0 g output maybe 1.5V
For B accelerometer, 0 g output maybe 1.54V
30% of ADXL330, 0 g
output is 1.53V
4% of ADXL330, 0 g
output is 1.5V
From ADXL330 datasheet
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Calibration
 Maintain a calibration constant, adjust the offset error
 ADXL330 with calibration
 Transfer function: V = (1.5 + Ccal) + (0.3 * g)

Ccal is calibration constant
 For A accelerometer, 0 g output is 1.5V


Ccal = 0
If NADC = 2879, acceleration should be 0.859 g
 For B accelerometer, 0 g output is 1.54V


Ccal = 0.04
If NADC = 2879, acceleration should be ?? G
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Calibration Reference
 Calibration needs a reference
 Precision of the reference decide the precision of the
calibration
 How do you calibrate the accelerometer ADXL330??

You must produce a precise 0 g (or 1 g) acceleration
Acceleration => voltage
To calibrate, you want
to know the voltage
output at 0 g. But how
to make 0 g?
 How do you calibrate a temperature sensor?
You must produce a precise temperature
Temperature => voltage
To calibrate, you want
to know the voltage
output at certain
temperature (25 oC).
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Digital Communications
 Sending bits between each other
10011101
A
B
 Serial communication
Our focus
10011101
 Sending bits one by one
A
 UART, SPI, I2C, USB, etc.
 Parallel communication
1
0
 Sending multiple bits at one time
0
1
1
A
 Ethernet
1
0
1
B
B
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Agreements
For digital, wired
communication
 In order to communicate, two parties must at least agree
on:
 Logic level
0V -> logic low (0)
3.3V -> logic high (1)
MSP430
 RS232 (COM port)
 Output data
 Start, stop
 Output data valid time (clock)

-12V -> logic high (1)
12V -> logic low (0)
What number it represent?
Read
0 1 1 0 1 0 0 1
A
2x Read
001 1 1 1 00
B
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Protocols
 Chips to Chips
 SPI: Serial Peripheral Interface
 I2C: Inter Integrated Circuit
 Manufacturer defined
 UART: Universal Asynchronous Receiver Transmitter
 Systems to systems (usually)
A
 Define logic level


RS-232: old traditional
RS-485: popular in industrial control system
UART
UART
RS232 or
RS485
RS232 or
RS485
 Define output data
 UART: Universal Asynchronous Receiver Transmitter
 Define both
 USB
B
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
SPI
 Master–Slave mode
 Synchronous protocol
 All transmissions are referenced to a common clock
 Clock generated by the master (MCU)
 Four main signals
 Master Out Slave In (MOSI): data from master to slave
 Master In Slave Out (MISO): data from slave to master
 Serial CLocK (SCLK or SCK): clock
 Chip Select (CS): select particular peripheral when multiple
peripherals are connected to master
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
SPI Transmission
 Read data at clock edge
MOSI
MISO
CLK
CLK
1
CS
Slave read
0 1 1 0 1 0 0
0
CS
Master read
1 1 01
0 0 1
 Data Register Exchange
 Master transfer a byte to slave, push slave to transfer a byte back to master
 Master Read and Write
 Simultaneously
 Master write only
 Ignore the byte it receive
 Master read only
 Master must transfer a dummy byte in order to initiate a slave transmission
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
2
IC
Pull-up
resistor
 Two wires, multiple devices
 SDA (serial data)
 SCL (serial clock)
 I2C transmission
 START: SDA is pulled low while SCL stays high
 Transfer:


SDA sets the transferred bit while SCL is low (blue)
data is read when SCL rises (green)
 STOP: SDA is pulled high while SCL stays high
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
2
IC
 Acknowledge (ACK) and Not Acknowledge (NACK)
 Upon the transmission of the eighth data bit
 Transmitter releases the SDA
 Master then generates an additional clock pulse on SCL
 Triggers the receiver to acknowledge the byte by pulling
SDA low
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
2
IC
7-bit address
 Addressing
 7-bit address
 Direction bit


If 0: master write to slave
If 1: master read from slave
10-bit address
 10-bit address
 Clock stretching
 A slave may hold the clock line (SCL) low after receiving
(or sending) a bit, indicating that it is not yet ready to
process more data
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Comparison
SPI
I2C
Four wires
Two wires
Full duplex
Half duplex
Higher throughput (then I2C)
Lower throughput
Synchronous protocol
Synchronous protocol
No slave acknowledgment
Acknowledgment
Simple
Complicated
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Manufacturer Defined Protocols
 The ideals are similar
 Feed a clock
 Start, stop
 Read/write the data
 Read datasheet
 Many times
 Get use to those timing diagrams
 We will use the temperature/humidity sensor SHT11
on Taroko as an example
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Implementations
 How to implement these protocols?
 Hardware


There are SPI, I2C peripherals on MSP430F1611
You need to properly configure (setting registers) the modules
 Software
 Use GPIO
 Generate clock
 Read data: set the pin to input
 Write data: set the pin to output
// use P1.0 as clock pin
P1SEL &= ~(0x01); // GPIO
P1DIR |= 0x01; // output
// clock
P1OUT |= 0x01; // high
P1OUT &= ~(0x01); // low
// add some delay between clock
P1OUT |= 0x01; // high
(delay some time)
P1OUT &= ~(0x01); // low
 Things to consider
 Timing: some devices cannot operate too fast
 Check devices datasheet
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
In General
 You want to interface some digital ICs to MSP430
 CC2420 radio chip (SPI)
 8 Mbit flash memory (SPI)
 SHT11 sensor (manufacturer defined, similar to I2C)
 Typical process
1. How to interface?

Connections: two wires, three wires, four wires, etc.
2. How to communicate?
 Available commands
 Timing diagram: start, transfer, (ack), stop
3. How to configure the IC
 Setting the registers on the IC
4. Start reading/writing data from/to the IC
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
An Example: SHT11
 The temperature/humidity sensor on Taroko
 How to Interface
 two wires bi-direction
1.
Use a GPIO pin as clock (SCK),
it is always output direction
2. Use another GPIO as DATA,
dynamic setting it to
input(read) or output(write)
direction
SHT11 datasheet
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
An Example: SHT11
 How to start
 What are the commands available
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Pull-up
An Example: SHT11
 Timing diagram
Data pin in output
direction
Set data pin to input
direction, then SHT11
controls the DATA line
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
An Example: SHT11
 Configure device
If you don’t
understand what
is the meaning,
check datasheet
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Timing
 Check the timing
requirement carefully
 The device won’t work
at all if you exceed the
limit
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
I2C επικοινωνία με το SHT11

#include "include/include.h"

#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0


















// clock output
void sht1x_pulse(void)
{
HUM_SCK_HIGH; //CLOCK pulse -> approx 1.67us;
_NOP();
_NOP();
_NOP();
HUM_SCK_LOW;
}
return error; //error=1 in case of no acknowledge
}
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
I2C επικοινωνία με το SHT11






//---------------------------------------------------------------------------------char s_read_byte(unsigned char ack)
//---------------------------------------------------------------------------------// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;


HUM_DATA_DIR_OUT; // set data pin as output direction
HUM_DATA_OUT_HIGH; //release DATA-line
HUM_DATA_DIR_IN; // set data pin as input direction
for (i=0x80;i>0;i/=2) //shift bit for masking
{
HUM_SCK_HIGH; //clk for SENSI-BUS
if (HUM_DATA_IN) val=(val | i); //read bit
_NOP();_NOP();
HUM_SCK_LOW;
}
HUM_DATA_DIR_OUT; // set data pin as output direction
if(ack == 1) { HUM_DATA_OUT_LOW; } //in case of "ack==1" pull down DATA-Line
else { HUM_DATA_OUT_HIGH; }














sht1x_pulse();
HUM_DATA_OUT_HIGH; //release DATA-line
return val;



}
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
I2C επικοινωνία με το SHT11



























//---------------------------------------------------------------------------------void s_transstart(void)
//---------------------------------------------------------------------------------// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
HUM_DATA_DIR_OUT;//OUTPUT DATA
HUM_DATA_OUT_HIGH;
HUM_SCK_LOW;
//Initial state
_NOP();
HUM_SCK_HIGH;
_NOP();
HUM_DATA_OUT_LOW;
_NOP();
HUM_SCK_LOW;
_NOP();
_NOP();
_NOP();
HUM_SCK_HIGH;
_NOP();
HUM_DATA_OUT_HIGH;
_NOP();
HUM_SCK_LOW;
}
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
I2C επικοινωνία με το SHT11



















//---------------------------------------------------------------------------------void sht1xReset(void)
//---------------------------------------------------------------------------------// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
HUM_DATA_DIR_OUT;//OUTPUT DATA
HUM_DATA_OUT_HIGH;
HUM_SCK_LOW;
//Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
sht1x_pulse();
}
s_transstart(); //transmission start
}
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
I2C επικοινωνία με το SHT11






















//---------------------------------------------------------------------------------char s_softreset(void)
//---------------------------------------------------------------------------------// resets the sensor by a softreset
{
unsigned char error=0;
sht1xReset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
//---------------------------------------------------------------------------------char sht1xReadStatusReg(unsigned char *p_value, unsigned char *p_checksum)
//---------------------------------------------------------------------------------// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
Network and Systems Laboratory
nslab.ee.ntu.edu.tw




































//---------------------------------------------------------------------------------char sht1xWriteStatusReg(unsigned char *p_value)
//---------------------------------------------------------------------------------// writes the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//---------------------------------------------------------------------------------char sht1xMeasure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//---------------------------------------------------------------------------------// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode){ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
HUM_DATA_DIR_IN; // set data line to input direction
for (i=0;i<65535;i++) {
if((HUM_DATA_IN)==0) {
break; //wait until sensor has finished the measurement
}
}
if(HUM_DATA_IN) error+=1; // or timeout (~2 sec.) is reached
*(p_value+1) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
I2C επικοινωνία με το SHT11































void sht1xCalculate(float *p_humidity ,float *p_temperature)
//---------------------------------------------------------------------------------------// calculates temperature [ C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [ C]
{ const float C1=-4.0; // for 12 Bit
const float C2= 0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=0.01; // for 14 Bit @ 3V
const float T2=0.00008; // for 14 Bit @ 3V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [ C]
t_C=t*0.01 - 39.6; //calc. Temperature from ticks to C, @ 3V
rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [ C]
*p_humidity=rh_true; //return humidity[%RH]
}
void sht1xInit(void) {
P1IE &= ~(0xE0);
P1DIR |= 0x80;
P1OUT |= 0x80;
P1DIR |= 0x40;
}
//disable P1.5, P1.6, P1.7 interrupt
// turn on SHT11 sensor
// set SHT11 SCK to output direction
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
UART
 UART: Universal Asynchronous Receiver Transmitter
 Two wires: Rx, Tx
 Full duplex
 Asynchronous

No common clock required
 Without common clock
 How do they communicate??
A
B
Rx
Tx
Tx
Rx
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Universal data rates
 Two devices agree on same data rate
 Baudrate: 1200, 2400, 4800, 9600, …, (bits per second)
A
B
(1 MHz system clock)
(8 MHz system clock)
Baudrate
generator
9600
UART
UART
9600
Baudrate
generator
Baudrate: 9600 bps
Length of a bit =
1/9600 (seconds)
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
UART Data
 Send one byte at a time
 Data
 One start bit

This bit became parity bit
when parity checking is
enabled
Pull-down the line
 7 or 8 bits data
 One or two stop bit
 Pull-up the line for one or two slots
 Simple error checking: parity (optional)
 Even parity: If the data has odd number of 1, parity bit = 1 (make it
even); else parity bit = 0
 Odd parity: If the data has even number of 1, parity bit = 1 (make it
odd); else parity bit = 0
 Four parameters for UART communication
 Baudrate, data-bit, parity, stop-bit
 We wrote: 9600 8N1,

Means: baudrate=9600, 8-bit data, no parity, 1 stop bit
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
RS232
 Since the 1960s
 Cable lengths of up to 25 meters
 Signal level
 logic high -> -5 to -15V (typically -12V)
 logic low -> +15 and +1V (typically +12V)
 Interface
Signal
Function
25-pin
9-pin
Direction
Tx
Transmitted data
2
3
From DTE to DCE
Rx
Received data
3
2
To DTE from DCE
RTS
Request to send
4
7
From DTE to DCE
CTS
Clear to send
5
8
To DTE from DCE
DTR
Data terminal ready
20
4
From DTE to DCE
DSR
Data set ready
6
6
To DTE from DCE
DCD
Data carrier detect
8
1
To DTE from DCE
RI
Ring indicator
22
9
To DTE from DCE
FG
Frame ground (chassis) 1
-
Common
SG
Signal ground
5
Common
7
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
RS485
 Two-wire, half-duplex
 Multipoint serial connection
 Signal level
 Difference between the wires’ voltages


A – B > 0.2V => logic high (0)
A – B < -0.2V => logic low (1)
 Why it is popular in industrial applications
 Only two wire
 35 Mbit/s up to 10 m and 100 kbit/s at 1200 m
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
USB Serial IC
 That’s what we have on Taroko
 FT232BL (www.ftdichip.com)
 To communicate with PC
 It simulate a serial communication on PC through USB
interface

Virtual com port
Network and Systems Laboratory
nslab.ee.ntu.edu.tw
Applications
All you need to do is properly configure
UART for both sides (baudrate, databit, parity, stop-bit), and start
sending/receiving data
RS232 IC
COM PORT
UART
USB
MSP430
PC or
other
systems
USB
serial IC
(FT232)
Other
systems
RS485 IC
RS485 IC