Menu

HI2C question - Is this allowed?

Help
MBB
2018-03-05
2018-03-07
  • MBB

    MBB - 2018-03-05

    I'm using version 0.98.01 with an 18F4550.

    This below code is not working.

    If var = 1 Then
    #define HI2C_BAUD_RATE 400
    End If
    '
    If var = 2 Then
    #define HI2C_BAUD_RATE 100
    End If

    Does GCB allow #define statements to be used like this?

     
  • Anobium

    Anobium - 2018-03-05

    You assumption   is correct. All #define are processed prior to ordering of conditions.

    You can use a script.

     
  • stan cartwright

    stan cartwright - 2018-03-05

    Dim var as word : var=400 : HI2C_BAUD_RATE var assembles if useful

     
    • Anobium

      Anobium - 2018-03-06

      This method WILL NOT work.

      ** Dim var as word : var=400 : HI2C_BAUD_RATE var assembles if useful**

      Why? the script that calculates the correct regiser settngs will NOT read the variable. You must use a Constant. I have not assessed the impact of using a script so I cannot comment. But, using a variable will fail to set the appropiate registers correctly.

       
    • Chris Roper

      Chris Roper - 2018-03-06

      It may not produce a compiler error but it certainly will not work.
      You are trying to access a runtime variable with a compile time statement.

      The preprocessor will ignore the undknown portion.
      That holds for all compilers not just GCBASIB.

      On Mon, 5 Mar 2018 11:33 pm stan cartwright, stanleyella@users.sourceforge.net wrote:

      Dim var as word : var=400 : HI2C_BAUD_RATE var assembles if useful

      HI2C question - Is this allowed?
      https://sourceforge.net/p/gcbasic/discussion/579126/thread/b515fe05/?limit=25#2a1f


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/gcbasic/discussion/579126/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
  • MBB

    MBB - 2018-03-05

    Thanks!

     
    • Anobium

      Anobium - 2018-03-06

      Pleasure.

      You can use a constant #define myI2CSpeed 100 then change use #define HI2C_BAUD_RATE myI2CSpeed

      Anobium

       
  • stan cartwright

    stan cartwright - 2018-03-06

    So you can redefine a contant with another constant not a variable and that would work with if then? Doesn't seem like if it's a #define.

     
    • Anobium

      Anobium - 2018-03-06

      It   is a   Define and   it works.

       
    • Chris Roper

      Chris Roper - 2018-03-07

      So you can redefine a contant with another constant not a variable and
      that would work with if then?

      You can't use if.
      You may be thinking of #ifdef
      That is a preprocessor derivative that works with #define
      But only in the preprocessor at compile time
      The #derivatives nolonger exist at runtime.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.