go_away

Author Topic: how do I increment a hex value?  (Read 2826 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
how do I increment a hex value?
« on: November 21, 2010, 10:25:43 AM »
I need to read from a memory section starting at 0x180, and the following next 127 bytes, and store it in an array.

For example, _SFR_MEM8(0x180) is byte #1, _SFR_MEM8(0x181) is byte #2.

How do I fix this code so that it works?

Code: [Select]
uint8_t i;
#define MAX_FRAME_SIZE (127)
char wireless_recieved_frame[MAX_FRAME_SIZE];
for(i=0;i<127;i++)
{
wireless_recieved_frame[i]=_SFR_MEM8(what_goes_here?);
}

I noticed WebbotLib has a function called char hexchar(char x), but not sure how to use it as 0x180 is 384.

Offline greasemonkey94

  • Jr. Member
  • **
  • Posts: 29
  • Helpful? 1
Re: how do I increment a hex value?
« Reply #1 on: November 21, 2010, 10:55:02 AM »
how about using pointers?
i don't know much about electronics and only a little bit of c++

something like

int *ptr;

ptr=384;

uint8_t i;

#define MAX_FRAME_SIZE (127)
char wireless_recieved_frame[MAX_FRAME_SIZE];
for(i=0;i<127;i++)
   {
   wireless_recieved_frame=_SFR_MEM8(ptr++);
   }


of course i may be completely of my rocker but a pointer is supposed to store the memory address , ptr is the memory address

and *ptr is what is at the address.


but if I am completley wrong,please excuse me,
only trying to help.

Offline rbtying

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: how do I increment a hex value?
« Reply #2 on: November 21, 2010, 11:08:49 AM »
how about using pointers?
i don't know much about electronics and only a little bit of c++

something like

int *ptr;

ptr=384;

uint8_t i;

#define MAX_FRAME_SIZE (127)
char wireless_recieved_frame[MAX_FRAME_SIZE];
for(i=0;i<127;i++)
   {
   wireless_recieved_frame=_SFR_MEM8(ptr++);
   }


of course i may be completely of my rocker but a pointer is supposed to store the memory address , ptr is the memory address

and *ptr is what is at the address.


but if I am completley wrong,please excuse me,
only trying to help.

That looks right, in concept, at least.  I'm assuming incrementing by 1 byte is good enough for the program?  Normally you'd deference the pointer with *ptr, but if you have a read method...

Code: [Select]
#define MAX_FRAME_SIZE (127)
char wireless_recieved_frame[MAX_FRAME_SIZE];
for(int *ptr = 0x180; ptr < MAX_FRAME_SIZE; ptr++)
{
wireless_recieved_frame[i]=_SFR_MEM8(ptr);
}

Although, not knowing what _SFR_MEM8 needs as a parameter, it could just as easily be a standard integer (since you'd never deref the pointer):

Code: [Select]
#define MAX_FRAME_SIZE (127)
char wireless_recieved_frame[MAX_FRAME_SIZE];
for(int i = 0x180; i < MAX_FRAME_SIZE; i++)
{
wireless_recieved_frame[i]=_SFR_MEM8(i);
}

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 334
  • Helpful? 3
Re: how do I increment a hex value?
« Reply #3 on: November 21, 2010, 05:10:58 PM »
What does "hex" have to do with this question?  Are you just trying to store the values of 127 consecutive bytes of RAM in an array?  If so, you can do it by copying data:

char wireless_received_frame[127];
char *ptr = 0x180;
for (i=0;i<127;i++)
  wireless_received_frame = *(ptr++);

Greasemonkey94's code is close to this, but he uses a pointer to an integer, which means incrementing it by one will cause the pointer address to increment by two (and you'll miss every other byte).  Also, if you want to store the data in RAM rather than the address of the data, I believe you need the asterisk.  I'm not really all that familiar with the _SFR_MEM8 macro; I don't think you need it for the values you're using, but you can always check the appropriate header file to see what it's really doing for you.

You can also do it trivially without copying data:

char * wireless_received_frame = 0x180;

Of course if any of the RAM values change that you care about, the wireless_received_frame array values will automatically update, which might not be what you want.

- Ben

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: how do I increment a hex value?
« Reply #4 on: November 21, 2010, 05:26:13 PM »
Turns out I was incrementing correctly after all.

In my code example, 'what_goes_here?' should be '0x180+i', which is what I was doing.

I was confused because I was storing 5 bytes of data, and when I read 5 bytes of data starting at 0x180, *some* of it was non-sense.

Turns out the hardware added an additional byte before the data at 0x180, and another byte after the data, to store additional information - resulting in 7 bytes total. So when I read only 5 bytes starting at 0x180, the output resembled rabid squirrels.

 


Get Your Ad Here