Reading a value from an address, using an array

Discussion in 'Programmer's Corner' started by ArakelTheDragon, Apr 19, 2017.

  1. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    Good day! I need to read 12 values from 12 addresses, which are reserved only for these 12 values no matter how many times the application is run, I read the information from a binary file, record it on the 12 addresses and than use an array to compare the 12 values from the addresses, to 12 values in the array. The idea is not to compare every variable 1 by 1, because that will to bad for me. Perhaps there is a way to reserve the addresses and just compare it to the value in the array which is the same as the address? Meaning "if value on address 0x105 equals the value in array element 5, do something".
    How about like this:

    for (i=0; i<12 ;i++)
    parameter = 0x100 + i;

    How do I use it afterwards in order to compare with an address in the array.
    In array "parameter[2]" I have recorded "0x102", I guess I have to somehow reserve addresses from "0x100" to "0x111" for use only by putting something there by using the address in the array, can someone tell me how to do that please?

    The whole idea is that I dont declare the variables separetaly, but use one array in order to compare a value with the value on address "what is recorded in the array" instead of using 1 by 1 the variables, which makes the program too long.

    So I compare the value read from a file "100 decimal" to the value in address "0x102" which is in the array element "paremeter[2]".
    I also need to reserve the addresses from "0x100" to "0x111", so the value from them is always read and always coresponds to the same value for comparison.
     
  2. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    I was thinking will this work:

    int parameter [12];
    for (i=0; i<12 ;i++)
    {
    parameter = 0x100+1; /* The parameter element "i" is equal to the value of address "0x100 + i" */
    }

    Will this set the value of the array to "0x100 - 0x111"?

    Or maybe this:
    int parameter [12];
    int value [12];
    for (i=0; i<12 ;i++)
    parameter = *value; /* Parameter element i, equals the value in array "value i" */

    Since the site edits my code, you can check it here:
    https://www.dropbox.com/s/9fs9b46z3udt2m8/All-about-circuits-arrays-addresses.txt?dl=0
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    First off, if you use the Code Tags feature in the reply box you can post code without the formatting getting removed:
    CODE TAGS.jpg
    I am not quite sure what data you are trying to define or compare beyond you seem to need to reserve 12 locations, and you seem to be working in the C language.

    You want 12 elements ranging from 0x100 (4) to 0x111 (7). That is just 4 elements, so something is off. Instead let me show you how to define the elements of the array from 1 to 12

    C has a "const" keyword (for constant) that allows you to create data in your program that you can use but never change.

    Code (Text):
    1.    const int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1001, 0x1010, 0x1011, 0x1100};
    Or perhaps even easier:
    Code (Text):
    1.    const int parameter [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    Either way you get an array called "parameter" with 12 elements numbered 0 thru 11 with a value of 1 thru 12.

    Now if you thing you will ever want to change the values of this array you can make a regular array that is not constant by simple not using the "const" keyword:

    Code (Text):
    1.     int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1010, 0x1011, 0x1100};
     
  4. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    Sorry for the mistakes, I wrote it too fast.

    1. I am using "ANSI C" with the GNU C Compiler
    2. I need to reserve 12 addresses, to read the value from them, and if its different than what I need, to increment a counter. The addresses and value in them must not change, no matter how many times the application is run,

    I understand this, despite that you wrote it with binary values as hexadecimal:

    Code (Text):
    1. const int parameter [] = {0x0001, 0x0010, 0x0011, 0x0100, 0x0101, 0x0110, 0x0111, 0x1000, 0x1010, 0x1011, 0x1100};
    I guess it should be from "0x100" to "0x10B". We both wrote it wrong :D.
    I understand that "const" should place the array in the permanent memory (ROM) as an initialization of the "retlw" instruction, but I dont think its what I need.

    How do I reserve 12 addresses, record a value in them, read the value and compare it to something, using an array in order to shorten the program?
     
  5. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    I don't understand why you don't just use another array? If you want to make sure it is not changed, used the const keyword as ErnieM suggested. Why do you think it is not what you need? But you did say, record a value, so the const keyword will not work.

    How about the new or malloc keywords? This will allocate a block of memory to which you can save values. But you will have to calculate pointers to the locations to which you are saving those values. And just using an array is much simpler. You can use malloc and new to define the memory used by the array, if the automatic assignment is not good for you. See the attached sketch for my technique using "new".

    At this stage, I am assuming the "something" to which you are comparing these values is another array. A simple for loop, comparing two arrays is all you need. And even if you decide to use pointers to the values you recorded, the loop will still work, substituting the pointer calculation for the array reference in the case of the recorded values.

    Note that if you decide to use only pointers (why you would, I don't know yet), you must take into account the size of the values recorded. a char value is shorter that an int value which is shorted than a long value... In constructing an aray with the new keyword, you must supply the value type and it takes the length in account when reserving memory.

    The attached sketch is a proof of concept, to dynamically allocate memory to an array, depending on input data.

    Good luck!

    Code (C):
    1. #define halt while(-1){}
    2. unsigned int Size = 3; // value of the desired array to pass
    3.  
    4. void setup() {
    5.   Serial.begin(9600);
    6. }
    7.  
    8. void loop() {
    9.   char* anArray=NULL; // define a pointer to the array
    10.  
    11.   anArray = makeArray(Size); // create the array
    12.  
    13.   for (int i = 0; i <= Size; i++) { // print out the test
    14.     Serial.println(anArray[i]);
    15.   }
    16.   Serial.println();
    17.   halt;
    18.   // normally, array memory should be deleted around here
    19. }
    20.  
    21. char* makeArray(int mySize) { // function returns a pointer
    22.  
    23.   char* myArray = new char[mySize]; // create the array
    24.  
    25.   for (int i = 0; i <= mySize; i++) { // *Debug* fill the array with test values
    26.     myArray[i] = 65+ i; // *Debug* only used for testing
    27.   }
    28.   return myArray; // return the pointer to the allocated array
    29. }
    Here is the code with the test code stripped out:

    Code (C):
    1. #define halt while(-1){}
    2. unsigned int Size = 3; // value of the desired array to pass
    3.  
    4. void setup() {
    5.   Serial.begin(9600);
    6. }
    7.  
    8. void loop() {
    9.  
    10.   [B]char* anArray=NULL; // define a pointer to the array
    11.  
    12.   anArray = makeArray(Size); // create the array[/B]
    13.  
    14. // the remainder of the sketch, referencing the array created
    15.  
    16.    Serial.println();
    17.    halt;
    18. // normally, array memory should be deleted around here
    19. }
    20.  
    21. char* makeArray(int mySize) { // function returns a pointer
    22.   char* myArray = new char[mySize]; // create the array
    23.   return myArray; // return the pointer to the allocated array
    24. }
     
    Last edited: Apr 19, 2017
  6. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    Thank you for the explanation and code. The idea is that normally I program microcontrollers and real time operating systems are not that known to me, however I needed to record in a file values, and then read them again. The file must contain 2 addresses for 1 parameter, and we have 12 parameters. Then we must read from the binary file 2 values for every parameter, from a specific address. If this was a microcontrtollers things would be different, like the "#byte" directive which will alocate 1 memory address at the address I choose.

    For real time operating systems I was thinking of this:

    Code (Text):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4.  
    5. int main()
    6. {
    7.                                             /*Assign the address to a pointer */
    8.  
    9. char* pointer = (int*) 0xff73000;
    10.                                             /* Access the value */
    11.  
    12. char value = *pointer;
    13. char fist_byte = pointer[0];
    14. char second_byte = pointer[1];
    15.  
    16. for (i=0; i<12 ;i++)
    17. {
    18.      if (pointer [i] <= sensor || pointer [i] >= sensor)
    19.            event(record);
    20. }
    21.  
    I dont know the "new" command, I remember about "callos()" and "malloc()" something, but not much and I believe "calloc()" was better?

    I know in real time operating systems "char = 8-bits", "int = 16-bits", I though every element will be the same, I dont know the "new" keyword.
     
    Last edited: Apr 19, 2017
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    Yeah, is should have been
    Code (Text):
    1. const int parameter [] = {0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0110, 0b0111, 0b1000, 0b1010, 0b1011, 0b1100};
    Let's concentrate on this:
    Why do you need to know the address of the data? Why can't this address ever change? Why not just a nice variable name?

    If the value cannot change, when is it known? When you are writing the program (called "compile time) ? At program start up? At some other time? If at compile time you can use a bunch of constants, most probably an array or some other structured element.

    Why does one parameter require two addresses? Or is it two parameters for each address?

    What is this "binary file"?
     
    djsfantasi likes this.
  8. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    What is this "real time operating systems" you are talking about?

    And I totally agree with ErnieM...
    I suggest that if you answer these questions, we can help you better. IMHO, I think you think that you think you know what you think. But to use, it is not making sense. It looks like you have a simple requirement but don't understand enough to recognize it. So you have come up with a complicated scheme to solve your problem. Except the scheme does not solve your problem.

    Help! Help us help you...
     
  9. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    Oh, and one last word. This line of code is missing the binary value for 9, which is shown in your decimal list.

    Code (Text):
    1. const int parameter [] = {0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0110, 0b0111, 0b1000, 0b1010, 0b1011, 0b1100};
     
    ErnieM likes this.
  10. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    Here is the situation. I think this: normally for low level embedded coding we have master devices and slave devices, lets say that a master device will write the information at a certain address every time, that way, the slave device does not need any confirmation from the master, does not need to send requests for data, does not need an interface or to acknowledge the data. The master records the data in a file on addresses 0 to 23, from 0 to 11 are the low border values, from 11 to 23 are the high border values for the 12 parameters, or the slave can write how many times the error was met with the system data gotten from elswhere, in a file, from where the master can read it at any time. This is why the addresses must not change, since we will check them every second or so, to see the amount of errors occurred.
     
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    So the "address" is a location within a file? Not a location in memory?

    I still am having trouble seeing how this will work. What platform are you using that supports a shared file system? Or shared memory?
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    So far every explanation of what task needs to be accomplished contains a fair amount of what can be called gibberish.

    Lets try to be concrete. What is this master device? What is the slave device?

    And what is this mysterious "file" you keep referring to? What holds the file? Does it have a part number? WHAT IS IT???

    Post part numbers, not vague descriptions.

    A system block diagram would be helpful.
     
    djsfantasi likes this.
  13. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    Once again, I totally agree. This is beginning (or has begin) to appear like the ramblings of a troll.
    • You state that you are experienced in microcontrollers.
    • Yet, you are proposing a structure that is not available in any platform that I know.
    • You refuse to provide the details of the platform you plan on using, which would address my concerns.
    • We gave gone on for 10 posts before you mention this is a master-slave configuration.
    • There is no functional description of any detail provided. Like a block diagram.
    • There is gibberish instead of concrete facts.
    Troll? You decide. I am waiting for more detail.
     
  14. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    Last edited by a moderator: May 2, 2017
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    There is nothing in this "specification" that contradicts using an array for both the parameters and the error counts.
     
  16. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    What about "The application shall have 12 parameters (unsigned integers), each of them on a defined memory
    address (the same through multiple run of the application)." which is on the 9th-10th row?
     
  17. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    I suspect this requirement cometh from an old environment and are stated with that environment's constraints in mind.

    It's just my opinion.

    The requirement may be the two processors share results between them. Which can be done in a number of ways without using static memory locations. And I did note, they must be persistent. Also doable with the caveat that how depends on what. I.e., EEPROM, local file system, remote file system, etc.

    Besides responding with "This" when asked "What", consider adding "Why" and "How" to your response.
     
    ErnieM likes this.
  18. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,894
    1,786
    Asked and answered.
    Variables do not change addresses.
     
    djsfantasi likes this.
  19. ArakelTheDragon

    Thread Starter Member

    Nov 18, 2016
    110
    1
    But the addresses need to be on a defined memory address from the programmer!

    If this was a microcontroller, I would be able to easily define the address and then use the variable for this address.
    But lets see if possible how to do it for real time operating systems?

    Perhaps if possible I can use a pointer? The pointer points towards an address, on which address we have the value.
    I know "&" returns the address.
     
  20. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    3,485
    1,244
    What don't you understand? An array is a defined fixed address.

    What do you mean, "by the programmer?" And I have asked before, what "real time operating system"? Without details, there is no way your question can be answered.
     
Loading...