PDA

View Full Version : Partial Digital PORTA?



actionplus
- 1st March 2004, 16:12
I am currently using PORTA.0 to PORTA.2 as analog inputs.
Is there anyway to get PORTA.3 on a PIC16F877 to digital?

This is my current setting
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog

I am trying to use this PORTA.3 as an input from a optical sensor that I place on a spinning disk. You guessed it. Try to build myself a tachometer.

Currently, I have all kinds of numbers poping up on the LCD when I make one pass. I get numbers from 2,7,10,47 depending on how long it takes to pass the reflective surface. Maybe I am not giving the PIC a clean signal to look at.

Any remedies?

Thanks
Randy

languer
- 1st March 2004, 23:57
Randy,

From the datasheet I was able to gather the attached table. It looks from your statement that you are selecting A5 plus A3-A0 as analogs inputs (the whole port A), and E2,E1,E0 as digital inputs. It also looks like you can not have A2-A0 as analogs and A3 as digital.

The closest thing you could have is A3,A1,A0 as analogs and A5,A2 as digital. This would read:

ADCON1 = %10000100

Hope this helps.

actionplus
- 5th March 2004, 16:04
Will
ADCON1=%10000101 give me PORTA.0 and PORTA.1 analog and PORTA.2 and PORTE's digital?

languer
- 5th March 2004, 19:49
It should. Note that this configuration will also make PORTA.3 the A2D reference, so you must supply a voltage reference to this pin.

actionplus
- 5th March 2004, 20:44
Do you how to get the Schmitt trigger running on the PIC16F877A.
It is on the PORTA.4.

tHanks
Randy

languer
- 6th March 2004, 06:24
It should be set by default. The pin, when used as digital, has the schmitt trigger as input. The output is however open-drain (i.e. it needs a pull-up if you want to use it).

From the datasheet:

"Pin RA4 is multiplexed with the Timer0 module clock
input to become the RA4/T0CKI pin. The RA4/T0CKI
pin is a Schmitt Trigger input and an open-drain output."

The pin is also shared as a comparator output, but by default it is off:

"On a Power-on Reset, these pins are configured as analog inputs and read as ‘0’. The comparators are in the off (digital) state."

Hope it helps.

actionplus
- 6th March 2004, 19:34
Thanks, you have been very helpful

Here is what I have setup at the moment

I have the optial sensor(phototransistor OPB703 from Optek) connected to PORTA.4.

Count PORTA.4,100,counts
rpm=counts*600

Do I have this right? Because I still get bunch of numbers when I pass the reflective surface. The sensor picks up the signal the count keeps adding "counts" rather than counting 1 at each pass.
Any clue why?


Now, since the schmitt trigger is set automatically when it is digital and you said that I need a extrenal resistor at the output.

What I don't understand is that, I am thinking that I am using the PORTA.4 as an input for the schmitt trigger. Where will the output be for this then?

Thanks
Randy

p/s-I guess you can see that I am really new at this.

languer
- 8th March 2004, 20:37
Disclaimer:
1) I have never done a tachometer...
... although I have done frequency counters.

2) There is no such things as being new at something, it is only a degree of knowledge (and it varies from day-to-day, coffee anyone?). Questions are extremely good.

First let me try the port question.
<ul>If you are only using the RA4 pin as an input, then you have nothing to worry about (it is only if you wanted to use it as an output that you need an external pull-up).</ul>

Next let me try your application-specific question.
<ul>The math used is good (i.e. you will get RPMs out of the equation).
Two things come to mind, what are you trying to measure (i.e. what are the limits of your measurements)?. For example, on a 4MHz clock, the COUNT function has a 25kHz max frequency. This should be enough to measure 150,000RPM with your 100mS gate (if I did the math correct). By the same token the minimum you could measure would be 600RPM, and your resolution would be 600RPM (or 1 count). To achieve better resolution you have to sample longer (i.e. 1s, 10s, etc.). Problem is that if your maximum RPM is very high you are quickly getting into 32bit numbers. As it is 150,000RPM (rocket anyone?) will require an 18bit number.
Within reason, you could sample longer and get better results (since, in my mind, you may be sampling 1,000 to 20,000RPM). What other people have done is use the PULSIN function to measure one pulse (assumption made -> 50% duty cycle), you could even average a few pulses to get a more stable measurement (I would use a median-type filter/average). I've attached a few links of examples:

http://list.picbasic.com/forum/messages/4011/4360.html
http://list.picbasic.com/forum/messages/20/1045.html

Now lets try to tackle your problem at hand. You say that your numbers are jumping all over the place:
1. Do you look at the 'counts' number or the 'RPM' number? I presume you have declared both as WORDs. The 'counts' number is a better indicator of your measurement since the 'RPM' could overflow a WORD variable.
2. How is the sensor information applied to the COUNT pin? I am sure you need some signal conditioning before applying this signal into the PIC since you may have double-counts or things of that nature (i.e. of analog nature).
</ul>

Hope this leads you to some answers (more likely more questions - that's almost always the case, but then how would you learn if it were not for the questions).

actionplus
- 9th March 2004, 07:47
Thank you for your explainations. You are getting to be very informative.

Here is a few more information

RPM
Min?Max-3000rpm
What would the best xtal I should use? I am currently running a 10MHz 18pF xtal.

Signal conditioning.
None at the moment. I have the sensor directly connected to PORTA.4.
I think I should be thinking of cleaning up the signal. Maybe a LM3??, forgot which one. Shouldn't be to hard to find out. I have in paper somewhere.

Another question I have is that, does the PIC have the capability of Input Capture of either raising edge of a signal or falling edge?
I will try to look it up. Thought I will ask you if you happen to know of the top of your head.

Thanks again
Randy