Menu

Passing objects to functions

Anobium
2013-06-28
2013-08-18
  • Anobium

    Anobium - 2013-06-28

    RESOLVED: See https://sourceforge.net/p/gcbasic/discussion/579125/thread/41bb58f4/#6c34

    Can I or how do I pass a port object to a function?

    I want to create a function that tests the state of a nunber of ports. I wanted to create a reusable function that is passed the port address.

    is this possible?

     

    Last edit: Anobium 2013-08-18
  • Hugh Considine

    Hugh Considine - 2013-06-29

    I'm not quite sure what you mean here, but you can probably do something using the Peek and Poke commands. For example, to make a program that could pulse PORTB.3 for a second, you could write something like this:

    ;Chip Settings
    #chip 16F88
    
    Do Forever
        FlashPin @PORTB, 8
        Wait 1 s
    Loop
    
    Sub FlashPin (In DestVar As word, In DestBit)
        Poke DestVar, Peek(DestVar) Or DestBit
        Wait 1 s
        Poke DestVar, Peek(DestVar) And Not DestBit
    End Sub
    

    Using @ before the name of a variable (including a special function register) will give you the address of that variable, which can then be stored in a word variable and used by Peek and Poke to indirectly access the location.

     
  • Anobium

    Anobium - 2013-06-29

    Perfect. I wanted the indirect addressing scheme so I can inspect the status of input ports from a function.

    I will get busy.

    Anobium

     
  • Anobium

    Anobium - 2013-06-29

    Perfect. I wanted the indirect addressing scheme so I can inspect the status of input ports from a function.

    I will get busy.

    Anobium

     
  • Chuck Hellebuyck

    Another great undocumented feature of this amazing compiler.
    I'll add this to my list of gems I've found from Hugh's posts.

    Keep them coming.

    I'm working on a Great Cow Basic Programmers Manual with all the documented and undocumented features (to make them documented) of GCB.

     
  • Anonymous

    Anonymous - 2013-07-01

    Well, Chuck,

    I guess Nuts & Volts writers think alike! I've been doing the same thing. I also have put together a complete lab guide to go with (55 projects of all sorts for the PIC).

    Thomas Henry

     
    • Chuck Hellebuyck

      Thomas
      I can't find your email.
      Can you email me at chuck at elproducts dot com?
      I want to talk more about what your doing with GCB.

       
  • Anobium

    Anobium - 2013-07-01

    And, I am porting the F1 Evaluation Platform demonstrator to GCB.

    Pretty complex it will show what this tool can do.

    :-)

     
    • Chuck Hellebuyck

      Sounds interesting.
      I can't wait to see your results.

       
  • Anobium

    Anobium - 2013-07-02

    Goal. Port the demonstration code to GCB. With the following functionality (which is similar to the Microchip demonstration code). The 16f1937 with the specific LCD, clock driven via the LCD clock, MCP9800 temperature sensor, ADC, LEDs, use of various the basic inputs/outputs and the user interface.

    Progress so far.

    All the input funtions, buttons and ADC are operational. Hence, my request for information on using 'objects by reference' to create reusable functions.

    The temperature sensor working via I2c. This works across a range of selections from 0.5 to 0.0625 units degrees C.

    Hardware serial is working via the serial IO adapter on the board. This used for debugging. Connected to my PC via a Max232.

    The LCD is initialized. I have ported the LCD device driver last night.

    So far.... I have the majority of the functions operational. LCD adressing and the clock (with the associated interrupts) need to understood and sorted. I only have characters not any real data on LCD display with all the real data on my PC terminal displayed using ANSI code (which is pretty cool!).

    The issues are:
    1) Understanding the spaghetti of C++.
    2) Understanding the use of the variables in the demonstration code. It is poorly documented and there is use of global and local variables without a naming convention or strategy. Well.... its looks like that to me.
    3) LCD. I have used the standard LCD driver in past projects but I need to get my mind around this device.

    Anyone willing to discuss?

     

    Last edit: Anobium 2013-07-02
  • Chuck Hellebuyck

    I don't have that board but it sounds like you've created some interesting projects.
    I almost think its easier to ignore the C code and just create the code from scratch.

     
  • Anobium

    Anobium - 2013-07-02

    I cannot get the in-direct addressing to work. Am I making a silly error?

    Anobium

    ;Chip Settings

    #chip 16F88

    Do Forever
    FlashPin @PORTB, 8
    Wait 1 s
    Loop

    Sub FlashPin (In DestVar As word, In DestBit)
    Poke DestVar, Peek(DestVar) Or DestBit
    Wait 1 s
    Poke DestVar, Peek(DestVar) And Not DestBit
    End Sub

     
  • Anobium

    Anobium - 2013-08-18

    RESOLVED. The code below works.. remember to set your ports as outputs and be very careful when putting comments on the same line as the call to the TOGGLE subroutine.

    Example works, this toggles 4 LEDS every 100 ms.

    Anobium.

      #chip 16F1937, 32
      #config Osc = intOSC, MCLRE_OFF, PLLEN_ON, VCAPEN_OFF
    
      ' Define the LED ports
        Dir PORTD.1 Out
        Dir PORTE.2 Out
        Dir PORTE.1 Out
        Dir PORTE.0 Out
    
      Do Forever
    
          ' DO NOT USE A COLON BETWEEN THE COMMMAND AND THE COMMENT!!
          Toggle @PORTE, 1: ' equates to RE0 or PORTE.0 ... the first bit
          Wait 100  ms
          Toggle @PORTE, 1
          Wait 100 ms
    
          Toggle @PORTE, 2 ' equates to RE1 or PORTE.1  ... the second binary bit.
          Wait 100  ms
          Toggle @PORTE, 2
          Wait 100 ms
    
          Toggle @PORTE, 4  ' equates to RE2 or PORTE.1, third binary bit = %00000100
          Wait 100  ms
          Toggle @PORTE, 4
          Wait 100 ms
    
          '    Toggle @PORTD, 2  :' equates to RD0... THIS does not work WITH A COLON
          Toggle @PORTD, 2  ' equates to RD0
          Wait 100  ms
          Toggle @PORTD, 2
          Wait 100 ms
       Loop
    
      Sub Toggle ( In DestPort As word, In DestBit )
            Poke DestPort, Peek(DestPort) xor DestBit
      End sub
    
     

Log in to post a comment.