1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

call a function with arguments by Reference

Discussion in 'Oshonsoft' started by zas, Jun 13, 2015.

  1. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    Hello,
    is there a way to call a function, sub or something else with arguments by Reference?

    Recurrently i have the Problem that i want to read some values from EEPROM to RAM. The Way that I do byte by byte in a Bytearray. The following program uses the values as a Longarray. I can not find a possibility to call a function and pass the value as a Reference of my Bytearray. I need the Long values for my sort algorithm.

    Maybe there is as other way to read EPROM Values directly to a LongArray with oshonsoft or pass a value as RAM address of the Variable. Then I can use the implemented Pointer()
    My current way is to check the asm code and take from it the ram address of the variables, not nice.

    Problem with Oshonsoft for PIC
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Can you post the snippet of code that isn't behaving.. Oshonsoft can use pointers.. Maybe there is another way to d it!!
     
  3. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    Maybe I have a next Problem. The Elements of the Array are randomly distributed in the RAM. I hope that is an Option in Oshonsoft. The example Code is below.
    Pointers are the solution. But I cant find a way to get the start Adress of the Longarray in the Ram

    Dim brakepoint As Byte
    Dim terminator As Byte 'END of Values
    Dim amount_alarm As Byte 'Number of Timers
    Dim arr(10) As Long 'Takes The Values from EEPORM
    'Eeprom-Values
    '0 Byte = Number of Timers
    '1 Byte Hour, Timer 1
    '2 Byte Minute, Timer 1
    '3 Byte Weekday Low nibble, Timer 1
    '4 Byte Relais Output, Timer 1
    '... , Timer xyz
    'Terminator 0x3b


    Const pos_amount_alarm = 0 'Position of "Number of Timers"
    amount_alarm = 0 'Number of Timers init value = 0

    'The Values in the EEPROM
    EEPROM eepromvalue, 0x05, 0x08, 0x00, 0x01, 0x0f, 0x10, 0x00, 0x02, 0xf0, 0x11, 0x00, 0x03, 0x01, 0x20, 0x00, 0x04, 0x02, 0x22, 0x22, 0x05, 0x05, 0x3b
    Read pos_amount_alarm, amount_alarm
    Call get_value_from_eprom(amount_alarm, arr) 'Get EEPROM value

    Function get_value_from_eprom(number_of_timer as Byte, arr_ref As reference) As Byte ' The Second Argument should be the RAM Address of my Array but i don’t know how
    'Function fill the LongArray with the EEPROM Values
    'need Reference of the arr
    Dim pos As Byte 'current pos in EPPROM
    Dim temp As Byte 'The EEPORM Value
    brakepoint = 1
    arr_ref = arr_ref + 1 ' Byte 0 = Number of Timers
    For pos = arr_ref To number_of_timer Step 1
    Read pos, temp
    Pointer(arr_ref) = temp 'When I have the SRAM Adrress fill the Longarry
    arr_ref = arr_ref + 1
    Next pos
    End Function
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK

    If you need to manipulate ram in this manner, it's always best to group the items..

    Its very easy to implement a function that will write / read a long in the onboard eeprom..
    The other thing that needs mentioning is that function don't need to pass anything.. You can do as much with the Proc statement and using global variables..

    Oshonsoft allows direct placement of variables... So if you store several items in EEPROM that need to be transferred to RAM.. A byte pointer can do the trick... If all your variables are stored in EEPROM in the exact order you have them in ram, then a simple byte read which covers all the bytes, words and longs can be used..

    Instead of reading a long,word or byte in a function... Just read the whole lot.. Reading is very quick

    If your ram is like this

    Dim brakepoint As Byte
    Dim terminator As Byte 'END of Values
    Dim amount_alarm As Byte 'Number of Timers
    Dim arr(10) As Long 'Takes The Values from EEPORM

    You could use the pointer as a byte set the pointer to breakpoint then just increment the pointer by 1 until all 43 have been read..
     
    • Like Like x 1
  6. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    Thank you for your reply Ian,
    I try it. Im not sure I understand is 100%. But I think the Oshonsoft allows direct placement only for variables. But not for Arrays. I get a compiler error.
    So I Think when there is no possibility for direct placement of an Array your solution is not practicable.
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    The compiler should know!!! As the pointer is to a byte it won't care... It will just run through all the variables as though they were bytes... There will be 43 consecutive bytes... I know it works because I do it the same way..

    In either case using this global technique you can do it in the function you already made...

    BTW you know you can place variables exactly where you want them!!! You don't even need a pointer.

    Dim brakepoint As Byte @ 0x05
    Dim terminator As Byte @ 0x06 'END of Values
    Dim amount_alarm As Byte @ 0x07 'Number of Timers
    Dim arr(10) As Long @ 0x08 'Takes The Values from EEPORM

    Now you know exactly where the are...
     
    • Like Like x 1
  8. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    I understand the way you describe. I try to compile the declaration as you describe. The compiler throw an error see below

    Dim brakepoint As Byte @ 0x05
    Dim terminator As Byte @ 0x06 'END of Values
    Dim amount_alarm As Byte @ 0x07 'Number of Timers
    Dim arr(10) As Long @ 0xAA 'Takes The Values from EEPORM –-> error in line 4 “incorrect variable name”


    When I try
    Dim arr As Long @ 0xaa its OK.
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Okay... But if you leave out the 0xAA it will still be located at ox08

    Why have you chosen 0xAA??? I would try to keep arrays low down in a page.. This doesn't break page boundaries, but if you forget, it causes heartache...
     
  10. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,255
    Likes:
    1,218
    Location:
    Cardiff, Wales
    I'm not familiar with Oshonsoft, but is arr a reserved name associated with arrays within the syntax? Or is the compiler expecting square brackets arr[10] rather than braces?
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    I have been pondering this... As arr(10) is global you don't need to do anything

    Write two routines... One to read the array and one to write the array and call them separately..
    I quickly wrote this...

    Code (basic):

    Dim x As Byte
    Dim y As Byte

    Dim arr(10) As Long

    loop:
       Call readarray()
       Goto loop
    End  
    Proc readarray()
       Dim x As Byte
       Dim y As Byte

       For x = 0 To 9
         arr(x) = readlong(x * 4)    
       Next x
    End Proc  

    Function readlong(x As Byte) As Long
       Read x, readlong.LB
       x = x + 1
       Read x, readlong.HB
       x = x + 1
       Read x, readlong.3B
       x = x + 1
       Read x, readlong.4B
       
    End Function
     
     
    • Thanks Thanks x 1
  12. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    Hello alec_t, Ian
    alec_t: "arr" is not reserved. The Syntax for declaration in Oshonsoft is with circle brackets

    Ian: I choose 0xAA without cause. Oshonsoft (PIC IDE) reserved at my current controller the RAM from 0x00 to 0x36.But your explanation about the page boundaries is interesting. I don not think about it. Good statement!
     
  13. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    Hello Ian,
    the Solution looks encouraging I will try it.When ist work it solves all Problems.:)
     
  14. zas

    zas New Member

    Joined:
    Feb 11, 2015
    Messages:
    12
    Likes:
    0
    Hello Ian,
    Good job. It works fine.
    I must retire my statement in the last posts. The arrays are continuous in the RAM. I was confuse yesterday.
    Thank you.
    I have send a Bug Report to Oshonsoft. Why I cant set the placement of the array directly in the RAM like variables.
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Great.... There is always another way!!!! Just finding it is a bit of a toil..
     

Share This Page