There is an error in the Analog resolution Declaration

Started by Yasin, Jan 13, 2023, 08:23 PM

Previous topic - Next topic

Yasin

Dear Les. The PIC18F87K90 has a 12-bit adc. I was having a problem with the ADC metering. At the end of my research, I followed the advice on page 3 of the silicon errata file. I declared the ADC as 10 bit in the compiler. I've made several attempts. And finally I think the declared is malfunctioning.

https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/Errata/80000500N.pdf

This is for "Declare Adin_Res = 8"
__adin_
    rlcf WREG,F
    rlcf WREG,F
    andlw 252
    iorlw 1
    iorwf ADCON0,F
    iorlw 128
    andwf ADCON0,F
    movf ADCON2,W
    andlw 248
    iorlw 3
    movwf ADCON2
    movlw 50
    rcall __delay_us_
    bsf ADCON0,PP_GO_DONE
    btfsc ADCON0,PP_GO_DONE
    bra $ - 2
    clrf PP7H
    movf ADRESH,W
    movwf PP7
    return

This is for "Declare Adin_Res = 10" or ""Declare Adin_Res = 12"

__adin_
    rlcf WREG,F
    rlcf WREG,F
    andlw 252
    iorlw 1
    iorwf ADCON0,F
    iorlw 128
    andwf ADCON0,F
    movf ADCON2,W
    andlw 248
    iorlw 3
    movwf ADCON2
    movlw 50
    rcall __delay_us_
    bsf ADCON0,PP_GO_DONE
    btfsc ADCON0,PP_GO_DONE
    bra $ - 2
    movff ADRESH,PP7H
    movf ADRESL,W
    movwf PP7
    return

When "Declare Adin_Res = 8", PP7H is cleared.
When "Declare Adin_Res = 10 or 12", ADRESH is loaded on PP7H.
Actually, it should be different. If 8-bit resolution is desired, the 12-bit value read should be shifted 4 bits to the right. If 10-bit resolution is desired, the 12-bit value read should be shifted 2 bits to the right. Because the Vref value will fill the upper bits. I could not get the values 255 (for 8 bits) and 1023 (for 10 bits) as outputs in my attempts using declaration.
I'm leaving a proteus simulation file below. I hope you will understand better.

Best regards.


Test Files

Ivano

I also noticed this malfunction with pic18F26K83 but I didn't look into it because I needed 12 bits and I didn't use "Declare Adin_Res"
I also thought the "Declare Adin_Res" was only for pic with 10 bit adc

top204

You need to change the justification of the 8 or 10/12 bit resolutions in the main program for the ADC to give the correct value. This has been standard from day one of the compiler. See the ADC sample programs and you will see the justification being altered. The Declare tells the compiler's library subroutine to return an 8-bit value, or a 16-bit value.

I could not do this automatically, because microchip kept changing what SFR to alter and what bit of the SFR to alter for left or right justification, so it is up to the user.

With the devices for the past 10 year or so, the Declares are now somewhat redundant because of the complexity of the ADC peripherals in the recent devices, and the fact they were originally placed in the compiler 21 years ago for the original old devices that had very little RAM and flash so everything was done possible in the compiler to save them. So it is always better to setup the ADC with a procedure before-hand, and it is actually better to create a procedure to read the ADC using the SFRs. That way you can get the best out of the peripheral and make it more accurate and faster etc...

In the device's datasheet, look for the "ADFM" bit, or some similar name on some devices. This bit keeps moving around SFR's and bit positions in different devices. It is better to open the device's .def file and use the meta-macros I created for all devices, and look for the SFRs that are dedicated to the ADC. For example: ADCON2bits_ADFM on a PIC18F44K20 device, but the SFRs containing it do change. The bit is cleared for Left Justification, (8-bit resolution), and set for Right Justification (10-bit or 12-bit resolution)

ADCON2bits_ADFM = 1  ' Right Justification

or

ADCON2bits_ADFM = 0  ' Left Justification