Pages: [1]   Go Down
Author Topic: Counting hexadecimal / binary  (Read 1917 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
blablablabla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for example, if i have an register 0x00 and i want to count up to let´s say 0xff each step in a loop ?? i want to make a function and just pass the variable between the increasing steps (0x01,0x02,....0xdf, 0xef,0xff).


any code out there? don´t even know how to start!

thanks
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

like this?

Code:
int i;
for (i = 0x0; i <=0xff; i++)
{
    fn_whatever(i);
}

-j
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
blablablabla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah! great! well, i was also interested in a solution writtten in C, as i it wouldnt be that easy like the above.


but thanks a lot! it works.
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i was also interested in a solution writtten in C, as i it wouldnt be that easy like the above.
I don't understand this statement; that is C code.  Of course, being C, it's also C++, "Arduino" code, etc.

C doesn't care if the constants are hex (0xff), octal (075), decimal (6789), or even ASCII ('c').  It doesn't know about binary constants, but somone wrote a macro to handle that, I think included in Arduino 7.

-j
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
blablablabla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry, you are right! but i´ve another question which is hard to find through all references. how can i "eval" within arduino language. for example, i have some registers called reg_level0, reg_level1, reg_level2 ...
i want to set some bytes in a for-statement such as

Code:
for (i = 0; i < 5; i++) {
function_blablab(reg_level[i], 0x00);
}

of course above example couldnt be compiled as it interprets the square brackets as an array? do you know another syntaa for that?

thanks
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think what you're asking is possible.

My first thought is to put them into an array instead of having several discrete values.

-j
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
blablablabla
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yep, workaround is indeed an array, here´s the code


Code:
byte reg_level0 = 0x01;
byte reg_level1 = 0x02;
byte reg_level2 = 0x03;
byte reg_level3 = 0x04;
byte reg_level4 = 0x05;
byte reg_level[5] = { reg_level0, reg_level1, reg_level2, reg_level3, reg_level4  };

void test(int i) {
for (i = 0; i < 5; i++)  {
  function_blalba(reg_level[i], 0x40);
  delay(delaytime);
 
}
}
Logged

Pages: [1]   Go Up
Jump to: