CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC24 ADC Question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
chrislaw79



Joined: 03 Jun 2013
Posts: 4

View user's profile Send private message

PIC24 ADC Question
PostPosted: Thu Oct 27, 2016 6:03 am     Reply with quote

Device: PIC24FJ128306
Complier Version: 5.063

I have an issue where I am using ADC channels 0 and 1 (PIN_B0 and PIN_B1) as analog inputs and what would be ADC channels 10, 11, and 15 (PIN_B10, PIN_B11, and PIN_B15) as digital outputs. When I change the state of the digital outputs is seems to have an effect on the analog input reading. Unfortunately I am using my supply voltage for my analog reference and I honestly believe that may be my problem but I am wondering (say praying) if I have a an issue with my adc set up. I have included my ADC code snippet below. My design uses a 32 MHz oscillator connected to OSC1.

Code:
#include <24FJ128GA306.h>
#device ADC=12

#fuses NOWRT
#fuses NOPROTECT
#fuses HS, OSCIO, EC, PR
#fuses WINDIS, WDT, WPOSTS10         //WPOSTS6

#USE delay(clock = 32M, type=osc, restart_wdt) 

#define ADC_CYCLES= 16;
#define ADC_SWITCH_TIME 10
#define ZERO_PSI 0x333

#define RELAY1          PIN_B15
#define RELAY2          PIN_B10
#define RELAY3          PIN_B11


void main (void)
{
     unsigned char LoopCounter = 0;
     unsigned int16 Reading, RX, LX;

     SETUP_ADC(ADC_CLOCK_DIV_4 | ADC_TAD_MUL_16);//ADCON2
     SETUP_ADC_PORTS( sAN1 | sAN0, VSS_VDD );
 
    while(TRUE)
    {
          set_adc_channel(0);
     delay_us(ADC_SWITCH_TIME);
     Reading = 0;
          Reading = ((READ_ADC()));

          LX += Reading;
   
     set_adc_channel(1);
     delay_us(ADC_SWITCH_TIME);
     Reading = 0;
          Reading = ((READ_ADC()));

     RX += Reading;
   
     LoopCounter++;

          if(LoopCounter == ADC_CYCLES)
          {

                   LX = LX/ADC_CYCLES;
                   RX = RX/ADC_CYCLES;
                 
                   
                    if(LX < ZERO_PSI)
                           output_high(RELAY1);
                   else
                           output_low(RELAY1);

                   
                   if(RX < ZERO_PSI)
                           output_high(RELAY2);
                   else
                           output_low(RELAY2);

               
                    if((LX < ZERO_PSI) || (RX < ZERO_PSI))
                           output_high(RELAY3);
                   else
                           output_low(RELAY3);

                   
                   LX = RX = 0;
                   LoopCounter = 0;

          }


     }

}
temtronic



Joined: 01 Jul 2010
Posts: 9200
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Oct 27, 2016 6:49 am     Reply with quote

Comments though I don't use that PIC.

1) using VDD as Vref is always a BAD idea. It's not stable, subject to EMI, power surges etc....Doesn't that PIC have an internal Vref available?

2) you appear to be controlling relays. If these are mechanical (not SSR) then they WILL drop the VDD of the PIC. A 'scope' will show this happens.

Sure way to prove all this is to NOT energise the relays. Instead just read the ADC inputs and display on say an LCD or send to PC. IF the readings are rock stable (+-5 bits) then it IS the relays affecting the Vref.

You may also require to add capacitance to them depending on the relay currents. EM relays typically spike 10X their holding current. Without a good 'reservoir' of electrons, VDD again dips, ADC ref dips, ADC rdgs are bad....

Jay
chrislaw79



Joined: 03 Jun 2013
Posts: 4

View user's profile Send private message

PostPosted: Thu Oct 27, 2016 7:15 am     Reply with quote

Thanks for your response. I figured it was the relays.

When I disable the relays the readings are a lot more stable. The Relays when energized see to pull Vdd down by up to .003 volts (doesn't seem like a lot but it certainly affects the output).

Lesson learned. If I provide a separate but stable AVdd, I imagine this would clear up my problem.
temtronic



Joined: 01 Jul 2010
Posts: 9200
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Oct 27, 2016 7:52 am     Reply with quote

Yes, for ANY good ADC response you need a super stable Vref especially for anything over 8 bits !!
You also need the AVDD to be 'decoupled' from DVDD small R, big C(*2 if possible), and thick power traces on PCB.
All this of course is AnalogADC101 which probably isn't taught in schools these days...
You could write a book about proper analog ADC designs...hmm.. yeah, several are out there......

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 27, 2016 11:25 am     Reply with quote

The variables RX, LX are never initialized to 0 near the start of main.
chrislaw79



Joined: 03 Jun 2013
Posts: 4

View user's profile Send private message

PostPosted: Thu Oct 27, 2016 11:31 am     Reply with quote

The RX and LX variables are initialized in the actual program. I must have forgotten to copy those lines over when I cut down the actual program to the snippet shown above.
Ttelmah



Joined: 11 Mar 2010
Posts: 19445

View user's profile Send private message

PostPosted: Thu Oct 27, 2016 11:37 am     Reply with quote

chrislaw79 wrote:
Thanks for your response. I figured it was the relays.

When I disable the relays the readings are a lot more stable. The Relays when energized see to pull Vdd down by up to .003 volts (doesn't seem like a lot but it certainly affects the output).

Lesson learned. If I provide a separate but stable AVdd, I imagine this would clear up my problem.


Remember also that the stability needs to extend to how the grounds are connected as well....

Question. How are you measuring the 0.003v?. If this is with a DVM, you need to realise that DVM's generally use dual slope integrating ADC's. These take a long time to get a reading (often up to perhaps 1/4second). Fast changes will not be seen. Really good DVM's that can record fast transients, are very expensive. A change of 0.003 on a DVM, might well correspond to a real change of a large fraction of a volt for a few uSec....
chrislaw79



Joined: 03 Jun 2013
Posts: 4

View user's profile Send private message

PostPosted: Tue Nov 01, 2016 8:52 am     Reply with quote

temtronic wrote:
Yes, for ANY good ADC response you need a super stable Vref especially for anything over 8 bits !!
You also need the AVDD to be 'decoupled' from DVDD small R, big C(*2 if possible), and thick power traces on PCB.
All this of course is AnalogADC101 which probably isn't taught in schools these days...
You could write a book about proper analog ADC designs...hmm.. yeah, several are out there......

Jay


As far as decoupling AVDD from DVDD how large capacitor are we talking? Will a 220Uf tantalum suffice? I see a lot of different decoupling techniques and I want to make sure I get it right as I only have one more shot.
newguy



Joined: 24 Jun 2004
Posts: 1906

View user's profile Send private message

PostPosted: Tue Nov 01, 2016 9:23 am     Reply with quote

Best option is a precision voltage reference IC. Feed would be: DVdd -> 1+uF ceramic -> choke rated for ~100mA -> 1+uF ceramic AND 0.1uF ceramic -> precision Vref IC. Output of Vref IC -> 1+uF ceramic -> Vref+ of PIC.

You can sort of get away without a precision Vref IC but you'll need the choke. With a 220uF tantalum cap, you're going to need a choke rated for a higher saturation current otherwise the thing will take forever to charge. Also please add a 0.1uF ceramic in parallel with the tantalum cap, placed close to the PIC.
Ttelmah



Joined: 11 Mar 2010
Posts: 19445

View user's profile Send private message

PostPosted: Tue Nov 01, 2016 10:06 am     Reply with quote

The point about capacitors is that bigger is not necessarily better. For blocking HF signals, small ceramic and polyester capacitors in particular perform much better than most larger types. This is why you will see supply designs that have an electrolytic capacitor close to the regulator, then multiple small capacitors each close to the legs of the main chips around the board.
Tantalum capacitors have high packing density, and better HF performance than most standard electrolytic types, but 'beware' they also have some of the nastiest failure modes. For use in some types of equipment only special rated tantalum's are allowed, and other capacitor types are generally preferred. Use only with care....
If you look at recommended designs for supplies for the PIC's you will often see something like five ceramic capacitors close to each of the supply legs....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group