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

Reading ADC

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



Joined: 18 Nov 2013
Posts: 160

View user's profile Send private message

Reading ADC
PostPosted: Fri Dec 04, 2015 3:57 pm     Reply with quote

The CCS manual seems to indicate that when reading an ADC channel with a 12-ADC (e.g. the 18F67K22), the value returned needs to shifted right by 4-bits when the #device ADC=16. Is that correct?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 6:08 pm     Reply with quote

This is correct.

The compiler clears the ADFM bit (see below in bold) which will left-justify
the 12 bit result:
Quote:
.................... setup_adc(ADC_CLOCK_DIV_4);
0003C: MOVF ADCON2,W
0003E: ANDLW C0
00040: IORLW 04
00042: MOVWF ADCON2
00044: BCF ADCON2.ADFM
00046: BSF ADCON0.ADON


The compiler doesn't change this when it reads the ADRES registers.
It directly moves the ADRES registers into the 'result' variable without
any shifting:
Quote:
.................... result = read_adc();
00068: BSF ADCON0.GO
0006A: BTFSC ADCON0.GO
0006C: BRA 006A
0006E: MOVFF ADRESL,result
00072: MOVFF ADRESH,result+1

So, you would have to right-shift it by 4 bits in your code, if you want it
right-justified.

However, you could also define the ADFM bit with #byte and #bit and
getenv() statements, and then set it = 1 in your code. Then the PIC
will return a right-justified result.
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 6:10 pm     Reply with quote

depends upon how you set the AD format select bit. If 'left justified', then YOU must right shift the result, if 'right justified' then the PIC does the shift.
at least that's how I read section 23.2.2 of the datasheet.

being an assembler guy I tend to read the datasheet first, then the C functions....

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19494

View user's profile Send private message

PostPosted: Sat Dec 05, 2015 12:33 am     Reply with quote

Why not just use ADC=12?.
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