Menu

#Include port direction directives.

Help
mkstevo
2016-11-17
2016-11-17
  • mkstevo

    mkstevo - 2016-11-17

    I have a working program that I was trying to tidy up, I thought I'd move some (all) of my #Define constants and port assignments into an #Include file. Having done this, the program compiles, but my 'Out' pins are being left in an ambiguous state. I'm not sure about the 'In' pins, I didn't test them.

    Moving all my 'Dir' directives back into the main program file corrects this and the program (and in/outs) then works as normal.

    'Defines header for JT
    
    'Pic 16F1829
    
    #Chip 16F1829, 32
    #Config CP=On
    'Read protected
    
    'LCD connection settings
    #Define LCD_IO 4
    #Define LCD_SPEED FAST
    #Define LCD_NO_RW
    
    'Port assignments
    #Define LCD_RS        PortA.0
    #Define LCD_Enable    PortA.1
    
    #Define LCD_DB4       PortA.2
    #Define LCD_DB5       PortC.0
    #Define LCD_DB6       PortC.1
    #Define LCD_DB7       PortC.2
    
    #Define Swallow_En    PortC.5
    #Define C_En          PortA.4
    #Define B_Out         PortA.5
    
    #Define Up_Sw         PortB.7
    #Define Dn_Sw         PortC.7
    #Define L_Sw          PortC.6
    #Define R_Sw          PortC.3
    #Define OK_Sw         PortC.4
    
    #Define T_En          PortB.6
    #Define T_Sen         PortB.5
    
    #Define B_Switch      PortB.4
    
    'Storage locations in Eeprom
    #Define E_Tick         0
    #Define E_Error        E_Tick + 2
    #Define E_StoreT       E_Tick + 4
    
    'Port directions
    Dir     Up_Sw         In
    Dir     Dn_Sw         In
    Dir     L_Sw          In
    Dir     R_Sw          In
    Dir     OK_Sw         In
    
    Dir     T_En          Out
    Dir     T_Sen         In
    
    Dir     B_Switch      In
    Dir     B_Out         Out
    Dir     C_En          Out
    Dir     Swallow_En    Out
    

    If the port directions are in the 'header' file, they fail to work.

    #Option Explicit
    'Pic 16F1829 
    
    #Include "JT_Defines.h"
    
    'Port directions
    Dir     Up_Sw         In
    Dir     Dn_Sw         In
    Dir     L_Sw          In
    Dir     R_Sw          In
    Dir     OK_Sw         In
    
    Dir     T_En          Out
    Dir     T_Sen         In
    
    Dir     B_Switch      In
    Dir     B_Out         Out
    Dir     C_En          Out
    Dir     Swallow_En    Out
    
    'Variables used
    Dim     Temp          As Byte
    Dim     T_Word        As Word
    Dim     T_Hi          As Byte
    Dim     T_Lo          As Byte
    Dim     SwitchValue   As Byte
    
    Dim     T_Error       As Byte
    Dim     Paid          As Word
    Dim     P_Value       As Word
    Dim     Rem_T         As Word
    Dim     T_Start       As Word
    
    Dim     Hund          As Byte
    Dim     Tens          As Byte
    Dim     Unit          As Byte
    
    Dim     Adjust        As Byte
    Dim     Blink         As Byte
    
    Dim     ErrorType     As Word
    Dim     Store_T       As Word
    

    If placed in the main file, below the #Include command, they do work.

    Ahhh... Just answered (partially) my own question. I see that the compiler doesn't 'Include' the file at the location of the #Include directive, rather it is added at the foot of the main program file. I can only assume this is the reason for the program's failure. I must imagine that doing so messes with the compilation order relating to the direction of the ports.

     
  • Hugh Considine

    Hugh Considine - 2016-11-17

    The best option here would be to put the Dir commands into a subroutine, say "InitJT". Then, add a startup directive to the .h file, like so:

    #startup InitJT
    

    Then, if any constants from the file are used, or any subroutines from it are called, GCBASIC will call that subroutine at the start of the program.

    The weird handling of #include and #define is something to look out for - unfortunately, there may not be a way of changing it that won't break many existing programs.

     
    • mkstevo

      mkstevo - 2016-11-17

      Many thanks for your suggestion. If I understand you correctly:

      I could place my Dir commands within a subroutine that is itself placed into the header file.
      Calling that sub with the #startup command, again, from within the header file?

      I'll try that now. One mo.

      Success! Thanks.

      'Defines header for JT
      
      'Pic 16F1829
      
      #Chip 16F1829, 32
      #Config CP=On
      'Read protected
      
      'LCD connection settings
      #Define LCD_IO 4
      #Define LCD_SPEED FAST
      #Define LCD_NO_RW
      
      'Port assignments
      #Define LCD_RS        PortA.0
      #Define LCD_Enable    PortA.1
      
      #Define LCD_DB4       PortA.2
      #Define LCD_DB5       PortC.0
      #Define LCD_DB6       PortC.1
      #Define LCD_DB7       PortC.2
      
      #Define Swallow_En    PortC.5
      #Define C_En          PortA.4
      #Define B_Out         PortA.5
      
      #Define Up_Sw         PortB.7
      #Define Dn_Sw         PortC.7
      #Define L_Sw          PortC.6
      #Define R_Sw          PortC.3
      #Define OK_Sw         PortC.4
      
      #Define T_En          PortB.6
      #Define T_Sen         PortB.5
      
      #Define B_Switch      PortB.4
      
      #StartUp SetPortDir   'Port directions cannot be set directly within
                                                      'include files but can be called within a sub.
      
      'Storage locations in Eeprom
      #Define E_Tick         0
      #Define E_Error        E_Tick + 2
      #Define E_StoreT       E_Tick + 4
      
      Sub SetPortDir
          'Port directions
          Dir     Up_Sw         In
          Dir     Dn_Sw         In
          Dir     L_Sw          In
          Dir     R_Sw          In
          Dir     OK_Sw         In
      
          Dir     T_En          Out
          Dir     T_Sen         In
      
          Dir     B_Switch      In
          Dir     B_Out         Out
          Dir     C_En          Out
          Dir     Swallow_En    Out
      End Sub
      

      The above placed into my header file works as expected. Many thanks for your assistance.

      Regarding the handling of #Include and #Define and Dir, I wouldn't want anyone thinking I'm complaining, I was just bemused!

       
  • Anobium

    Anobium - 2016-11-17

    Dear Bemused,

    There is no harm in asking.

    Have a scan through glcd.h for a really good example of the use of these capabilities.

     
  • Anobium

    Anobium - 2016-11-17

    And, with the v.0.95.010 compiler any variables defined in your header but not used will not be assigned and therefore not use any memory. This is stated in the Help but who reads the manual!

     
    • mkstevo

      mkstevo - 2016-11-17

      Thanks, I didn't know that. Now I've got help working (mostly) on my iMac, I can swot up!

       

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.