View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
problem with the ADC of 16F88 |
Posted: Tue Jul 16, 2013 10:07 am |
|
|
Greetings! I`ve written a simple program for 16F88.
Code: |
#include <16F88.h>
#DEVICE ADC=10
#include <MATH.h>
#fuses NOWDT,NOPUT,XT,NOMCLR,NOBROWNOUT,NOLVP,NOCPD,NOWRT,NODEBUG,NOPROTECT,NOFCMEN,NOIESO
#use delay(clock=4M)
#use rs232(rcv=PIN_B5, xmit=PIN_B2, PARITY=N, BITS=8, ERRORS, STOP=1)
#use timer(timer=2, tick=8us ,bits=8, isr)
unsigned int16 ef;
int1 done;
void main()
{
output_drive(pin_a4);
setup_adc(ADC_CLOCK_INTERNAL | ADC_CLOCK_DIV_32);
setup_adc_ports(VSS_VDD);
setup_adc_ports(1);
setup_adc_ports(2);
setup_adc_ports(4);
setup_adc_ports(8);
set_adc_channel(4);
delay_us(200);
while(1)
{
read_adc(ADC_START_ONLY);
done=0;
while(!done) {
done = adc_done();
}
ef = read_adc();
if(ef>598)
{
output_high(pin_a4);
}
else
{
output_low(pin_a4);
}
}
}
|
I have 3 volts on channel 4(pin a2) but i dot`n have reaction - pin A4 is always low.
Any ideas what I`m doing wrong? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Jul 16, 2013 10:52 am |
|
|
from the datasheet...
bit 5-3 CHS<2:0>: Analog Channel Select bits
000 = Channel 0 (RA0/AN0)
001 = Channel 1 (RA1/AN1)
010 = Channel 2 (RA2/AN2)
011 = Channel 3 (RA3/AN3)
100 = Channel 4 (RA4/AN4)
101 = Channel 5 (RB6/AN5)
110 = Channel 6 (RB7/AN6)
...
you selected adc channel #4 (above while(1) loop)
read it ..
then use it as an output !!
I have to ask why !!
jay |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Tue Jul 16, 2013 11:17 am |
|
|
I`ve tried with set_adc_channel(2); - no difference - no reaction on pin A4! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Jul 16, 2013 11:58 am |
|
|
What happens if you apply 5 volts to the input ?
What happens if you apply 0 volts to the input ?
Your reference of '598' is very close to 3 volts and might not 'trigger' the output high.
Also you may need to disable any other peripheral associated with those pins. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Jul 16, 2013 1:12 pm |
|
|
Hi,
Your use of 'setup_adc_ports' is wrong. Why did you do it that way? What reference told you to do it like that? You are supposed to 'Or' the desired
A/D channels together in one statement, not call setup_adc_ports multiple times, once for each A/D channel you intend to use!
As a side note, you are attempting program development blind, with zero feedback, and then saying 'why doesn't it work?". You need to be able to
see what's going on inside your program either by adding diagnostic printf statements, or using a debugger. If you were printing the output of your
call to the ADC then you would know immediately why the output wasn't changing!
John |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jul 16, 2013 3:04 pm |
|
|
Quote: | #use rs232(rcv=PIN_B5, xmit=PIN_B2, PARITY=N, BITS=8, ERRORS, STOP=1) | Transmit and receive pin are reversed from the hardware. It works because the CCS compiler will generate a software UART but wouldn't it be nice to use the hardware UART?
Quote: | #use timer(timer=2, tick=8us ,bits=8, isr) | The ISR parameter requires the global interrupt to be enabled. Luckily you aren't using the timer and is this line of code only wasting memory.
Code: | setup_adc_ports(VSS_VDD);
setup_adc_ports(1);
setup_adc_ports(2);
setup_adc_ports(4);
setup_adc_ports(8); | This is so wrong! I know the CCS manual can be improved a lot, but it seems like you don't even try to read it.
I try to stay positive in my posts but the quality of the 200+ posts you have made in this forum makes me feel sad. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Jul 16, 2013 5:30 pm |
|
|
Code: | setup_adc_ports(VSS_VDD);
setup_adc_ports(1);
setup_adc_ports(2);
setup_adc_ports(4);
setup_adc_ports(8);
|
Not to mention that each successive setup_adc line overrides the previous
one...so only the last line has any effect _________________ Google and Forum Search are some of your best tools!!!! |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Sat Jul 20, 2013 1:38 pm |
|
|
OK! I corrected this! There is one more thing! The returned result of adc is with wrong justification. How can I fix this? The result is with left justification, but I think the normal is right.
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Jul 20, 2013 2:26 pm |
|
|
Specify the field width.
In C, if you use (say)
printf("%LD",value);
The value is printed using as many spaces as needed, starting from the left.
If instead you use
printf("%4LD",value);
then the result will be printed using four field positions, and 'right justified'.
If you think about it, how can the output 'right justify', unless it knows how 'wide' the number wants to be?.
Best Wishes |
|
|
|