Shop OBEX P1 Docs P2 Docs Learn Events
Subscripted variables — Parallax Forums

Subscripted variables

ERIC THE GREATERIC THE GREAT Posts: 19
edited 2008-09-11 23:10 in General Discussion
Can you have a subscripted variable that is a word? ·Example DIGIT VAR WORD (4). I suspect that you cant. If not, what is the alternative.
Thanks· Eric

Post Edited (ERIC THE GREAT) : 9/1/2008 11:27:20 PM GMT

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-09-01 23:21
    Please edit your message using the pencil icon to the top right of the message and add a subject line for assistance.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • BeanBean Posts: 8,129
    edited 2008-09-01 23:29
    No SX/B does not allow arrays of WORD variables.
    One way around it is to copy data from an array of BYTEs into a WORD variable.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-01 23:32
    Could you please give me a simple example?
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-01 23:38
    You cannot have an array of words in SX/B. Don't worry, though, SX/B is so flexible you can add it in yourself (to a degree). Start by defining an eight-byte array:

    storage         VAR     Byte (8)
    


    Now define a PUT subroutine and GET function for that array like this:

    PUT_WORD        SUB     2, 3
    GET_WORD        FUNC    2, 1
    


    And, finally, write your code:

    ' Use: PUT_WORD element, value
    
    SUB PUT_WORD
      __PARAM1 = __PARAM1 << 1                      ' align for word
      \ SB __PARAMCNT.1                             ' skip if word value
      \ CLR __WPARAM23_MSB                          ' clear MSB if byte value
      storage(__PARAM1) = __WPARAM23_LSB            ' save LSB
      INC __PARAM1                                  ' point to MSB location
      storage(__PARAM1) = __WPARAM23_MSB            '  and save it
      ENDSUB
    
    ' -------------------------------------------------------------------------
    
    ' Use: result = GET_WORD element
    
    FUNC GET_WORD
      __PARAM1 = __PARAM1 << 1                      ' align for word
      __PARAM2 = storage(__PARAM1)                  ' retrive LSB
      INC __PARAM1                                  ' point to MSB
      __PARAM3 = storage(__PARAM1)                  '  and retrieve it
    
      RETURN __WPARAM23                             ' return value to caller
      ENDFUNC
    


    Since the SX has 16-byte banks you could use this for groups of eight words -- just change the original array definition.
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-07 15:24
    Could you please explain how this code works. I am having some problem tracing it. Thanks, Eric
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-09-07 15:43
    storage var byte (8) emulates w_storage var word (4)
    so storage can hold 4 words (You cannot declare a word array like w_storage).

    The two subroutines are· used to store and retrieve word values

    pp var word
    n var byte

    PUT_WORD n,pp····· 'store pp as element n, eg. w_storage(n) =·pp
    pp = GET_WORD n·· 'retrieve element n and assign it to pp, eg. pp = w_storage(n)

    Note that n must be in the range 0 to 3 (because storage only holds 4 words)

    regards peter
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-07 20:21
    I am attatching a file in which I am trying to use SUB PUT_WORD and FUNC GET_WORD to handle an array of words. I need an example on how to use them. The program lines that are comments are the ones that I am trying to implement the array on. I need to be able to do a tenth volt calculation, make an array of four digits, and use the array in the ISR to display the digits.

    Thanks Eric
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-09-07 20:36
    Try this update.
    I added a word WVALUE that holds a retrieved value from the word array.
    Note that I let the interrupt jump over sub declarations and startup code.

    regards peter
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-07 21:58
    I need an example of the syntax required to actually load and read from the array. Thankyou for being patient with me. I am really learning a lot. I just want to say that you have all been a great help to me, and I really appreciate it.

    Eric
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-09-07 22:05
    Actually, I don't see why you need a word array.
    All your DIGITx results have value 0 to 9.
    What do you want to store in the word array?

    regards peter
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-07 22:23
    Without the array I will be out of ram as I will have too many variables.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-09-07 22:26
    But what do you want to store and why must it be words?

    regards peter
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-07 22:34
    The value of digit is also calculated from pulses which is a word. If digit is a byte, it wont compile, unless you know a trick that I dont.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-09-07 23:05
    Is this what you are after? I added two subs to fill a byte array
    with digits from your calculation.
    The isr then uses the digits to get the pattern.
    You need to add a sub UPDATE_SEGS that writes the pattern to the correct segment.

    Note that the word array is still there, but not used.

    regards peter
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-07 23:24
    Thankyou Peter, this is exactly what I needed
    Eric
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-09-07 23:29
    I leave it to you then to optimize the code.

    regards peter
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-08 00:33
    One of the challenges you may face is that you're running an interrupt which can throw off the timing of COUNT. I needed the ability to count rising edges and update a 7-seg display so I came up with the attached program; it replaces COUNT with code in the ISR. You enable and start the timed counter with START_COUNT and can retrieve the value when it's finished (the cntReady flag is used as an indicator) with GET_COUNT.

    Since your pulse inputs are coming in pretty swiftly (based on your original program) I updated my program so that it can count rising edges at up to 100 KHz.

    [noparse][[/noparse]Edit] For fun I updated the program to remove leading zeroes from the displayed value -- this looks more professional.

    [noparse][[/noparse]Edit] I did one more version that is closer to your goal: this one allows you to direct the counter to one pin or another as you're doing in your program. This should get you pretty close.

    Post Edited (JonnyMac) : 9/8/2008 1:31:40 AM GMT
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-09-08 02:36
    You have two division issues with your original program:

    1) If the sFactor is zero then scale will be zero -- in SX/B a zero divisor returns zero
    -- To fix item #1 I think you want to multiply your pulses by 2 when sFactor is set to 200

    2) In SX/B 1.51.03 there is a divide by 1 error; this has been fixed in the 2.0 beta
    -- To deal with #2 don't divide anything when sFactor is set to 1.

    Post Edited (JonnyMac) : 9/8/2008 6:15:51 AM GMT
  • ERIC THE GREATERIC THE GREAT Posts: 19
    edited 2008-09-11 23:10
    I actually have a working DC voltmeter of scale 1,10,or100vdc. I am in the process of adding a second ADC configured to handle negative voltage. This will allow me to account for polarity in DC, as well as being able to measure AC. I have been concerned about the count command and the ISR, but tests show no effect to a tenth of a volt. I think that this is because on the average, 1 thousanth of a second is insignificant when the whole program runs at 50mhz, and the human eye can see only 30hz. I also have ways to adjust the ADC to compensate for irregularities like this. I do agree that putting a custom count command in the ISR is the best approach, and I will try to figure out what you have sent me and try and use it later. Under normal operating conditions SFACTOR cant be zero, there is no problem there. I am using SX Key 3.2.3 which I guess is recent enough to handle the divide by one issue. I have an untested program version which I will attach which should take care of DC polarity as well as AC. I know that the program can be optimized, but the main riority is to get the concept operating, and optimization later. I am·in the process of changing the hardware, so that I can·test the software.·One interesting thought is that when measuring AC with a positive and negative ADC, there will be a major averaging effect which should approxomate RMS voltage, which is what a AC voltmeter reads anyway. I have done crude tests with rectifiers and a single ADC·and this seems to hold true. I am working on an invention which will require a AC voltmeter that can control things, which is my main drive for the project. I dont know if other readers realize this but you can do a lot with a microcontroller voltmeter. Almost any real world quantity can be converted into voltage. A basic example is if you pass a current through a thermister, you will get a voltage drop·that is proportional to current. With some creative programming, volts can be converted into degrees. On the complex end, on an engine, the oxygen sensor eminates a voltage, the throttle position sensor eminates a voltage, the crank position sensor eminates a voltage, or pulse, along with other sensors. The voltmeter program could be modified to fire fuel injectors and time ignition coils, making an ECU for a fuel injected engine.

    Thanks for your help, Eric
Sign In or Register to comment.