Aliasing a Port pin as boolean

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
dman776
Posts: 115
Joined: Mon May 28, 2007 3:59 pm
Location: Texas

Aliasing a Port pin as boolean

Post by dman776 » Fri Apr 25, 2008 3:10 am

I have the following code in a module...

Code: Select all

#option VS_DREQ = PORTD.0
Dim DREQ As VS_DREQ.VS_DREQ@
However, I would like to alias the DREQ variable as a Boolean (ie. if the port.pin is high, return TRUE, if low, return FALSE).

like this:

Code: Select all

if DREQ then
  // do stuff
endif
Is this possible without wrapping this in a method call?[/code]

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Fri Apr 25, 2008 4:15 am

I can't think of a way to do what you require. Sorry.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Fri Apr 25, 2008 5:02 am

Hi,
You can do it by aliasing the Port bits as booleans.

Code: Select all


#option VS_DREQ = PORTD     '**** ALIAS TO THE PORT ONLY
#option VS_DREQ_SELECTED_BIT = 0 '**** ALIAS TO THE Bit Number

Dim DREQ As VS_DREQ.Booleans(VS_DREQ_SELECTED_BIT)  '***** Bit number can be changed from the option

//***** EXAMPLE 

Dim i As Byte

DREQ = true

If DREQ Then 
    Inc(i) 
EndIf

I did not tried the code altought it seems to compile fine and generated code seems correct

Code: Select all

        BRA MAIN
MAIN
?I000000_F000_000020_M000000 ; L#MK DREQ =TRUE
        BSF LATD,0,0
?I000001_F000_000022_M000000 ; L#MK IF DREQ THEN
        BTFSC PORTD,0,0
?I000002_F000_000023_M000000 ; L#MK INC(I)
        INCF M0_U08,1,0
ENDIF_0
Please let us know if it work for you.

Regards
octal

dman776
Posts: 115
Joined: Mon May 28, 2007 3:59 pm
Location: Texas

Post by dman776 » Fri Apr 25, 2008 2:59 pm

Thanks guys.
I ended up writing a function...

Code: Select all

Public Function IsReady() As Boolean
   Dim vPort As Word
   Dim vPin As Byte
   vPort = AddressOf(DREQ)
   vPin = BitOf(DREQ,false)              // get pin number
   Result = vPort.Booleans(vPin)
End Function
thanks for the confirmation...

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Fri Apr 25, 2008 7:32 pm

Hi dman776,
using a function you add the overhead due tu the function code itself, and the overhead due to call to the function since it's not inline (inlining it adds the size overhead due to the duplication of its code).
Using a function does not let you affect the value TRUE/FALSE to the PIC pin to set the pin in HIGH/LOW state.

If you want to use a single OPTION to manage the PortPin you want, you can still use the folowing solution (Thanks to David)

Code: Select all


#option VS_DREQ = PORTD.7
Dim ThePort As VS_DREQ
Dim DREQ As ThePort.Booleans(VS_DREQ@)

Best regards
octal
Last edited by octal on Fri Apr 25, 2008 7:37 pm, edited 1 time in total.

dman776
Posts: 115
Joined: Mon May 28, 2007 3:59 pm
Location: Texas

Post by dman776 » Fri Apr 25, 2008 7:33 pm

SWEET! That's the magic I was looking for!

Thanks octal and David!

Post Reply