Hi,
is it possible (and what's the best way if any) to store physical pins references in SF Structures?
To be explainful
When we create sub/func, we can pass a pin ByRef and then let the procedure act on the pin.
I need to pass a set of pins to a procedure/function, and to make things clean, it would be nice to be able to store the ref to the pins in a structure and pass the structure to the sub/func.
Regards
Is it possible to store Pin ref in a structure field
Moderators: David Barker, Jerry Messina
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Is it possible to store Pin ref in a structure field
The library module shift.bas shows the definitions and usage of programmable pins...
The SetInput() and SetOutput() routines show how to setup the data structures, while Out() and In() show a bit of how to use those structures.
I would only do this if you really need to. Runtime programmable pins carry a LOT of overhead.
You have to be careful... a lot of older examples show adding +9 and +18 to the PORT address to get TRIS and LAT and that doesn't work for a number of newer devices. The LAT_OFFSET and TRIS_OFFSET macros in shift.bas show how to find the correct addresses of those registers
Even after that I recall there are a few devices/ports where this doesn't work (J94 PORTK???)
Code: Select all
// a pin structure...
structure TPin
PortAddr as word // PORT addr (input), LAT addr (output)
TrisAddr as word // TRIS addr
Pin as byte
PinMask as byte
end structure
I would only do this if you really need to. Runtime programmable pins carry a LOT of overhead.
You have to be careful... a lot of older examples show adding +9 and +18 to the PORT address to get TRIS and LAT and that doesn't work for a number of newer devices. The LAT_OFFSET and TRIS_OFFSET macros in shift.bas show how to find the correct addresses of those registers
Code: Select all
{
****************************************************************************
* Name : LAT_OFFSET/TRIS_OFFSET *
* Purpose : these macros load 'wresult' with the LAT and TRIS register *
* : offsets from PORT. they assume wresult is a 16-bit word *
* : located in the access bank (PROD is a good choice) *
****************************************************************************
}
macro LAT_OFFSET(wresult)
asm-
movlw low(LATA-PORTA)
movwf wresult
movlw high(LATA-PORTA)
movwf wresult+1
end asm
end macro
macro TRIS_OFFSET(wresult)
asm-
movlw low(TRISA-PORTA)
movwf wresult
movlw high(TRISA-PORTA)
movwf wresult+1
end asm
end macro
Re: Is it possible to store Pin ref in a structure field
Hi Jerry,
thank you for the answer. The offset thing (18 ...) is the main problem I was facing. I couldn't really find a clean way to avoid it to make things hardware independant.
We need POINTERs support in Swordfish BASIC ...
thank you for the answer. The offset thing (18 ...) is the main problem I was facing. I couldn't really find a clean way to avoid it to make things hardware independant.
We need POINTERs support in Swordfish BASIC ...
Re: Is it possible to store Pin ref in a structure field
I think Jerrys "multiple software I2C module" also shows some structure pin def examples..
https://www.sfcompiler.co.uk/wiki/pmwik ... User.PSI2C
Code: Select all
// a pin structure...
public structure TPin
PortAddr as word
Pin as byte
PinMask as byte
end structure
https://www.sfcompiler.co.uk/wiki/pmwik ... User.PSI2C
-
- Swordfish Developer
- Posts: 1473
- Joined: Fri Jan 30, 2009 6:27 pm
- Location: US
Re: Is it possible to store Pin ref in a structure field
You can't really make it truly hardware independent.I couldn't really find a clean way to avoid it to make things hardware independant.
The problem is that the PORTx, LATx, and TRISx registers change offsets depending on the device, so once you compile it for
one device another device won't work. Here are examples showing some of the different layouts I've run across...
Code: Select all
// "standard" addresses
D:\Swordfish\Includes\18F4520.bas
PORTA as byte absolute $0F80
LATA as byte absolute $0F89 // LAT = PORT + 9
TRISA as byte absolute $0F92 // TRIS = PORT + 18
D:\Swordfish\Includes\18F24K40.bas
PORTA as byte absolute $0F8D
LATA as byte absolute $0F83
TRISA as byte absolute $0F88
D:\Swordfish\Includes\18F24K42.bas
PORTA as byte absolute $3FCA
LATA as byte absolute $3FBA
TRISA as byte absolute $3FC2
D:\Swordfish\Includes\18F24Q10.bas
PORTA as byte absolute $0F8C
LATA as byte absolute $0F82
TRISA as byte absolute $0F87
// the 18F97J94 PORTK and PORTL have a different PORT/LAT/TRIS
// layout than the other ports
D:\Swordfish\Includes\18F97J94.bas
PORTK as byte absolute $0EE7
LATK as byte absolute $0EE8
TRISK as byte absolute $0EE9
That's why I had to replace the code in shift.bas with those macros to compute the offsets.
As far as port pins go, since the PIC itself has no mechanism for such a thing I don't know that there's a way to do.We need POINTERs support in Swordfish BASIC