View previous topic :: View next topic |
Author |
Message |
SeeCwriter
Joined: 18 Nov 2013 Posts: 160
|
Reading ADC |
Posted: Fri Dec 04, 2015 3:57 pm |
|
|
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
|
|
Posted: Fri Dec 04, 2015 6:08 pm |
|
|
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: 9246 Location: Greensville,Ontario
|
|
Posted: Fri Dec 04, 2015 6:10 pm |
|
|
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: 19552
|
|
Posted: Sat Dec 05, 2015 12:33 am |
|
|
Why not just use ADC=12?. |
|
|
|