CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

spi_write()... or just handling int8

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








spi_write()... or just handling int8
PostPosted: Mon Aug 31, 2009 10:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 31, 2009 3:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 7:24 am     Reply with quote

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);
}
 
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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