View previous topic :: View next topic |
Author |
Message |
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
ADC and INPUT pin |
Posted: Sat Jan 03, 2009 9:35 pm |
|
|
I'm new at this and I'm having a problem trying to make work the input pin and the ADC at the same time. Any idea why it is not working ? Here is the code. Thank you.
Code: | void main()
{
int pot_board;
Set_Tris_A(0b111111); //sets up port A as an input port
setup_adc( ADC_CLOCK_INTERNAL ); //sets up the adc
setup_adc_ports( ALL_ANALOG );
while(true) {
set_adc_channel(0); // data from port A0 ( pot from board )
delay_us(50);
pot_board=read_adc();
printf("%2x", pot_board);
//controls the h-bridge
output_d(0xf6);
while(Input(PIN_B3))output_d(0xf3);
while(Input(PIN_B2))output_d(0xfc);
}
} |
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Jan 03, 2009 9:51 pm |
|
|
Please tell us which PIC you are using and what compiler version.
You should not need the Set_Tris statement unless you are doing something exotic with the ports. The compiler usually handles the TRIS registers fine itself. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Guest
|
|
Posted: Sat Jan 03, 2009 9:53 pm |
|
|
SherpaDoug wrote: | Please tell us which PIC you are using and what compiler version.
You should not need the Set_Tris statement unless you are doing something exotic with the ports. The compiler usually handles the TRIS registers fine itself. |
I'm using the PICkit 2 the came with the MPlab IDE v8.10
and the pic16f887 debug board |
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Sat Jan 03, 2009 9:55 pm |
|
|
Anonymous wrote: | SherpaDoug wrote: | Please tell us which PIC you are using and what compiler version.
You should not need the Set_Tris statement unless you are doing something exotic with the ports. The compiler usually handles the TRIS registers fine itself. |
I'm using the PICkit 2 the came with the MPlab IDE v8.10
and the pic16f887 debug board |
I forgot to say it is the CCS compiler
thanx |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 04, 2009 1:03 pm |
|
|
If you use CCS i/o functions (which you are doing), you don't need to
set the TRIS. The compiler will do it for you.
You are reading two pins on Port B. What do you have connected to
these two pins ? Describe the circuit. If it's a chip, post the
manufacturer and part number. |
|
|
Guest
|
|
Posted: Sun Jan 04, 2009 1:14 pm |
|
|
PCM programmer wrote: | If you use CCS i/o functions (which you are doing), you don't need to
set the TRIS. The compiler will do it for you.
You are reading two pins on Port B. What do you have connected to
these two pins ? Describe the circuit. If it's a chip, post the
manufacturer and part number. |
I'm using an LM339, that is connected to a couple of infrared sharp GP2d12 but it works fine if I ignore the ADC code and the ADC code works fine also if I ignore the input pins but they dont want to work together :(
any ideas? thanx |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 04, 2009 1:51 pm |
|
|
Quote: | void main()
{
int pot_board;
Set_Tris_A(0b111111); //sets up port A as an input port
setup_adc( ADC_CLOCK_INTERNAL ); //sets up the adc
setup_adc_ports( ALL_ANALOG ); |
The 16F887 data sheet shows that this PIC has analog pins on Port B
and on Port A. You have picked "All analog", which makes all "ANx"
pins be analog pins. That's why you have problems with digital input
on port B. Look in the 16F887.h file to find the constant which will enable
only AN0 as an analog pin and leave the rest of them as digital.
Look in this section:
Quote: | // Constants used in SETUP_ADC_PORTS() are: |
Here's the file location:
Quote: | c:\program files\picc\devices\16f887.h |
|
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Mon Jan 05, 2009 8:42 am |
|
|
Thank you so much PCM programmer. that works, can't belive it was thats simple, |
|
|
|