You are on page 1of 15

ADC

General Info
Properties
Methods
Events
Types and constants
Application Notes
Typical Usage
Types & definitions
Embedded Components
Component ADC
A/D converter
Component Level: High
Category: CPU Internal Peripherals-Converter-ADC
Typi cal Usage:
(Examples of a typical usage of the component in user code. For more information please see the page Component Code Typical Usage.)
The examples are accompanied by sequence charts which demonstrate the sequence of calling
the methods and events and internal processing of the A/D conversion. These charts show
detailed information about ADC component algorithm for typical usage of this component. Each
chart is composed of three processes. The first ADC peripheral process represents a function of
the internal ADC peripheral module of CPU. Second ADC component process represents a
generated ADC component code (interrupt routines, methods). Third User application process
represents a user written code. Second and third processes are processed by CPU core, thus
they cannot be performed concurrently and the control flow of CPU core is divided into these
processes.
The meaning of the colors used in the diagrams:
Typical settings and usage of ADC component
(1) Continuous conversion of all channels, with interrupt service
(2) One conversion of all channels, with interrupt service
(3) One conversion of a speci fied channel, with interrupt service
(4) One conversion of all channels, in polling mode
(5) One conversion of all channels of each component, in shared mode
Version specific typical usage for 5683x derivatives:
(6) Correction for gai n and offset errors
(1) Continuous conversion of al l channel s, with i nterrupt service
Conversion of all channels/samples is performed repeatedly (using interrupts).
Property Interrupt service is enabled, Number of conversion is set to 1. Conversion is started using
Start method. When a conversion of all channels/samples is finished the OnEnd event is invoked
and a new set of measured values is available. All these measured values are available for
reading until the next measurement cycle is finished. Conversion can be stopped using Stop
method.
The example demonstrates the continuous conversion of three channels/samples.
MAIN.C
Pgina 1 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
#def i ne NUM_MEASUREMENTS 10
unsi gned i nt EvnCnt = 0;
byt e Val ues[ 3] ;
voi d mai n( voi d)
{
. . .
AD1_St ar t ( ) ; / / Run measur ement s
/ / measur ed dat a ar e pr ocessed i n t he event s
/ / t hi s cycl e wai t s f or end
f or ( ; ; ) {
i f ( EvnCnt == NUM_MEASUREMENTS) {
/ / af t er 10 cycl es t he conver si on i s st opped
AD1_St op( ) ;
}
. . .
}
}
EVENTS.C
ext er n unsi gned i nt EvnCnt ;
ext er n byt e Val ues[ 3] ;
voi d AD1_OnEnd( voi d)
{
EvnCnt ++; / / I ncr ement count er
/ / measur ed val ues ar e avai l abl e and may be r ead:
AD1_Get Val ue( ( byt e *) Val ues) ; / / Get AD conver si on r esul t s
}
This chart shows a sequence of continuous conversion of all channels if an A/D peripheral does
not support measuring all channels at once.
Pgina 2 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
This chart shows a sequence of continuous conversion of all channels if an A/D peripheral
supports measuring all channels simultaneously.
Pgina 3 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
This chart shows a sequence of continuous conversion of all channels if an A/D peripheral
supports measuring all channels simultaneously and in addition continuous mode.
Pgina 4 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
(2) One conversion of all channels, with interrupt service
All channels are measured after Measure method is invoked. Property Interrupt service is
enabled, Number of conversion is set to 1. When a conversion of all channels/samples is finished
the OnEnd event is invoked and measured values are available (at least until the next
conversion is started).
The following example demonstrates conversion of all channels.
MAIN.C
bool Val uesAvai l abl e = FALSE; / / cont r ol var i abl e
byt e Val ues[ 3] ;
voi d mai n( voi d)
{
AD1_Measur e( TRUE) ;
AD1_Get Val ue( ( byt e *) Val ues) ; / / Get AD conver si on r esul t s
. . .
f or ( ; ; ) {
i f ( Val uesAvai l abl e) {
Val uesAvai l abl e = FALSE; / / cl ear t he cont r ol var i abl e
Pgina 5 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
AD1_Measur e( FALSE) ; / / st ar t next measur ement
}
. . .
}
}
EVENTS.C
ext er n bool Val uesAvai l abl e;
ext er n byt e Val ues[ 3] ;
voi d AD1_OnEnd( voi d)
{
AD1_Get Val ue( ( byt e *) Val ues) ; / / Get AD conver si on r esul t s
Val uesAvai l abl e = TRUE; / / set cont r ol var i abl e
}
This chart shows a sequence of measurements on all channels if an A/D peripheral does not
support measuring all channels simultaneously.
Pgina 6 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
This chart shows a sequence of measurements on all channels if an A/D peripheral supports
measuring all channels simultaneously. Number of measurement is set to 2.
Pgina 7 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
Pgina 8 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
(3) One conversion of a specified channel, with interrupt service
The conversion of single channel can be done using MeasureChan method. Property Interrupt
service is enabled, Number of conversion is set to 1. When a conversion of a channel/sample is
finished the OnEnd event is invoked and the measured value is available by until the next
conversion is started.
The following example demonstrates periodic measurement of three channels in the loop.
MAIN.C
bool Val ueAvai l abl e = FALSE; / / cont r ol var i abl e
byt e Val ue;
byt e channel = 0;
voi d mai n( voi d)
{
. . .
AD1_Measur eChan( TRUE, channel ) ;
AD1_Get ChanVal ue( channel , &Val ue) ;
f or ( ; ; ) {
i f ( Val ueAvai l abl e) {
Val ueAvai l abl e = FALSE; / / cl ear t he cont r ol var i abl e
/ / an act i on, i . e. pr ocess t he measur ed val ue
++channel ;
i f ( channel > 3) channel = 0;
AD1_Measur eChan( FALSE, channel ) ;
}
. . .
}
}
EVENTS.C
ext er n bool Val ueAvai l abl e;
ext er n byt e Val ue;
ext er n byt e channel ;
voi d AD1_OnEnd( voi d)
{
/ / Get AD conver si on r esul t s of t he speci f i ed channel
AD1_Get ChanVal ue( channel , &Val ue) ;
Val ueAvai l abl e = TRUE; / / set cont r ol var i abl e
}
The following chart shows single conversion of selected channel, the first conversion without
waiting for a result and the second conversion with waiting for a result.
Pgina 9 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
(4) One conversion of all channels, in polling mode
The measurement on all channels is performed in Measure method, i.e., Measure method does
not finish until the conversion of all channels is finished. Property Interrupt service is disabled,
Number of conversion is set to 1.
MAIN.C
bool Val uesAvai l abl e = FALSE; / / cont r ol var i abl e
byt e Val ues[ 3] ;
voi d mai n( voi d)
{
. . .
AD1_Measur e( FALSE) ; / / measur e al l channel , not wai t f or r esul t
whi l e ( AD1_Get Val ue( ( byt e *) Val ues) == ERR_NOTAVAI L) ; / / Wai t f or r esul t
. . .
AD1_Measur e( TRUE) ; / / measur e al l channel , wai t f or r esul t
AD1_Get Val ue( ( byt e *) Val ues) ; / / Get AD conver si on r esul t s
.
. .
}
This chart shows a sequence of conversion of all channels if an A/D peripheral does not
support measuring all channels simultaneously.
Pgina 10 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
This chart shows a sequence of conversion of all channels if an A/D peripheral supports
measuring all channels at once.
Pgina 11 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
This chart shows a sequence of conversion of all channels if an A/D peripheral supports
measuring all channels simultaneously. Number of conversion is set to 2.
Pgina 12 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
(5) One conversion of all channels of each component, in shared mode
Several ADC components can be used for one ADC device. This example assumes that two AD
Pgina 13 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
converter components 'AD1' and 'AD2' in the project share one ADC device (Sharing properties
set to enabled). Two channels (AN0 and AN2 pins) are set in the component 'AD1' and one
channel (AN1 pin) is set in the component 'AD2'. Number of conversion of each component is set
to 1.
MAIN.C
bool ADResul t 1; / / Fl ag f or component ' AD1'
bool ADResul t 2; / / Fl ag f or component ' AD2'
byt e Val ues[ 2] ; / / component ' AD1' i s set t o t wo channel s measur ement
byt e Val ue; / / component ' AD2' i s set t o one channel s measur ement
voi d mai n( voi d)
{
ADResul t 1 = FALSE;
ADResul t 2 = FALSE;
AD1_Measur e( FALSE) ; / / Run measur ement of t wo channel s f r omcomponent ' AD1'
AD2_Measur e( FALSE) ; / / Run measur ement of t wo channel s f r omcomponent ' AD2'
.
. .
whi l e ( ! ADResul t 1) ; / / Wai t f or measur ement compl et e
AD1_Get Val ue8( ( byt e *) Val ues) ; / / Get AD conver si on r esul t s of component ' AD1'
whi l e ( ! ADResul t 2) ; / / Wai t f or measur ement compl et e
AD1_Get Val ue8( &Val ue) ; / / Get AD conver si on r esul t of component ' AD2'
}
EVENTS.C
ext er n bool ADResul t 1;
ext er n bool ADResul t 2;
voi d AD1_OnEnd( voi d)
{
/ * Measur ement of t wo channel s f r omcomponent ' AD1' i s compl et ed */
ADResul t 1 = TRUE;
}
voi d AD2_OnEnd( voi d)
{
/ * Measur ement of one channel f r omcomponent ' AD2' i s compl et ed */
ADResul t 2 = TRUE;
}
Version specific typical usage for 5683x derivatives:
(6) Correcti on for gain and offset errors
This example demonstrates finding ADC calibration coefficients (M1,B) and correction for gain
and offset errors of any measured input voltage. This example assumes that one sample is
enabled and conversion mode is sequential. The sample must be in single ended mode.
MAIN.C
wor d Ref Hi ; / / Resul t var i abl e f or hi gh cal i br at i on r ef er ence
wor d Ref Lo; / / Resul t var i abl e f or l ow cal i br at i on r ef er ence
wor d Resul t ; / / Resul t var i abl e f or any i nput vol t age
wor d M1, B; / / Cor r ect i on coef f i ci ent s
voi d mai n( voi d)
{
.
. .
/ * Pl ace ADC 0 t o Cal i br at e mode and sel ect hi gh cal i br at i on r ef er ence */
AD1_Set Cal i br at i on( 0x03) ;
/ * Measur e hi gh cal i br at i on r ef er ence */
AD1_Measur e( TRUE) ;
/ * St or e t he r esul t */
AD1_Get Val ue16( &Ref Hi ) ;
/ * Pl ace ADC 0 t o Cal i br at e mode and sel ect l ow cal i br at i on r ef er ence */
AD1_Set Cal i br at i on( 0x01) ;
Pgina 14 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...
/ * Measur e hi gh cal i br at i on r ef er ence */
AD1_Measur e( TRUE) ;
/ * St or e t he r esul t */
AD1_Get Val ue16( &Ref Lo) ;
/ * Pr epar e cor r ect i on coef f i ci ent s */
M1 = Ref Hi - Ref Lo;
B = Ref Hi - ( wor d) ( ( dwor d) ( ( wor d) 3071 << 4) * M1 / ( ( wor d) 2048 << 4) ) ;
/ * Pl ace ADC 0 t o Nor mal mode */
AD1_Set Cal i br at i on( 0x00) ;
/ * Measur e sel ect ed channel i n t he component i nspect or */
AD1_Measur e( TRUE) ;
/ * St or e t he r esul t */
AD1_Get Val ue16( &Resul t ) ;
/ * Cor r ect i on of t he r esul t */
Resul t = ( wor d) ( ( dwor d) ( Resul t - B) * ( ( wor d) 2048 << 4) / M1) ;
.
. .
}
PROCESSOR EXPERT is trademark of Freescale Semiconductor, Inc.
Copyright 1997 - 2013 Freescale Semiconductor, Inc.
Pgina 15 de 15 Component documentation - Typical Usage
21/03/2014 http://127.0.0.1:62853/help/ntopic/com.freescale.doc.processorexpert.components/Beans/...

You might also like