View previous topic :: View next topic |
Author |
Message |
xtina
Joined: 14 May 2009 Posts: 7
|
problem with ADC code on 16F877A |
Posted: Mon May 25, 2009 1:20 pm |
|
|
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
|
|
Posted: Mon May 25, 2009 1:44 pm |
|
|
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
|
|
Posted: Mon May 25, 2009 1:50 pm |
|
|
plz what is the solution |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 25, 2009 1:57 pm |
|
|
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
|
|
Posted: Mon May 25, 2009 2:31 pm |
|
|
no it didn't work man |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
xtina
Joined: 14 May 2009 Posts: 7
|
|
Posted: Mon May 25, 2009 3:53 pm |
|
|
also it didn't work |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 25, 2009 4:19 pm |
|
|
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
|
|
Posted: Mon May 25, 2009 11:27 pm |
|
|
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) |
|
|
|