Port/TRIS as a parameter

General discussion on mikroPascal.
Post Reply
Author
Message
JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Port/TRIS as a parameter

#1 Post by JimKueneman » 04 Feb 2009 05:49

There are a number of libraries that pass a Port to allow the library to setup I/O pins. How is that done?

procedure Init(var Port: Byte; Input, Output: Byte);

I have tried several things and none have worked.

1) How do they get the TRIS out of the Port? Is the offset the same for all PICs?

2) How do you get from the passed var Port to what is passed in ClearBit? I am assuming you use the pointer with @Port but everything I have done has failed.

ClearBit(someTRIS, Output);
ClearBit(somePORT, Output);

Thanks,
Jim

BTW is there a library I could download to look at to learn how to do some of these basic things?

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Port/TRIS as a parameter

#2 Post by janni » 04 Feb 2009 17:38

JimKueneman wrote:There are a number of libraries that pass a Port to allow the library to setup I/O pins. How is that done?
As you have suggested, by passing port parameter by reference:

Code: Select all

procedure Init(var Port: Byte; Input, Output: Byte);
 var TRISptr: ^byte;
 begin
  TRISptr:=@Port+$12; // for PIC18s
  {TRISptr:=@Port or $80;} // for PIC16s
  TRISptr^:=Input and (not Output);
 end;
(assuming that Input has input bits set and Output has output bits set).

Naturally, you may pass directly TRIS instead of PORT:

Code: Select all

procedure Init(var Tris: Byte; Input, Output: Byte);
 begin
  Tris:=Input and (not Output);
 end;
1) How do they get the TRIS out of the Port? Is the offset the same for all PICs?
As above, because TRISx is equidistant from PORTx within processor family.
2) How do you get from the passed var Port to what is passed in ClearBit? I am assuming you use the pointer with @Port but everything I have done has failed.
ClearBit is an inline routine. Hard to say what compiler will need to do to clear the bit. It may just insert one assembly instruction (when both PORT and PIN are explicit), or use indirect addressing (through FSR). ClearBit(port,bit) is equivalent to port.bit:=0;

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

#3 Post by JimKueneman » 05 Feb 2009 14:48

Worked perfectly, thanks.

Jim

Post Reply

Return to “mikroPascal General”