• AVR Freaks

Hot!dsPIC33EP Analog Channel Read Suggestion:

Author
karan123
Super Member
  • Total Posts : 851
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
2017/06/19 21:28:27 (permalink)
0

dsPIC33EP Analog Channel Read Suggestion:

Dear All,
 
I am working on dsPIC33EP512MU810 with Internal ADC with Internal (+)reference in 12-bit mode.
 
I am taking reference of dsPIC33E/PIC24E Family Reference Manual Example 16-6:
 
In Description of ADxCON2 bits, CHPS<1:0> can sample 4 Channel simultaneously.
Out of four my three channels RB4, RB5, RB7 are working fine .
 
1) I am unable to understand how ADC channels are responsible for four reading?
Means which channel is responsible for which reading ?
 
2) I have use 12 ADC Channels from AN0-AN11.. How to Configure ADC for that..?
 
3) How to read 12 ADC channels with Interrupts ?
 

void initAdc1(void)
{
/* Set port configuration */
ANSELA = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
// Ensure AN4, AN5, AN6 and AN8 are analog
ANSELB = 0x0F0;


/* Initialize ADC module */
AD1CON1 = 0x04E4; // Enable 12-bit mode, auto-sample and auto-conversion

AD1CON2 = 0x040C; // Sample 4 channels alternately using channel scanning

AD1CON3 = 0x0F0F; // Sample for 15*TADbefore converting

AD1CON4 = 0x0000;
AD1CSSH = 0x0000;

AD1CSSL = 0x0F0 ;
 
/* Assign MUXA inputs */
AD1CHS0bits.CH0SA = 0; // CH0SA bits ignored for CH0 +ve input selection
AD1CHS0bits.CH0NA = 0; // Select VREF- for CH0 -ve input

/* Enable ADC module and provide ADC stabilization delay */
AD1CON1bits.ADON = 1;
Delay_us(20);
}
void Delay_us(unsigned int delay)
{
for (i = 0; i < delay; i++)
{
__asm__ volatile ("repeat #39");
__asm__ volatile ("nop");
}
}
// In main loop
while(1)
{
while (!_AD1IF); // Wait for all 8 conversions to complete
_AD1IF = 0; // Clear conversion done status bit
// READ RB4
ADCValues[0] = ADC1BUF0;
LCDGotoxy(1,0);
LCDDisplaySplitNumbers(ADCValues[0]);          // Display   RB4-AN4

// READ RB5
ADCValues[1] = ADC1BUF1;
LCDGotoxy(1,6);
LCDDisplaySplitNumbers(ADCValues[1]);          //Display  RB5-AN5
// READ RB6
ADCValues[2] = ADC1BUF2; 
LCDGotoxy(3,0);
LCDDisplaySplitNumbers(ADCValues[2]);            //Display  RB6-AN6      
 
// READ RB7
ADCValues[3] = ADC1BUF3; 
LCDGotoxy(3,6);
LCDDisplaySplitNumbers(ADCValues[3]);            // Display  RB7-AN7
}

 
Thanksyou..
 
KR
#1

9 Replies Related Threads

    Weydert
    Super Member
    • Total Posts : 483
    • Reward points : 0
    • Joined: 2008/07/02 04:22:40
    • Location: Aachen/Germany
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2017/06/19 23:25:53 (permalink)
    3 (1)
    hi,
    for 12bit conversion only one channel (CH0) can be uses.
    Several analog inputs are sampled and converted one after the other. (= channel scanning)
    Which inputs are used is selected by the bits in ADxCSSL and ADxCSSH. The number of inputs selected in CSSL/CSSH must 'match'  the value in ADxCON2bits.SMPI. For Example: AD1CSSL=0x00F0 and AD1CSSH=0 ->
    4 inputs to scan (AN4, AN5,AN6,AN7) ->AD1CON2bits.SMPI=4-1=3 ; (AD1CON2 = 0x040C: channel scanning enabled, SMPI=3))
    For 12 inputs - AN0, AN1 ... AN11 select AD1CSSL=0x0FFF, AD1CSSH=0x0000 and AD1CON2bits.SMPI=11.
    #2
    karan123
    Super Member
    • Total Posts : 851
    • Reward points : 0
    • Joined: 2009/02/02 11:11:21
    • Location: 0
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2017/06/20 01:22:18 (permalink)
    0
    Thanks for update...
    I will test then update to you..
     
    Thanks again..
     
     
    #3
    karan123
    Super Member
    • Total Posts : 851
    • Reward points : 0
    • Joined: 2009/02/02 11:11:21
    • Location: 0
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2017/06/23 22:22:23 (permalink)
    0
    Hi,
     
    I have tested It all worked on Real Hardware..
    Now, How to read 12 ADC channels Simultaneously  with Interrupts  or with DMA would be Better.?
     
    --
    Thanks
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11396
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2017/06/23 22:57:44 (permalink)
    4 (1)
    An ADC can only convert one channel at a time.  Your PIC has two ADCs, so you could convert two channels at a time.  Your PIC also has the ability to sample up to four channels at the same time per ADC, but they will be converted sequentially.
    #5
    Weydert
    Super Member
    • Total Posts : 483
    • Reward points : 0
    • Joined: 2008/07/02 04:22:40
    • Location: Aachen/Germany
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2017/06/24 01:58:04 (permalink)
    0
    hi KR,
    How to read 12 ADC channels Simultaneously

    can you explain this a little bit more:
    - do you require that the analog inputs are sampled exact at the same time?
    - or may they be sampled one after the other and was is the max.  allowed difference in time between sampling input N and input N+1 (µs ?, ms ?)
     
    If you need the analog inputs sampled at the same time, you can do this only for (4 inputs, 10bit mode)/ADC module. i.e 8 inputs for your device. (if you mange to synchronize ADC1 and ADC2)
     
    Where are the conversion results to be used for ? Only displaying them on a LCD?
    LCDGotoxy(1,0);
    LCDDisplaySplitNumbers(ADCValues[0]); // Display RB4-AN4

    I think these two functions will take longer than converting all 12 channels and when you after this read ADCValues[1] this value is not from the first conversion sequence (12 inputs)  but from a later one.
    You need an idea for your timing: How to consume all the results from the ADC without skipping samples  or -if skipping samles- skip it in a controlled way.
     
    With DMA in ping-pong mode you get an interrupt after 12 inputs are converted. During this interrupt you could copy the results to another buffer -only if a flag tells you that the buffer is not use in the while() loop for display or other purpose. If the buffer is still in use, the results are not copied.
     
    You also could trigger the conversion-process by a timer to slow down conversion/second in a way that you can consume all conversion results.
    #6
    karan123
    Super Member
    • Total Posts : 851
    • Reward points : 0
    • Joined: 2009/02/02 11:11:21
    • Location: 0
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2017/08/16 02:33:14 (permalink)
    0
    @jtemples and @Waydert

    Thanks both of you and sorry for delay in reply.
    Actually Now I have able to all 12 channels.

    Can I process 10 Channels with one module (ADC 1) and other 2 Channels on another ADC module (ADC2)?
    Basically I want to process last two ADC Channels in 1us.

    With Timer Interrupts will be good option?
    Or I should go to DMA ping pong mode?
    Personally I have not worked DMA in any MCU?


    --
    Karan
    post edited by karan123 - 2017/08/16 02:58:35
    #7
    S12345S
    S12345S
    • Total Posts : 9
    • Reward points : 0
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2019/10/14 03:02:19 (permalink)
    0
    hai karan123
    I am working on dsPIC33EP512MU810 microcontroller .
    I using 10 bit adc to read 11 inputs by using channel scanning with dma .while i used for 3 inputs getting correct values.but for increased to 11 inputs reading wrong values.
     
    thank you
     
     
    post edited by S12345S - 2019/10/14 03:08:27
    #8
    karan123
    Super Member
    • Total Posts : 851
    • Reward points : 0
    • Joined: 2009/02/02 11:11:21
    • Location: 0
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2019/10/14 05:12:00 (permalink)
    0
    Hi,

    Please create new thread...

    --
    Karan
    #9
    theozo
    Starting Member
    • Total Posts : 42
    • Reward points : 0
    • Joined: 2015/03/15 02:30:18
    • Location: Hungary
    • Status: offline
    Re: dsPIC33EP Analog Channel Read Suggestion: 2019/10/23 12:40:22 (permalink)
    0
    Hi!
    Each converter has 4 separate sample hold registers (at 10 bit mode), so it can sample simultaneously 4 (or 2 depending on the setup) channels, but it will perform the A/D conversations sequentially. In this case for one A/D trigger event there going to be 4 conversations and depending on the setup up to 4 A/D interrupts. This mode is for the case if it is important for you to take the 4 samples at the exact same moment. Unfortunately the limit is 4, unless you have a second A/D module, then you can sample 8 signals simultaneously.
    I am making a rotary encoder, for this case it is important, that all the signals are sampled at the same moment otherwise at high speeds I can get erroneous results.
     
    You can have 10 channels on one A/D by using channel scanning which is a sequential mode. For the 10 channels you will end up with 110 ksps sample rate.
     
    Unfortunately the 1us is not possible for 2 channels. The throughput of the A/D module is 1100ksps. You have to divide it by the number of your channel. Even one channel can be tricky too, because you need a certain sampling time, in this case if you have 1 channel, then you will have less than 100ns for sampling. As I remember the module discharges the sampling capacitor after all conversation, and this also takes a few TAD time (which I am not sure about).
    I would check if the output impedance of the analog source is small enough for the purpose. I can't tell the equation by heart, but it is in the reference manual. There is an RC circuitry, composed by the output impedance of the analog source, a 2K resistance of the switch inside the A/D module, some trace resistance of the chip (I can't remember it's value), and there is the holding capacitor. The question if the sampling capacitor can be charged within the available time within the 1 LSB error level.
    In my case I use 940ksps simultaneous sampling on 2 channels. The TAD frequency is 13.1MHz, my source's output impedance is about 300 Ohms, and as I remember the calculated minimum sampling time was about 24ns. I receive valid data from the A/D module.
     
    If you need  higher sample rate there is the GS series of the 33EP family. I recently bought a PIC33EP128GS805 for my next project. It has 4 A/D modules each capable of 3.3 Msps. Unfortunately 3 of them has one dedicated channel, but one of the four is capable of channel scanning.
     
    Regards
    theo
     
    Edit:
    The sampling switch is 3K Ohms, the trace resistance is about 250 Ohms and the sample hold capacitor is 4.4pF. Minimum TAD is 76ns. Minimum sampling time is 2TAD. Conversion completion to sample start 0.5 TAD, with auto sampling. This all together is 14.5 TAD for one sampling/conversion period, means 14.5*76ns. That means 907ksps maximum without sequential sampling.
    The simplest approach for me to calculate minimum sampling time is to use the RC time constant call it T. It is known that an RC circuit produces 33% error after T time of a 0-1 transition. After the second T time the error going to be 0.33*0.33 etc.
    It is simple to get n where 0.33^n < 1/(2^10) for 10 bits. The number n going to be 7. So we need to wait 7 RC time for the signal to settle. In my case it was (3000+250+300)*4.4e-12 = 109ns. (not 24) The value of 2TAD (the minimum) is 152ns.
    I checked my code I used sequential sampling, because some times the A/D couldn't finish before the new A/D trigger kicked in at 940ksps. (2 channel 470ksps per channel)
    post edited by theozo - 2019/10/23 16:21:33
    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5