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

Differential A/D

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



Joined: 19 May 2018
Posts: 27

View user's profile Send private message

Differential A/D
PostPosted: Sat Jul 13, 2019 6:21 pm     Reply with quote

Using the command set_adc_channel(chan, [differential])
gives me a compile error 66 Previous identifier must be a pointer.
Target is 18f87k22.

Any Ideas? Version 5.076
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 13, 2019 6:44 pm     Reply with quote

The 18F87K22.h file gives the syntax:
Code:
 void set_adc_channel(int8 pos_channel, int8 neg_channel);

What does "[differential]" mean ? It's supposed to be an int8.
I made the test program below and it compiled OK with vs. 5.085.

Test program:
Code:
#include <18F87K22.h>
#fuses NOWDT,PUT,BROWNOUT
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)

//=================================
void main()
{
int8 chan_pos, chan_neg;

set_adc_channel(chan_pos, chan_neg);

while(TRUE);
}
gtx15



Joined: 19 May 2018
Posts: 27

View user's profile Send private message

PostPosted: Sun Jul 14, 2019 5:03 am     Reply with quote

Your right, Differential is the 8 bit number for the analog input to the negative input of the differential opamp leading to the a/d. The manual shows brackets around this number, I will try your code. Thank you!
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Sun Jul 14, 2019 7:06 am     Reply with quote

This would normally be used with fixed (constant) values, rather than
using variables.

So (for example):

void set_adc_channel(1,2);

Would give the ADC reading the voltage difference between AN1 and
AN2.

Remember both channels need to be enabled.
The second value sets the CHSN bits, the first the CHS bits.

I've not used this on this chip, but on PIC24's supporting this, the compiler
does correctly load the sign bit into the result, so you have to use a
signed variable to hold this.
gtx15



Joined: 19 May 2018
Posts: 27

View user's profile Send private message

PostPosted: Sun Jul 14, 2019 8:05 am     Reply with quote

Using this code: set_adc_channel (1, 6) ;
Does not correctly set the chs bits.

Also this is from the Manual. set_adc_channel (chan [,neg]))
temtronic



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

View user's profile Send private message

PostPosted: Sun Jul 14, 2019 8:59 am     Reply with quote

You need to post your code, at least a small, compilable version...it'll really help figure out what's going on.

Also, you are aware that the 'negative' input of the ADC inputs really cannot be a NEGATIVE voltage.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Sun Jul 14, 2019 10:58 am     Reply with quote

When the differential ADC is used an extra bit is set in the peripheral
indicating the sign of the result.
However though the result can be negative (when the negative input is
above the positive input), the two voltages have both to be positive of
ground as Jay says.

As Jay says you need to post an example program. We need to test this
with your compiler version. The latter is vital, since features like this are
often largely untested and so may well not work correctly on all compiler
versions.
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Mon Jul 15, 2019 2:48 am     Reply with quote

I just tested with your compiler version, and (remarked inline):
Code:

....................    set_adc_channel(1,6);
00080:  MOVLW  04
00082:  MOVWF  01
00084:  MOVF   ADCON0,W
//correct CHS=bits 2 to 5. AN1 needs 00001xx into
//this register.
00086:  ANDLW  83
00088:  IORWF  01,W
0008A:  MOVWF  ADCON0
0008C:  MOVLW  07
0008E:  MOVWF  01
00090:  MOVF   ADCON1,W
00092:  ANDLW  F8
00094:  IORWF  01,W
00096:  MOVWF  ADCON1
//Again correct. AN6 needs 111 into bits 0 to 2 of
//ADCON1. The number needed is one greater than expected, since
//these bits have 000 selecting Vss


So your compiler _is_ correctly setting up for differential operation.

The chip returns the sign bit in the top four bits of the result. So this
is automatically read, so long as you right justify. If you left justify it is
in the low four bits which then means you would have to fiddle.
So you need:
Code:

#include <18F87K22.h>
#device ADC=12
#fuses NOWDT,PUT,BROWNOUT
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)

//=================================
void main()
{
   signed int16 val;
   setup_adc(ADC_TAD_MUL_4 | ADC_CLOCK_DIV_4);
   //setup to give 4uSec Tad - min=1.4, max 25
   //Tacq=4uSec (2.45uSec required min).
   setup_adc_ports(sAN1 | sAN6, VSS_2V048);
   //Run with Vss to internal 2.048v, AN1 and AN6
   set_adc_channel(1,6);
   //AN1 as +ve, AN6 as -ve

   while(TRUE)
   {
      val=read_adc();
      printf("%ld\n\r", val);
      delay_ms(100);
   }
}


To print the result as a signed value.

Be aware that the -ve channel can only be AN0 to AN6.
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