Maker Pro
Maker Pro

ADC 12 BIT PIC18F4423, How to combine and shift the adresh and adresl in assembly?

cat5244

Jul 24, 2021
1
Joined
Jul 24, 2021
Messages
1
Hi everyone,

I have an assembly project that was developed using 10-bit Adc PIC. However, because the PIC is not available at this time, so we want to use a 12 bit PIC184423F ADC. Obviously, this will not be a straight swap to make the existing firmware work. I know that the resolution of the step size for a 12 bit is 1.22, different than the step size of 4.88 of the 10 bit. Now shifting right will be the same as dividing the ADC result, and thus this will give us the result like if we were using a 10 bit ADC. There's this 16-bit operation from this author 16 Bit Operations Chuck McManis. that contains a macro LSR16 to rotate a 16 bit right. However, the problem that I don't understand clearly is that the PIC184420f only holds an 8-bit registers, so how will be possible to combine the adresh and adresl into a single 16-bit register and shift it, if the pic18f is only for 8-bit register. Please I need help to find a solution to the problem of how to make the 12 bit ADC result into a 10-bit result. Below there's a simple example where I'm just saving the adresh and adresl into two 8 bit register. I don't seem to understand how to shift the ADC result to make 10 bit.

Adc
movlw 0x9D
movwf ADCON2
movlw 0x08
movwf ADCON0
bsf ADCON0,GO
wait
btfsc ADCON0,GO
bra wait
movff ADRESL,low
movff ADRESH,high
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,612
Joined
Nov 17, 2011
Messages
12,612
RTFM ;)
The solution is in the header of the code you linked to:
Code:
; These are the psuedo "registers" used by the 16 BIT operations
;       
_REG_A       EQU     H'007E'
_REG_B       EQU     H'007C'
Obviously the 16 bit numbers are stored in
_REG_A which is made of 2 × 8 bit memory locations located at 0x007E and 0x007F and
_REG_B which is made of 2 × 8 bit memory locations located at 0x007C and 0x007D.

The header also states:
Code:
; This include file provides some macros for dealing with
; 16 bit quantities. It assumes a little endian format
; where the least significant byte is lower in address than
; the most significant byte.
It seems all you have to do is put the LSB and MSB from the ADC (ADRESL and ADRESH) into these memory locations and call the macro for shifting. Since the macro does a shift by 1, you have to call it twice to shift the 12 bit number by 2 positions yo get the 10 bit number you need.

I'm not familiar with PICs and MPLAB so that is all the advice I can give.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,739
Joined
Jun 21, 2012
Messages
4,739
I'm not familiar with PICs and MPLAB so that is all the advice I can give.
Your first statement in post #2: RTFM ;) is the key to using any PIC... well, successfully using any sufficiently advanced technology, actually, unless you happen to be a real magician. Read The Fine Manual!

As I have said before, Microchip always somehow manages to stuff ten pounds of "stuff" into a one-pound package. If you need to use most or all of the functionality a given PIC provides, this can save time and simplify or eliminate the need for some support circuity during the initial design process. But if you need to use just a subset of the available functionality, then you must also know how to disable or ignore the functionality you don't use. It is absolutely critical that you download, read and understand the complete datasheet for the PIC you plan to use. This is true for any microprocessor of course, but I have found it is especially true for Microchip PIC microprocessors.

RTFM... more than once! You are sure to miss, or misinterpret, something important the first time through. It helps me if I print out a hard copy of the datasheet and keep it handy while I design and write the application program that will be downloaded into the PIC.
 
Top