View previous topic :: View next topic |
Author |
Message |
blak3r
Joined: 11 Jan 2004 Posts: 45
|
how to setup A0 & A1 as analog and rest digital on 16F87 |
Posted: Sun May 02, 2004 8:18 pm |
|
|
I want to configure port A as follows:
RA0 = ADC (input)
RA1 = ADC (input)
RA2 = Digital Output
RA3 = Digital Output
RA4 = Digital Input
RA5 = Digital Output
Below i pasted the Analog Defines from the 16f876.h file.
There isn't a define for just having RA0 and RA1 as analog and the rest digital.
What is the best way to go about doing this?
I really haven't come across a comprehensive reference on how to setup the ports using the compiler if someone know a good reference i'd appreciate it.
////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
// SET_ADC_CHANNEL(), READ_ADC()
// Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS 0x86 // None
#define ALL_ANALOG 0x80 // A0 A1 A2 A3 A5 E0 E1 E2 Ref=Vdd
#define ANALOG_RA3_REF 0x81 // A0 A1 A2 A5 E0 E1 E2 Ref=A3
#define A_ANALOG 0x82 // A0 A1 A2 A3 A5 Ref=Vdd
#define A_ANALOG_RA3_REF 0x83 // A0 A1 A2 A5 Ref=A3
#define RA0_RA1_RA3_ANALOG 0x84 // A0 A1 A3 Ref=Vdd
#define RA0_RA1_ANALOG_RA3_REF 0x85 // A0 A1 Ref=A3
#define ANALOG_RA3_RA2_REF 0x88 // A0 A1 A5 E0 E1 E2 Ref=A2,A3
#define ANALOG_NOT_RE1_RE2 0x89 // A0 A1 A2 A3 A5 E0 Ref=Vdd
#define ANALOG_NOT_RE1_RE2_REF_RA3 0x8A // A0 A1 A2 A5 E0 Ref=A3
#define ANALOG_NOT_RE1_RE2_REF_RA3_RA2 0x8B // A0 A1 A5 E0 Ref=A2,A3
#define A_ANALOG_RA3_RA2_REF 0x8C // A0 A1 A5 Ref=A2,A3
#define RA0_RA1_ANALOG_RA3_RA2_REF 0x8D // A0 A1 Ref=A2,A3
#define RA0_ANALOG 0x8E // A0
#define RA0_ANALOG_RA3_RA2_REF 0x8F // A0 Ref=A2,A3
// Constants used for SETUP_ADC() are:
#define ADC_OFF 0 // ADC Off
#define ADC_CLOCK_DIV_2 1
#define ADC_CLOCK_DIV_8 0x41
#define ADC_CLOCK_DIV_32 0x81
#define ADC_CLOCK_INTERNAL 0xc1 // Internal 2-6us
// Constants used in READ_ADC() are:
#define ADC_START_AND_READ 7 // This is the default if nothing is specified
#define ADC_START_ONLY 1
#define ADC_READ_ONLY 6 _________________ "Everything should be made as simple as possible, but not one bit simpler" -- Albert Einstein
http://www.blakerobertson.com |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sun May 02, 2004 9:27 pm |
|
|
I don't think you can, since the PIC itself doesn't support it. Take a look at page 114 of the datasheet.
But there are ways around it. For example you can use analog/digital mux. Or if your input doesn't change too fast, make RA3 a digital input and RA4 a digital output instead, then use RA0_RA1_RA3_ANALOG. This way you still can read RA3, even though you'll need to use ADC functions to read the pin. |
|
|
blak3r
Joined: 11 Jan 2004 Posts: 45
|
|
Posted: Sun May 02, 2004 10:44 pm |
|
|
Wow... i think you're right.
When you read the I/O ports section of the datasheet it doesn't give any indication that you can't do any configuration.
Also, i had experience configuring the analog ports on the 16f676 and you can do ORing to determine the pins which are analog. So, i figured there was probably a similar way todo this on the 876.
Thanks _________________ "Everything should be made as simple as possible, but not one bit simpler" -- Albert Einstein
http://www.blakerobertson.com |
|
|
guest Guest
|
A3 as analog input and _also_ digital output |
Posted: Sun May 02, 2004 10:49 pm |
|
|
All you need is the following:
setup_adc_ports( RA0_RA1_RA3_ANALOG );
setup_adc ( ADC_CLOCK_DIV_32 );
set_tris_a ( 0b00010011 ); // A0, A1, A4 for input, others output
while (1)
{
output_low ( PIN_A3 ); delay_ms(100); // output_xxx() automatic set TRIS for you
output_high ( PIN_A3 ); delay_ms(900);
}
Do some experiment, it only took me 10 minutes. |
|
|
blak3r
Joined: 11 Jan 2004 Posts: 45
|
|
Posted: Sun May 02, 2004 10:58 pm |
|
|
interesting...
So, you tested the output on RA3 and it worked?
I'm coding from home today. I don't have my eval board. I'll experiment tommorow though _________________ "Everything should be made as simple as possible, but not one bit simpler" -- Albert Einstein
http://www.blakerobertson.com |
|
|
|