i am new using microcontrollers. at the moment im starting using 16f877 and need to start by programming the adc (10 bit). all i did for now is the code shown below. is it enough to read data? or does it need alteration?
i am using mplab to compile and proteus for schematics.
thanks
maria
if (ADRESH == 1)
{
data = ADRESL + 256;
}
else if (ADRESH == 2)
{
data = ADRESL + 512;
}
else if (ADRESH == 3)
{
data = ADRESL + 768;
}
else if (ADRESH == 0)
{
data = ADRESL;
}
to
Code:
data = (ADRESH<<8)+ADRESL;
Reading data is all you need. Alteration depends on your application and how the data must be processed.
In your sample code, you might want to output the ADC value to some port(s) and display the value using LEDs to understand what value it is holding. Or you could send it via hyperterminal or display on LCD or 7-segment, but I think the LED solution would be easiest at this point.
<<8 means *256
So, when ADRESH shows 1, it adds 256 to ADRESL, 512 to ADRESL when ADRESH contains 2 and 768 to ADRESL when ADRESH contains 3. Basically, they are both the same thing.
Just to clarify things, << means bit shift left. So, << 8 is bit shift left 8 times, ie, multiply by (2^8). So <<2 means multiply by 4.
The same way >> means bit shift right. So >>8 means bit shift right 8 times, ie, divide by (2^8)