|
|
View previous topic :: View next topic |
Author |
Message |
khor Guest
|
problem in ADC |
Posted: Thu Mar 20, 2003 2:43 am |
|
|
i have a problem in comparing the two analog input signals, i am using Analog to digital function in pic16f877, if input_1 in higher than input_2 the pin_b7 is high, else is low.
when i comparing the input, the output is sometime showing high and low. At the thershold voltage is showing low but when i increase to 0.5V the output is high instead of low.
my coding is on below
#include <16f877.h>
#device ADC=10
#fuses XT, NOWDT, NOLVP,PUT,NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
main()
{
int val0, val1;
setup_adc_ports( ALL_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL);
while(true)
{
set_adc_channel(0);
delay_us(80);
val0=read_adc();
set_adc_channel(7);
delay_ms(80);
val1=read_adc();
if(val0 > val1)
output_high(pin_b7);
else
output_low(pin_b7);
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 12859 |
|
|
R.J.Hamlett Guest
|
Re: problem in ADC |
Posted: Thu Mar 20, 2003 4:10 am |
|
|
:=i have a problem in comparing the two analog input signals, i am using Analog to digital function in pic16f877, if input_1 in higher than input_2 the pin_b7 is high, else is low.
:=when i comparing the input, the output is sometime showing high and low. At the thershold voltage is showing low but when i increase to 0.5V the output is high instead of low.
:=
:=my coding is on below
:=
:=#include <16f877.h>
:=#device ADC=10
:=#fuses XT, NOWDT, NOLVP,PUT,NOPROTECT
:=#use delay(clock=4000000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=
:=main()
:={
:=int val0, val1;
:=setup_adc_ports( ALL_ANALOG);
:=setup_adc( ADC_CLOCK_INTERNAL);
:=
:=
:=while(true)
:={
:= set_adc_channel(0);
:= delay_us(80);
:= val0=read_adc();
:=
:= set_adc_channel(7);
:= delay_ms(80);
:= val1=read_adc();
:=
:=if(val0 > val1)
:=output_high(pin_b7);
:=
:=else
:=output_low(pin_b7);
:=
:= }
:=}
Very simple.
Remember an int, is 8bits, not 16. You are reading a 10bit value, and putting it into an 8bit store. You will then only have the low 8 bits, and the results of the comparison will be odd...
Make your storage variables into int16 types, and the comparison should be fine.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 12862 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|