Menu

Pass bit variable to Sub

ToniG
2022-06-27
2022-06-28
  • ToniG

    ToniG - 2022-06-27

    How do I pass a bit var to a subroutine ?

    I thought I had it working but I had a typo & was just referencing the source variable from the main. (reproduced in following code)

    Capturing port pins on logic analyzer...

    #chip 12F683, 8
    #config Osc = Int
    #Define DIO1 GPIO.0
    #Define DIO2 GPIO.1
    Dir DIO1 Out
    Dir DIO2 Out
    
    
    Dim SEGon as Bit
        DIGn = 2
        SEGn = 3
        SEGon = Off
        wait 500 us
    
    Main:
      Test1(DIGn, SEGn, SEGon)
      DIO1 = SEGon
      DIO2 = Tmp_1.SEGn
    
      SEGon = !SEGon    'Toggle
      wait 50 us
    Goto Main
    End
    
    Sub Test1(In Dig_n, In Seg_n, In SEG_on as bit)
    ''    Dim SEG_on as Bit
    
        Tmp_1.SEG_n = SEG_on        '< Works Not
    '    Tmp_1.SEG_n = SEGon      '< Works ok (bitVar from main not the passed bit state)
    
    
    End Sub
    

    I have the same issue if I pass a bit value.

    Test1(DIGn, SEGn, On)
      wait 50 us
      Test1(DIGn, SEGn, Off)
      wait 50 us
    
     

    Last edit: ToniG 2022-06-27
  • Anobium

    Anobium - 2022-06-27

    Use a Macro to pass a register.bit value.

     
  • ToniG

    ToniG - 2022-06-27

    I am not using registers, the pins being set here are just for testing with simulator.

    I have a main program with a "command" that calls the Sub(include file) with Parameters, the 3rd Param is the state of the bit 0 or 1. The bit being set is in an array.
    & I had another issue that was masking somewhat the main problem...

    Anyway I have it working,, in main code I didn't have In SEG_on as Bit & the sub was reading a byte?
    The test code here still doesn't work but I will put that aside. (too tired to think now)

     
  • Anobium

    Anobium - 2022-06-27

    When I meant register.bit value that means port.bit value, register.bit value or variable.bit value.

    As in

      myPulseout portb.1, 100 ms
    
        'myPulseOut Macro
        macro myPulseout (Pin, Time)
            Set Pin On
            Wait Time
            Set Pin Off
        end macro
    
     
  • William Roth

    William Roth - 2022-06-27

    I am not having trouble passing the variable from the sub back to main. The code below is a modified version of the original code. Modified to DIM some undeclared variables and added a 2nd Tmp Variable.

    Both LEDS toggle as expected. This shows that the variables are being returned to the Main routine.

    I am using GCB Build 1132 with an 18F25K22

    #chip 18f25K22, 8
    #OPTION Explicit
    #CONFIG MCLRE = ON
    
    #Define DIO1 PORTC.1   // Gn LED
    #Define DIO2 PORTC.2   //RED LED
    
    Dir DIO1 Out
    Dir DIO2 Out
    
    DIM DIGn, SEGn, TMP_1,TMP_2
    Dim SEGon as Bit
    
    DIGn = 2
    SEGn = 3
    SEGon = OFF
    
    Wait 200 ms
    
    Main:
        Test1(DIGn, SEGn, SEGon)
    
        DIO1 = Tmp_1.SEG_n    // SEG_n Returned from SUB
        DIO2 = TMP_2.SEG_n
    
        SEGon = !SEGon    'Toggle Bit
    
        Wait 200 ms 
        Goto Main
    
    Sub Test1(In Dig_n, In Seg_n, In SEG_on as bit)
    
        Tmp_1.SEG_n = SEG_on    
        Tmp_2.SEG_n = SEG_on
    
    End Sub
    
     

    Last edit: William Roth 2022-06-27
  • William Roth

    William Roth - 2022-06-27

    A problem is that I don't think the following is supported

    var1.var2  = var3
    

    For Example:

    Dim ByteVar
    ByteVar = 3
    
    PortC.ByteVar = 1
    

    This will not turn on PortC.3

     
    • Anobium

      Anobium - 2022-06-27

      It should do.

      Missing a DIR PORTC OUT ?

      Which chip?

       
      • Anobium

        Anobium - 2022-06-27

        This seems to work here.

        #chip 16F877a, 4
        #OPTION Explicit
        
        
        Dim ByteVar as Byte
        
        Dir Portc Out
        
        do
            for ByteVar = 0 to 7
                PortC.ByteVar = 1
                wait 50 ms
                PortC.ByteVar = 0
                wait 50 ms
            Next ByteVar
        loop
        
         
  • ToniG

    ToniG - 2022-06-28

    @ToniG The test code here still doesn't work

    Sorry, my bad. somehow a typo got inserted in my testing & main was calling with SEG_on.

    The following is the original sub I was having trouble with, declaring SEG_on as bit got it working. The other issue was I was calling another sub (now deleted) & I had the params reversed.
    In this sub I set TMtmp_1.Seg_n = SEG_on

    [I really need to get more sleep...]

    tmSegBuf(DIGn, SEGn, BitState)
    
    
    Sub tmSegBuf(In Dig_n, In Seg_n, In SEG_on as bit, Optional In Dbuf() = DispBuf)
        If Seg_n = 0 then Exit Sub
        Seg_n--                         ' Seg 1 = bit0 of array byte
        TMtmp_1 = DBuf(Dig_n)
         TMtmp_1.Seg_n = SEG_on         
        DBuf(Dig_N) = TMtmp_1
    End Sub
    
     
  • William Roth

    William Roth - 2022-06-28

    So all is good now ?

     
  • ToniG

    ToniG - 2022-06-28

    So all is good now ?

    Yes, this is resolved - thanks all.


    Just the wee issue with toggling a Var bit with Not

    https://sourceforge.net/p/gcbasic/discussion/596084/thread/9a9ce1752a/

     

Log in to post a comment.