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

problem with ADC code on 16F877A

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



Joined: 14 May 2009
Posts: 7

View user's profile Send private message

problem with ADC code on 16F877A
PostPosted: Mon May 25, 2009 1:20 pm     Reply with quote

Code:
#include <16f877a.h>
#device ADC = 10
#fuses xt, noprotect, nowdt, nolvp
#use delay (clock=4000000)

#byte porta=5
#byte portd=8

void main()
{
   set_tris_a(1);
   set_tris_d(0);
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);
   
   while(true)
   {
      if (read_adc() > 409.6)
      {
      portd=0x0A;
      }
     
      if (read_adc() < 409.6)
      {
      portd=0x00;
      }
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 25, 2009 1:44 pm     Reply with quote

Quote:
if (read_adc() > 409.6)

The read_adc() function doesn't return a floating point value. It returns
an unsigned integer value from 0 to 1023 (for 10-bit ADC mode).
xtina



Joined: 14 May 2009
Posts: 7

View user's profile Send private message

PostPosted: Mon May 25, 2009 1:50 pm     Reply with quote

plz what is the solution
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 25, 2009 1:57 pm     Reply with quote

Simplify it. Do it like this:
Code:
while(true)
   {
    if(read_adc() > 410)
      {
       portd=0x0A;
      }
    else
      {
       portd=0x00;
      }
   }
xtina



Joined: 14 May 2009
Posts: 7

View user's profile Send private message

PostPosted: Mon May 25, 2009 2:31 pm     Reply with quote

no it didn't work man
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 25, 2009 2:38 pm     Reply with quote

Try another test program, to see if your ADC setup is working at all:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
xtina



Joined: 14 May 2009
Posts: 7

View user's profile Send private message

PostPosted: Mon May 25, 2009 3:53 pm     Reply with quote

also it didn't work Sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 25, 2009 4:19 pm     Reply with quote

Have you ever made this PIC do anything (with the CCS compiler) ?
For example, blinking an LED.

Did you build the board yourself or did you buy it ? If you bought the
board, post the manufacturer and part number of the board and a link to
the website for it.
xtina



Joined: 14 May 2009
Posts: 7

View user's profile Send private message

PostPosted: Mon May 25, 2009 11:27 pm     Reply with quote

ya i try it on my line follower robot and works perfectly, but i wanna add my ultrasonic sensor to avoid obstacles.(I try it on another compiler and it works plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz help me)
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