According to the datasheet, in calculating ADCS<5:0> the formula is 2*Tad/Tcy - 1.
For a Tad of 668ns, that gives:
2*(668e-9)/(5e-8) - 1 = 26.72
To ensure it is long enough, I chose 27. That caused my code to get stuck in an endless loop. In fact, any value greater than 16 causes my program not to work, while any value 16 or less executes.
My main code looks like this:
Code: Select all
ADCON1 = 0;
ADCSSL = 0;
ADCON3 = 0x1010;
ADCON2 = 0x6000; //B0 and B1 for Vref+ and Vref-
ADCON1.F15 = 1;
Delay_us(40);
ADCHS = 3;
ADCON1.F1 = 1;
Delay_us(1);
ADCON1.F1 = 0;
while(ADCON1.F0==0){}
lastval = ADCBUF0;