Query about Module inclusion by reference or define.

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Senacharim
Posts: 139
Joined: Tue Aug 10, 2010 5:19 pm
Location: Ventura, CA

Query about Module inclusion by reference or define.

Post by Senacharim » Tue Nov 30, 2010 4:33 pm

Hi!
I'm working up a little "home-brew" ftp module. The issue I'm running into is I'm constructing it for two different PICs. (the 18F2620 and the 18F25K22).

One of these PICs has two USARTs (which is really cool).

So, I'm trying to set up a #define to set whether it's using USART1 or USART2, but I'm having trouble getting it to work. Thing is, I'm pretty sure it's doable, and I'm just lacking the syntax.

Here's what I've tried (unsuccessfully)

Code: Select all

#ifndef FTP
  #define FTP

Module FTP


#if FTP_USART = USART
    Include "USART.bas"
#elseif FTP_USART = USART2
    Include "USART2.bas"
#else
    #error FTP_USART, "Define a _VALID_ USART!!"
#endif
'the part above works fine

'the part below gives me grief, sometimes by throwing odd errors from the compiler in other, entirely unrelated parts of the code which aren't even in the module.

'--------------------------------=-=---------------------------------------
Public Function CommTimeout() As Boolean
'--------------------------------=-=---------------------------------------
  Dim
    TimeOut As Word
'---------------=-=---------------
    TimeOut = 50000 '1/4 second in 10us bites.
    If (FTP_USART.OERR = 1) Then FTP_USART.ClearOverrun() EndIf
    While (FTP_USART.RCIF = 0 And TimeOut > 0)
        Dec(TimeOut)
        DelayUS(5)
    Wend
    If (TimeOut > 0) Then
        CommTimeout = true
    Else
        CommTimeout = false
    EndIf
End Function

#endif
Any help appreciated. Is what I'm attempting possible, or is my reach exceeding my grasp?

Thanx in advance.
Surviving Member
Bermuda Triangle Battalion
from 2026 to 1992

Voted "Most likely to time travel"--Class of 2024.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Nov 30, 2010 9:53 pm

I don't think that works out very well... using #defines or #options to try and do text substitution.

What I've done in the past is to create a new module that has alias names for all the publics in the two modules I want to select from, and just use that new module

ftp_usart.bas

Code: Select all

module FTP_USART

#if FTP_USART_ = 1
#warning "using USART"
include "USART.bas"
// alias public bitnames to RCSTA(x)...
Public Dim
   OERR As usart.OERR
   
// alias public interrupt flags...
Public Dim
   RCIF As usart.RCIF,
   TXIF As usart.TXIF,
   RCIE As usart.RCIE,
   TXIE As usart.TXIE,
   RCIP As usart.RCIP,
   TXIP As usart.TXIP
   
// public boolean flags...
Public Dim  
   DataAvailable As usart.DataAvailable,
   ReadyToSend As usart.ReadyToSend,
   ContinousReceive As usart.ContinousReceive,
   Overrun As usart.Overrun,
   FrameError As usart.FrameError

// public subs and functions
public dim
    SetBaudrate as usart.SetBaudrate,
    ClearOverrun as usart.ClearOverrun,
    ReadByte as usart.ReadByte
    

#elseif FTP_USART_ = 2
#warning "using USART2"
include "USART2.bas"
Public Dim
   OERR As usart2.OERR
   
// alias public interrupt flags...
Public Dim
   RCIF As usart2.RCIF,
   TXIF As usart2.TXIF,
   RCIE As usart2.RCIE,
   TXIE As usart2.TXIE,
   RCIP As usart2.RCIP,
   TXIP As usart2.TXIP
   
// public boolean flags...
Public Dim  
   DataAvailable As usart2.DataAvailable,
   ReadyToSend As usart2.ReadyToSend,
   ContinousReceive As usart2.ContinousReceive,
   Overrun As usart2.Overrun,
   FrameError As usart2.FrameError

// public subs and functions
public dim
    SetBaudrate as usart2.SetBaudrate,
    ClearOverrun as usart2.ClearOverrun,
    ReadByte as usart2.ReadByte
    

#else
    #error FTP_USART, "Define a _VALID_ USART!!"
#endif

end module
ftp.bas

Code: Select all

Module FTP

// 1 or 2
#define FTP_USART_ = 2

include "ftp_usart.bas"

'--------------------------------=-=---------------------------------------
Public Function CommTimeout() As Boolean

  Dim TimeOut As Word
    
    TimeOut = 50000 '1/4 second in 10us bites.
    If (FTP_USART.OERR = 1) Then FTP_USART.ClearOverrun() EndIf
    While (FTP_USART.RCIF = 0 And TimeOut > 0)
        Dec(TimeOut)
        DelayUS(5)
    Wend
    If (TimeOut > 0) Then
        CommTimeout = true
    Else
        CommTimeout = false
    EndIf
End Function

end module
It's a bit of work, and you'll have to add whatever flags/routines you want to use in both sections of the declarations in ftp_usart.bas, but I've never come across another way to do it.

User avatar
Senacharim
Posts: 139
Joined: Tue Aug 10, 2010 5:19 pm
Location: Ventura, CA

Post by Senacharim » Tue Nov 30, 2010 10:18 pm

Tedious a solution it may be, but it IS a solution.

Thank you, I shall apply it.
Surviving Member
Bermuda Triangle Battalion
from 2026 to 1992

Voted "Most likely to time travel"--Class of 2024.

Post Reply