Re: Robby, __int8 limited to: -128 to 127.



"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E21DD3DF-4F2F-4362-A750-59C41430ECE1@xxxxxxxxxxxxx
int spiA0=1,spiA1=0,spiA2=0;

//INCREMENT_ADDRESS BY AMOUNT OF INCREMENTATIONS REQUIRED
INC_ADDR(16,&spiA2, &spiA1,&spiA0);

=========================================Function!
void INC_ADDR( //***INCREMENT_ADDRESS***
int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED
int *A2, //MSB OF MESSAGE ADDRESS IN PAR FLASH
int *A1, //MIDSB OF MESSAGE ADDRESS IN PAR FLASH
int *A0) //LSB OF MESSAGE ADDRESS IN PAR FLASH
{
int i;

for(i=0;i<INCBY;i++)
{
if((*A0)==255) //Before A0 turns to 0, statement is true
{(*A1)++; //Increment central byte of flash adress
if((*A1)==255) //Before A1 to 0, statement is true
{ (*A2)++;//Increment MSBYTE of flash adress
} }

(*A0)++;
}}

Try this code with initial address of 00FEFF (that is, A1=254 and
A0=255) and incrementing by one.

1. Check for A0==255 succeeds
2. Increment A1. A1 is now 255
3. Check for A1==255 succeds
4. Increment A2. It is now 1
5. Increment A0. It wraps around and becomes 0.

So you end up with 01FF00. But the correct value is 00FF00.

Exercise for the reader: figure out what the code will do for an initial
address of 00FFFF, what the correct value after increment should be, and
why the two values differ.

By the way, you don't need to increment one at a time in a loop. I
assume your compiler is capable of adding two numbers. Then you can do
this:

void INC_ADDR(int INCBY, int *A2, int *A1, int *A0) {
int carry = (*A0 > 255 - INCBY);
*A0 += INCBY;

if (carry) {
++(*A1);
if (*A1 == 0) {
++(*A2);
}
}
}

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • RE: Robby, __int8 limited to: -128 to 127.
    ... And yes an 8bit int cannot hold a value of 256. ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... In order to access the data from my external memory I need a three byte ... and increment the A0 value from 1 to 2. ...
    (microsoft.public.vc.language)
  • Writing Classes
    ... Clock application ... private int hr; //store hours ... public void setTime{ ... //Method to increment the time by one second ...
    (comp.lang.java.programmer)
  • Re: Definition of expression and statement.
    ... The fact that a while loop is recognized ... behavior, e.g., if a is an "int" variable and is initially set to ... The various modifier operators, ... increment and decrement, have TWO uses: ...
    (comp.lang.c)
  • Re: Question about ++
    ... I have got a question about the post increment operator. ... It seems int he code above the ++ is completly disregarded ... Java completely evaluates the right hand side of an assignment before ... Do the assignment, setting i to the right hand side result, 1. ...
    (comp.lang.java.programmer)
  • Re: Pointers gone mad!
    ... Yes, its true Igor, I didn't see it with those values. ... int INCBY, //AMOUNT OF INCREMENTATIONS REQUIRED ... //MIDSB OF MESSAGE ADDRESS IN PAR FLASH ... if {// previous increment overflowed ...
    (microsoft.public.vc.language)

Loading