Sending and Initialising a port in a procedure

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Sending and Initialising a port in a procedure

#1 Post by bobspencerr » 16 Jun 2014 10:21

Hi all,
Can anyone tell me how to send a port to a procedure and then tristate it just like they do in the libraries for LCD_Init.

I have tried to create a procedure here but no matter what I do it fails or stalls the chip.
Any assistance greatly appreciated.

Code: Select all

InitialiseMotors(LatB, 1);

Var MotorPort : byte;

procedure InitialiseMotors(port_:byte; pin_:byte);
var tris_address, lat_address:^word;
begin
    MotorPort := @port_;
    tris_address := @MotorPort - 16;
    tris_address^.Pin_ := 0;
end;

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Sending and Initialising a port in a procedure

#2 Post by VCC » 16 Jun 2014 10:26

Hi,
I don't know if it helps, but try to use DWord addresses. :D

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: Sending and Initialising a port in a procedure

#3 Post by bobspencerr » 16 Jun 2014 10:30

Sadly No

I just tried this and it just locked the processor up again

Thanks for trying though
I appreciate it :D

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Sending and Initialising a port in a procedure

#4 Post by VCC » 16 Jun 2014 10:43

Not tested, but this is how I imagine it:

Code: Select all

procedure InitialiseMotors(port_: DWord; Pin_: Byte);
var 
  tris_address, lat_address: ^DWord;
begin
  tris_address := port_ - 16;
  tris_address^.Pin_ := 0;
end;

begin
  InitialiseMotors(@LatB, 1);
  repeat
  until False;
end.

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: Sending and Initialising a port in a procedure

#5 Post by bobspencerr » 16 Jun 2014 10:48

Thanks VCC but no go
The chip doesn't lock up now but the port is not being activated.

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: Sending and Initialising a port in a procedure

#6 Post by bobspencerr » 16 Jun 2014 10:48

Thanks VCC but no go
The chip doesn't lock up now but the port is not being activated.

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: Sending and Initialising a port in a procedure

#7 Post by bobspencerr » 16 Jun 2014 10:52

Got it
Thanks so much people for the help.

Code: Select all

Var MotorPort : dword;

procedure InitialiseMotors(port_:dword; pin_:dword);
var tris_address, lat_address:^dword;
begin
    MotorPort := port_;
    tris_address := port_ - 16;
    tris_address^.Pin_ := 0;
end;

begin
  InitialiseMotors(@PortB, 1);

bobspencerr
Posts: 78
Joined: 17 Jun 2008 13:38

Re: Sending and Initialising a port in a procedure

#8 Post by bobspencerr » 16 Jun 2014 11:10

Hmmm

I was too hasty.
It allowed me to assign the port inside the procedure but doesn't allow me to access the port through the name..

eg:

MotorPort.1 = 1;

This does however:

Code: Select all

Var MotorPort : ^dword;

procedure InitialiseMotors(port_:dword; pin_1, Pin_2:dword);
var tris_address, lat_address:^dword;
begin
    tris_address := port_ - 16;
    tris_address^.Pin_1 := 0;
    tris_address^.Pin_2 := 0;
    MotorPort := port_;
end;

begin
  InitialiseMotors(@PortB, 1, 3);
  while TRUE do
  begin
    MotorPort^ := not MotorPort^;    // Invert PORTB value
    Delay_ms(1000);
  end;
end.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: Sending and Initialising a port in a procedure

#9 Post by Dany » 21 Jun 2014 17:09

Hi, should you not use a "var" parameter for a port? By not doing so, you receive only a copy of the PortData, but you can not change it.

Example (for 8 bit PICs):

Code: Select all

var   LCD1602Port, LCD1602Tris: ^byte;
...
procedure LCD1602Init(var LCDPrt: byte);  // <------------- var parameter
// LCD initialization routine //
// Resets the LCD and initializes the LCD to 4 bit mode, 2 rows, cursor off
var I: byte;
begin
  LCD1602Port := @LCDPrt;
{$IFDEF P16}
  LCD1602Tris := LCD1602Port + 128;
{$ELSE}
  LCD1602Tris := LCD1602Port + 18;
{$ENDIF}
...
  LCDPrt := $20; // example of usage
  LCD1602Port^ := $20; // example of usage, same as above
  LCD1602Tris^ := 0; // example of usage
  LCD1602Tris^.3 := 1; // example of usage
  etc...
  
As you can see you can use the var parameter directly to get the port's value or to set it. One can also get the address of the port, simply using the "@" prefix (gives you a pointer to the address of the port).
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroPascal PRO for PIC32 General”