View previous topic :: View next topic |
Author |
Message |
Tony Pinto
Joined: 30 Jan 2014 Posts: 3 Location: chennai
|
adc program help pic 16f877a |
Posted: Thu Jan 30, 2014 2:13 am |
|
|
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: 19557
|
|
Posted: Thu Jan 30, 2014 2:35 am |
|
|
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
|
|
Posted: Thu Jan 30, 2014 3:41 am |
|
|
Thank You very much Ttelmah !!!!! :-) _________________ Tony |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Jan 30, 2014 8:16 am |
|
|
this will never execute, and you don't need it anyway. |
|
|
|