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 CCS Technical Support

adc help (-5V to +5V)

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



Joined: 24 Jun 2011
Posts: 9

View user's profile Send private message

adc help (-5V to +5V)
PostPosted: Tue Jul 24, 2012 10:01 pm     Reply with quote

Hi everyone,
I am stuck at the moment, please how do I sample voltage range of -5V_to_+5V. Currently I could sample only positive voltage range and output it through an lcd. But whenever the voltage goes below +5v, its only zero that would be displayed. How can this be solved?
Here's an excerpt of the code:
Code:

set_adc_channel(0); // Select channel 0
sample = read_adc(); // Get input
scaled = sample*0.0048828125f; // Scale input using a 10bit //resolution(PIC18f2550)
printf(lcd_putc,"\f%3.2f",scaled);
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Tue Jul 24, 2012 10:41 pm     Reply with quote

You can't - the A/D can only accept and process positive voltages.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jul 24, 2012 11:07 pm     Reply with quote

Another way to solve it is to put an op-amp in the signal path that offsets your voltage (and divides it depending on your ADC's vref).

Another solution is to use a Bipolar ADC (examples might be the LTC1859) that have +/- 10VDC support with 16bit results. (but operate via SPI)

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 6:56 am     Reply with quote

I'd go with the Op-Amp option as you probably already have some at home.

you could use voltage deviders to scale the signal to measure and then use an Op-Amp to ground reference your signal.

I would also add Zeners or some kind of protection to the PIC ADC ...

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 7:58 am     Reply with quote

The circuit below will let you convert +/-5V to valid ADC values.

Code:

           4k7         |
  +5V ----WWWWW--------| PIC analogue input
                  |    |
Unknown           |
Input ----WWWWW---
Voltage    4k7


Mike


Last edited by Mike Walne on Wed Jul 25, 2012 8:00 am; edited 1 time in total
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 7:58 am     Reply with quote

Gabriel wrote:

I would also add Zeners or some kind of protection to the PIC ADC ...


A good idea - but depending on the MCU, possibly unneeded.

The PIC's typically have transient schottky diodes on all I/O pins.

I say typically because it's always good to check.

This means they'll accept Vss - 0.3 to Vcc + 0.3 before shunting any excess to Vss or Vcc.

Typically, this current is pretty appreciable - like 20mA max. (ALWAYS CHECK the datasheet though!!)

So depending on the output driver of the op-amp, a current limit resistor may be all that's needed.

Always check the datasheets of course.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 8:02 am     Reply with quote

Mike Walne wrote:
The circuit below will let you convert +/-5V to valid ADC values.

Code:

           4k7         |
  +5V ----WWWWW--------| PIC analogue input
                  |    |
Unknown           |
Input ----WWWWW---
Voltage    4k7



The problem with those high values is that that it might exceed the sample/hold times needed by the A/D to provide accurate readings.

Depends on the signal being sampled.
Depends on the PIC.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 9:57 am     Reply with quote

Its even simpler then...

Use the voltage devider proposed by Mike and buffer for impedance matching with an Op-Amp.

regarding the internal protection diodes, id rather use external ones, especially when there is a "wierd" signal attached to the PIC.
those diodes I only trust for spurious transients and minor noise problems...

what if the sensor generating the -5/5V signal fails and/or the buffer amp or ground referencing amp fails and locks to an out of range voltage? for sustained periods? maybe your calculations for the devider are good but the _actual_ value of the resistors used varies significantly and places your upper or lower signal limit out of spec?

Go external... play it safe.
_________________
CCS PCM 5.078 & CCS PCH 5.093
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 10:01 am     Reply with quote

Gabriel wrote:
maybe your calculations for the devider are good but the _actual_ value of the resistors used varies significantly and places your upper or lower signal limit out of spec?

Go external... play it safe.


That's what the series current limit resistor is for on the output of the op-amp.

But yes, the argument could be made for safety in situations that warrant.

if voltages high enough to blow the op-amp (keep in mind, a Rail-to-Rail Op-Amp will limit voltages for you and you can pick one that maxes out at +5V on Vcc) and course through it to the ADC's input -- you probably have a fried board on your hands anyway. :D

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 10:29 am     Reply with quote

Code:


           4k7         |
  +5V ----WWWWW--------| PIC analogue input
                  |    |
Unknown           |
Input ----WWWWW---
Voltage    4k7



The output impedance (as seen by the PIC) is the parallel equivalent of the two resistors, i.e. 2k35.

Microchip recommend a maximum 2k5 source impedance for their A2D converters.

My circuit meets the Microchip requirement.

Mike
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 10:59 am     Reply with quote

i agree with you.

I am basing my comments on that fact that we really have no idea what the actual circuit is, and from my previous experience.

in my particular case i had a sensor running at 10V with diff outputs of 7.5V
The signal varied 100mv total, thus i needed eliminate the common voltage, ground ref and then amplify so that 100mv = 5V... my amps where running at 10V... if my Rgain varied a bit (temperature, shock, the whim of the gods) when my sensor was close to its limit values i might be out of spec for the PIC.
External components where a must, especially since i had to amplify substantially.

Im not arguing that he _has_ to include the external protections... im saying that he must consider it to play it safe in his final design...

i tend to design with the idea that the world is going to end soon and that zombies will mess with all my trimpots.... but thats just me.

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 11:05 am     Reply with quote

Quote:
But whenever the voltage goes below +5v, its only zero that would be displayed. How can this be solved?


so you are saying you already brought your input to less that VSS?

You mention nothing regarding Magic Blue Smoke.

What Sorcery is this!
_________________
CCS PCM 5.078 & CCS PCH 5.093
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 1:17 pm     Reply with quote

Gabriel wrote:
Quote:
But whenever the voltage goes below +5v, its only zero that would be displayed. How can this be solved?


so you are saying you already brought your input to less that VSS?

You mention nothing regarding Magic Blue Smoke.

What Sorcery is this!



Hahahah!

If his source couldn't muster more than 20mA, the PIC's internal clamping diodes would have saved him.

Otherwise, I don't know about any smoke -- but if he exceeded that, he may have not seen any smoke - but killed the input. Shocked
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D


Last edited by bkamen on Sat Aug 04, 2012 9:11 pm; edited 1 time in total
sterob



Joined: 24 Jun 2011
Posts: 9

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 8:39 pm     Reply with quote

THANKS GUYS, I used the voltage divider method as suggested, and clipped the output using a zener diode, so that it does not go below the zener diode voltage....merci beaucoup!!!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun Aug 05, 2012 1:45 pm     Reply with quote

The sample/hold times

You can add a .1uf ceramic cap from the PIC ADC inlet pin to ground.

And so long as you don't sample too often - get accurate values at the adc - due to the charge storage of the capacitor - in fact I do this sort of thing - with a 20k divider - and sample several times per sec with accurate results.

This has the benefit of not loading so badly as the 4.7 k as well.

Try it and see- it really works.
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