Hi there. I wonder if anyone can help. I’ve been using microcontrollers for a while now, but never for A to D conversion. I’m having difficulty getting to grips with it. I’m including the code here, which is just trying to sample AN0 with a variable resistor tied to vss and vdd on its ends, with the wiper going to pin 2 (AN0). The value is (should be) shown on a LCD display.
I’m using Sourceboost C++ compiler. The chip I’m using is a PIC18F4520. I’ve done bit-wise setting of the adcon0-2 registers, in an effort to make things readable (or maybe not)! Hopefully, someone will point out my stupidity!
Hi Helios! Whatever the position of the vr, the display just shows 53187. I can even take a test wire from Vss or Vdd to pin 2, without anything happening. I've tried 2 devices (one brand new) just in case the microcontroller was faulty.
I'm sure it's something obvious, but I'm not seeing it right now.
It looks like you made a typo here: if(test_bit(adcon0, GO != 0))
instead of if(test_bit(adcon0, GO) != 0)
Although I'd expect that you're waiting for value of bit GO of adcon0 changes to 1, which should be if(test_bit(adcon0, GO) == 0)
But really, there's no reason to be using goto in the first place.
1 2 3 4 5 6 7 8 9
while (true){
set_bit(adcon0, GO);
while (!test_bit(adcon0, GO));
value = (ADRESH << 8) | ADRESL;
gotoxy(1,1);
sprintf(disp, "Value is: %u. ", value);
lprint(disp);
}
I don't know if there's anything else wrong. That's just what's obviously wrong.
Thanks for that. I set the GO bit on adcon0 to start the reading, and was waiting for the bit to be reset when the data had been acquired. I did change the condition temporarily, but it didn't sort the problem.
I'll certainly tidy the code up, rather than using a "goto spaghetti"!