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

Wrong ADC measure, with offset.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

Wrong ADC measure, with offset.
PostPosted: Tue Jun 02, 2020 9:09 am     Reply with quote

Hi there, I'm newbie with CCS, so i'm still learning every day.

I'm using the dsPIC33FJ256710A, with CCS 5.068 and I'm trying to read and temp sensor with a NTC so i made a voltage divider following by op amp in follower mode. I had used an tester to measure this voltage on my interest pin, I get 1.65 volts, that it's ok (+3.3 voltage source with 10k divider resistor I used two 10k for simulate a NTC), also I have an REF ic, to get 2.50 volts. BUT I read ADC 1.85 volts (after conversion).

Paste the code I've used:
Code:

   setup_adc_ports(sAN3 | sAN4 | sAN9 | sAN16 | sAN19 | sAN23 | sAN26 | sAN29 | sAN30 | sAN31, VSS_VREF);
   setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31);


I set ADC_CLOCK_INTERNAL and ADC_TAD_MUL_31, because my other works mate, they setting like this and it works.

Some config:
Code:

#device PASS_STRINGS=IN_RAM
#device CONST=READ_ONLY
#device ADC=12
#build (stack=512)                  //importante para los printf y los floats
#device ICSP=1
#use delay(clock=20MHz,crystal=8MHz)


And here I read the value:
Code:

set_adc_channel(channel);
halDelayUs(10);                    //set 10 us of delay
...
resultADCtoFloat[0] = (float) (read_adc() * dacVref) / adcBit;



dacVref = 2.50, adcBit = 4095 and channel it's a function parameter, I tagged my pins like this #define MZM_BIAS_DSP 16. My sample rate it's about 100 us, I used a timer interrupt for this.


So i have no idea what's happening here.
Thanks and regards!
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Jun 02, 2020 9:40 am     Reply with quote

I'm assuming you have mistyped the chip part number, and you have
a dsPIC33FJ256GP710A.

So you have connected 2.5v, to pin 29?. Check this is right.
You have connected the Avdd, and Avss pins?.
Good decoupling on these pins?.
What adc channel are you reading?. What is in the variable 'channel' for
the result?.
adcBit is wrong. Should be 4096, not 4095. The PIC ADC reaches it's
full output one count _before_ the voltage reaches the reference level.
You don't need the 10uSec delay. That is what the ADC_TAD_MUL_31
does. It automatically makes the ADC delay for acquisition before it
samples. ADC_CLOCK_INTERNAL is OK on a DSPIC.
Always start by reading the raw number returned by read_adc. Don't
perform maths till you know this is right. What pin is the signal on?.
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Tue Jun 02, 2020 1:55 pm     Reply with quote

You should definitely check your data sheet to see what the maximum input impedance is for your ADC. A lot of PIC24s are 2.5k and a lot of dsPICs are 200 ohms. Your 10k voltage divider is way over both of those and will cause a voltage offset in your reading which can vary with temperature. I don't know offhand what your chip's electrical characteristics are, so make sure to double check the data sheet.
temtronic



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

View user's profile Send private message

PostPosted: Tue Jun 02, 2020 4:51 pm     Reply with quote

I learned a long,l ong time ago to pay a wee bit of money and add 'rail-to-rail' voltage follower opamps between all analog inputs and the PIC ADC pins.
The money up front saves in barbershop fees from pulling your hair out as to WHY doesn't this work.....arrrgh moments...
Seems quad opamps are as cheap as duals, so, yup, I always use quads.

Jay
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Tue Jun 02, 2020 5:50 pm     Reply with quote

We also do a follower circuit on our ADC inputs.

Though depending on how you do your follower circuit you may have to make sure the impedance on both inputs to the op-amp match or you get a similar offset problem do to bias current (we ran into that one time).
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

PostPosted: Wed Jun 03, 2020 1:10 am     Reply with quote

Ttelmah wrote:
I'm assuming you have mistyped the chip part number, and you have
a dsPIC33FJ256GP710A.

So you have connected 2.5v, to pin 29?. Check this is right.
You have connected the Avdd, and Avss pins?.
Good decoupling on these pins?.
What adc channel are you reading?. What is in the variable 'channel' for
the result?.
adcBit is wrong. Should be 4096, not 4095. The PIC ADC reaches it's
full output one count _before_ the voltage reaches the reference level.
You don't need the 10uSec delay. That is what the ADC_TAD_MUL_31
does. It automatically makes the ADC delay for acquisition before it
samples. ADC_CLOCK_INTERNAL is OK on a DSPIC.
Always start by reading the raw number returned by read_adc. Don't
perform maths till you know this is right. What pin is the signal on?.


Yes, correct dsPIC33FJ256GP710A.

Reference connected to pin 29, and checked it's voltage, 2.50 volts exactly. I tried to read raw value of the adc, I get a value between 3070 and 3060.

@jeremiah
I used a voltage follower so I shouldn't have any problem with this.
The op amp is LM7101. It's a rail-to-rail output.

Must be an incorrect config of the dspic, as I said, i get 1.65 volts on this pin (reading with a Fluke voltimeter) .

Thanks for the tips. Any idea else?

Regards!
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Jun 03, 2020 1:18 am     Reply with quote

Repeat "which pin is the voltage coming 'in' on, and what channel is being
selected".

The 7101, can be prone to oscillation. Stick a scope on the voltage
input pin to the PIC. Is the signal stable?.
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

PostPosted: Wed Jun 03, 2020 1:42 am     Reply with quote

Ttelmah wrote:
Repeat "which pin is the voltage coming 'in' on, and what channel is being
selected".

The 7101, can be prone to oscillation. Stick a scope on the voltage
input pin to the PIC. Is the signal stable?.


Hi Ttelmah, sorry, the pin number it's 3 tagged as AN29/RE5.

Quote:

#define THRM1 29
...
set_adc_channel(THRM1);
fprintf(RS232_PORT1, "Valor adc: %d.", read_adc());


I've checked the define and i put "29" on set_adc_channel and no makes difference.

Looks like very stable because with this voltimeter I get 1.645 always, anyway I'm going to get and oscilloscope to see this voltage. I'm going to response, in a several minutes.

Thanks again!
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

PostPosted: Wed Jun 03, 2020 2:00 am     Reply with quote

I measure this pin with a picoScope and I get about 20 mV of ripple, it's very stable.



[img]https://imgur.com/a/GgDaU0F[/img]
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Jun 03, 2020 10:27 am     Reply with quote

OK.. Good.

Now do the same with the Vref pin.

I've checked, and your compiler does set everything up correctly. So
something is making it 'think' the voltage is high. The other possibility
would be the Vref is low, or again has noise on it.
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

PostPosted: Thu Jun 04, 2020 12:10 am     Reply with quote

Ttelmah wrote:
OK.. Good.

Now do the same with the Vref pin.

I've checked, and your compiler does set everything up correctly. So
something is making it 'think' the voltage is high. The other possibility
would be the Vref is low, or again has noise on it.


Hi,
the Vref it's fine I'm sure...i tested several times, also this vref is generated by ADR361AUJZ.
I have two circuits identical, (voltage divider + voltage follower) and both gives me 1.85 volts when if I test with external tools, i get 1.64 - 1.65 Volts.

It's very weird.

I'm trying to config VDD as VREF and see what happens. EDIT: same voltage 1.87V.

thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Jun 04, 2020 12:42 am     Reply with quote

You say it is fine, but are you actually testing the pin on the chip?.
My suspicion is you have a bad connection somewhere.

Way to proceed, set the ADC up to use Vss to Vdd as the reference,
and set the Vref input as an analog input.
Then get the raw readings from the pin being used as Vref and the
signal pin.

Assuming a 3.3v supply, you should see about 3100 on the Vref
pin, and about 2048 on the input. The ratio between these ought to be
about 0.66. The numbers and ratio, will give a pointer to what is really
going on.
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

PostPosted: Thu Jun 04, 2020 3:25 am     Reply with quote

Ttelmah wrote:
You say it is fine, but are you actually testing the pin on the chip?.
My suspicion is you have a bad connection somewhere.

Way to proceed, set the ADC up to use Vss to Vdd as the reference,
and set the Vref input as an analog input.
Then get the raw readings from the pin being used as Vref and the
signal pin.

Assuming a 3.3v supply, you should see about 3100 on the Vref
pin, and about 2048 on the input. The ratio between these ought to be
about 0.66. The numbers and ratio, will give a pointer to what is really
going on.


How I can read these pin, isn't tagged as ANx. Actually i can be able to modify my hardware, maybe next week




Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Jun 04, 2020 4:46 am     Reply with quote

Aaargh.
On the smaller versions of the chip (and 99.9% of PIC's), the Vref pin is also
available as an analog input.

I'm suspecting your Vref is actually drooping when a reading is made.
There can be mA drawn on the Vref pin during conversion.
The spec is 3.2mA max. Now your vRef is specified to handle this, but how
fast can it cope?. I'd be wanting a scope on the Vref, as the reading is
actually made.
afk_pic



Joined: 02 Jun 2020
Posts: 19

View user's profile Send private message

PostPosted: Thu Jun 04, 2020 5:24 am     Reply with quote

i hope this img can be useful

https://imgur.com/a/0dVoKAM

thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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