View Full Version : accessing ports pins using an index variable
xnihilo
- 18th March 2008, 17:58
Hi there,
I need your help about using PBP pro with a PIC16F684.
I have to access portA pins in my program, using a variable as an index because I need to check what pin triggered the interrupt and then I need to poll the pin.
(let's assume the INT triggering port A pin is default high with weak pullups, it goes low when an int occurs and I jump to the below routine once I know what pin triggered the INT and assign that pin number to trigpin variable)
for i = 0 to 255
IF porta.trigpin = 1 then exitloop
PAUSEus 1000
next i
exitloop:
I know that PCPpro refuses to use porta.trigpin or porta[trigpin]
Thanks a lot
skimask
- 18th March 2008, 18:05
for i = 0 to 255
IF porta <> 0 then exitloop
pause 1
next i
exitloop:
if any pin on porta is set, exit the loop
xnihilo
- 18th March 2008, 18:38
Okays, thanks.
However it does not exactly meet my needs:
Not all pins will be triggered when they go from High to Low.
My program gets an IR signal on a sensor connected to a PORTA pin and this triggers an INT. In the int routine, I check what pin received the IR signal and then I poll the pin to get a signal that is encoded.
On pin RA5, I set no WPU because the default state will be 0V and once 5V gets to this pin, it triggers an INT.
(I'm developping software and hardware for a lasergame).
Anyway thank you very much for your help.
falingtrea
- 18th March 2008, 20:54
You could bitwise xor the read of port A with a saved previous state. Then changed bits would be set. Then you could use a SELECT CASE to do actions if a specific bit is set
casetest = portA ^ prev_state
prev_case = portA
SELECT CASE casetest
CASE casetest.BIT0
( do stuff for bit 0 changes)
CASE casetest.BIT1
(do stuff for bit 1 changes)
etc, etc.
Charles Linquis
- 19th March 2008, 00:55
If you just need to know which pin changed, you can
use something like -
DeltaPortA = OldPortA ^ PortA
If DeltaPortA > 0 THEN
OldPortA = PortA
BitThatChanged = NCD DeltaPortA
EndIF
BitThatChanged will have a number from 1-8, corresponding to the bit that changed.
If more than one changed, the highest-order bit that changed will be in the variable.
If you need to know ONLY if it went from LOW to HIGH, you can use
DeltaPortA = OldPortA ^ PortA
If DeltaPortA > 0 Then
OldPortA = PortA
DeltaLowToHigh = DeltaPortA & PortA
BitThatChangedH = NCD DeltaLowToHigh
EndIF
BitThatChangedH will contain the number (1-8) of the highest order bit that changed from low to high.
You can use the same technique for figuring out which one went from HIGH to LOW.
Jerson
- 19th March 2008, 05:04
I know that PCPpro refuses to use porta.trigpin or porta[trigpin]
I think you need to use PortA.0[trigPin]
there's an entire thread dealing with bit handling on PBP
flotulopex
- 19th March 2008, 07:12
Hello xnihilo,
You may find some ideas here: http://www.picbasic.co.uk/forum/showthread.php?p=44326#post44326
HTH
sayzer
- 19th March 2008, 09:29
If I am getting it right, this should be it.
But, this should not be this easy, should it?
I must be wrong then.
<font color="#000000">TRISA = <font color="#FF0000"><b>%11111
</b></font>MyPort <font color="#000080"><b>VAR </b></font>PORTA
Index <font color="#000080"><b>VAR BYTE
</b></font>Start:
<font color="#000080"><b>FOR </b></font>Index = <font color="#FF0000"><b>0 </b></font><font color="#000080"><b>TO </b></font><font color="#FF0000"><b>5 </b></font><font color="#000080"><i>' Scan MyPort (PORTA) pins.
</i><b>IF </b></font>MyPort.<font color="#FF0000"><b>0</b></font>[Index] = <font color="#FF0000"><b>0 </b></font><font color="#000080"><b>THEN BRANCH </b></font>Index,[IntA0,IntA1,IntA2,IntA3,IntA4,IntA5]
<font color="#000080"><b>NEXT </b></font>Index
<font color="#000080"><i>' Above routine is the same as
' IF MyPort.0 = 0 then IntA0
' IF MyPort.1 = 0 then IntA1
' IF MyPort.2 = 0 then IntA2
' IF MyPort.3 = 0 then IntA3
' IF MyPort.4 = 0 then IntA4
' IF MyPort.5 = 0 then IntA5
</i><b>GOTO </b></font>Start
IntA0:
<font color="#000080"><i>'Do your stuff or PORTA.0 here.
</i><b>GOTO </b></font>Start
IntA1:
<font color="#000080"><i>'Do your stuff or PORTA.1 here.
</i><b>GOTO </b></font>Start
IntA2:
<font color="#000080"><i>'Do your stuff or PORTA.2 here.
</i><b>GOTO </b></font>Start
IntA3:
<font color="#000080"><i>'Do your stuff or PORTA.3 here.
</i><b>GOTO </b></font>Start
IntA4:
<font color="#000080"><i>'Do your stuff or PORTA.4 here.
</i><b>GOTO </b></font>Start
IntA5:
<font color="#000080"><i>'Do your stuff or PORTA.5 here.
</i><b>GOTO </b></font>Start
<font color="#000080"><b>END
</b></font>
xnihilo
- 19th March 2008, 20:36
thank you very much for your help guys
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.