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 CCS Technical Support

adc program help pic 16f877a

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



Joined: 30 Jan 2014
Posts: 3
Location: chennai

View user's profile Send private message

adc program help pic 16f877a
PostPosted: Thu Jan 30, 2014 2:13 am     Reply with quote

I'm new to programing. I need to write a program for 16f877a such that when the input an0 crosses 2.5v the port d must go high.
I've tried this but its not working plz point out my mistake and help.
Thank you in advance!!! God bless :-)
Code:


#include <16F877A.h>
// CONFIG
#fuses HS,NOWDT,NOPUT,BROWNOUT,NOLVP,NOCPD,NOWRT,NOCPD
#use delay(clock=20000000)       

void main()
{
float value;
set_tris_d(0);
output_d(0x00);
         
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);

while(1)
  {
   delay_ms(1000);
   value=read_adc();
   if(value>512)
     output_d(0xff);
   else
     output_d(0x00);
  }
return;
   
}

_________________
Tony
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Thu Jan 30, 2014 2:35 am     Reply with quote

First, right at the top after the processor include line:

#device ADC=10

This tells the code to return a 10bit ADC value.

Then don't use 'float'. The ADC returns an integer, so 'int16 value;'.

Floats are bulky and slow, and should _only_ be used if what is needed cannot be done using an integer....

Then ADC_CLOCK_INTERNAL, is _not_ a legal clock above 1MHz (read the data sheet about this).

setup_adc(ADC_CLOCK_DIV_32);

As a comment, get rid of the 'return'. There is nowhere to return 'to', and (correctly), you code never exits, but having this may 'mislead' you in the future, to thinking "it's going somewhere".

Best Wishes
Tony Pinto



Joined: 30 Jan 2014
Posts: 3
Location: chennai

View user's profile Send private message

PostPosted: Thu Jan 30, 2014 3:41 am     Reply with quote

Thank You very much Ttelmah !!!!! :-)
_________________
Tony
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Jan 30, 2014 8:16 am     Reply with quote

Code:

return;


this will never execute, and you don't need it anyway. Very Happy Very Happy
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