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

ADC2 on dspic not working with vref+ [SOLVED]

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



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

ADC2 on dspic not working with vref+ [SOLVED]
PostPosted: Tue Mar 07, 2017 11:55 am     Reply with quote

Goodday,
I can't get my head around this problem. If I set the ADC ref to VSS_VDD both ADC1 and ADC2 works correctly. If I set it to VSS_VREF then only ADC1 works. What am I missing here.

Code:
#include <33EP128GM604.h>
#device ADC=10

#FUSES OSCIO
#FUSES NOIESO
#define CLK_FREQ 20000000
#use delay(internal=CLK_FREQ)

void main(void){
  int16 Tempchrg,TempIn;
   
  setup_adc_ports(sAN1 | sAN32, VSS_VREF);

  setup_adc(ADC_CLOCK_DIV_16 | ADC_TAD_MUL_4);
  setup_adc2(ADC_CLOCK_DIV_16 | ADC_TAD_MUL_4);
  set_adc_channel2(1);
  set_adc_channel(32);
  delay_ms(10);

  while (TRUE) {
    Tempchrg = read_adc2();
    TempIn = read_adc();
  }
}

Values with VSS_VDD for the 2 ADC are 494 and 494, which are correct.
If I use VSS_VREF where VREF are 3.0V and fed into RB1/Vref+ then I get the following ADC1 542 (correct) ADC2 1023 (in-correct).
If I swap the channels I get ADC1 543 (correct) ADC2 770.
If I change the input voltage ADC1 follow and ADC2 are stuck at the levels mentioned above.

Been around this issue now for a day and ran out of ideas.

Regards


Last edited by alan on Wed Mar 08, 2017 6:45 am; edited 1 time in total
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 12:26 pm     Reply with quote

Sorry forgot:

CCS Ver 5.070.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 12:44 pm     Reply with quote

I'm surprised it works with Vss_Vdd. You aren't setting up the reference for the second ADC. setup_adc_ports2.....
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 12:56 pm     Reply with quote

Ttelmah there are no prototype for setup_adc_ports2.

By calling setup_adc_ports it setup ADC2 as well.
From the listing:
Code:
00828:  MOV     #206,W4
0082A:  MOV     W4,E0E
0082C:  MOV     #2,W4
0082E:  MOV     W4,E1E
00830:  MOV     #2,W4
00832:  MOV     W4,E2E
00834:  MOV     #2000,W4
00836:  MOV     W4,AD1CON2
00838:  MOV     #2000,W4
0083A:  MOV     W4,AD2CON2
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 1:06 pm     Reply with quote

OK. The other chips I've used with dual ADC's have had separate setups for the other port.
Does it work if you select a channel below 32?. Obviously this has significance as being ADC1 only at this point and above. Are you sure it is correctly referencing ADC1 as ADC1?. I've seen other chips, where sometimes the peripherals are reversed in the CCS references....
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 1:13 pm     Reply with quote

I did try another channel. Channel 7 which have a different voltage and it have the correct value if on ADC1 but ADC2 have the same behaviour.

Regards
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Tue Mar 07, 2017 2:06 pm     Reply with quote

OK. Ttelmah there are a setup_adc_reference2 which I found in the PCD manual.

If I do:
Code:
    setup_adc_reference(VSS_VREF);
    setup_adc_reference2(VSS_VDD);


then everything works except that ADC2 then runs off the Avdd supply.
ADC1 reads Ch1 and Ch32,
ADC2 reads Ch7 and Ch28.

This however produces wrong results on ADC2.
Code:
    setup_adc_reference(VSS_VREF);
    setup_adc_reference2(VSS_VREF);


So this looks like the chip can't use vref on ADC2. Will have to look at the code to do a change. Did this orginally because I sample 4 channels and didn't want to wait for channel selection everytime, With single ADC it is 4 channel changes, with the 2 ADC's it was only 2 channel changes.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 2:38 am     Reply with quote

OK. Well done on getting so far.
It initially suggests that the compiler is not setting the VCFG bits correctly in AD2CON2.

So though I don't have that chip tried just compiling a quick bit of code containing just the reference setups:
Code:

....................    setup_adc_reference(VSS_VREF);
00232:  MOV     #2000,W4
00234:  MOV     W4,322
....................    setup_adc_reference2(VSS_VREF);
00236:  MOV     #2000,W4
00238:  MOV     W4,362


Now this is interesting, since it is correct!... ADCON2 is register 0x362, and 0x2000 is the value to set this to use Vss to Vref. Duh...

Even more interesting, your original setup line gives the same values being used.

So suggests something else in the peripheral setup is wrong, or that possibly the chip itself has a problem with this configuration.

So lets check the data sheet.

Page 333.

VCFG2:0 table of configurations. Note the 'Note2' flag against all the selections with Vref+ and Vref-

Read the note:
"2: ADC2 does not support external VREF± inputs."

So the reason it won't work, is that this ADC doesn't support it..... Sad
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 5:21 am     Reply with quote

Aah, interesting, my datasheet only have a NOTE1:

Quote:
Note 1: The ‘010’ and ‘011’ bit combinations for VCFG<2:0> are not applicable on ADC2.

I use '001'

I had Rev C of datasheet, now updated to Rev D

Thank you for response, now I know I can not use ADC2. Will change the code.

On the previous PCB versions I have put a voltage ref on the Avdd pins, but what I read on the forum is that this does not really improve the conversion results, so with new PCB I moved the ref to vref+. So back to the drawing board.

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Wed Mar 08, 2017 9:15 am     Reply with quote

I must admit it was a surprise to me... :(

However worth saying that unless you need silly speeds, given the capabilities of the ADC here, it might just be worth programming ADC1 to multiplex on two pins and auto sample these, rather than using the second ADC.
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