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

Problem with PortA on 16F876

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



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

Problem with PortA on 16F876
PostPosted: Sat Mar 17, 2018 5:06 pm     Reply with quote

Hi everybody,
Could someone please help me ? I am trying to use pin RA0 TO RA4 as digital outputs and RA5 as analog input on pic16f876, but when i read the analog input (RA5) my digital outputs are set to 0.
Heres a part of my code:
Code:

void main()
{
setup_adc_ports(PIN_A5);
setup_adc(ADC_CLOCK_INTERNAL );
SET_TRIS_A(0x20);
output_high(pin_A0);
output_high(pin_A1);
output_high(pin_A2);
output_high(pin_A3);
output_high(pin_A4);
set_adc_channel( 4 );
delay_us(20);
vbat=(((read_adc())*0.00438)*3.34821);

_________________
i am newbe
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Sat Mar 17, 2018 7:20 pm     Reply with quote

Did you disable the comparator module?
_________________
David
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 17, 2018 7:54 pm     Reply with quote

Quote:
setup_adc_ports(PIN_A5);

PIN_A5 is not a correct parameter for that function.

Look in the 16F876.h file, in the ADC section, to see what parameters
are allowed for that PIC. Hint: You will not be able to select pin A5 alone
with the 16F876. I suggest you change to the 16F886. Then you can do it.
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 17, 2018 8:07 pm     Reply with quote

OK.. please look at the ADC section of the datatsheet, the chart 11.2 . There isn't a configuration mode like you want, at least I can't see it.

some other points...

re:
setup_adc(ADC_CLOCK_INTERNAL );

Only valid for using ADC while PIC is SLEEPing according to the datasheet, if clock > 1Mhz.

this.
setup_adc_ports(PIN_A5);
appears to be incorrect according to the header file I have for the 876 or 876A.

You do not need to use set_tris()... the compiler will do that automatically for you !

You really need to post your complete program, we have no idea what fuses you've set, clock source and speed,etc.
As such we can't 'copy/paste/compile/test/report back' easily.
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

PostPosted: Sun Mar 18, 2018 5:51 am     Reply with quote

thank you very much for your help,
here the full code , i get no error when compiling. i only have this pic"16f876" is there any solution so i can use one analogue input and 5 digital output on porta.please help.

Code:
#include <16F876A.h>
#device ADC=10
#device PASS_STRINGS=IN_RAM
#fuses XT,NOWDT,PUT,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NODEBUG
#use delay(clock=4000000)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
float vbat;
void main()
{
setup_adc_ports(PIN_A5);
setup_adc(ADC_CLOCK_INTERNAL );
SET_TRIS_A(0x20);
output_high(pin_A0);
output_high(pin_A1);
output_high(pin_A2);
output_high(pin_A3);
output_high(pin_A4);
set_adc_channel( 4 );
delay_us(20);
vbat=(((read_adc())*0.00438)*3.34821);
}



_________________
i am newbe
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Mar 18, 2018 6:31 am     Reply with quote

Yes. With Port A0 as analog.

The select for this is:
setup_adc_ports(AN0);

This is the only 'single pin' selectable as an analog input on PortA on this PIC.

This is where reading the data sheet first, applies. Look at ADCON1 in the data sheet. PCFG3:0 (these are the bits that setup the ADC). There are just two lines in this that select one channel. One using separate Vref inputs, and one with the supplies as the Vref.
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