View previous topic :: View next topic |
Author |
Message |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
16F877A ADC help |
Posted: Tue Oct 14, 2008 10:43 am |
|
|
I'm new to PIC programming. I wrote a little code to measure variance of an LDR connected to A0 and run a motor through L298, but it's not working. Nothing happens when analog input change. Can someone point me the error ?
I used 16F877A with 4MHz crystal, CCS C 4.057 .
Do I have to give ref voltages also ? That means do I have to use another two pins to give 5V and 0V other than power supply when only using one analog port?
Code: |
#include "D:\PIC C codes\16F877A\sensor+pwm\sensor+pwm.h"
int x=0,y=0;
void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,99,1);
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// TODO: USER CODE!!
set_adc_channel(0);
while(1)
{
delay_ms(10);
x=read_adc();
y=(x*100)/255;
output_high(PIN_B0);
output_high(PIN_C1);
set_pwm1_duty(y);
if(x<=256)
{
output_high(PIN_B1);
}
else
{
output_high(PIN_B2);
}
}
} |
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Tue Oct 14, 2008 10:56 am |
|
|
Thanks, code seems very helpful. Anyway I won't be able to test it until day after tomorrow. I'll contact you if I got any trouble with this.
Anyway when do I have to use ref voltages ? If I didn't use ref voltages does PIC take default ref voltages as 5v and 0v |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 14, 2008 1:54 pm |
|
|
You could set your own Vref voltages (instead of using the default Vdd
and Vss), if you want to:
1. Reduce the voltage range over which the ADC measurement is made,
to increase the resolution. With the default Vdd and Vss references,
the 1024 steps are spread over 5.0 volts. That's 4.88 mv per step.
But what if your input voltage is only 0 to 3.0v, and you want the full
ADC range of 0 to 1023 to be applied over that range ? Then you could
change the Vref+ value to be 3.0v (or maybe a little higher).
2. Make it so each step of the ADC (0 to 1023) corresponds to an
exact number of millivolts. For example, if you set the Vref+ to 4.096v,
then each step is 4.0 mv. This could make the math easier (and faster)
in your code that uses the ADC result.
Here's an ADC tutorial that explains Vref:
http://www.piclist.com/images/www/hobby_elec/e_pic7_5.htm |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Tue Oct 14, 2008 6:56 pm |
|
|
PCM programmer wrote: | You could set your own Vref voltages (instead of using the default Vdd
and Vss), if you want to:
1. Reduce the voltage range over which the ADC measurement is made,
to increase the resolution. With the default Vdd and Vss references,
the 1024 steps are spread over 5.0 volts. That's 4.88 mv per step.
But what if your input voltage is only 0 to 3.0v, and you want the full
ADC range of 0 to 1023 to be applied over that range ? Then you could
change the Vref+ value to be 3.0v (or maybe a little higher).
2. Make it so each step of the ADC (0 to 1023) corresponds to an
exact number of millivolts. For example, if you set the Vref+ to 4.096v,
then each step is 4.0 mv. This could make the math easier (and faster)
in your code that uses the ADC result.
Here's an ADC tutorial that explains Vref:
http://www.piclist.com/images/www/hobby_elec/e_pic7_5.htm |
thanks |
|
|
Ttelmah Guest
|
|
Posted: Wed Oct 15, 2008 3:24 am |
|
|
As one little comment, if you want good accuracy, don't use ADC_CLOCK_INTERNAL. Look at the data sheet, 'selecting the A/D conversion clock'.
Best Wishes |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Wed Oct 15, 2008 6:48 am |
|
|
Ttelmah wrote: | As one little comment, if you want good accuracy, don't use ADC_CLOCK_INTERNAL. Look at the data sheet, 'selecting the A/D conversion clock'.
Best Wishes |
gee thanks |
|
|
|