View Full Version : SerOut2 Dynamic Port Selection
nba30
- 22nd March 2005, 23:55
I am connecting 15 serial devices to a 40 pin PIC (18F452) and I was wondering if there was a way to have a variable number: 1-15 select which port the serial data is sent to?
For example:
-------------------
SELECT CASE address
CASE 1
port var portd.0
CASE 2
port var portd.1
CASE 3
port var portd.2
CASE ELSE
goto error
END SELECT
SerOut2 port,baud,["output string",10,13]
-------------------
Where "address" is a variable byte that stores a decimal number.
This example will not compile since you are not allowed to declare an alias more than once.
I also tried a LOOKUP statement but I cannot store the port addresses (or alias) into variable bytes or words. I also thought about using the address variable and the Pin numbers but since I am dealing with a larger PIC, I cannot access all of this pins with this command.
Any ideas on how to do this without making a routine for each port that I need to read/write to?
THANKS!
anj
- 23rd March 2005, 03:38
Look at the thread "Can you make name variables" a few days ago
1419
ie
port var porta.0
....
case X = 1
X = 2
...
SerOut2 port[X],baud,["output string",10,13]
I know this port referencing method doesnt always work with all commands, but its worth a try
Andrew
mister_e
- 23rd March 2005, 05:05
i agree with the previous can work in some case BUT can you modify or use the PBPs defaults pin number.... see section 4.11 of your PBP manual.
nba30
- 23rd March 2005, 05:54
Thanks for the ideas guys. I will give them a shot tomorrow.
I re-read the section in the manual about pin numbers and according to the table, I can only address PortB (0-7) and PortC (8-15). While that does give me 16 pins to work with, I really need to use the interupt pins RB.0-2.
We'll see how it goes...
Thanks again
Ingvar
- 23rd March 2005, 09:17
You can address each pin on the pic by offsetting from Porta.0, the syntax is PortA.0(index). Index 0-7 accesses PortA0-7, index 8-15 accesses PortB0-7, index 16-23 accesses PortC0-7 and so on.
' address 0 1 2 3 4 5 6 .....
' a0 a1 a5 b0 b1 b5 b7 .....
Lookup address,[00,01,05,08,09,13,15 .....],SerPin
SerOut2 PortA.0(SerPin),baud,["output string",10,13]
nba30
- 23rd March 2005, 15:28
It seems like the Lookup function along with PORTA.0(serpin) should work, but the only thing it does is output on pins RB0 and RB1.
Even if I write a simple program to turn on/off the pins, it still keeps addressing pins RB0 and RB1 only.
Am I missing something?
Thanks.
<div class="smallfont" style="margin-bottom:2px">Code:</div>
<pre class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:98px; overflow:auto"><div dir="ltr" style="text-align:left;">
B0 VAR BYTE
serpin VAR BYTE
loop:
For b0 = 0 TO 7
LookUp b0,[0,1,2,3,4,8,16,24],serpin
High PORTA.0(serpin)
Pause 500
Next b0
GoTo loop
<div>
passstill
- 10th April 2005, 11:37
Hi,
I tried to use the code above but it dosen't work, I'm using PBP 2.45,
does anyone could give me any tips about it?
Ciao
Alan
Joka kuritta kasvaa se kunniatta kuole
You can address each pin on the pic by offsetting from Porta.0, the syntax is PortA.0(index). Index 0-7 accesses PortA0-7, index 8-15 accesses PortB0-7, index 16-23 accesses PortC0-7 and so on.
' address 0 1 2 3 4 5 6 .....
' a0 a1 a5 b0 b1 b5 b7 .....
Lookup address,[00,01,05,08,09,13,15 .....],SerPin
SerOut2 PortA.0(SerPin),baud,["output string",10,13]
Melanie
- 10th April 2005, 18:50
Careful Ingvar...
It only works on a very few PICs in a very few cases...
It does NOT work across all PICs and neither does it work against variants of the same PIC or versions of PICBasic.
I actually withdrew a posting I made about a year ago detailing the very same thing when I discovered that in all probability the chances of it NOT working are greater than the chances of success.
The only guaranteed things to work are the sixteen pins (0-15) as detailed in the manual, and then they are PIC type dependant (as per the table in the manual).
anj
- 11th April 2005, 08:08
Gday Mel
I have been watching this post with interest.
I know the array id method listed works for 16F876s 16F84s and 16F88s,
but looking at the datasheets Ports A,B and C are all contiguous in memory
ie x05, x06 etc.
on 12/3 this year PeterDeco posted re a similar problem, and whilst misinterpreting the post, tried portA.0 + X rather than portA.0(X), and said this also worked.
Based on the fact "PortA" just gets referenced to its memory locn via the include files, say x05 in a 16f88, is it possible ( knowing the memory map for the specific pic/variant ), to use a calculated reference point, rather than assume the bits are contiguous and ref by array practices.
Basically, what i am saying is do the functions that "expect a port" reference need it to come in as a standard symbol, or would the correct memory offset ( as a variable ) suffice.
I cant test, as i dont know how to make it fail currently ( under the old method ) with the chips i normally play with.
Andrew
Melanie
- 11th April 2005, 10:57
I spent quite a while (at least for me) on this last year.
I concluded that if it can't work across all PICs and all versions of PICBasic consistantly everytime, it shouldn't be used.
The only ways I address PIC pins is by aliasing the pin or by using the direct numeric (0-15).
The offset addressing method works for RAM and I've detailed this in the FAQ section, but I cannot reliably apply the same to Port Addressing. I have not redone my tests on V2.45 which had not been released at that time.
Ingvar
- 11th April 2005, 17:27
Bummer, i 've only used it once on ports and it worked just as expected. Good to know that it might not the next time .......
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.