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

how to setup A0 & A1 as analog and rest digital on 16F87

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



Joined: 11 Jan 2004
Posts: 45

View user's profile Send private message Visit poster's website

how to setup A0 & A1 as analog and rest digital on 16F87
PostPosted: Sun May 02, 2004 8:18 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun May 02, 2004 9:27 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sun May 02, 2004 10:44 pm     Reply with quote

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
PostPosted: Sun May 02, 2004 10:49 pm     Reply with quote

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. Wink
blak3r



Joined: 11 Jan 2004
Posts: 45

View user's profile Send private message Visit poster's website

PostPosted: Sun May 02, 2004 10:58 pm     Reply with quote

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 Very Happy
_________________
"Everything should be made as simple as possible, but not one bit simpler" -- Albert Einstein

http://www.blakerobertson.com
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