I am a new to PIC programming and need some advice on whether or not I am on the right track with my code. I am writing a function that reads the input values (0-5V) from port 7 and returns a voltage value based from (0-6.5V).
Here is what I have so far:
float measure(void) {
do {
ADCON1= 0x00; // configure ports as AD
buffer.vol = ADC_Read(7); // reads value from port 7
Real = (buffer.vol/5)*6.5; // converts it to voltage between 0 -6.5v
} while(1);
}
Does this look correct? Do I need to change anything?
You do not need to reconfigure the pin inside the loop. Start-up configuration should occur at the beginning of main
.
ADC_Read
does not return a floating-point value; you need to convert it yourself. For a 12-bit ADC, you can have
const float conversion_factor = 6.5f / ( ( 1 << 12 ) - 1 );
and then do
Real = ADC_Read(7) * conversion_factor;
It's not clear what buffer
is for. Unless you need to save the unscaled voltage for some reason, don't.