View previous topic :: View next topic |
Author |
Message |
ILLIAS28
Joined: 11 Jan 2011 Posts: 42
|
Problem with PortA on 16F876 |
Posted: Sat Mar 17, 2018 5:06 pm |
|
|
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
|
|
Posted: Sat Mar 17, 2018 7:20 pm |
|
|
Did you disable the comparator module? _________________ David |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Mar 17, 2018 7:54 pm |
|
|
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: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Mar 17, 2018 8:07 pm |
|
|
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
|
|
Posted: Sun Mar 18, 2018 5:51 am |
|
|
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: 19538
|
|
Posted: Sun Mar 18, 2018 6:31 am |
|
|
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. |
|
|
|