SlideShare a Scribd company logo
1
Analog-to-Digital Conversion
(PIC24, dsPIC with DMA)
Industrial Embedded Systems
2
 ADC architectures
 Steps of A/D conversion
 Configure A/D module of PIC24
 A/D Interrupt
 Using multiple channels
 dsPIC utilizing DMA
Industrial Embedded Systems
Outline
3
 Successice Approximation Register
 AD Converter with feedback
 Generally N=8-16 bit (PIC24FJ128GA010 has N=10bit ADC)
 Conversion time: N cycle
Initially set VDAC to ½ Vref, then see if Vin higher or lower than VDAC. If >
½ Vref, then next guess is between Vref and ½Vref, else next guess is
between Vref and GND. Do this for each bit of the ADC
ADC architectures
SAR
4
Counter ramp ADC
 Simple
 Slow
 Conversion time: 2N cycle
 Fast
 Conversion time: 1 cycle
 Requires the most transistors of any
architecture, N-bit converter requires 2N-1
comparators.
 Commercially available flash converters
up to 12 bits.
Flash ADC
Industrial Embedded Systems
ADC architectures
5Industrial Embedded Systems
Input hardware for ADC
 Some low-pass filtering and
a Zener diode to protect the
input PIN from overvoltage
 If input voltage is
higher, a voltage divider
can be used to fit the
voltage level.
 To improve the
performance a voltage
follower is also applied (use
a rail-to-rail Op-Amp)
6
 Configuring AD modul
 Enable analog input channel(s)
 Set the reference voltage
 Clock cycle, clock source (TAD)
 Sampling and conversion settings
 Form of the result
 Enable modul
 Configure A/D interrupt (if required)
 Delete flag belonging to the ADC
 Enable interrupt
 Select the given analog channel
 Sampling time (1-31 TAD)
 Time to charge up the storage capacitor
 Conversion time (12 TAD)
 The end of the conversion is indicated by a flag
 Read the value from the buffer register
 If required delete interrupt flag
Industrial Embedded Systems
Steps of AD conversion
7
 AD module has 6 Special Function Register (SFR)
 AD1CON1, AD1CON2, AD1CON3, AD1CHS, AD1PCFG, AD1CSSL
 PIC24 has 16 analog input pin (AN0-AN15)
 Enable analog input channel(s)
 Using AD1PCFG register
 0s will mark the analog inputs, and 1s will configure the respective
pins as digital inputs
 AD1PCFG = 0xFFDF; // AN5 analog input
 Set the reference voltage
 AD1CON2 register<15:13>
 Internal voltage level (AVDD, AVSS)
 External reference voltage
 Combination of the two
 AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS
Configure AD modul
Industrial Embedded Systems
8
 Clock cycle (TAD)
 In the case of PIC24FJ128GA010 minimum clock cylce TAD is 75ns
 Clock derived from sytem clock (TCY)
 AD1CON3bits.ARDC = 0; //system clock, TCY=2*TOSC
 TAD=TCY (ACDS + 1)
 AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns
Configure AD modul
Industrial Embedded Systems
9
 Sampling time
 Its value depends on the input resistane of the signal and the storage
capacitor
 The frequency of the input signals also has to be taken into consideration
 The accuracy can be improved by increasing the sampling time
Configure AD modul
Industrial Embedded Systems
10
 Sampling and conversion settings
 The sampling time and the beginning of the conversion can be determined
 By software (delete AD1CON1bits.SAMP bit)
 By hardware (Active transition on INT0 pin ends sampling and starts
conversion)
 Automatic conversion
 AD1CON3bits.SSRC = 0b000; //software conversion
 AD1CON3bits.SSRC = 0b111; //automatic conversion
 In the case of software conversion the enough sampling time should be
guaranteed in the code
 In the case of automatic conversion the sampling has to set:
 AD1CON1bits.SAMC = 31; //TSAMP=31TAD=7.75us
 After conversion automatically
 Sampling begins immediately after last conversion completes
 Sampling begins when SAMP bit is set
 AD1CON1.ASAM = 0; //no automatic sampling
Configure AD modul
Industrial Embedded Systems
11
 Data output format
 integer/fractional and signed/nonsigned
 AD1CON1bits.FORM = 0b00; //integer
 PIC put the result of the AD conversion in to a 16-bit register
ADC1BUF x
 Enable the module
 AD1CON1bits.ADON = 1; //turn on ADC
Configure AD modul
Industrial Embedded Systems
12
void initADC()
{
AD1PCFG = 0xFFDF; // enable AN5 chnnel
AD1CON1bits.SSRC = 0b000; //software conversion
AD1CON1bits.FORM = 0b00; //integer
AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS
AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC
AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns
AD1CON1bits.ADON = 1; //turn on ADC
}
Configure AD modul
Industrial Embedded Systems
13
 Potenciometer  Temperature sensor
 Select the input channel
 AD1CHS.CH0SA = 5; //selecting AN5 (potenciometer)
Analog inputs on demo board
Industrial Embedded Systems
14
#include <p24fj128ga010.h>
_CONFIG1( JTAGEN_OFF & FWDTEN_OFF )
_CONFIG2( POSCMOD_HS & FNOSC_PRI )
void initADC(); // declaration of ADC initialization
void initT1(); //declaration of Timer1 initialization
int main ( void )
{
initADC(); // call ADC init
initT1(); // call Timer1 init
TRISA = 0xFF00; // PORTA lower 8 bit output
LATA = 0x0000; // delete PORTA
while(1) //infinite loop
{
AD1CHSbits.CH0SA=5; //select AN5 (potentiometer)
AD1CON1bits.SAMP = 1; //start sampling
TMR1=0; //wait for sampling with software
while(TMR1<50);
AD1CON1bits.SAMP = 0; //end of sampling
AD1CON1bits.DONE = 1; //start conversion
while (!AD1CON1bits.DONE); //end of conversiom
LATA=(ADC1BUF0>>2); //put the 10bit result to the 8 LED
} //end of infinite loop
} //end of main loop
Example 1
Industrial Embedded Systems
15
void initADC() // define of ADC initialization
{
AD1PCFG = 0xFFDF; // enable AN5 chnnel
AD1CON1bits.SSRC = 0b000; //software conversion
AD1CON1bits.FORM = 0b00; //integer
AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS
AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC
AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns
AD1CON1bits.ADON = 1; //turn on ADC
}
void initT1() // define of Timer1 initialization
{
T1CONbits.TON = 1; //Timer1 turn on
T1CONbits.TCS = 0; //system clock =fosc/2
T1CONbits.TCKPS = 0b01; // prescaler 8
TMR1=0; //delete counter
}
Example 1
Industrial Embedded Systems
16
#include <p24fj128ga010.h>
_CONFIG1( JTAGEN_OFF & FWDTEN_OFF )
_CONFIG2( POSCMOD_HS & FNOSC_PRI )
void initADC(); // AD inicializalo fv deklarálása
int main ( void )
{
initADC(); // call ADC init
TRISA = 0xFF00; // PORTA lower 8 bit output
LATA = 0x0000; // delete PORTA
while(1) //infinite loop
{
AD1CHSbits.CH0SA=5; //select AN5 (potentiometer)
AD1CON1bits.SAMP = 1; //Start sampling (we do not need Timer1)
while (!AD1CON1bits.DONE); //end of conversion
LATA=(ADC1BUF0>>2); //put the 10bit result to the 8 LED
} //end of infinit loop
} //end of main loop
Example 2 (automatic conversion)
Industrial Embedded Systems
17
void initADC() // define ADC initialization
{
AD1PCFG = 0xFFDF; // enable AN5 chnnel
AD1CON1bits.SSRC = 0b111; //automatic conversion
AD1CON1bits.FORM = 0b00; //integer
AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS
AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC
AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns
AD1CON3bits.SAMC = 31; //Tsamp=31TAD
AD1CON1bits.ADON = 1; //turn on ADC
}
Example 2 (automatic conversion)
Industrial Embedded Systems
18
 Priority level
 IPC3bits.AD1IP
 Flag
 IFS0bits.AD1IF
 Enable bit
 IEC0bits.AD1IE
 We can select, after how many samling/conversion
 We can give after how many completion of sample/convert sequence an
interrupt occur
 AD1CON2bits.SMPI
 Interrupt function
void _ISR _ADC1Interrupt (void)
{
… //instruction
IFS0bits.AD1IF = 0; // delete ADC interrupt flag!!!
}
ADC interrupt
Industrial Embedded Systems
19
#include <p24fj128ga010.h>
_CONFIG1( JTAGEN_OFF & FWDTEN_OFF )
_CONFIG2( POSCMOD_HS & FNOSC_PRI )
void initADC(); //declaration of ADC initialization
void _ISR _ADC1Interrupt (void); //declaration of interrupt function
int main ( void )
{
initADC(); // call ADC initialization
SRbits.IPL = 0; // priority level of processor
IPC3bits.AD1IP = 3; //priority level of ADC
IEC0bits.AD1IE = 1; //enable ADC interrupt
TRISA = 0xFF00; // PORTA lower 8 bit output
LATA = 0x0000; // delete PORTA
AD1CHSbits.CH0SA=5; //select AN5 (potentiometer)
AD1CON1bits.SAMP = 1; //Start sampling (we do not need Timer1)
while(1); //infinite loop
}
Example 3 (automatic conversion + interrupt)
Industrial Embedded Systems
20
void _ISR _ADC1Interrupt (void)
{
LATA=(ADC1BUF0>>2); //instruction: put the result to the LED
IFS0bits.AD1IF = 0; //delete ADC interrupt flag
}
void initADC() // define of ADC initialization
{
AD1PCFG = 0xFFDF; // enable AN5 chnnel
AD1CON1bits.SSRC = 0b111; //automatic conversion
AD1CON1bits.FORM = 0b00; //integer
AD1CON1bits.ASAM = 1; //after conversion automatically new sampling
AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS
AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC
AD1CON2bits.SMPI = 8; //interrupt after 8 sampling/conversion
AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns
AD1CON3bits.SAMC = 31; //Tsamp=31TAD
AD1CON1bits.ADON = 1; //turn on ADC
}
Example 3 (automatic conversion + interrupt)
Industrial Embedded Systems
21
 To read multiple channel the „scan” instruction can be helpful, which put
the ADC value in different ADC1BUFx register
 To turn on scan:
 AD1CON2bits.CSCNA = 1;
 The required channels should be enabled in the AD1PCFG register
 In the AD1CSSL register we have to give the channels
 pl. AD1CSSL = 0x0030; //AN4 és AN5
 If we want to read the value of n channel, it is worth to make an interrupt
after n sampling/conversion sequence
 pl. AD1CON2bits.SMPI = 0b0001; //2 channels
 Read the value of the channels
 pl. value01 = ADC1BUF0; //AN4
value02 = ADC1BUF1; //AN5
 PIC fill up the ADC1BUFx register from x=0
Read multiple channel
Industrial Embedded Systems
22
void initADC()
{
AD1PCFG=0xFFCF; //AN4 and AN5
AD1CON1bits.SSRC = 0b111; //automatic conversion
AD1CON1bits.FORM = 0b00; //integer
AD1CON1bits.ASAM = 1; //new sampling after conversion
AD1CON2bits.VCFG = 0b000; //ref+=VDD, ref-=VSS
AD1CON2bits.SMPI = 2; //interrupt after 2 AD conversion
AD1CON2bits.CSCNA = 1; //scan
AD1CON3bits.ADRC = 0; //TCY=2*TOSC
AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns
AD1CON3bits.SAMC = 31; //Tsamp=31TAD
AD1CON1bits.ADON = 1; //ADC turn on
AD1CSSLbits.CSSL4=1; //include AN4 in the scan cycle
AD1CSSLbits.CSSL5=1; //include AN5 in the scan cycle
}
Example 4 (read multiple channel)
Industrial Embedded Systems
23
 Read the actual value of the temperature sensor and the poteniometer and
write their value in Celsius and Volt to the LCD display
Industrial Embedded Systems
Example 4 (read multiple channel)
void LCDFloat(float L,int length, unsigned int acc)
{
signed int intpart=(int) L;
if (intpart==0)
LCDNumber(intpart,1);
else
LCDNumber(intpart,-1);
putLCD('.');
int fraction, multi=1;
while(acc--)
{
multi*=10;
if(intpart>=0) //if positive
{
fraction=(L-(int)L)*multi;
}
else fraction=((int)L - L)*multi; //if negative
LCDNumber(fraction,1);
}
}
24
 ADC of dsPICs
 Up to 32 ADC input channel (100/121/144 pin device)
 10-bit ADC with four S&H block (simultaneous sampling, 1.1 Msps)
 12-bit ADC with one S&H block (500 ksps)
 DMA (Direct Memory Access) can be used
 DMA is an efficient mechanism of copying data between
peripheral SFRs and buffers or variables stored in RAM with
minimal CPU intervention
 If multiple conversions are done without DMA 16 registers named
ADCxBUF0-15 can be used for buffering the results and the ADC
interrupt is used to signal when a group of conversions is finished
 By using DMA, these registers are not present and DMA memory
is used to buffer the results and the DMA interrupt occuring when
a group of conversions has finished
Using dsPIC with DMA
Industrial Embedded Systems
25
DMA block diagram in dsPIC
Industrial Embedded Systems
 CPU communicates with conventional SRAM across the data space X-bus (modified
Hardvard architecture). It also communicates to port 1 of the dual port SRAM. It talks to
the peripherals acrosss a separate peripheral data space bus
 DMA communicates with port 2 of the dual port SRAM and the DMA port of each of the
DMA-ready peripheral across a dedicated transfer bus
26
DMA data transfer
Industrial Embedded Systems
27
Operating modes
Industrial Embedded Systems
 Moves a single block of data from a fixed (peripheral) address
 Alert CPU that the block is ready for processing
One-shot mode
28
Operating modes
Industrial Embedded Systems
 Moves multiple blocks of data to/from a fixed (peripheral) address
 Alert CPU that each block is ready for processing
 Automatically re-initializes DMA channel for the next block transfer
 Suitable for applications when CPU can stay ahead of CPU
Auto-repeat mode
29
Operating modes
Industrial Embedded Systems
 Alerts CPUwhen each block is half way through its total count
 Allows CPU time to start processing the buffer before it is overwritten
 Requires that the DMA data transfers stay ahead of CPU processing of
the buffer
Half Buffer transfer interrupt
30
Operating modes
Industrial Embedded Systems
 Moves multiple blocks of data to/from a fixed (peripheral) address
 Alert CPU that each block is ready for processing
 Automatically re-initializes DMA channel for next block transfer to an
alternate buffer (ping-pongs between 2 buffers)
 Allows CPU to process one buffer while the other fills/empties
Ping-Pong
31
Adressing mode
Industrial Embedded Systems
 Conventioanl DMA transfer moves the data into a sequential buffer
 The peripheral provides the address of DPSRAM address, the task of the
DMA to coordinate the data transfer and generate the CPU interrupts
 Scatter-gather addressing mode
PIA: Peripheral indirect addressing mode
32
 The presence of DMA requires the following:
 The ADC ISR (Interupt Service Routine) is no longer used. Now the
ADC interrupt occurs adter each conversion, at point the DMA
module transfers the results to a DMA buffer. The DMA interrupt is
used to do something with the ADC samples
 The DMA channel must be configured to be linked to the ADC module
and a buffer in DMA memory allocated for the ADC results
 There are two choices for storing the ADC results in DMA memory
 Conversion order
 Scatter/gather mode
DMA
Industrial Embedded Systems
33
 Configure oscillator of the dsPIC
Industrial Embedded Systems
Example 5 (ADC with DMA)
// Configure Oscillator to operate the device at 40Mhz
// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// Fosc= 8M*40/(2*2)=80Mhz for 8M input clock
PLLFBD=38; // M=40
CLKDIVbits.PLLPOST=0; // N1=2
CLKDIVbits.PLLPRE=0; // N2=2
OSCTUN=0; // Tune FRC oscillator, if FRC is used
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
// Clock switch to incorporate PLL
__builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary
// Oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL(0x01); // Start clock switching
while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur
// Wait for PLL to lock
while(OSCCONbits.LOCK!=1) {};
34
 Initialize ADC
Industrial Embedded Systems
Example 5 (ADC with DMA)
void initADC(void)
{
AD1CON1bits.FORM = 0; // integer
AD1CON1bits.SSRC = 2; // Timer3 as sample clock source
AD1CON1bits.ASAM = 1; //auto sampling
AD1CON1bits.AD12B = 0; // 10-bit ADC operation
AD1CON1bits.SIMSAM = 1; //Simultaneous sampling
AD1CON2bits.CSCNA = 1; // Scan Inputs
AD1CON2bits.CHPS = 0;
AD1CON3bits.ADRC = 0; // ADC Clock is derived from Systems Clock
AD1CON3bits.ADCS = 63; // Tad=Tcy*(ADCS+1)= (1/40M)*64 = 1.6us (625Khz)
// ADC Conversion Time for 10-bit Tc=12*Tab = 19.2us
AD1CON1bits.ADDMABM = 0; //scatter/gather mode
AD1CON2bits.SMPI = 3; // 4 ADC Channel is scanned
AD1CON4bits.DMABL = 0b011; // Each buffer contains 8 words
AD1CSSH = 0x0000; //no scan for AN16-31
AD1CSSLbits.CSS4=1; // AN4 for scan
AD1CSSLbits.CSS5=1; // AN5 for scan
AD1CSSLbits.CSS10=1; // AN10 for scan
AD1CSSLbits.CSS13=1; // AN13 for scan
35
 Initialize ADC
Industrial Embedded Systems
Example 5 (ADC with DMA)
//AD1PCFGH/AD1PCFGL: Port Configuration Register
AD1PCFGL=0xFFFF;
AD1PCFGH=0xFFFF;
AD1PCFGLbits.PCFG4 = 0; // AN4 analog input
AD1PCFGLbits.PCFG5 = 0; // AN5 analog input
AD1PCFGLbits.PCFG10 = 0; // AN10 analog input
AD1PCFGLbits.PCFG13 = 0; // AN13 analog input
IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
IEC0bits.AD1IE = 0; // Do Not Enable A/D interrupt
AD1CON1bits.ADON = 1; // Turn on the A/D converter
}
36
 Initialize DMA
Industrial Embedded Systems
Example 5 (ADC with DMA)
void initDMA(void)
{
DMA0CONbits.AMODE = 0b10; // Peripheral indirect addressing mode
DMA0CONbits.MODE = 0b10; //Continuous Ping-Pong mode
DMA0PAD=(int)&ADC1BUF0; //contains the static address of the peripheral data register
DMA0CNT = 31; //8*4-1, the block transfer complete
DMA0REQ = 13; // Select ADC1 as DMA Request source
DMA0STA = __builtin_dmaoffset(BufferA);
DMA0STB = __builtin_dmaoffset(BufferB);
IFS0bits.DMA0IF = 0; //Clear the DMA interrupt flag bit
IEC0bits.DMA0IE = 1; //Set the DMA interrupt enable bit
DMA0CONbits.CHEN=1; // Enable DMA
}
37
 Locate buffer for DMA
Industrial Embedded Systems
Example 5 (ADC with DMA)
#define MAX_CHNUM 13 // Highest Analog input number (AN4,AN5,AN10,AN13)
#define SAMP_BUFF_SIZE 8 //size of input buffer/channel
#define NUM_CHS2SCAN 4 //Number of channels
// Number of locations for ADC buffer = 14 x 8 = 112 words
//The buffer should be aligned to 128 words or 256 bytes
int BufferA[MAX_CHNUM+1][SAMP_BUFF_SIZE] __attribute__((space(dma),aligned(256)));
int BufferB[MAX_CHNUM+1][SAMP_BUFF_SIZE] __attribute__((space(dma),aligned(256)));
38
 Interrupt function
Industrial Embedded Systems
Example 5 (ADC with DMA)
void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void)
{
if(DmaBuffer == 0)
{
// ProcessADCSamples(&BufferA[4][0]);
ProcessADCSamples(&BufferA[5][0]);
// ProcessADCSamples(&BufferA[10][0]);
// ProcessADCSamples(&BufferA[13][0]);
}
else
{
// ProcessADCSamples(&BufferB[4][0]);
ProcessADCSamples(&BufferB[5][0]);
// ProcessADCSamples(&BufferB[10][0]);
// ProcessADCSamples(&BufferB[13][0]);
}
DmaBuffer ^= 1;
IFS0bits.DMA0IF = 0; // Clear the DMA0 Interrupt Flag
}
39
 Singal processing function
Industrial Embedded Systems
Example 5 (ADC with DMA)
void ProcessADCSamples(int * AdcBuffer)
{
int a=0;
int i=0;
while(i<8)
{
a+=*AdcBuffer++; //simple averaging
i++;
}
LATA=a>>5; //divison by 4 to show the ten bit value on 8 leds and divison by 8 due to
the averaging
}

More Related Content

What's hot

Conception avec pic
Conception avec pic Conception avec pic
Conception avec pic
nawzat
 
Tp voip
Tp voipTp voip
Tp voip
amalouwarda
 
All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programsGouthaman V
 
Introduction to the 16-bit PIC24F Microcontroller Family
Introduction to the 16-bit PIC24F Microcontroller FamilyIntroduction to the 16-bit PIC24F Microcontroller Family
Introduction to the 16-bit PIC24F Microcontroller Family
Premier Farnell
 
Language for Embedded System
Language for Embedded System Language for Embedded System
Language for Embedded System
vkrhanjeeth .
 
IoT.pptx
IoT.pptxIoT.pptx
IoT.pptx
PROFPROF11
 
Formation Bus de Terrain _Partie 3_1 _Modbus tcp
Formation Bus de Terrain _Partie 3_1 _Modbus tcpFormation Bus de Terrain _Partie 3_1 _Modbus tcp
Formation Bus de Terrain _Partie 3_1 _Modbus tcp
Wojciech GOMOLKA
 
Programmation de systèmes embarqués : Introduction aux systèmes embarqués
Programmation de systèmes embarqués : Introduction aux systèmes embarquésProgrammation de systèmes embarqués : Introduction aux systèmes embarqués
Programmation de systèmes embarqués : Introduction aux systèmes embarqués
ECAM Brussels Engineering School
 
Automates Programmables Industriels (API).pdf
Automates Programmables Industriels (API).pdfAutomates Programmables Industriels (API).pdf
Automates Programmables Industriels (API).pdf
MENNANIZinedine
 
Exercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieur
Exercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieurExercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieur
Exercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieurzahir99
 
introduction automatisme industriel
introduction automatisme industrielintroduction automatisme industriel
introduction automatisme industriel
Adnane Ahmidani
 
ARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATURE
ARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATUREARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATURE
ARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATURE
Hajer Dahech
 
traitement de signal
traitement de signaltraitement de signal
traitement de signal
sarah Benmerzouk
 
Cours Bus de communication et réseaux industriels. Chapitre 1 : introduction.
Cours Bus de communication et réseaux industriels. Chapitre 1 :  introduction.Cours Bus de communication et réseaux industriels. Chapitre 1 :  introduction.
Cours Bus de communication et réseaux industriels. Chapitre 1 : introduction.
Tarik Zakaria Benmerar
 
Cours capteur final
Cours capteur finalCours capteur final
Cours capteur final
Abdellah Zahidi
 
cnacan (2).ppt
cnacan (2).pptcnacan (2).ppt
cnacan (2).ppt
wafawafa52
 
Projet de communication numérique Réalisation d'une chaîne de transmission nu...
Projet de communication numérique Réalisation d'une chaîne de transmission nu...Projet de communication numérique Réalisation d'une chaîne de transmission nu...
Projet de communication numérique Réalisation d'une chaîne de transmission nu...
Yassine Nasser
 
interferences entre-symboles
interferences entre-symbolesinterferences entre-symboles
interferences entre-symboles
BAKKOURY Jamila
 
Supervision et gestion d’énergie
Supervision et gestion d’énergieSupervision et gestion d’énergie
Supervision et gestion d’énergie
Nicolas ODIN
 
cour PIC16F877.pptx
cour PIC16F877.pptxcour PIC16F877.pptx
cour PIC16F877.pptx
KamalZeghdar
 

What's hot (20)

Conception avec pic
Conception avec pic Conception avec pic
Conception avec pic
 
Tp voip
Tp voipTp voip
Tp voip
 
All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programs
 
Introduction to the 16-bit PIC24F Microcontroller Family
Introduction to the 16-bit PIC24F Microcontroller FamilyIntroduction to the 16-bit PIC24F Microcontroller Family
Introduction to the 16-bit PIC24F Microcontroller Family
 
Language for Embedded System
Language for Embedded System Language for Embedded System
Language for Embedded System
 
IoT.pptx
IoT.pptxIoT.pptx
IoT.pptx
 
Formation Bus de Terrain _Partie 3_1 _Modbus tcp
Formation Bus de Terrain _Partie 3_1 _Modbus tcpFormation Bus de Terrain _Partie 3_1 _Modbus tcp
Formation Bus de Terrain _Partie 3_1 _Modbus tcp
 
Programmation de systèmes embarqués : Introduction aux systèmes embarqués
Programmation de systèmes embarqués : Introduction aux systèmes embarquésProgrammation de systèmes embarqués : Introduction aux systèmes embarqués
Programmation de systèmes embarqués : Introduction aux systèmes embarqués
 
Automates Programmables Industriels (API).pdf
Automates Programmables Industriels (API).pdfAutomates Programmables Industriels (API).pdf
Automates Programmables Industriels (API).pdf
 
Exercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieur
Exercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieurExercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieur
Exercices corrigés-sur-convertisseurs-statiques-2-bac-science-d ingénieur
 
introduction automatisme industriel
introduction automatisme industrielintroduction automatisme industriel
introduction automatisme industriel
 
ARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATURE
ARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATUREARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATURE
ARDUINO + LABVIEW : CONTRÔLE DE LA TEMPÉRATURE
 
traitement de signal
traitement de signaltraitement de signal
traitement de signal
 
Cours Bus de communication et réseaux industriels. Chapitre 1 : introduction.
Cours Bus de communication et réseaux industriels. Chapitre 1 :  introduction.Cours Bus de communication et réseaux industriels. Chapitre 1 :  introduction.
Cours Bus de communication et réseaux industriels. Chapitre 1 : introduction.
 
Cours capteur final
Cours capteur finalCours capteur final
Cours capteur final
 
cnacan (2).ppt
cnacan (2).pptcnacan (2).ppt
cnacan (2).ppt
 
Projet de communication numérique Réalisation d'une chaîne de transmission nu...
Projet de communication numérique Réalisation d'une chaîne de transmission nu...Projet de communication numérique Réalisation d'une chaîne de transmission nu...
Projet de communication numérique Réalisation d'une chaîne de transmission nu...
 
interferences entre-symboles
interferences entre-symbolesinterferences entre-symboles
interferences entre-symboles
 
Supervision et gestion d’énergie
Supervision et gestion d’énergieSupervision et gestion d’énergie
Supervision et gestion d’énergie
 
cour PIC16F877.pptx
cour PIC16F877.pptxcour PIC16F877.pptx
cour PIC16F877.pptx
 

Similar to 04 adc (pic24, ds pic with dma)

Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
Omkar Rane
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converterCorrado Santoro
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
Omar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
Omar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
Omar Sanchez
 
chapter 4
chapter 4chapter 4
chapter 4
GAGANAP12
 
Analog to Digital converter in ARM
Analog to Digital converter in ARMAnalog to Digital converter in ARM
Analog to Digital converter in ARM
Aarav Soni
 
STM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosSTM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicos
ps6005tec
 
Atmel microcontrollers-a tmega328-p_datasheet
Atmel microcontrollers-a tmega328-p_datasheetAtmel microcontrollers-a tmega328-p_datasheet
Atmel microcontrollers-a tmega328-p_datasheet
AlexTronciu
 
Lecture7
Lecture7Lecture7
Lecture7
Mahmut Yildiz
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
VishalPatil57559
 
Lpc 17xx adc
Lpc 17xx adcLpc 17xx adc
Lpc 17xx adc
PriyangaKR1
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
Ariel Tonatiuh Espindola
 
Lecture 12 (adc) rv01
Lecture 12  (adc) rv01Lecture 12  (adc) rv01
Lecture 12 (adc) rv01
cairo university
 
F5 m instruction manual
F5 m instruction manualF5 m instruction manual
F5 m instruction manual
Toàn Huỳnh
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
Mohamed Ali
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
G Lemuel George
 

Similar to 04 adc (pic24, ds pic with dma) (20)

Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converter
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Módulo adc 18f4550
Módulo adc   18f4550Módulo adc   18f4550
Módulo adc 18f4550
 
chapter 4
chapter 4chapter 4
chapter 4
 
Analog to Digital converter in ARM
Analog to Digital converter in ARMAnalog to Digital converter in ARM
Analog to Digital converter in ARM
 
STM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosSTM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicos
 
Atmel microcontrollers-a tmega328-p_datasheet
Atmel microcontrollers-a tmega328-p_datasheetAtmel microcontrollers-a tmega328-p_datasheet
Atmel microcontrollers-a tmega328-p_datasheet
 
Lecture7
Lecture7Lecture7
Lecture7
 
EEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdfEEE UNIT-2 PPT.pdf
EEE UNIT-2 PPT.pdf
 
Lpc 17xx adc
Lpc 17xx adcLpc 17xx adc
Lpc 17xx adc
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
 
Lecture 12 (adc) rv01
Lecture 12  (adc) rv01Lecture 12  (adc) rv01
Lecture 12 (adc) rv01
 
F5 m instruction manual
F5 m instruction manualF5 m instruction manual
F5 m instruction manual
 
Ds1307 datasheet
Ds1307 datasheetDs1307 datasheet
Ds1307 datasheet
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
 
Chapter5 dek3133
Chapter5 dek3133Chapter5 dek3133
Chapter5 dek3133
 
m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
 

04 adc (pic24, ds pic with dma)

  • 1. 1 Analog-to-Digital Conversion (PIC24, dsPIC with DMA) Industrial Embedded Systems
  • 2. 2  ADC architectures  Steps of A/D conversion  Configure A/D module of PIC24  A/D Interrupt  Using multiple channels  dsPIC utilizing DMA Industrial Embedded Systems Outline
  • 3. 3  Successice Approximation Register  AD Converter with feedback  Generally N=8-16 bit (PIC24FJ128GA010 has N=10bit ADC)  Conversion time: N cycle Initially set VDAC to ½ Vref, then see if Vin higher or lower than VDAC. If > ½ Vref, then next guess is between Vref and ½Vref, else next guess is between Vref and GND. Do this for each bit of the ADC ADC architectures SAR
  • 4. 4 Counter ramp ADC  Simple  Slow  Conversion time: 2N cycle  Fast  Conversion time: 1 cycle  Requires the most transistors of any architecture, N-bit converter requires 2N-1 comparators.  Commercially available flash converters up to 12 bits. Flash ADC Industrial Embedded Systems ADC architectures
  • 5. 5Industrial Embedded Systems Input hardware for ADC  Some low-pass filtering and a Zener diode to protect the input PIN from overvoltage  If input voltage is higher, a voltage divider can be used to fit the voltage level.  To improve the performance a voltage follower is also applied (use a rail-to-rail Op-Amp)
  • 6. 6  Configuring AD modul  Enable analog input channel(s)  Set the reference voltage  Clock cycle, clock source (TAD)  Sampling and conversion settings  Form of the result  Enable modul  Configure A/D interrupt (if required)  Delete flag belonging to the ADC  Enable interrupt  Select the given analog channel  Sampling time (1-31 TAD)  Time to charge up the storage capacitor  Conversion time (12 TAD)  The end of the conversion is indicated by a flag  Read the value from the buffer register  If required delete interrupt flag Industrial Embedded Systems Steps of AD conversion
  • 7. 7  AD module has 6 Special Function Register (SFR)  AD1CON1, AD1CON2, AD1CON3, AD1CHS, AD1PCFG, AD1CSSL  PIC24 has 16 analog input pin (AN0-AN15)  Enable analog input channel(s)  Using AD1PCFG register  0s will mark the analog inputs, and 1s will configure the respective pins as digital inputs  AD1PCFG = 0xFFDF; // AN5 analog input  Set the reference voltage  AD1CON2 register<15:13>  Internal voltage level (AVDD, AVSS)  External reference voltage  Combination of the two  AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS Configure AD modul Industrial Embedded Systems
  • 8. 8  Clock cycle (TAD)  In the case of PIC24FJ128GA010 minimum clock cylce TAD is 75ns  Clock derived from sytem clock (TCY)  AD1CON3bits.ARDC = 0; //system clock, TCY=2*TOSC  TAD=TCY (ACDS + 1)  AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns Configure AD modul Industrial Embedded Systems
  • 9. 9  Sampling time  Its value depends on the input resistane of the signal and the storage capacitor  The frequency of the input signals also has to be taken into consideration  The accuracy can be improved by increasing the sampling time Configure AD modul Industrial Embedded Systems
  • 10. 10  Sampling and conversion settings  The sampling time and the beginning of the conversion can be determined  By software (delete AD1CON1bits.SAMP bit)  By hardware (Active transition on INT0 pin ends sampling and starts conversion)  Automatic conversion  AD1CON3bits.SSRC = 0b000; //software conversion  AD1CON3bits.SSRC = 0b111; //automatic conversion  In the case of software conversion the enough sampling time should be guaranteed in the code  In the case of automatic conversion the sampling has to set:  AD1CON1bits.SAMC = 31; //TSAMP=31TAD=7.75us  After conversion automatically  Sampling begins immediately after last conversion completes  Sampling begins when SAMP bit is set  AD1CON1.ASAM = 0; //no automatic sampling Configure AD modul Industrial Embedded Systems
  • 11. 11  Data output format  integer/fractional and signed/nonsigned  AD1CON1bits.FORM = 0b00; //integer  PIC put the result of the AD conversion in to a 16-bit register ADC1BUF x  Enable the module  AD1CON1bits.ADON = 1; //turn on ADC Configure AD modul Industrial Embedded Systems
  • 12. 12 void initADC() { AD1PCFG = 0xFFDF; // enable AN5 chnnel AD1CON1bits.SSRC = 0b000; //software conversion AD1CON1bits.FORM = 0b00; //integer AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns AD1CON1bits.ADON = 1; //turn on ADC } Configure AD modul Industrial Embedded Systems
  • 13. 13  Potenciometer  Temperature sensor  Select the input channel  AD1CHS.CH0SA = 5; //selecting AN5 (potenciometer) Analog inputs on demo board Industrial Embedded Systems
  • 14. 14 #include <p24fj128ga010.h> _CONFIG1( JTAGEN_OFF & FWDTEN_OFF ) _CONFIG2( POSCMOD_HS & FNOSC_PRI ) void initADC(); // declaration of ADC initialization void initT1(); //declaration of Timer1 initialization int main ( void ) { initADC(); // call ADC init initT1(); // call Timer1 init TRISA = 0xFF00; // PORTA lower 8 bit output LATA = 0x0000; // delete PORTA while(1) //infinite loop { AD1CHSbits.CH0SA=5; //select AN5 (potentiometer) AD1CON1bits.SAMP = 1; //start sampling TMR1=0; //wait for sampling with software while(TMR1<50); AD1CON1bits.SAMP = 0; //end of sampling AD1CON1bits.DONE = 1; //start conversion while (!AD1CON1bits.DONE); //end of conversiom LATA=(ADC1BUF0>>2); //put the 10bit result to the 8 LED } //end of infinite loop } //end of main loop Example 1 Industrial Embedded Systems
  • 15. 15 void initADC() // define of ADC initialization { AD1PCFG = 0xFFDF; // enable AN5 chnnel AD1CON1bits.SSRC = 0b000; //software conversion AD1CON1bits.FORM = 0b00; //integer AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns AD1CON1bits.ADON = 1; //turn on ADC } void initT1() // define of Timer1 initialization { T1CONbits.TON = 1; //Timer1 turn on T1CONbits.TCS = 0; //system clock =fosc/2 T1CONbits.TCKPS = 0b01; // prescaler 8 TMR1=0; //delete counter } Example 1 Industrial Embedded Systems
  • 16. 16 #include <p24fj128ga010.h> _CONFIG1( JTAGEN_OFF & FWDTEN_OFF ) _CONFIG2( POSCMOD_HS & FNOSC_PRI ) void initADC(); // AD inicializalo fv deklarálása int main ( void ) { initADC(); // call ADC init TRISA = 0xFF00; // PORTA lower 8 bit output LATA = 0x0000; // delete PORTA while(1) //infinite loop { AD1CHSbits.CH0SA=5; //select AN5 (potentiometer) AD1CON1bits.SAMP = 1; //Start sampling (we do not need Timer1) while (!AD1CON1bits.DONE); //end of conversion LATA=(ADC1BUF0>>2); //put the 10bit result to the 8 LED } //end of infinit loop } //end of main loop Example 2 (automatic conversion) Industrial Embedded Systems
  • 17. 17 void initADC() // define ADC initialization { AD1PCFG = 0xFFDF; // enable AN5 chnnel AD1CON1bits.SSRC = 0b111; //automatic conversion AD1CON1bits.FORM = 0b00; //integer AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns AD1CON3bits.SAMC = 31; //Tsamp=31TAD AD1CON1bits.ADON = 1; //turn on ADC } Example 2 (automatic conversion) Industrial Embedded Systems
  • 18. 18  Priority level  IPC3bits.AD1IP  Flag  IFS0bits.AD1IF  Enable bit  IEC0bits.AD1IE  We can select, after how many samling/conversion  We can give after how many completion of sample/convert sequence an interrupt occur  AD1CON2bits.SMPI  Interrupt function void _ISR _ADC1Interrupt (void) { … //instruction IFS0bits.AD1IF = 0; // delete ADC interrupt flag!!! } ADC interrupt Industrial Embedded Systems
  • 19. 19 #include <p24fj128ga010.h> _CONFIG1( JTAGEN_OFF & FWDTEN_OFF ) _CONFIG2( POSCMOD_HS & FNOSC_PRI ) void initADC(); //declaration of ADC initialization void _ISR _ADC1Interrupt (void); //declaration of interrupt function int main ( void ) { initADC(); // call ADC initialization SRbits.IPL = 0; // priority level of processor IPC3bits.AD1IP = 3; //priority level of ADC IEC0bits.AD1IE = 1; //enable ADC interrupt TRISA = 0xFF00; // PORTA lower 8 bit output LATA = 0x0000; // delete PORTA AD1CHSbits.CH0SA=5; //select AN5 (potentiometer) AD1CON1bits.SAMP = 1; //Start sampling (we do not need Timer1) while(1); //infinite loop } Example 3 (automatic conversion + interrupt) Industrial Embedded Systems
  • 20. 20 void _ISR _ADC1Interrupt (void) { LATA=(ADC1BUF0>>2); //instruction: put the result to the LED IFS0bits.AD1IF = 0; //delete ADC interrupt flag } void initADC() // define of ADC initialization { AD1PCFG = 0xFFDF; // enable AN5 chnnel AD1CON1bits.SSRC = 0b111; //automatic conversion AD1CON1bits.FORM = 0b00; //integer AD1CON1bits.ASAM = 1; //after conversion automatically new sampling AD1CON2bits.VCFG = 0b000; // ref+=VDD, ref-=VSS AD1CON3bits.ADRC = 0; //system clock, TCY=2*TOSC AD1CON2bits.SMPI = 8; //interrupt after 8 sampling/conversion AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns AD1CON3bits.SAMC = 31; //Tsamp=31TAD AD1CON1bits.ADON = 1; //turn on ADC } Example 3 (automatic conversion + interrupt) Industrial Embedded Systems
  • 21. 21  To read multiple channel the „scan” instruction can be helpful, which put the ADC value in different ADC1BUFx register  To turn on scan:  AD1CON2bits.CSCNA = 1;  The required channels should be enabled in the AD1PCFG register  In the AD1CSSL register we have to give the channels  pl. AD1CSSL = 0x0030; //AN4 és AN5  If we want to read the value of n channel, it is worth to make an interrupt after n sampling/conversion sequence  pl. AD1CON2bits.SMPI = 0b0001; //2 channels  Read the value of the channels  pl. value01 = ADC1BUF0; //AN4 value02 = ADC1BUF1; //AN5  PIC fill up the ADC1BUFx register from x=0 Read multiple channel Industrial Embedded Systems
  • 22. 22 void initADC() { AD1PCFG=0xFFCF; //AN4 and AN5 AD1CON1bits.SSRC = 0b111; //automatic conversion AD1CON1bits.FORM = 0b00; //integer AD1CON1bits.ASAM = 1; //new sampling after conversion AD1CON2bits.VCFG = 0b000; //ref+=VDD, ref-=VSS AD1CON2bits.SMPI = 2; //interrupt after 2 AD conversion AD1CON2bits.CSCNA = 1; //scan AD1CON3bits.ADRC = 0; //TCY=2*TOSC AD1CON3bits.ADCS = 0; //TAD=TCY=250ns>75ns AD1CON3bits.SAMC = 31; //Tsamp=31TAD AD1CON1bits.ADON = 1; //ADC turn on AD1CSSLbits.CSSL4=1; //include AN4 in the scan cycle AD1CSSLbits.CSSL5=1; //include AN5 in the scan cycle } Example 4 (read multiple channel) Industrial Embedded Systems
  • 23. 23  Read the actual value of the temperature sensor and the poteniometer and write their value in Celsius and Volt to the LCD display Industrial Embedded Systems Example 4 (read multiple channel) void LCDFloat(float L,int length, unsigned int acc) { signed int intpart=(int) L; if (intpart==0) LCDNumber(intpart,1); else LCDNumber(intpart,-1); putLCD('.'); int fraction, multi=1; while(acc--) { multi*=10; if(intpart>=0) //if positive { fraction=(L-(int)L)*multi; } else fraction=((int)L - L)*multi; //if negative LCDNumber(fraction,1); } }
  • 24. 24  ADC of dsPICs  Up to 32 ADC input channel (100/121/144 pin device)  10-bit ADC with four S&H block (simultaneous sampling, 1.1 Msps)  12-bit ADC with one S&H block (500 ksps)  DMA (Direct Memory Access) can be used  DMA is an efficient mechanism of copying data between peripheral SFRs and buffers or variables stored in RAM with minimal CPU intervention  If multiple conversions are done without DMA 16 registers named ADCxBUF0-15 can be used for buffering the results and the ADC interrupt is used to signal when a group of conversions is finished  By using DMA, these registers are not present and DMA memory is used to buffer the results and the DMA interrupt occuring when a group of conversions has finished Using dsPIC with DMA Industrial Embedded Systems
  • 25. 25 DMA block diagram in dsPIC Industrial Embedded Systems  CPU communicates with conventional SRAM across the data space X-bus (modified Hardvard architecture). It also communicates to port 1 of the dual port SRAM. It talks to the peripherals acrosss a separate peripheral data space bus  DMA communicates with port 2 of the dual port SRAM and the DMA port of each of the DMA-ready peripheral across a dedicated transfer bus
  • 27. 27 Operating modes Industrial Embedded Systems  Moves a single block of data from a fixed (peripheral) address  Alert CPU that the block is ready for processing One-shot mode
  • 28. 28 Operating modes Industrial Embedded Systems  Moves multiple blocks of data to/from a fixed (peripheral) address  Alert CPU that each block is ready for processing  Automatically re-initializes DMA channel for the next block transfer  Suitable for applications when CPU can stay ahead of CPU Auto-repeat mode
  • 29. 29 Operating modes Industrial Embedded Systems  Alerts CPUwhen each block is half way through its total count  Allows CPU time to start processing the buffer before it is overwritten  Requires that the DMA data transfers stay ahead of CPU processing of the buffer Half Buffer transfer interrupt
  • 30. 30 Operating modes Industrial Embedded Systems  Moves multiple blocks of data to/from a fixed (peripheral) address  Alert CPU that each block is ready for processing  Automatically re-initializes DMA channel for next block transfer to an alternate buffer (ping-pongs between 2 buffers)  Allows CPU to process one buffer while the other fills/empties Ping-Pong
  • 31. 31 Adressing mode Industrial Embedded Systems  Conventioanl DMA transfer moves the data into a sequential buffer  The peripheral provides the address of DPSRAM address, the task of the DMA to coordinate the data transfer and generate the CPU interrupts  Scatter-gather addressing mode PIA: Peripheral indirect addressing mode
  • 32. 32  The presence of DMA requires the following:  The ADC ISR (Interupt Service Routine) is no longer used. Now the ADC interrupt occurs adter each conversion, at point the DMA module transfers the results to a DMA buffer. The DMA interrupt is used to do something with the ADC samples  The DMA channel must be configured to be linked to the ADC module and a buffer in DMA memory allocated for the ADC results  There are two choices for storing the ADC results in DMA memory  Conversion order  Scatter/gather mode DMA Industrial Embedded Systems
  • 33. 33  Configure oscillator of the dsPIC Industrial Embedded Systems Example 5 (ADC with DMA) // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 8M*40/(2*2)=80Mhz for 8M input clock PLLFBD=38; // M=40 CLKDIVbits.PLLPOST=0; // N1=2 CLKDIVbits.PLLPRE=0; // N2=2 OSCTUN=0; // Tune FRC oscillator, if FRC is used // Disable Watch Dog Timer RCONbits.SWDTEN=0; // Clock switch to incorporate PLL __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary // Oscillator with PLL (NOSC=0b011) __builtin_write_OSCCONL(0x01); // Start clock switching while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur // Wait for PLL to lock while(OSCCONbits.LOCK!=1) {};
  • 34. 34  Initialize ADC Industrial Embedded Systems Example 5 (ADC with DMA) void initADC(void) { AD1CON1bits.FORM = 0; // integer AD1CON1bits.SSRC = 2; // Timer3 as sample clock source AD1CON1bits.ASAM = 1; //auto sampling AD1CON1bits.AD12B = 0; // 10-bit ADC operation AD1CON1bits.SIMSAM = 1; //Simultaneous sampling AD1CON2bits.CSCNA = 1; // Scan Inputs AD1CON2bits.CHPS = 0; AD1CON3bits.ADRC = 0; // ADC Clock is derived from Systems Clock AD1CON3bits.ADCS = 63; // Tad=Tcy*(ADCS+1)= (1/40M)*64 = 1.6us (625Khz) // ADC Conversion Time for 10-bit Tc=12*Tab = 19.2us AD1CON1bits.ADDMABM = 0; //scatter/gather mode AD1CON2bits.SMPI = 3; // 4 ADC Channel is scanned AD1CON4bits.DMABL = 0b011; // Each buffer contains 8 words AD1CSSH = 0x0000; //no scan for AN16-31 AD1CSSLbits.CSS4=1; // AN4 for scan AD1CSSLbits.CSS5=1; // AN5 for scan AD1CSSLbits.CSS10=1; // AN10 for scan AD1CSSLbits.CSS13=1; // AN13 for scan
  • 35. 35  Initialize ADC Industrial Embedded Systems Example 5 (ADC with DMA) //AD1PCFGH/AD1PCFGL: Port Configuration Register AD1PCFGL=0xFFFF; AD1PCFGH=0xFFFF; AD1PCFGLbits.PCFG4 = 0; // AN4 analog input AD1PCFGLbits.PCFG5 = 0; // AN5 analog input AD1PCFGLbits.PCFG10 = 0; // AN10 analog input AD1PCFGLbits.PCFG13 = 0; // AN13 analog input IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit IEC0bits.AD1IE = 0; // Do Not Enable A/D interrupt AD1CON1bits.ADON = 1; // Turn on the A/D converter }
  • 36. 36  Initialize DMA Industrial Embedded Systems Example 5 (ADC with DMA) void initDMA(void) { DMA0CONbits.AMODE = 0b10; // Peripheral indirect addressing mode DMA0CONbits.MODE = 0b10; //Continuous Ping-Pong mode DMA0PAD=(int)&ADC1BUF0; //contains the static address of the peripheral data register DMA0CNT = 31; //8*4-1, the block transfer complete DMA0REQ = 13; // Select ADC1 as DMA Request source DMA0STA = __builtin_dmaoffset(BufferA); DMA0STB = __builtin_dmaoffset(BufferB); IFS0bits.DMA0IF = 0; //Clear the DMA interrupt flag bit IEC0bits.DMA0IE = 1; //Set the DMA interrupt enable bit DMA0CONbits.CHEN=1; // Enable DMA }
  • 37. 37  Locate buffer for DMA Industrial Embedded Systems Example 5 (ADC with DMA) #define MAX_CHNUM 13 // Highest Analog input number (AN4,AN5,AN10,AN13) #define SAMP_BUFF_SIZE 8 //size of input buffer/channel #define NUM_CHS2SCAN 4 //Number of channels // Number of locations for ADC buffer = 14 x 8 = 112 words //The buffer should be aligned to 128 words or 256 bytes int BufferA[MAX_CHNUM+1][SAMP_BUFF_SIZE] __attribute__((space(dma),aligned(256))); int BufferB[MAX_CHNUM+1][SAMP_BUFF_SIZE] __attribute__((space(dma),aligned(256)));
  • 38. 38  Interrupt function Industrial Embedded Systems Example 5 (ADC with DMA) void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) { if(DmaBuffer == 0) { // ProcessADCSamples(&BufferA[4][0]); ProcessADCSamples(&BufferA[5][0]); // ProcessADCSamples(&BufferA[10][0]); // ProcessADCSamples(&BufferA[13][0]); } else { // ProcessADCSamples(&BufferB[4][0]); ProcessADCSamples(&BufferB[5][0]); // ProcessADCSamples(&BufferB[10][0]); // ProcessADCSamples(&BufferB[13][0]); } DmaBuffer ^= 1; IFS0bits.DMA0IF = 0; // Clear the DMA0 Interrupt Flag }
  • 39. 39  Singal processing function Industrial Embedded Systems Example 5 (ADC with DMA) void ProcessADCSamples(int * AdcBuffer) { int a=0; int i=0; while(i<8) { a+=*AdcBuffer++; //simple averaging i++; } LATA=a>>5; //divison by 4 to show the ten bit value on 8 leds and divison by 8 due to the averaging }