Re: Robby, __int8 limited to: -128 to 127.
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Wed, 12 Dec 2007 22:41:07 -0500
"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
.
- References:
- Pointers gone mad!
- From: Robby
- Robby, __int8 limited to: -128 to 127.
- From: Jeff☠Relf
- RE: Robby, __int8 limited to: -128 to 127.
- From: Robby
- Pointers gone mad!
- Prev by Date: Robert, try: “ if ( * A1 ++ == 255 ) ++ * A2 ; ”.
- Next by Date: Re: DLL declarations created by VC++ app wizard
- Previous by thread: Robert, try: “ if ( * A1 ++ == 255 ) ++ * A2 ; ”.
- Next by thread: DLL declarations created by VC++ app wizard
- Index(es):
Relevant Pages
|
Loading