Issues with 18F26K40 A/D (Seems messed compared to old trusty 18F1330 or 18F2620)
Please let me know if anyone else is seeing the issues I describe below. Thanks for your help.
I just received new K parts from Microchip Direct (18F26K40 with 1632EPY date code) last week. I also got a SNAP debugger card which connects and works well with MPLAB X IDE v5.25 but is unable to connect with MPLAB X IPE v5.25. The SNAP debug/download comes back with:
Device Id Revision = 0xa0430000
This does not match the Errata document so I have no idea if it is an A3 or A4 device.
I see two big problems in the behavior and looking at the code generated by mcc it does not seem to be addressed:
1. ADGO takes two cycles to set so this code fails because ADGO is not set yet and ADRESH is returned before the conversion starts. The work-around is to set ADGO twice before doing the wait for ADGO to return to zero:
ADCON0bits.ADGO = 1;
while(ADCON0bits.ADGO); // Wait for conversion to finish
The errata does talk about this issue but claims it is fixed in rev A4 parts. Either it is not fixed or Microchip was not nice and sent me old crappy parts.
2. When I run at 64MHz using the internal oscillator, I can't get the full range of results unless I use the slow FRC clock. When I tried using Fosc with the slowest setting (ADCLK to 0x3f), I could see the high 8 bits get up to 0xe0. With ADCLK set to 0x1f which should have a tAD of 1us it only got up to 0xb0. The spec says tAD is 1us to 9us. Both of these failures were when I had the input set to +5V. Another problem I ran into was when trying to interface to a joy stick which simply switches between two channels (RA0 and RA1). When I did this I found that if one channel was above a volt or so that the other channel would not read 0 when it was at 0V. I found this gem in the data sheet:
"It is recommended that when switching from an ADC channel of a higher voltage to
a channel of a lower voltage, the software selects the Vss channel before switching"
So, I added code to switch the source to read 0 (ADPCH = 0x3c) before setting the channel and that had no effect. What worked was to selected 0, delay 100us then set the source select to my desired channel and I was able to read 0V as a 0. The data sheet does not say how long to wait. So my joystick reads almost work except, I can't get the full range. I found that going back to FRC mode that I did not need to select 0 first.
The only way to use the ADC with a 64MHz internal clock seems to be to use the FRC clock.
The full range error is not mentioned in the errata.