I'm quit new one PIC microcontroller, and i'm stuck with a new problem.
I was using the A/D converter of my PIC18F46j50 to catch 4 push buttons on 1 PIN (RB2), and it worked well. But since I soldered a 32.768kHz oscillator for Timer1, it doesn't work anymore.
unsigned int HAL_SWITCH_GetValADC(void){
ADCON0bits.VCFG0 = 0;
ADCON0bits.VCFG1 = 0;
ADCON0bits.CHS = 0b1000;
ADCON1bits.ADFM = 1; // A/D Result format select bit : right justified
ADCON1bits.ADCAL = 0; // A/D Calibration bit
ADCON1bits.ACQT = 0b111; // A/D Acquisition Time 12 Tad = 12us
ADCON1bits.ADCS = 0b001; //Tad = 1us (Fosc/8)
ADCON0bits.ADON = 0b01; // A/D enable
PIR1bits.ADIF = 0; //make sure A/D Int not set
ADCON0bits.GO=1; //and begin A/D conv.
while(!PIR1bits.ADIF);
NOP();
return (((unsigned int) ADRESH << 8) | ADRESL);
}
Using breakpoint I determined that the problem come from the while loop, when i debug step by step, it works fine, but the programm never stops if the breakpoint is on NOP();
got error like "No source code lines were found at current PC" + few adress, mostly those of the interupt instructions… I don't know if their is a link.
Also someone told me to change the clock used by the A/D convertor… How can I manage to do so ?
Ask for more code if it can help.
Best Answer
ADCON1bits.ACQT = 0b111
<- that's not 12 Tad, but 20 Tad (according to datasheet), which translates roughly to 20us. Try using 0b101, for the value you are trying to achieve. Try setting bit 6 of ADCON1 register (ADCAL, responsible for automatic calibration) to 1 and running your code again. You have also put semicolon after the while loop, which does not seem like a good idea at the moment. Weren't you going for something more like:while(!PIR1bits.ADIF) {
NOP(); } return (((unsigned int) ADRESH << 8) | ADRESL);
EDIT: What is nop function exactly? Is it literal use of nop assembly instruction or is it a delay function? Other thing, you should probably put whole A/D routine with saving the result to into a loop, since it's 10-bit A/D converter. PIC18F46J50 datasheet suggests something like this(page 400):