PDA

View Full Version : 16F877a PORTA ADC and digital help



RFsolution
- 21st October 2006, 22:39
Hi,

I was wondering if it would be possible to use PortA as analog but
use 1 pin as digital output ?

So:
RA0-RA3 analog 10bit
RA4 digital output
RA5 analog input 10bit

Can anyone help me with the ADCON settings ?

Further RE0-RE2 will be digital output

thanks

Learning everyday

sayzer
- 21st October 2006, 23:56
Hi RF,

Since RA4 is not an analog pin you get it as digital at default.

Then, the rest is as you needed. See the table below.
-----------------------------

<img src="http://img4.picsplace.to/img4/26/adc.GIF" alt="Image Hosting by PicsPlace.to" >

-----------------------------

RE0-RE2 meaning that AN5, AN6 and AN7 are needed as digital in your case.

Thus, you endup with
ADCON1.0=0
ADCON1.1=1
ADCON1.2=0
ADCON1.3=0

ADCON1.4=0 'ignore
ADCON1.5=0 'ignore

Then, assuming you want to use RC OSC,
ADCON1.6=0 '(here you should use DEFINE)

Then, you need 10-bit res.,

ADCON1.7=1 'right justified is needed with 10-bit. Left justified is needed with 8-bit.


You are set.

-------------------------------
Edit: Don't forget to set your TRIS registers for input/output thingie.

RFsolution
- 23rd October 2006, 11:20
Hi Sayzer,

It is working, thanks I start to understand the ADCON setting

Can you point me how Tris and internal pull-up works on a 877 ?

sayzer
- 23rd October 2006, 12:31
Sure.

TRISA = %00100000

Sets PORTA.5 as an input pin and all others in PORTA as output pin.

There is something here to pay attention for.
Although PORTA is 6-bit (RA0 to RA5), TRIS register uses 8-bit (0 to 7).
In this case, only right bits are considered. Thus, Bit7 and Bit6 in TRISA register are ignored ; whether they are 1 or 0 it won't matter.


TRISE = %00000000

Sets PORTE.0, PORTE.1 and PORTE.2 as output pins.

Also here, PORTE is only 3-bit (RE0 to RE2). So that, all other bits in TRISE register will be ignored.


You can also set an individual bit by,

TRISE.0=0

Sets RE0 as an output pin.

or

TRISE.0=1 sets RE0 as an input pin.


If you set a pin as analog, it must be an input pin.


For Internal Weak Pull-ups, there is a bit in OPTION_REG register.
Bit7.

OPTION_REG.7 is PORTB Pull-up Enable bit

OPTION_REG.7 = 1
PORTB pull-ups are disabled.

OPTION_REG.7 = 0
PORTB pull-ups are enabled by individual port latch values.

Once you set this bit as 0 then you can have weak pull ups on PORTB pins that were set to be input pins beforehand.

-----------------------

RFsolution
- 23rd October 2006, 13:08
Ok fine,

Understood, so PORTB has only the weak pull-up feature ?

another question concerning HSERIN

I'm using a 16f877 with max 232 at 4mhz

I'm having problems reading multiple variables in 1 serin string
It works, but after some times it hangs so I think there is a buffer overflow
What worries me is that when i measure with an osciloscope, there are pulses on portc.7 the UART RX even with no max232 chip connected ???

Here is a part of the code:

DEFINE HSER_RCSTA 90h ' enable serial port,
' enable continuous receive
'
define HSER_TXSTA 24h ' enable transmit,
' BRGH=1
'
define HSER_SPBRG 103 ' set baudrate to 2400
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

TRISB = %00000000
'OPTION_REG.7 = 0 ' Enable PORTB pullups
TRISC = %10000000
TRISD = %00001111
TRISE = %000

loop:
Hserin 500,LCD,[wait ("TAR"),dec3 var1,wait ("ANT"),DEC3 var2,wait ("AUX"),DEC3 var3,wait ("EXT"),DEC3 var4]


LCD:
pause 10
Lcdout $fe, 1 ' Clear screen

Lcdout "VAR1: ",#VAR1," VAR2: ",#var2

Lcdout $fe, $c0, "VAR3: ",#VAR3," VAR4:",#VAR4

Goto loop

Any Idea ?

sayzer
- 23rd October 2006, 15:13
Hi RF,

I do not have much experience in serin/serout commands.

I may mislead you with some incorrect or never tried advices.

But, at first look, I am not sure whether you can use more then one "WAIT" in one serin command or not.

I believe another member with valuable and reliable Serin/Serout experiences will respond.

----------------------------

earltyso
- 8th February 2008, 18:44
Hi,
I just found this thread as I was looking at how to set up 4 of the 13 availiable A2D converter pins on my 18F4620. Can someone clarrify for me......

If I want to set up AN0, AN1, AN4 & AN12 (8 bit resolution only) and leave every other pin on PORTA & B as digital input/ouputs.....

is this code correct?


ADCON1.0=0
ADCON1.1=0
ADCON1.4=0
ADCON1.12=0

'tristates will already be defined above