Analog Interfacing Overview: Analog and Digital Signals Analog Interfacing MKL25Z Analog Modules
Analog Interfacing Overview: Analog and Digital Signals Analog Interfacing MKL25Z Analog Modules
➢ Analog Interfacing
2
Analog & Digital Signals
Q: Why converting signals to digital form?
0.5
0.25
-0.25
-0.5
-0.75
-1
-1.25
0 1 2 3 4 5 6 7 8 9 10
k
Microcontrollers in Control
Analog B0
input
𝑽−
𝑰𝑵
ATD .
. Digital
. output
➢ The SC signal triggers
the conversion Bn-1
process.
➢ When conversion is
Clock
complete, the EOC is
asserted to indicate
that the digital
output is ready.
DTA Converter
Analog reference voltages
VH VL
Write
8 8
6
B0 6
4
VOUT
Internal Latch
4
0
0 1 2 3 4
k
5 6 7 8 9
DTA .
.
2
0
0 2 4
k
6 8 10
Analog Digital
output . input
Bn-1
Every Write pulse stores
the new binary input
value to the DTA latch,
so its output is held
constant until the next
Write pulse; Zero-Order-
Hold ZOH.
ATD Conversion Example
If the digital value from the ATD is D, then its analog input is:
𝑉𝐻 − 𝑉𝐿
𝑉𝐼𝑁 = 𝑉𝐿 + × 𝐷 = 𝑉𝐿 + Δ × 𝐷
2𝑛 − 1
If 𝑉𝐿 is zero, then 𝑉𝐼𝑁 = Δ × 𝐷.
Example:
A 12-bit A/D converter with reference voltages 𝑉𝐿 = 0.5𝑉 and 𝑉𝐻 = 3.5𝑉, what
is the corresponding input voltage for digital value 100?
Solution:
3
𝑉𝐼𝑁 = 0.5 + 12 × 100 = 0.573𝑉
2 −1
Signal Conditioning Circuit
In many practical cases the sensor output voltage range V1→V2 is different
from the range of the ATD VL→VH , so we need a circuit to translate the sensors
output range to fit that of the ATD to make use of the full dynamic range of the
ATD.
This is the signal conditioning circuit.
In some cases, the sensor’s output comes in the form of a current or charge
so we need to convert it to voltage:
15
Volts
10
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
Sensed Variable
The Big Picture – A Depth Gauge
V_ref // Your software
Analog to ADC_Code = ADC0->R[0];
Pressure
Digital V_sensor = ADC_code*V_ref/1023;
Sensor
Converter Pressure_kPa = 250 * (V_sensor/V_supply+0.04);
Depth_ft = 33 * (Pressure_kPa – Atmos_Press_kPa)/101.3;
Water
Pressure Voltages ADC
V_sensor ADC_Code V_ref
Output Codes
111..111
111..110
111..101
111..100
V_sensor ADC_Code
000..001
Ground 000..000
𝑛𝑢𝑛𝑘𝑛𝑜𝑤𝑛
𝑉𝑢𝑛𝑘𝑛𝑜𝑤𝑛 = 𝑉𝑘𝑛𝑜𝑤𝑛
𝑛𝑘𝑛𝑜𝑤𝑛
▪ Many MCUs include an internal fixed voltage source which ADC can measure for this purpose
Can also solve for 𝑉𝑟𝑒𝑓
2𝑁
𝑉𝑟𝑒𝑓 = 𝑉𝑘𝑛𝑜𝑤𝑛
𝑛
My ADC tells me that channel 27 returns a code of 0x6543, so I
can calculate that:
216
𝑉𝑟𝑒𝑓 = 1.0𝑉
0x6543
13
Inputs to ADC
▪ Differential
▪ Use two channels, and compute difference between them
▪ Very good noise immunity
▪ Some sensors offer differential outputs (e.g. Wheatstone Bridge)
▪ Multiplexing
▪ Typically share a single ADC among multiple inputs
▪ Need to select an input, allow time to settle before sampling
▪ Signal Conditioning
▪ Amplify and filter input signal
▪ Protect against out-of-range inputs with clamping diodes
14
Sample and Hold Devices
Sampling
switch
Output
Signal
Analog Input
Hold
Signal Capacitor
▪ Some A/D converters require the input analog signal to be held constant during
conversion (e.g. successive approximation devices)
▪ In other cases, peak capture or sampling at a specific point in time requires a sampling
device.
▪ A “sample and hold” circuit performs this operation
▪ Many A/D converters include a sample and hold circuit
15
KL25 ANALOG INTERFACING
PERIPHERALS
16
MKL25Z Analog Modules
▪ 80-pin QFP
▪ Inputs
▪ 1 16-bit ADC with 14
input channels
▪ 1 comparator with 6
external inputs, one 6-
bit DAC
▪ Output
▪ 1 12-bit DAC
18
Freedom KL25Z Analog I/O
Inputs
14 external ADC channels
6 external comparator channels
Output
1 12-bit DAC
19
Using a Pin for Analog Input or Output
Data Bus Address
▪ Configuration
bit n Bus
▪ Direction Address
Decoder
Port Data
D Direction
▪ Data
Q
Register
Freescale: is the pin
mux location in this
▪ Output (different ways to PDOR select diagram accurate?
I/O Clock
PDIR select
Port Data Pin Control
D Input Q Register
Register MUX field
20
Pin Control Register to Select MUX Channel
Topic Outlines:
➢ Features
➢ Operation
➢ Example application
MKL25Z ADC
Features
Software selectable Features:
➢ Multiple resolution: 8 to 16 bits using successive approximation.
➢ Software-or-hardware trigger:
– Hardware (SC2[ADTRG=1]): Caused by a timer, the analog comparator, or external pin signal.
– Software (SC2[ADTRG=0]): Conversion is initiated following a write to SC1A.
➢ Additional features:
• Compare logic can discard results within (or outside) a specified range.
• Configurable sample-and-hold time, and conversion speed/power.
• A hardware average function enabled by SC3[AVGE=1], compare function enabled by SC2[ACFE]=1.
The ADC should be calibrated by the calibration instructions in Calibration Function in the manual.
MKL25Z ADC
Block Diagram
Trigger Control
Clock Selection
Result registers
Input Multiplexers
Vref Selection
Compare Function
Calibration Function
Averaging Function
Result Formatting
MKL25Z ADC
Memory Map
The ADC module occupies 27 memory words, 13 of them are listed in the partial
list below. The unlisted 14 are used by the calibration function.
Partial Memory Map:
Using the ADC
▪ ADC initialization
▪ Configure clock
▪ Select voltage reference
▪ Select trigger source
▪ Select input channel
▪ Select other parameters
▪ Trigger conversion
▪ Read results
28
MKL25Z ADC
Functional Description: Configuration
The ADC can be used with the default configuration settings, or in case these settings are not
satisfactory, they can be changed by program via the proper configuration and control registers.
Configuration Parameter Default Options
Input clock source and Bus clock Bus clock/2, ALTCLK, ADACK
divide ratio used for ADCK. Divide ratio is 1 2, 4, 8
Single-input or Differential Single ended Differential
VREFSH & VREFSL Select External VREFH and VREFL Alternate VALTH and VALTL (Internal/External)
SIM->SCGC6 |= SIM->SCGC6_ADC0_MASK
Note: You may find the following definition line in the MCU header file:
#define SIM_SCGC6_ADC0_MASK 0x08000000u
Starting a conversion
➢ A conversion is initiated by one of the following ways:
1) Software SC2[ADTRG]=0: By a write to SC1A.
2) Hardware SC2[ADTRG]=1: By ADHWT event, and a hardware trigger select event, ADHWTSn’s occurred.
3) The transfer of the result to the data registers when continuous conversion is enabled, SC3[ADCO]=1.
– The code reads the result from the ADCx_Rn data result register.
For conversion formats shorter than 16 bits, the upper unused bits need to be filled with:
– All zeros (unsigned format) in single-ended conversions.
1) Input on channel 1
3) Use software trigger Bit 6:5 ADIV 00 ADCK = the input clock ÷ 1
Bit 4 ADLSMP 1 Long sample time
4) Use low-power with a long sample time Bit 3:2 MODE 10 Single-ended 10-bit/differential 11-bit result
Bit 1:0 ADICLK 00 Bus clock
5) Don’t use interrupts
RA = 0xxx
Holds results of conversion.
MKL25Z ADC
Functional Description: Input Connections
➢ The MCU family data sheet provides information on connections between ADC
channels and MCU pins, whereas
➢ the FREEDOM-KL25Z manual explains how MCU pins are connected to the board’s
header connectors.
ADC channel MCU signal (with ALT0 Freedom KL25Z
(single-ended) multiplexer setting) connector and pin
number
0 PTE20 J10 1
3 PTE22 J10 5
4 PTE21 (a), PTB29 (b) J10 3 (a), J10 9 (b)
5 PTD1 (b) J2 12 (b)
6 PTD5 (b) J2 4 (b)
7 PTE23 (a), PTD6 (b) J10 7 (a), J2 17(b)
8 PTB0 J10 2
9 PTB1 J10 4
11 PTC2 J10 10
12 PTB2 J10 6
13 PTB3 J10 8
14 PTC0 J1 3
15 PTC1 J10 12
23 PTE30 J10 11
MKL25Z ADC
Example Application: Hot Plate Sensor (Sensor Characteristic)
The temperature of the hotplate is measured using a thermistor whose resistance varies
with temperature. One type of thermistor (called negative temperature coefficient, or
NTC) has a resistance that falls with increasing temperature as shown:
The NTC resistance at 25°C is 33 kΩ. The manufacturer provides this information in the
device’s data sheet. We can create a voltage divider with an NTC thermistor and a fixed
resistor. The output voltage VTemp will depend on the temperature as shown:
MKL25Z ADC
Example Application: Hot Plate Sensor (Linearization)
We need to linearization the sensor’s input-output characteristic in order to get its output
proportional to the input if the sensor is nonlinear.
The linearizing formula for the given temperature sensor is obtained by curve fitting as:
𝑇
= − 1.1309010−25 𝑛2 + 2.3265610−20 𝑛2 − 1.846310−15 𝑛2
+ 7.1864110−11 𝑛2 − 1.4321610−6 𝑛2 + 0.01555762𝑛 − 36.9861
MKL25Z ADC
Example Application: Hot Plate Sensor
Initialization:
Assume connecting VTemp to pin one of connector J10, which will send the signal through
PTE20 to ADC channel 0:
0x00
// Enable clock to ADC0 Module
// Enable clock to Port E Module
MKL25Z ADC
Example Application: Hot Plate Sensor
Operation:
The code to read the ADC and calculate the temperature:
➢ The code starts a conversion on channel 0 and uses polling to determine when the
conversion is complete.
➢ Then it reads the ADC result and calculates the temperature using a polynomial
approximation. The equation given is reorganized to reduce the complexity and
speed execution.
0x00
MKL25Z DAC
MKL25Z DAC
Features
➢ Digital input 12 bits.
➢ Selectable Reference voltage:
– 𝑉+𝑅𝑒𝑓 can be connected to either DACREF_1 (VREFH) or DAC_REF2 (VDDA).
– The lower reference voltage is connected to ground.
➢ Selectable software or hardware conversion trigger
𝑉+𝑅𝑒𝑓
➢ Analog output range to 𝑉+𝑅𝑒𝑓 :
4096
𝑛+1
Transfer Function: 𝑉𝑜𝑢𝑡 = 4096 𝑉+𝑅𝑒𝑓 (Note: an offset of 1 is added to 𝑛 !)
➢ An amplifier buffers the voltage output signal.
➢ Data for the DAC (12 bits) is written to the DACDAT register:
– The data’s upper four bits are written to DACx_DAT0H.
– The lower byte is written to DACx_DAT0L … x is the DAC number 0 or 1.
➢ DMA support
MKL25Z DAC
Block Diagram
STATUS
MKL25Z DAC
Functional Description: Initialization
The following DAC initialization steps are made before it can be used:
2. In case the analog output should be made available at an MCU pin: (PTE30)
– Enable clock to PORTE module:
Port E Clock Gate Control (bit13 in the SIM_SCGC5 register) controls the clock gate
to the Port E module.
0 Clock disabled
1 Clock enabled
– Connect the DAC output to pin PTE30:
Set the MUX field (bit8-bit10) in the pin control register PCR 30 of PORTE
(PORTE_PCR30) to the binary value 000.
After initialization, the DAC should be enabled and configured by selecting parameters
such as 𝑉+𝑅𝑒𝑓 , buffer mode, and power level mode via the proper control registers.
DAC Operating Modes
▪ Normal
▪ DAT0 is converted to voltage immediately
▪ Buffered
▪ Data to output is stored in 16-word buffer
▪ Next data item is sent to DAC when a selectable trigger event occurs
◦ Software Trigger - write to DACSWTRG field in DACx_C0
◦ Hardware Trigger - from PIT timer peripheral
▪ Normal Mode
◦ Circular buffer
▪ One-time Scan Mode
◦ Pointer advances until reaching upper limit of buffer, then stops
▪ Status flags in DACx_SR
57
MKL25Z DAC
Functional Description: Configuration
DAC input source DAC Data register DACDAT[11:0] DAC data buffer
63
MKL25Z DAC
Example: Waveform Generator