Maker Pro
Maker Pro

Question about passing a variable value to an array in C

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Hello all, as the title states, I am trying to pass a variable into an array in C. Specifically I am trying to get the random value of TMR0 of the pic that I am working on into the array. The TMR0 status is an unsigned number from 0-255(I believe). I don't find much information on Google on passing a variable into an array. Is this not the right way of doing things? Is there an intermediate step in between?

My thinking is:
Code:
char Random[2] = TMR0;

It would be just too easy to simply feed a number into an array :rolleyes:
Thanks for any help
 

dorke

Jun 20, 2015
2,342
Joined
Jun 20, 2015
Messages
2,342
A few things:

1. You need to declare the array size and type .

2. What you "printed " is accessing element 3
in the "Random "array,is that what you wanted?

3. How is "TMR0" defined in your code?

4. If you just stick a value directly,you do "find" it there right?
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
What I think you want John is.

Char Random [2]; Define array size and name
Random [0] = TMRO; Put the value of TMRO into the first element of the array.

Adam
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
What I think you want John is.

Char Random [2]; Define array size and name
Random [0] = TMRO; Put the value of TMRO into the first element of the array.

Adam
Spot on Adam.

Yes, I am taking the value of TMR0 which is a three digit unsigned number, and asking to place each digit into an array so that I can access only the last digit (I actually only need the last digit so other methods that don't take all of the numbers are ok too).
From some research, its a bit more complex than I thought. Apparently using string copy and limiting it to the last digit and then placing that last digit into the new variable should do the trick. Do I need to typecast into int? I intend to use the value as a number in the next step.

This is trivial, but my syntax and grasp of C is novice at best. This is what I think I should be looking at, your thoughts please. Thanks!

Code:
char RANDOM[1];                             //create a destination array 1 cell for last digit from var. TMR0
strcpy(RANDOM, &TMR0[3]);                   //copy last digit of TMRO (third digit) into var. RANDOM
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Do you actually want the last digit, or could you use either a mod 8 or mod 16 number? Both can be done with masking and won't require a tine consuming divide.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Assuming TMR1 is an unsigned byte:

Code:
byte a, b, c, d;
a = b = TMR1;
c = 0;

for (d = 160; d >= 10; d >>= 1) {
if (b >= d) {
b -= d;
c += d;
}
}

d = a - c;

d should be what you're after.

You can unroll the loop to make it marginally faster.

Sorry about the formatting, my phone wont let me type spaces unless it thinks they mean something :(.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
The above code can be further optimized by removing all reference to a and c because b ends up with the value you want. It then becomes even easier to unroll the loop

Code:
byte b = TMR1;
b = b >= 160 ? b - 160 : b;
b = b >= 80 ? b - 80 : b;
b = b >= 40 ? b - 40 : b;
b = b >= 20 ? b - 20 : b;
b = b >= 10 ? b - 10 : b;
 
Top