Hi, I have used the following code to test the adc. I have used a pot as an analog input between 0 and 5V. I am trying to output a:
0 to a 7seg display for a 0 - 1V range
1 for a 1 - 2 V range etc up to 5
The range for 0 is 0V to 0.84V, 1 is 0.85 to 1.72V, 2 is 1.73V to 2.55 etc.
I have just started using the adc and I assume the way I have coded this is the wrong way to do it. can anyone give some tips on the best way to code this?
0 to a 7seg display for a 0 - 1V range
1 for a 1 - 2 V range etc up to 5
The range for 0 is 0V to 0.84V, 1 is 0.85 to 1.72V, 2 is 1.73V to 2.55 etc.
I have just started using the adc and I assume the way I have coded this is the wrong way to do it. can anyone give some tips on the best way to code this?
Code:
for(;;)
{
__delay_us(10);
ADCON0bits.GO = 1;
while(ADCON0bits.nDONE);
int value = ADRESH;
value = (value << 2) + (ADRESL >> 6);
//0 - 171
if (value >= 0 && value <= 171)
{
no_zero();
}
//172 - 342
if (value >= 172 && value <= 342)
{
no_one();
}
//343 - 513
if (value >= 343 && value <= 513)
{
no_two();
}
//514 - 684
if (value >= 514 && value <= 684)
{
no_three();
}
//685 - 855
if (value >= 685 && value <= 855)
{
no_four();
}
//856 - 1023
if (value >= 856 && value <= 1023)
{
no_five();
}
}
}