|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
spi_write()... or just handling int8 |
Posted: Mon Aug 31, 2009 10:22 am |
|
|
Hi
Take a look at this.
The first ex. is clean and nice:-)
The second ex. is not so clean, why?
I have no success with casting (int8) in the second ex.
any good explanation?
Code: |
.................... tmp=x>>8&0xff;
0090: MOVFF x+1,tmp
.................... spi_write( tmp );
0094: MOVF SSPBUF,W
0096: MOVFF tmp,SSPBUF
009A: RRCF SSPSTAT,W
009C: BNC 009A
....................
.................... spi_write( x>>8&0xff );
009E: MOVFF x+1,@@60
00A2: CLRF @@x61
00A4: CLRF x61
00A6: MOVF SSPBUF,W
00A8: MOVFF x+1,SSPBUF
00AC: RRCF SSPSTAT,W
00AE: BNC 00AC
.................... |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 31, 2009 3:01 pm |
|
|
Quote: | The second ex. is not so clean, why? |
It appears that no matter how it's done, the compiler puts in 2 or 3
ROM words of unnecessary instructions. The "make8" method has
the least number (only 2 words) of all the methods shown below. But
how important is this ? Why worry about optimizing out 2 instructions ?
Code: |
#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
int16 x;
x = 0x1234;
// Try different ways of getting the MSB of 'x' for the
// spi_write() function:
spi_write(x >> 8);
spi_write(make8(x, 1));
spi_write(x/256);
while(1);
}
|
|
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Tue Sep 01, 2009 7:24 am |
|
|
My favorite way to get an 8-bit quantity out of an int16 would do it like this:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
int16 x;
#byte x_low = x
#byte x_hi = x+1
x = 0x1234;
spi_write(x_low); // Should send 0x34
spi_write(x_hi); // Should send 0x12
while(1);
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|