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

Problem in PIC16F877A

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



Joined: 24 Mar 2010
Posts: 2

View user's profile Send private message

Problem in PIC16F877A
PostPosted: Wed Mar 24, 2010 5:33 pm     Reply with quote

Anyone can help me? This is my program. I don't know what's wrong with it.
Code:

#include <16F877.h>
#device ADC=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main ()
{
unsigned int16 Range,total, i;

setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );

printf("Wetness value\n\r");

do{
   i = Read_ADC();
   Range=i*(6275/10000.0);
   printf("Value = %ld\n\r",i);
   delay_ms(1000);
}while(true);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 24, 2010 6:24 pm     Reply with quote

Quote:
#device ADC=16

Why are you using ADC=16 ? That will cause the ADC result to be
left-justified. Is that what you want ? Normally, people want the
result to be right-justified.

This will give you a 10-bit result, and it will be right-justified:
Code:

#device adc=10
mapey87



Joined: 24 Mar 2010
Posts: 2

View user's profile Send private message

PostPosted: Wed Mar 24, 2010 8:25 pm     Reply with quote

I already change it. But the output I get is 163....The output that I want is from 0-15...or should I use float instead of int16?
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Thu Mar 25, 2010 3:09 am     Reply with quote

First, as a comment, don't use ADC_CLOCK_INTERNAL. This is only designed for use, with clock rates below 1MHz, or if you put the processor to sleep, when performing the conversion. The 'correct' conversion clock for your 20MHz master clock, is:
ADC_CLOCK_DIV_32
This affects the accuracy slightly.

Now, your maths (6275/10000), effectively multiplies the ADC value by 0.6275. The ADC value is 0 to 1023, so it'll give numbers from 0 to 641. Nowhere near what you want....

It saves significant time to use integer arithmetic where possible, so something like:
Code:

   i = Read_ADC();
   Range=i*16; //Will give 0 to 16368
   printf("Value = %5.2lw\n\r"); //will print this as 0.00 to 16.36
   delay_ms(1000);


Best Wishes
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