CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

16F877A ADC help

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

16F877A ADC help
PostPosted: Tue Oct 14, 2008 10:43 am     Reply with quote

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 Sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 14, 2008 10:52 am     Reply with quote

Quote:
Do I have to give ref voltages also ?

No.

See this example:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Tue Oct 14, 2008 10:56 am     Reply with quote

PCM programmer wrote:
Quote:
Do I have to give ref voltages also ?

No.

See this example:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1


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 Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 14, 2008 1:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 14, 2008 6:56 pm     Reply with quote

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







PostPosted: Wed Oct 15, 2008 3:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 15, 2008 6:48 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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